Search code examples
canvasthree.jsautodesk-forgeautodesk-viewerautodesk

resize an object in three.js / Autodesk 3D Viewer (Autodesk Forge Viewer)


How do I resize and move or translate an object?

This change is such that I have to move the curtain up or down

I need to resize or position an object Animated or in one place (no difference)

viewer3D : LMV v3.3.6

try 1:

oViewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelectedCallback);
function onSelectedCallback(event) {
    var fragId = oViewer.getSelection()[0];
    if (typeof fragId == 'undefined') {return; }
    var fragIdsArray = (Array.isArray(fragId) ? fragId : [fragId]);

    fragIdsArray.forEach(function (subFragId) {

        var mesh = oViewer.impl.getRenderProxy(oViewer, subFragId);
        var mtx_for_method1 = mesh.matrixWorld.elements;
        mtx_for_method1[12] += 10;
        mtx_for_method1[13] += 20;
        mtx_for_method1[14] += 30;

    });

    oViewer.impl.invalidate(true);
}

try 2:

oViewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelectedCallback);
function onSelectedCallback(event) {
    var fragId = oViewer.getSelection()[0];
    if (typeof fragId == 'undefined') {return; }
    var fragIdsArray = (Array.isArray(fragId) ? fragId : [fragId]);
    fragIdsArray.forEach(function (subFragId) {
        var mesh = oViewer.impl.getRenderProxy(oViewer, subFragId);
            var transMat = new THREE.Matrix4();
            var m1 = new THREE.Matrix4();
            var m2 = new THREE.Matrix4();
            var m3 = new THREE.Matrix4();
            var alpha = 0;
            var beta = Math.PI;
            var gamma = Math.PI / 2;
            m1.makeRotationX(alpha);
            m2.makeRotationY(beta);
            m3.makeRotationZ(gamma);
            transMat.multiplyMatrices(m1, m2);
            transMat.multiply(m3);
            var mtx_for_method2 = mesh.matrixWorld;
            mtx_for_method2.multiply(transMat);
            mtx_for_method2.matrixWorldNeedsUpdate = True;
    });

    oViewer.impl.invalidate(true);
}

Is this possible?


Solution

  • Of course you can but can see there's a few issues with your code and they are:

    • Use get the fragments using instanceTree.enumNodeFragments, viewer.getSelection only gets you the dbids of the component(s) currently selected

    Edit:

    • Use the fragment proxy instead of the mesh proxy...
    const model = NOP_VIEWER.model;
    model.getData().instanceTree.enumNodeFragments(
          dbid, fragId => {
            const fragProxy = NOP_VIEWER.impl.getFragmentProxy(model, fragId);
            fragProxy.scale = new THREE.Vector3(1.1,1.1,1.1);
            fragProxy.updateAnimTransform()
        });
    NOP_VIEWER.impl.invalidate(true)