问题
I'm hiding and unhiding parts of my Three.js scene using the .visible
property on my objects.
By default certain objects (rooms) have the .visible
property to false
. When the camera is within a certain mesh (the room bounding box) the .visible
property is set to true
and the room shows up.
But there seems to be a delay (seconds or less) after setting the .visible
property to true
and the room actually being rendered. This delay is shortened after entering the rooms more than once.
What is the cause of this delay? Is there a way to know when or if the room is ready for rendering? It doesn't seem like update events are being fired after setting the .visible
property to true
, so listening for those won't help.
I appreciate any help,
Greets!
EDIT
Because I couldn't use ColladaLoader2.js
I decided to simply traverse the models loaded with the ColladaLoader.js
and replace all geometry properties with a BufferGeometry
copy made from the existing Geometry
object. After that I found out that setting the .dynamic
property of an existing Geometry
object to false seems to have the same effect.
dae.traverse(function (obj) {
if (obj.hasOwnProperty('geometry')) {
obj.dynamic = false;
//obj.geometry = new THREE.BufferGeometry().fromGeometry(obj.geometry);
}
});
Now when I set the object's .visible
property to true
the engine freezes for a little while, instead of the earlier mentioned delay before the object becomes visible. For now I'll have to decide where I want to little freeze to occur, because I don't think all objects can be visible at the same time for performance reasons.
It would be nice to have more control and information about if an object and it's geometry is loaded and ready to be viewed, or if it needs to be reloaded into memory. Now it's unclear if a BufferGeometry
will show up immediately when .visible
is set to true
or a short freeze will occur.
回答1:
Geometry
needs to be converted to BufferGeometry
prior to rendering. This conversion will not occur if mesh.visible
is false
. The conversion can take some time if your geometries are complex, or if there are a lot of geometries to convert.
A work-around is to create your meshes using BufferGeometry
.
var bufferGeometry = new THREE.BufferGeometry().fromGeometry( geometry );
var mesh = new THREE.Mesh( bufferGeometry, material );
three.js r.73
回答2:
In my code I see the same thing when objects are first switched to visible, but subsequent switches between visible true/false do not seem to cause another delay.
Hence one workround would be to set the visible flag on every object and render everything when WebGLRenderer is not shown on the browser (e.g. when it is behind a level loading screen) then set the objects visible flags appropriately to how you want them prior to showing the screen... Now there should be no delay as you toggle them visible.
来源:https://stackoverflow.com/questions/34616054/three-js-object-visible-true-not-showing-up-right-away