【问题标题】:Three.js - Mesh, triangles and Lambert materialThree.js - 网格、三角形和 Lambert 材质
【发布时间】:2014-09-17 01:37:29
【问题描述】:

我有一个制作星星的功能,我们开始吧:

function CreateStar( radius, thickness, isWireframe, starColor) {

            // material
            var starMaterial = new THREE.MeshLambertMaterial({
                    color: starColor,
                    wireframe: isWireframe,
                    shading: THREE.FlatShading
                  });

            // array for vertices
            var vertices = [];
            // set "zero" vertex for thickness
            vertices.push( new THREE.Vector3(0, 0, thickness) );

            // calculate a vertex and a pit for a half of a ray... 5 times for each 72 degrees
            var deg = Math.PI / 180; // for me it's easier to work with degrees rather than radians
            var maxR, minR; 
            maxR = radius; // radius for a vertex
            var  x4Rad = maxR * Math.cos( - 72 * deg );
            minR = x4Rad/Math.cos( - 36 * deg ); // radius for a pit
            var firstVertex;

            for ( var i = 0; i < 5; i++ ){

                // vertex
                var vertX = maxR * Math.cos( (90 + (72 * i)) * deg );
                var vertY = maxR * Math.sin( (90 + (72 * i)) * deg );
                if ( i == 0 ) firstVertex = new THREE.Vector3( vertX, vertY, 0 );
                vertices.push( new THREE.Vector3( vertX, vertY, 0 ));

                // pit
                var pitX = minR * Math.cos( (126 + (72 * i)) * deg );
                var pitY = minR * Math.sin( (126 + (72 * i)) * deg );
                vertices.push( new THREE.Vector3( pitX, pitY, 0 ));
            }
            vertices.push( firstVertex ); // add the first vertex again to close the contour of the star

            var holes = []; // no holes in our contour
            var triangles, star;
            var geometry = new THREE.Geometry();

            geometry.vertices = vertices;
            triangles = THREE.Shape.Utils.triangulateShape( vertices, holes ); // triangulation

            for ( var j = 0; j < triangles.length; j++ ){
                geometry.faces.push( new THREE.Face3( triangles[j][0], triangles[j][1], triangles[j][2] ));
            } 

            star = new THREE.Mesh( geometry, starMaterial );
            //star = new THREE.Mesh( new THREE.CubeGeometry(200,200,200), starMaterial);
            return star;
}

我的问题是,当我从该函数返回一个立方体(在此代码中注释)并添加到场景时,我得到一个具有正确阴影的立方体,这取决于定向光源的位置,但是,当我返回一个星并将其添加到场景中,我得到......嗯......只是一个没有颜色的黑色星(它是黑色),没有阴影......什么都没有。那么为什么我可以将材质应用于立方体,但不能将其应用于星形呢?

谁能给我解释一下我出了什么问题?

三.js r68

【问题讨论】:

    标签: javascript opengl-es three.js


    【解决方案1】:

    也许你只需要重新计算你的法线。 试试看:

        geometry.computeFaceNormals();
        geometry.computeVertexNormals();
        star = new THREE.Mesh( geometry, starMaterial );
    

    【讨论】:

    • 朋友,你让我度过了一个美好的夜晚!它正在工作)非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 2017-06-14
    • 1970-01-01
    • 2019-07-08
    • 2017-01-13
    • 2013-06-11
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多