Three.js - 网格,三角形和Lambert材质

时间:2014-09-16 14:18:55

标签: javascript opengl-es three.js

我有一个明星的功能,我们走了:

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;
}

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

有人可以向我解释我的错误吗?

Three.js r68

1 个答案:

答案 0 :(得分:2)

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

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