使随机顶点/三角形隐藏在bufferGeometry中

时间:2017-04-12 16:57:34

标签: three.js fragment-shader vertex-shader buffer-geometry

我的问题是关于在bufferGeometry中使一些顶点/三角形不可见。我已经从another question复制了这个着色器:

<script type="x-shader/x-vertex" id="vertexshader">
    attribute float visible;
    varying float vVisible;
    attribute vec3 color;
    varying vec3 vColor;

void main() {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    vColor = color;
    vVisible = visible;
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
varying float vVisible;
varying vec3 vColor;

void main() {
    if (vVisible > 0.0) {
      gl_FragColor = vec4(vColor, 1.0);
     }else
        discard;
}
</script>

然后这就是我定义缓冲区几何的方法:

 var geometry = new THREE.BufferGeometry();
 var sides = 4;
 var heightSegments = 1;
 var height = 20;
var radius = 10;
var indices= [];
var vertices;
function vertexes(){
    for (var j=0; j <=height; j = j + (height/heightSegments)) {
        for (var i=0;i<=sides;i++) {


 vertex.push([radius*Math.cos(2*Math.PI*i/sides),j,radius*Math.sin(2*Math.PI*i/sides)]);

        }
    }
    for ( var j = 0; j<sides; j++ ) {
        for (var i = 0 ; i <sides*heightSegments; i+=sides) {

            indices.push(i + j);
            indices.push(i + j + 1);
            indices.push(i + sides + j + 1);
            indices.push(i + j + 1);
            indices.push(i + sides + j + 1 + 1);
            indices.push(i + sides + j + 1);
        }
    }

 } // three components per vertex

function updatePositions() {
    for ( var k=0; k<(sides+1)*(heightSegments+1) ; k++ )
    {
        vertices[ k*3 + 0 ] = vertex[k][0];
        vertices[ k*3 + 1 ] = vertex[k][1];
        vertices[ k*3 + 2 ] = vertex[k][2];
        line_visible[k] = 1;
        line_colors[ k*3 + 0 ] = color.r;
        line_colors[ k*3 + 1 ] = color.g;
        line_colors[ k*3 + 2 ] = color.b;
    }

}
vertexes();
vertices = new Float32Array( vertex.length*3 );
line_visible = new Float32Array(vertex.length);
var color = new THREE.Color(0xffff00);
var line_colors = new Float32Array(vertex.length*3);
updatePositions();

geometry.setIndex( indices );
geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
geometry.addAttribute('color', new THREE.BufferAttribute(line_colors, 3));
geometry.addAttribute('visible', new THREE.BufferAttribute(line_visible, 1));
var shader_material = new THREE.ShaderMaterial({
    vertexShader: document.getElementById('vertexshader').textContent,
    fragmentShader: document.getElementById('fragmentshader').textContent
});

var mesh = new THREE.Mesh( geometry, shader_material );
scene.add(mesh);

所以当我用这行代码隐藏一些顶点或三角形时:

geometry.attributes.visible.array[0]=0;

我还在上面的代码行之后添加了这行代码:

geometry.attributes.visible.needsUpdate = true;
没有改变!只是添加我随机想要隐藏它们,所以我认为setDrawRange不会起作用!

1 个答案:

答案 0 :(得分:1)

更改visible属性后,请务必设置needsUpdate标记:

geometry.attributes.visible.array[0]=0;
geometry.attributes.visible.needsUpdate = true;

这告诉THREE.js属性的值已经改变,需要重新推送到GPU。当GPU具有新值时,着色器应该按预期工作。