如何在对象上调整大小 - Autodesk Forge Viewer

Posted

技术标签:

【中文标题】如何在对象上调整大小 - Autodesk Forge Viewer【英文标题】:How to resize on object - Autodesk Forge Viewer 【发布时间】:2020-01-18 01:59:30 【问题描述】:

如何更改对象的大小?

我需要改变物体的高度

例如,我们需要改变门或窗帘的高度

在这段代码中我的对象消失了

let change = function () 

    const viewer = oViewer;
    const model = viewer.model;

    const frags = [
        123,
        361,
    ];

    for(let i in frags)

        let fragId = frags[i];

        // Get mesh with frag id
        let mesh = viewer.impl.getRenderProxy(model, fragId);

        // Selection ID
        let dbId = 1280; // viewer.getSelection()[0]

        model.getData().instanceTree.enumNodeFragments(dbId, fragId => 
            mesh.scale.x += 0.5;
            // mesh.scale.y = 5;
            // mesh.scale.z = 5;

            model.getFragmentList().setMesh(fragId, mesh, true);
            viewer.impl.invalidate(true);
        );
    
;

以下代码调整了对象的大小,但变得太大而无法缩放

const viewer = oViewer;
const model = viewer.model;

viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, onSelectedCallback);

function onSelectedCallback(event) 

    const fragId = event.fragIdsArray[0];

    if (typeof fragId == 'undefined') return;

    const fragIdsArray = (Array.isArray(fragId) ? fragId : [fragId]);

    fragIdsArray.forEach(function (subFragId) 

        let mesh = viewer.impl.getRenderProxy(model, subFragId).clone();

        mesh.scale.y += 0.2;
        mesh.scale.x += 0.2;
        mesh.scale.z += 0.2;

        model.getFragmentList().setMesh(subFragId, mesh, true);
        viewer.impl.invalidate(true);
    );

【问题讨论】:

【参考方案1】:

我找到了解决方案并为它编写了脚本

let transform = new function () 

    let _self = this;

    this.fragId = null;
    this.proxy = null;
    this.viewer = oViewer;
    this.model = this.viewer.model;

    this.setFragId = function (fragId) 
        this.fragId = fragId;
        this.proxy = this.viewer.impl.getFragmentProxy(this.model, this.fragId);
        this.proxy.getAnimTransform();
    ;

    this.update = function()
        this.proxy.updateAnimTransform();
        this.viewer.impl.sceneUpdated(true);
    ;

    this.scaleX = function (num) 
        this.proxy.scale.x = num + 1;
        this.update();
    ;

    this.scaleY = function (num) 
        this.proxy.scale.y = num + 1;
        this.update();
    ;

    this.scaleZ = function (num) 
        this.proxy.scale.z = num + 1;
        this.update();
    ;

    this.positionX = function (num) 
        this.proxy.position.x = num;
        this.update();
    ;

    this.positionY = function (num) 
        this.proxy.position.y = num;
        this.update();
    ;

    this.positionZ = function (num) 
        this.proxy.position.z = num;
        this.update();
    ;

;

对于查找片段 ID,您可以使用以下代码

let selection = new function () 

    this.viewer = oViewer;

    let _self = this;

    this.ids = function () 
        return this.viewer.getSelection();
    ;

    this.count = function () 
        return this.viewer.getSelectionCount();
    ;

    // Mesh Object
    this.mesh = new function () 
        this.all = function () 
            if (_self.count() === 0) return ;

            let meshes = _self.viewer.impl.selectionMeshes;
            let output = [];

            for (let index in meshes) 
                output.push(meshes[index]);
            

            return output;
        ;

        this.fragIds = function()
            let meshes = this.all();
            let ids = [];
            meshes.forEach(function(mesh)
                ids.push(mesh.fragId);
            );
            return ids;
        ;

        this.first = function () 
            return this.all()[0];
        ;

        this.last = function () 
            return this.all().reverse()[0];
        
    ;

;

如何使用?

用鼠标选择你的元素

打开浏览器控制台

输入selection.mesh.fragIds() // [11]

输入transform.setFragId(11)

现在您可以更改比例和位置 :)

transform.scaleX(number);

transform.scaleY(number);

transform.scaleZ(number);


transform.positionX(number);

transform.positionY(number);

transform.positionZ(number);

【讨论】:

【参考方案2】:

考虑设置对象的比例:

mesh.scale.set(x,y,z)

查看使用参考here

【讨论】:

谢谢,但没什么不同

以上是关于如何在对象上调整大小 - Autodesk Forge Viewer的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Autodesk Forge 查看器上通过拖放添加对象

opengl:调整大小时如何将对象保留在窗口中

我们如何在 React Native for iOS 中调整 RCTRootView 的大小/开始?

如何使用 jquery 触发一般 DOM 对象调整大小的事件

如何在 React Native for Android 中调整字体大小以适应视图?

如何自动调整可可 xcode 的大小?