简体   繁体   English

Three.js GridHelper:如何隐藏/显示网格?

[英]Three.js GridHelper: How to hide/show the grid?

I am adding a plane and a grid using Gridhelper to my 3d scene: 我正在使用Gridhelper为我的3D场景添加一个平面和一个网格:

// PLANE XY static
var gridplaneSize = 20;
var color = 0xFFDCBB;
var plGeometry = new THREE.PlaneGeometry(gridplaneSize, gridplaneSize);
var plMaterial = new THREE.MeshBasicMaterial( {color:color, ambient:color, side:THREE.DoubleSide, opacity:0.5, transparent:true, depthWrite: false } );
var planeMesh_xy = new THREE.Mesh(plGeometry, plMaterial);
planeMesh_xy.rotation.x = -Math.PI/2;
scene.add(planeMesh_xy);
planeMesh_xy.receiveShadow = true;
// GRID XY static
var gridstep = 1;
var gridcolor = 0xCCCCCC;
var gridHelper_xy = new THREE.GridHelper(gridplaneSize/2, gridstep);
gridHelper_xy.position = new THREE.Vector3(0, 0, 0);
gridHelper_xy.setColors( new THREE.Color(gridcolor), new THREE.Color(gridcolor) );
scene.add(gridHelper_xy);

Then I would like to decide if the plane or the grid is visible. 然后我想决定飞机或网格是否可见。

This works for the plane: 这适用于飞机:

planeMesh_xy.visible = false;

But not for the grid: 不是网格:

gridHelper_xy.visible = false; // not working

I also tried workarounds, not working: 我也尝试过解决方法,而不是工作:

gridHelper_xy.material.transparent = true;
gridHelper_xy.material.opacity = 0;
gridHelper_xy.parent.visible = false;

Can anyone tell me how to hide the grid then? 谁能告诉我如何隐藏网格呢?

What should happen when you set the visible flag of an object is that all of its children should inherit the flag. 设置对象的visible标志时应该发生的是它的所有子节点都应该继承该标志。

In r71 this should work out of the box (look at issue https://github.com/mrdoob/three.js/issues/1524 ) 在r71中,这应该是开箱即用的(请查看问题https://github.com/mrdoob/three.js/issues/1524

In r65 you would have to traverse your hierarchy setting the flag on all the sub-parts: 在r65中,您必须遍历您的层次结构,在所有子部分上设置标志:

object.traverse ( function (child) {

    child.visible = false;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM