Forge viewer: Auto-start animation - autodesk-forge

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.

Related

Autodesk Forge Viewer: how to set object occlusion

I'm playing around with the Autodesk forge viewer and adding custom elements, like it is done in the Autodesk's scene builder example.
I am looking for a possibility to set the "visibility" of one of the new objects - e.g. when moving around the camera, the green cube should remain on it's 3D-position but should not be occluded by any other object (like it is somehow in blue with outline when it is selected). Or maybe even better: if it is covered by another object, it should shine through (with configurable opacity).
Unfortunately after discussing with Engineering it's not yet possible - will continue the conversation to see if we could come up with a workaround for the upcoming releases ...
Like I suggested in the comments try append your geometry to Viewer3DImpl.sceneAfter for the time being (which will be publicly documented and exposed as Viewer3D.sceneAfter in a couple of weeks) or an overlay. Then setting depthTest to false should do what you want...
The choice of sceneAfter or an overlay depends on how you want the objects to be drawn during progressive rendering. sceneAfter is only drawn once at the end of everything else. Overlays are redrawn for each progressive frame...

Mouse click vs viewer.select function selection behave differently in Autodesk Forge multi models viewer

I have been working on multi-model from the last couple of weeks and found that selection of a model object using mouse click vs viewer select function behave differently.
I have two models one architectural and another mechanical. When I select a wall or room on the architectural model using mouse click, Object select with overlay without having any problem but when I try using viewer select function with same object, it hides behind the wall and overlay doesn't work. It is only happening with the architectural model and working fine with the mechanical model.
Please suggest me if I am doing something wrong with multi-model.
To programmatically select components when working with multiple models use viewer.select(dbid, model):
let model = viewer.impl.modelQueue().getModels()[index] or viewer.impl.findModel(modelId);
viewer.select(dbid, model)
And to get selected dbids use viewer.getAggregateSelection (see doc here) the event to subscribe to AGGREGATE_SELECTION_CHANGED_EVENT (doc here) to track selection changes:
If the issue persists can you send some screenshots and your models (links via a sharing service like weTransfer/Dropbox) for further investigation over to forge.help#autodesk.com?

Cloning a viewer material

I want to override the color of a component in the viewer, in order to conserve the same rendering effect than other components I would like to clone an existing material and simply modify the color of the clone.
I can change the color of an existing material as follow:
var renderProxy =
viewer.impl.getRenderProxy(
model, fragIds[0])
renderProxy.material.setHex(0xFF0000)
This affects all other components in the model which are using that material, which is not the desired result.
For that purpose I would like to clone material, modify it and affect the new material to a specific component. Invoking the material.clone() method is working:
var newMat = renderProxy.material.clone()
newMat.setHex(0xFF0000)
But the new material will loose all the specific properties that makes it look nice by the renderer.
So my question "is there a way to -easily- clone a viewer material without writing the cloning code for each property"?
You will need to clone the 'prism' material, rather than the 3js phong-material.
Start with this repo: https://github.com/wallabyway/fusion-chair-configurator
as an example, to create a 'metal' material, use these two lines of code (and copy the initPaint() function).
https://github.com/wallabyway/fusion-chair-configurator/blob/c6d5bd575cdf40194c9fbdd1c5f9bb27c70b356e/docs/js/app.js#L107-L108
The szPrism json string, contains lots of parameters. Prism materials are rather complex, but you can find out more about what these parameters do by understanding this article for the Autodesk Cloud Render ART help page (the real-time renderer built inside Revit2019, Fusion360, etc)...
http://help.autodesk.com/view/ARENDERING/ENU/?guid=GUID-49345267-CE6A-4006-BB58-5BEAFD8B0D0E
Try experimenting with Fusion360's 'render' mode. Start by opening 'appearance' and creating some custom materials. You can modify their parameters in real-time to better understand what they do and get the effect you are looking for.
Here is a tutorial video on Fusion360 custom materials: https://www.youtube.com/watch?v=D9AS5rQhtPo
Let me know if that helps.

How to write Javascript code for model browser while using autodesk-forge-viewer API

I am trying to replicate a part of demo shown here using autodesk-forge-viewer api. Particularly, I am not able to find code snippet around how can I do navigation for a particular area of 3D model. For example, if render 3D model is house, I require that If a click on "Bed Room" which is a link somewhere in browser, 3D rendered model should show me that "Bed Room". In sample app shown, they are providing the same as a feature of "Model Browser".
Could anyone please help me with that?
You first need to know the dbId's of the nodes you are interested in, most viewer API methods take dbIds as input. You get those either by listening to selection events, see that post, or by parsing the instance tree and checking the properties of the nodes to see if those are the ones you looking for.
You can then use methods such as viewer.fitToView([dbIds]), viewer.hide([dbIds]), viewer.show([dbIds]) ... Here is another article that might be useful: Hidding completely viewer nodes (no ghosting).
Hope that helps.

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.