Alex Page
Alex Page

Reputation: 11

Autodesk Forge - Isolating element: amount of transparency of others

When we isolate an element in a 3d view, is there anyway to control the amount of transparency of all the other elements? Say, change to 50% translucent? Have I missed something obvious? And can you do the same for 2d views?

Upvotes: 0

Views: 657

Answers (1)

Felipe
Felipe

Reputation: 4375

I dug out the following code for you, it shows how to set all leaf nodes to 50% opacity by changing their material properties:

AutodeskNamespace("Autodesk.ADN.Viewing.Extension");

function getLeafNodes(model, nodeId) {

  return new Promise((resolve, reject)=>{

    try{

      var leafIds = [];

      var instanceTree = model.getData().instanceTree

      nodeId = nodeId || instanceTree.getRootId()

      function _getLeafNodesRec(id){

        var childCount = 0;

        instanceTree.enumNodeChildren(id,
          function(childId) {
            _getLeafNodesRec(childId)
            ++childCount
          })

        if(childCount == 0){
          leafIds.push(id)
        }
      }

      _getLeafNodesRec(nodeId)

      return resolve(leafIds)

    } catch(ex){

      return reject(ex)
    }
  })
}

function nodeIdToFragIds(model, nodeId) {

  var instanceTree = model.getData().instanceTree

  var fragIds = []

  instanceTree.enumNodeFragments(
    nodeId, (fragId) => {
      fragIds.push(fragId)
    });

  return fragIds
}


Autodesk.ADN.Viewing.Extension.Basic = function (viewer, options) {

  Autodesk.Viewing.Extension.call(this, viewer, options);

  var _this = this;

  _this.load = function () {

    var fragList = viewer.model.getFragmentList()

    getLeafNodes(viewer.model).then((dbIds) => {

      dbIds.forEach((dbId) => {

        const fragIds = nodeIdToFragIds(
          viewer.model, dbId)

        fragIds.forEach((fragId) => {

          var material = fragList.getMaterial(fragId)

          if(material) {

            material.opacity = 0.5
            material.transparent = true
            material.needsUpdate = true
          }
        })
      })

      viewer.impl.invalidate(true, true, true)
    })


    return true;
  };

  _this.unload = function () {

    Autodesk.Viewing.theExtensionManager.unregisterExtension(
      "Autodesk.ADN.Viewing.Extension.Basic");

    return true;
  };
};

Autodesk.ADN.Viewing.Extension.Basic.prototype =
  Object.create(Autodesk.Viewing.Extension.prototype);

Autodesk.ADN.Viewing.Extension.Basic.prototype.constructor =
  Autodesk.ADN.Viewing.Extension.Basic;

Autodesk.Viewing.theExtensionManager.registerExtension(
  "Autodesk.ADN.Viewing.Extension.Basic",
  Autodesk.ADN.Viewing.Extension.Basic);

Some syntax requires ES6 transpiling. You can quickly paste the code there to test it: http://viewer.autodesk.io/node/gallery/#/extension-editor?id=560c6c57611ca14810e1b2bf

This works only for 3D, I'll see what we can do for 2D and update that topic.

Upvotes: 2

Related Questions