threejs 绘制辅助网格

ALLENJIAO 2019-12-20

GridHelper.js可以帮助绘制一个xz平面网格,它没有提供更多的参数,所以不能用于生成xy网格。

xy网格实现代码如下:

var size = 6000;
        var divisions = 50;

        // var gridHelper = new THREE.GridHelper(size, divisions, 0x1231c3, 0x135133);
        // this.scene.add(gridHelper);

        var geometry = new THREE.Geometry(); //创建geometry  
        geometry.vertices.push(new THREE.Vector3(-size / 2, 0, 0)); //添加顶点  
        geometry.vertices.push(new THREE.Vector3(size / 2, 0, 0));
        for (var i = 0, len = size / divisions; i <= len; i++) {
            var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({
                color: 0x455454
            })); //利用geometry和material创建line  
            line.position.y = i * divisions - size / 2; //设置line的位置  
            this.scene.add(line); //将line添加到场景中  

            var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({
                color: 0x324132
            }));
            line.position.x = i * divisions - size / 2;
            line.rotation.z = Math.PI / 2; //绕y轴旋转90度  
            this.scene.add(line);
        }

相关推荐