In my ThreeJS
app (r124) I have a GLB animation model that I attach a Spotlight to, to make it look like a camera drone with a light that turns on and off. I have a function to "turn on the light" and the only thing it does is make the light visible by setting its visible
property to true
:
this.turnOnLight = function(bUseLaserScan=false) {
if (self.CAC.modelInstance.visible === false)
self.CAC.modelInstance.visible = true;
}
Unfortunately, there is something about doing this that interferes with the render/animate loop. Every time I turn on the light (or off) there is a long pause in the 200-300 millisecond range, freezing everything in the scene for that length of time. In other words, the render loop is not being called for 200-300 milliseconds. If I comment out the statements that change the light's visibility property, the pause/delay goes away.
Why would simply turning a model's visibility property on and off freeze the scene, and how can I fix this problem?
If I was loading a large model or something every time I turn on the light sub-object, I could see this happening. But how is this happening when all I am doing is setting the visible
property to TRUE?
Here is how I construct the Spotlight and attach it to the camera drone main animation object. Note, self.CAC.modelInstance
is simply an object I use to aggregate ThreeJS objects and properties common to all my animation models.
function AnimatedLight(
modelName,
modelDeclJsonObj,
threeJsLightObjectName='PointLight',
topLevelAnimationModelObj=null,
bAddLensSphere=false,
bAddLaserScan=false) {
const self = this;
this._buildTheLight = function(
threeJsLightObjectName,
modelDeclJsonObj) {
const methodName = self.constructor.name '::' `_buildTheLight`;
const errPrefix = '(' methodName ') ';
let retLightObj = null;
if (misc_shared_lib.isEmptySafeString(threeJsLightObjectName))
throw new Error(`${errPrefix}The threeJsLightObjectName parameter is empty.`);
if (!misc_shared_lib.isNonNullObjectAndNotArray(modelDeclJsonObj))
throw new Error(`${errPrefix}The modelDeclJsonObj is not a valid object.`);
// Provide default values for the properties the modelDeclJsonObj
// object doesn't have.
//
// Default color is RED.
let color = typeof modelDeclJsonObj.color !== 'undefined' ? modelDeclJsonObj.color : 0xf41321; // 0xffffff;
let intensity = typeof modelDeclJsonObj.intensity !== 'undefined' ? modelDeclJsonObj.intensity : 8.6; // 1.0;
let distance = typeof modelDeclJsonObj.distance !== 'undefined' ? modelDeclJsonObj.distance : 0.0;
let decay = typeof modelDeclJsonObj.decay !== 'undefined' ? modelDeclJsonObj.decay : 1.0;
// These properties are only for spot-lights, which have an inner angle.
// NOTE: We store angles in the JSON model initialization declaration
// object because they are easier to specify then angles expressed
// in radians.
let angle = (0.8 * MAX_ANGLE_FOR_SPOTLIGHT); // Math.PI / 3; // Default value.
if (typeof modelDeclJsonObj.inner_angle_in_degrees !== 'undefined') {
if (typeof modelDeclJsonObj.inner_angle_in_degrees !== 'number')
throw new Error(`${errPrefix} The "inner_angle_in_degrees" property in the model JSON declaration object is not a number.`);
angle = THREE.MathUtils.degToRad(modelDeclJsonObj.inner_angle_in_degrees);
}
let penumbra = 0;
if (typeof modelDeclJsonObj.penumbra_angle_in_degress !== 'undefined') {
if (typeof modelDeclJsonObj.penumbra_angle_in_degress !== 'number')
throw new Error(`${errPrefix} The "penumbra_angle_in_degress" property in the model JSON declaration object is not a number.`);
// ThreeJS uses a range of 0 to 1.0 for the penumbra angle, so
// we divide by 180 to rescale the provided value.
penumbra = Math.min(THREE.MathUtils.degToRad(modelDeclJsonObj.penumbra_angle_in_degress / 180.0), 1);
}
// Build the correct ThreeJS light object given the specified
// light object type.
if (threeJsLightObjectName === 'PointLight') {
retLightObj = new THREE.PointLight(color, intensity, distance, decay);
}
else if (threeJsLightObjectName === 'DirectionalLight') {
retLightObj = new THREE.DirectionalLight(color, intensity);
}
else if (threeJsLightObjectName === 'SpotLight') {
// Create a mini-menu for this type of light.
retLightObj = new THREE.SpotLight(color, intensity, distance, angle, penumbra, decay);
// Is a lens sphere desired?
if (bAddLensSphere) {
// Yes. Create it.
// .................... BEGIN: SUB-OBJECT - Lens Sphere ............
const radius = 3;
self.lensSphereObj =
new THREE.Mesh(
new THREE.SphereBufferGeometry(radius, 20, 20),
new THREE.MeshPhongMaterial({color: 0xFF0000}));
// Add it to the top level animation model.
// self.CAC.modelInstance.add(cameraLensObj);
// Add it to the spotlight object.
retLightObj.add(self.lensSphereObj);
// .................... END : SUB-OBJECT - Lens Sphere ............
}
}
else
throw new Error(`${errPrefix}Invalid threeJsLightObjectName value: ${threeJsLightObjectName}`);
return retLightObj;
}
/**
* Makes the light visible.
*/
this.turnOnLight = function(bUseLaserScan=false) {
if (self.CAC.modelInstance.visible === false)
self.CAC.modelInstance.visible = true;
}
/**
* Makes the light invisible.
*/
this.turnOffLight = function() {
if (self.CAC.modelInstance.visible === true)
self.CAC.modelInstance.visible = false;
}
this.setTargetForSpotLight = function(targetObj) {
self.CAC.modelInstance.target = targetObj;
}
/**
* This method must be called to initialize this
* animated light for animation.
*/
this.initializeModel = function (
parentAnimManagerObj,
initModelArgs= null,
funcCallWhenInitialized = null,
) {
const methodName = self.constructor.name '::' `initializeModel`;
const errPrefix = '(' methodName ') ';
// Store the reference to the AnimationManager() object
// that owns us in the CommonAnimationObject we
// aggregate.
self.CAC.parentAnimManagerObj = parentAnimManagerObj;
// Create a ThreeJS light object from the model (light)
// declaration in the JSON declarations block.
self.CAC.modelInstance = self._buildTheLight(threeJsLightObjectName, initModelArgs);
self.lensSphereObj.position.set(
0,
0,
-20);
// Animated lights don't use a model loader.
self.CAC.modelLoader = null;
// Set the initial position.
self.CAC.modelInstance.position.x = initModelArgs.initialPos_X;
self.CAC.modelInstance.position.y = initModelArgs.initialPos_Y;
self.CAC.modelInstance.position.z = initModelArgs.initialPos_Z;
// Add the model to the scene.
g_ThreeJsScene.add(self.CAC.modelInstance);
// Finish the initialization process..
self.CAC.initializeModelCompletely(null);
// Execute the desired callback function, if any.
if (funcCallWhenInitialized)
funcCallWhenInitialized(self);
}
CodePudding user response:
The answer is a bit complicated. Three.js uses a loop in your material's shader code to render each source of light. For example, if you have 3 spot lights you'd see something like this:
for (int i = 0; i < 3; i ) {
// Perform spotlight calculations
}
When you remove a light, the engine re-compiles the shader code so it only loops twice: i < 2
. This has to be done for every material on the scene, so depending on your scene complexity and your graphics card, the new shader compilation might create a bottleneck.
To get around this, I recommend you simply change the light intensity to 0 to turn it off. That way you don't modify the shader, it just changes the value of a variable.
this.toggleLight = function() {
if (light.intensity === 0) {
light.intensity = 1; // ... or whatever value you need
} else {
light.intensity = 0;
}
}