Three.js(2)网格

线条

Three.js中,一条直线由点,材质,颜色组成。

点由THREE.Vector3表示,Threejs中没有提供单独画点的函数,它必须被放到一个THREE.Geometry形状中,这个结构中包含一个数组vertices,这个vertices就是存放无数的点(THREE.Vector3)的数组。
绘制一条线的步骤:

1.定义两个点

 var p1 = new THREE.Vector3( -100, 0, 100 );

var p2 = new THREE.Vector3( 100, 0, -100 ); 

2.把两个点放入geometry.vertices数组中

var geometry = new THREE.Geometry();

geometry.vertices.push(p1);

geometry.vertices.push(p2); 

3.THREE.Line画线

var line = new THREE.Line( geometry, material, THREE.LinePieces ); 

绘制网格,坐标平面

POINT:绘制一条线段,沿平行z轴复制移动,绕y轴旋转90度,平行z轴复制移动


<html>
    <head>
        <meta charset="UTF-8">
        <title>网格title>
        <script src="js/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;
                height=document.getElementById('canvas-frame').
                clientHeight;

                renderer=new THREE.WebGLRenderer({
                    antialias : true
                });

                renderer.setSize(width,height);
                document.getElementById('canvas-frame').appendChild(renderer.domElement);
                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 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);


                for (var i = 0; i < 20; i++){
                    var line = new THREE.Line( geometry, new
                        THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2}));
                    line.position.z = ( i * 50 ) - 500;
                    scene.add( line );

                     var line = new THREE.Line( geometry, new
                        THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
                    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(2)网格_第1张图片

你可能感兴趣的:(图形学)