three.js 画网格

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <script src="three.js-master/build/three.js"></script>
    <style type="text/css">
        div#canvas-frame {
            border: none;
            cursor: pointer;
            width: 100%;
            height: 600px;
            background-color: #EEEEEE;
        }
    </style>
    <script>
        var renderer;
        function initThree() {
            width = document.getElementById('canvas-frame').clientWidth;    //获得标签为canvas-frame宽度
            height = document.getElementById('canvas-frame').clientHeight;  //获得标签为canvas-frame高度
            renderer = new THREE.WebGLRenderer({antialias : true });          //创建一个渲染器默认为false
            renderer.setSize(width, height);                                    //设置渲染器大小
            document.getElementById('canvas-frame').appendChild(renderer.domElement);//将画布添加到标签中
            //renderer.setClearColorHex(0xFFFFFF, 1.0);//老版本中的函数
            renderer.setClearColor(0xFFFFFF,1.0);//设置画布的背景颜色,就是当渲染时,渲染的背景颜色是什么
        }

        var camera;
        function initCamera() {
            camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);//创建一个透视相机
            camera.position.x = 0;
            camera.position.y = 1000;
            camera.position.z = 0;//设置相机的位置
            camera.up.x = 0;
            camera.up.y = 0;
            camera.up.z = 1;
            camera.lookAt({
                x : 0,
                y : 0,
                z : 0
            });
        }

        var scene;
        function initScene() {
            scene = new THREE.Scene();
        }

        var light;
        function initLight() {
            light = new THREE.DirectionalLight(0xFF0000, 1.0, 0);
            light.position.set(100, 100, 200);
            scene.add(light);
        }
        var cube;
        function initObject() {
            var material=new THREE.LineBasicMaterial({vertexColors:THREE.VertexColors});//创建一个材质
            var color1=new THREE.Color(0x444444);
            var color2=new THREE.Color(0xFF0000);
            var geometry=new THREE.Geometry();//创建点的集合
            var p1=new THREE.Vector3(-500,0,0);
            var p2=new THREE.Vector3(500,0,0);
            geometry.vertices.push(p1);
            geometry.vertices.push(p2);
            geometry.colors.push(color1,color2);
            for(var i=0;i<=20;i++)
            {
                var line=new THREE.Line(geometry,material,THREE.LinePieces);
                line.position.z=(i*50)-500;
                scene.add(line);

                line=new THREE.Line(geometry,material,THREE.LinePieces);
                line.position.x=(i*50)-500;
                line.rotation.y=(90 * Math.PI) / 180;
                scene.add(line);
            }
        }
        function threeStart() {
            initThree();
            initCamera();
            initScene();
            initLight();
            initObject();
            renderer.clear();
            renderer.render(scene, camera);
        }
    </script>
</head>
<body onload="threeStart();">
<div id="canvas-frame"></div>
</body>
</html>

你可能感兴趣的:(three.js 画网格)