How do I get direct access to the Three.js scene? #1873
Replies: 15 comments 1 reply
-
@fselcukcan 🚨this is currently not supported by the public API, so be advised that even if you access the internal scene, That said, you can get access to internal details using We plan to expose access to the scene explicitly in a future release. Please comment on #738 if you would like to voice your support for this feature! |
Beta Was this translation helpful? Give feedback.
-
Thank you very much! Commented there as well. |
Beta Was this translation helpful? Give feedback.
-
I have accessed the scene property through the described
I wonder if it is possible to access the three.js instance used if possible, as well. Since I will make use of materials to change materials and also textures on the fly I will need it. Though I could include Three.js in a module script in the page I will author, I may make use of th existing one avoiding downloading again. Is it possible to access that too. I have only looked and could not see it between the own symbol properties of model-viewer. |
Beta Was this translation helpful? Give feedback.
-
If you look up |
Beta Was this translation helpful? Give feedback.
-
See related implementation here: And here: |
Beta Was this translation helpful? Give feedback.
-
Thank you I have checked them. What I want to do is to create new material instances and attach them to a mesh in model replacing the existing one. It looks like I need |
Beta Was this translation helpful? Give feedback.
-
@fselcukcan this is where it gets a little tricky (in case it did not already feel tricky enough :P). You will probably be able to get access to some constructors using JavaScript trickery, but TBH since this is totally unsupported territory YMMV. For example, I quickly applied the above hack to access the scene on our Glitch demo, which shows the Astronaut model. In that case, I got a reference to the scene. Then, I did this: const MeshStandardMaterial =
scene.model.children[0].children[0].children[0].material.constructor;
const newMaterial = new MeshStandardMaterial(/* options here */); In the above snippet, I found a material that was already in the scene and used it to get access to the constructor. You can do this for other classes too, like That said, please keep in mind 🔥 this is totally unsupported 🔥 and your strategy will depend on the model you load. |
Beta Was this translation helpful? Give feedback.
-
I can import the three.js with a script tag as well to saty on the safe and I actually did it. Thanks for the above trick as well. However I cannot happen to realize my original aim of updating the material/s in model viewer on runtime. It throws as follows:
I have an event handler function like this to update the selected material of selected mesh in two select html menus, scene I got by the method you have explained previously, and materials lib is an array of materials:
|
Beta Was this translation helpful? Give feedback.
-
@fselcukcan I'm not 100% sure, but I'm pretty sure that using another instance of Three.js will not work. You will have to use a trick like the one I showed you in order to access the constructors for Three.js classes. |
Beta Was this translation helpful? Give feedback.
-
Actually it did not work. And I have got the constructor as you mentioned and it worked when I have added emissive color to material I have created with it, not only color. But if the model does not have a constructor then this method is doomed. |
Beta Was this translation helpful? Give feedback.
-
@fselcukcan yes, unfortunately this approach is a total hack (and did I mention not supported? 😅). If you are able to clone the project and build the repo for yourself, another thing you can do is write your own mixin to extend the element class that gets defined. Your mixin can import the |
Beta Was this translation helpful? Give feedback.
-
@cdata @fselcukcan I think it's working, you can see my demo here. |
Beta Was this translation helpful? Give feedback.
-
Thanks @deers , i was also able to use the code and adapt it to my use case. |
Beta Was this translation helpful? Give feedback.
-
@ArvindSharda78 Your changes can only be maintained in the WebXR AR mode, since this stays in the browser. SceneViewer and QuickLook (the only option on iOS) are natives apps that must re-download your model, and hence cannot access the changes you've made in the browser. |
Beta Was this translation helpful? Give feedback.
-
Hello, I can access the scene with the following code. let scene = Though I'm aware this is not officially supported, I'm integrating ModelViewer into my personal project as a framework, and therefore I require more direct access to the THREE instances. is there any way to do that? |
Beta Was this translation helpful? Give feedback.
-
Is it possible to add a new texture and colors to the model on the fly?
How to find the Three.js Engine, Scene etc.?
For this maybe it is required to access the underlying engine, Three.js, and from there on go with three.js api to do these. The question for this is that does model-viewer exposes three.js and how?
Beta Was this translation helpful? Give feedback.
All reactions