【问题标题】:How to resize on object - Autodesk Forge Viewer如何在对象上调整大小 - 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);
    });
}

【问题讨论】:

    标签: javascript autodesk-forge autodesk-viewer autodesk viewer


    【解决方案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

      【讨论】:

      • 谢谢,但没什么不同
      猜你喜欢
      • 2020-01-16
      • 2020-03-08
      • 2020-08-06
      • 2021-01-16
      • 2021-11-26
      • 2020-05-06
      • 2021-04-26
      • 2017-11-29
      • 2017-06-28
      相关资源
      最近更新 更多