three.js(7)-几何体

作者: 姜治宇 | 来源:发表于2021-11-17 11:14 被阅读0次

    几何体其实就是由一系列的顶点组成,我们在坐标系打几个点看看:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
    </head>
    
    <body>
        <div id="webgl"></div>
    </body>
    
    </html>
    <script>
        var scene = new THREE.Scene();
        var axes = new THREE.AxesHelper(200);//50表示xyz轴的长度,红:x,绿:y,蓝:z
        scene.add(axes);//添加坐标轴
        var geometry = new THREE.BufferGeometry(); //创建一个Buffer类型几何体对象
        var vertices = new Float32Array([
            0, 0, 0, //顶点1坐标
            50, 0, 0, //顶点2坐标
            0, 100, 0, //顶点3坐标
            0, 0, 10, //顶点4坐标
            0, 0, 100, //顶点5坐标
            50, 0, 10, //顶点6坐标
    
        ]);
        //创建属性缓冲区对象
        var attr = new THREE.BufferAttribute(vertices, 3);//3个为一组,表示一个顶点的xyz坐标
        geometry.attributes.position = attr;
    
        // 点渲染模式
        var material = new THREE.PointsMaterial({
            color: 0xffff00,
            size: 10.0 //点对象像素尺寸
        }); //材质对象
        var points = new THREE.Points(geometry, material); //点模型对象
        scene.add(points); //点对象添加到场景中
    
        //创建相机对象
        var camera = new THREE.PerspectiveCamera(45, 2, 0.1, 2000);
        camera.position.set(300, 300, 100); //设置相机位置
        camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    
    
        /**
         * 创建渲染器对象
         */
        var renderer = new THREE.WebGLRenderer({
            antialias: true //消除锯齿
        });
        renderer.setSize(1000, 500);//设置渲染区域尺寸
        renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
        renderer.render(scene, camera);//渲染必须有场景和相机两个对象
    
        var cont = document.getElementById('webgl');
        cont.appendChild(renderer.domElement);
    </script>
    
    point.png

    我们将顶点连起来:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
    </head>
    
    <body>
        <div id="webgl"></div>
    </body>
    
    </html>
    <script>
        var scene = new THREE.Scene();
        var axes = new THREE.AxesHelper(200);//50表示xyz轴的长度,红:x,绿:y,蓝:z
        scene.add(axes);//添加坐标轴
        var geometry = new THREE.BufferGeometry(); //创建一个Buffer类型几何体对象
        var vertices = new Float32Array([
            0, 0, 0, //顶点1坐标
            50, 0, 0, //顶点2坐标
            0, 100, 0, //顶点3坐标
            0, 0, 10, //顶点4坐标
            0, 0, 100, //顶点5坐标
            50, 0, 10, //顶点6坐标
    
        ]);
        //创建属性缓冲区对象
        var attr = new THREE.BufferAttribute(vertices, 3);//3个为一组,表示一个顶点的xyz坐标
        geometry.attributes.position = attr;
    
        // 线条渲染模式
        var material = new THREE.LineBasicMaterial({
            color: 0xffff00 //线条颜色
        });//材质对象
        var line = new THREE.Line(geometry, material);//线条模型对象
        scene.add(line);//线条对象添加到场景中
    
        //创建相机对象
        var camera = new THREE.PerspectiveCamera(45, 2, 0.1, 2000);
        camera.position.set(300, 300, 100); //设置相机位置
        camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    
    
        /**
         * 创建渲染器对象
         */
        var renderer = new THREE.WebGLRenderer({
            antialias: true //消除锯齿
        });
        renderer.setSize(1000, 500);//设置渲染区域尺寸
        renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
        renderer.render(scene, camera);//渲染必须有场景和相机两个对象
    
        var cont = document.getElementById('webgl');
        cont.appendChild(renderer.domElement);
    </script>
    
    line.png

    如果将这些点连起来形成面:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
    </head>
    
    <body>
        <div id="webgl"></div>
    </body>
    
    </html>
    <script>
        var scene = new THREE.Scene();
        var axes = new THREE.AxesHelper(200);//50表示xyz轴的长度,红:x,绿:y,蓝:z
        scene.add(axes);//添加坐标轴
        var geometry = new THREE.BufferGeometry(); //创建一个Buffer类型几何体对象
        var vertices = new Float32Array([
            0, 0, 0, //顶点1坐标
            50, 0, 0, //顶点2坐标
            0, 100, 0, //顶点3坐标
            0, 0, 10, //顶点4坐标
            0, 0, 100, //顶点5坐标
            50, 0, 10, //顶点6坐标
    
        ]);
        //创建属性缓冲区对象
        var attr = new THREE.BufferAttribute(vertices, 3);//3个为一组,表示一个顶点的xyz坐标
        geometry.attributes.position = attr;
        // 三角面(网格)渲染模式
        var material = new THREE.PointsMaterial({
            color: 0xffff00, //三角面颜色
        }); //材质对象
        var mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
        scene.add(mesh);
        //创建相机对象
        var camera = new THREE.PerspectiveCamera(45, 2, 0.1, 2000);
        camera.position.set(300,300, 100); //设置相机位置
        camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
    
    
        /**
         * 创建渲染器对象
         */
        var renderer = new THREE.WebGLRenderer({
            antialias: true //消除锯齿
        });
        renderer.setSize(1000, 500);//设置渲染区域尺寸
        renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
        renderer.render(scene, camera);//渲染必须有场景和相机两个对象
    
        var cont = document.getElementById('webgl');
        cont.appendChild(renderer.domElement);
    </script>
    
    polygon.png

    相关文章

      网友评论

        本文标题:three.js(7)-几何体

        本文链接:https://www.haomeiwen.com/subject/rgdltrtx.html