mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.67k stars 35.37k forks source link

Rendering multiple scenes causes shader attributes to be lost for cloned meshes. #6332

Closed jee7 closed 5 years ago

jee7 commented 9 years ago

I have 2 scenes. The first scene has a Mesh cube1 with ShaderMaterial, that sends custom attributes to the shaders. Second scene has a Mesh cube2, that is a clone of cube1, with the material reassigned to MeshBasicMaterial. When I first render scene2 and then scene1, then the cube1 is black. Demo: http://tume-maailm.pri.ee/ylikool/ComputerGraphics-2015/other/threejs/bug/test.html

What I expect: expected The actual result: result

It works correctly, if:

I can also have the shaders for ShaderMaterial return a single color, and the cube1 would be in that color. So something is happening with the attribute values.

I suspect that this may be related to: https://github.com/mrdoob/three.js/issues/6033

Using Chrome 41.0.2272.101 m and Intel HD Graphics 4000.

mrdoob commented 9 years ago

Unrelated but... be aware that you're doing scene1.add(camera) and then rendering scene2 first. camera is in another scene and its matrixWorld hasn't been updated at that point.

WestLangley commented 9 years ago

you're doing scene1.add( camera )

Good point. The camera does not have to be added to the scene graph, and in this case, it is better if it is not.

jee7 commented 9 years ago

Thanks for the info. There doesn't seem to be much clear documentation, on when to add the camera to the scene and when not. At least, I haven't found it so far. But, yes, the matrixWorld seems to be not updated, like you said. Checked the lib, and seems that it is updated, when the camera has no parent, or possibly together with the scene graph of the current scene. So, if I have a one scene and one camera, I should add it? If I have multiple scenes and one camera, then I shouldn't add it to any of the scenes?

mrdoob commented 9 years ago

Usually you want to add the camera to the graph when you want it to be a child of an object. Otherwise, there is no need.

WestLangley commented 9 years ago

Otherwise, there is no need.

If you add a light as a child of the camera, for example, you need to add the camera to the scene, so the light will be part of the scene graph.

It used to be required that the camera be added to the scene. It is no longer a requirement, because there can be multiple scenes, and an object can have only one parent.

WestLangley commented 5 years ago

@jee7 Closing now. Please create a new issue if there is something you feel we need to address.