Apply custom appearance to individual model fragments in forge viewer - autodesk-forge

In the viewable, the model has three leaf nodes that are named “Solid1” but have a parent name of “Tread”, how do I search by parent name to get the dbId?
Following the answer from Default material for model in Forge Viewer I can see that we can set a color. Is it possible to instead apply a texture?
Thanks!

The Viewer has a search() function so you can search for any component based on its properties. You can then go up and down the instance tree to go from Thread to Solid1 or vice versa. See e.g.
https://forge.autodesk.com/blog/selection-override
Yes, you can also use texture for materials. See e.g. https://github.com/Autodesk-Forge/library-javascript-viewer-extensions/blob/master/src/Autodesk.ADN.Viewing.Extension.Material/Autodesk.ADN.Viewing.Extension.Material.js#L273
That code is used in this sample https://forge-rcdb.autodesk.io/configurator?id=58c7ae474c6d400bfa5aaf37
Just enable the "Material" extension by clicking on it, then you will be able to assign textured materials to faces.

Related

Node element not in instance tree

I'm trying to hide some elements of a model using the Forge viewer API, but I can't access some of them because their dbIds aren't found in the model instance tree.
It's very strange because I can't hide them using the context menu either, in the properties panel it shows undefined in the name, and it's also not found in the model browser panel.
Any ideas about how can I find those elements and show or hide them programmatically?
In the model that has this problem, those objects are curtain wall profiles.
In the SVF format it's common to have dbIDs without any visual representation. There might be different reasons for that but the one that's most common IMHO is instancing - you can have one object as a "template" (with no geometry), and a couple of objects (with geometries) that are "instances" of that template, inheriting its properties. That is also why the dbIDs of these "template" objects are not found in the model hierarchy.

Forge Viewer THREE.MeshLambertMaterial

One of the properties available on the var material = NEW THREE.MeshLambertMaterial is texture maps: {( map: new THREE.TextureLoader().load('wool.jpg') )}
We're very curious whether it would be possible to load in a texture this way and apply it to a specific object in the model?
Yes, but the way you modify materials in the Forge Viewer is a little bit different than what you would do in Three.js.
I have an extensive sample that illustrates how to modify materials, including custom textures here:
Viewing.Extension.Material
The live demo is there. To use it, load a model first with "Model Loader" +, then activate either "Theming color", "Material color" or "Texture" and pick a component of the loaded model. You can click in the square in each option to change color/texture.

Forge viewer: Auto-start animation

We would like to trigger/auto-start an animation/simulation of a specific object (Revit ElementID) when the model loads in the Forge viewer. The goal would be to move a cube (Revit ElementID) back and forth on the floor in a repeating loop. Would this be possible using any relevant extension or code?
Yes it is possible but there is no built-in extension you can use out of the box. The animation logic has to be a custom implementation.
From a given ElementId/UniqueId you need to find the corresponding dbId, see that reply for a starting point on how to do that.
From a given dbId, you can obtain fragmentIds and use those to apply transforms to the components in the viewer. The animation logic has to be by your own application. You can refer to that article How to create animations in the viewer? or one of the several demos I wrote performing animations:
Kinematics - source
Physics - source
You can use the Autodesk.Viewing.GEOMETRY_LOADED_EVENT and Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT to trigger the animation logic, so you can make sure the model is fully loaded. See also that article: Asynchronous viewer events notification.
Hope that helps.

Highlighting only the geometry of a parent object in autodesk's online viewer

We have revit model in which a parent element has both children and its own geometry. In the online viewer it is possible to select the parent through clicking the respective geometry, in which case only the geometry for the parent is highlighted. (selected through a click)
If I select the element through the tree the whole model gets highlighted.
(selected through the model browser)
In our web application, we are unable to get the former behavior. When passing the dbId of the parent to viewer.select, the parent plus all the children get highlighted. Same happens when we click the parent element's geometry. How can we select only the parent object programmatically?
We have tried passing different selection modes (overlayed, regular, mixed) to viewer.select but that didn't help and all objects got selected regardless of that parameter.
In this question it is mentioned by autodesk staff that only leaf nodes have geometries. That doesn't seem to be the case here:
Determine if node is an assembly or part in viewer
Thanks for the sample file. I did the following: open the model, select manually and, via console, typed .getSelection(), which returns me the dbId of the selected element. Now unselect everything. Then run .select(2890) and the result is as expected: only the top element is selected (as shown in the image below). I'm using Viewer 3.3.
You can try my sample live here, it uses the Basic Application tutorial.

How to set opacity of each nodes

Is there anyway to set opacity of some nodes of the model?
For instance, create a override material and use it in renderer for some nodes rather than whole scene.
When loading a model, the viewer builds its internal spatial indexing structures (BVHs), and one of the factors it takes into account is the transparency of the different fragments. This is in order to ensure that semi-transparent objects are rendered after the opaque ones.
However, when changing the material of a fragment on-the-fly (after initial load of your model), the order is not updated because the viewer doesn't have a way to rebuild the BVHs on demand, and you can end up with something like opaque objects hidden by transparent ones ... We have a pending change request in order to add an API that would allow developers to rebuild BVHs, unfortunately it has not yet been added yet.
You may take a look at the following resources for example of modifying materials in the viewer:
Autodesk.ADN.Viewing.Extension.Material
Forge Viewer Custom Shaders - Part 1
Forge Viewer Custom Shaders - Part 2
Custom transparent meshes with View & Data API
Hope that helps
Yes, sure.
The ForgeFader app creates and sets override materials on certain nodes in the viewer:
Check it out in:
src/client/viewer.components/Viewing.Extension.Fader/Viewing.Extension.Fader.Core.js.