三个js隐藏多个对象只隐藏单个对象
Three js hiding multiple objects just hide single object
当我设置我的灯光对象名称 light.name = 'globalLight'
并且我正在创建变量 lightGlobal = scene.getObjectByName('globalLight');
但是当我试图使用 [=13= 隐藏时,我 运行 遇到了问题] 只是让另一盏灯没有隐藏绿色的是 light0
蓝色的是 light1
渲染示例 The rendered image example
问题screenshot
如你所见,蓝光消失了,但绿光不会隐藏
我的代码:
ambient = new THREE.AmbientLight( 0x404040 );
scene.add( ambient );
lightCam = new THREE.DirectionalLight( 0xffffff, 1.0 );
light0 = new THREE.PointLight( 0x0000ff, 1.0 );
light0.position.set( 4.0, 4.0, 4.0 );
light0.castShadow = true;
light0.name = "globalLight";
light1 = new THREE.PointLight( 0x00ff00, 1.0 );
light1.position.set( 4.0, 4.0, -4.0 );
light1.castShadow = true;
light1.name = "globalLight";
scene.add( light0, light1 );
globalLight = scene.getObjectByName( "globalLight", true );
globalLight.visible = false;
Object3D.name
必须是唯一的。因此,不支持以相同方式命名两个灯。 Object3D.getObjectByName()
将 return 匹配给定名称的第一个对象。
此问题的一个解决方案是使用 Object3D.userData
并定义自定义 属性,例如:
light0.userData.tag = 'globalLight';
您可以考虑通过以下方法增强Object3D
:
THREE.Object3D.prototype.getObjectsByTag = function( tag, result ) {
// check the current object
if ( this.userData.tag === tag ) result.push( this );
// check children
for ( let i = 0, l = this.children.length; i < l; i ++ ) {
const child = this.children[ i ];
child.getObjectsByTag( tag, result );
}
return result;
};
当我设置我的灯光对象名称 light.name = 'globalLight'
并且我正在创建变量 lightGlobal = scene.getObjectByName('globalLight');
但是当我试图使用 [=13= 隐藏时,我 运行 遇到了问题] 只是让另一盏灯没有隐藏绿色的是 light0
蓝色的是 light1
渲染示例 The rendered image example
问题screenshot
如你所见,蓝光消失了,但绿光不会隐藏
我的代码:
ambient = new THREE.AmbientLight( 0x404040 );
scene.add( ambient );
lightCam = new THREE.DirectionalLight( 0xffffff, 1.0 );
light0 = new THREE.PointLight( 0x0000ff, 1.0 );
light0.position.set( 4.0, 4.0, 4.0 );
light0.castShadow = true;
light0.name = "globalLight";
light1 = new THREE.PointLight( 0x00ff00, 1.0 );
light1.position.set( 4.0, 4.0, -4.0 );
light1.castShadow = true;
light1.name = "globalLight";
scene.add( light0, light1 );
globalLight = scene.getObjectByName( "globalLight", true );
globalLight.visible = false;
Object3D.name
必须是唯一的。因此,不支持以相同方式命名两个灯。 Object3D.getObjectByName()
将 return 匹配给定名称的第一个对象。
此问题的一个解决方案是使用 Object3D.userData
并定义自定义 属性,例如:
light0.userData.tag = 'globalLight';
您可以考虑通过以下方法增强Object3D
:
THREE.Object3D.prototype.getObjectsByTag = function( tag, result ) {
// check the current object
if ( this.userData.tag === tag ) result.push( this );
// check children
for ( let i = 0, l = this.children.length; i < l; i ++ ) {
const child = this.children[ i ];
child.getObjectsByTag( tag, result );
}
return result;
};