three.js:定义我自制的几何体时出现“TypeError:v未定义”

时间:2012-10-26 17:41:44

标签: javascript firebug three.js

我正在尝试构建自己的three.js几何体。但是,当我尝试定义它时,像这样:

geometry = new THREE.FreeWallGeometry( 3, 5 );

我从three.js第714行的addSelf函数中得到错误“TypeError:v is undefined”。

如何找出导致此错误的原因?

这是我自制几何图形的当前代码:

define(
    [   "libs/three.js/build/three",
    ],
    function (
        three
        ) {

        console.log("setting up makeControls. regards, makeControls");

        //THREE.FreeWallGeometry = function ( length, height, depth, segmentsWidth, segmentsHeight, segmentsDepth, materials, sides ) {

        // X = length, Y = height, Z = depth,
        THREE.FreeWallGeometry = function ( noOfSegments, segmentLength ) {

            THREE.Geometry.call( this );

            var t1, t2,
            normal = new THREE.Vector3( 0, 0, 1);
            freePlane;

            var freePlane = function ( self, parametricPlane_X, parametricPlane_Y, parametricPlane_Z, equidistantSampler_T1, equidistantSampler_T2 ) {

                for ( t2 = 0; t2 < noOfSegments; t2 ++ ) {

                    for ( t1 = 0; t1 < noOfSegments; t1 ++ ) {

                        console.log("free: t1, t2 ", t1, t2);
                        //var x = t1 * segmentT1_length - length_half;
                        //var y = t2 * segmentT2_length - height_half;
                        var x = parametricPlane_X ( t1, t2 );
                        var y = parametricPlane_Y ( t1, t2 );
                        var z = parametricPlane_Z ( t1, t2 );

                        console.log("free: x, y z ", x, y, z);

                        self.vertices.push( new THREE.Vector3( x, - y, z ) );

                    }

                }

                for ( t2 = 0; t2 < noOfSegments; t2 ++ ) {

                    for ( t1 = 0; t1 < noOfSegments; t1 ++ ) {

                        var a = t1 + noOfSegments * t2;
                        var b = t1 + noOfSegments * ( t2 + 1 );
                        var c = ( t1 + 1 ) + noOfSegments * ( t2 + 1 );
                        var d = ( t1 + 1 ) + noOfSegments * t2;



                        //console.log ("free: a, b, c, d ", a, b, c, d);

                        var face = new THREE.Face4( a, b, c, d );

                        if (!self.vertices[face.a]) {
                            console.log("this face.a can't index vertices: ", face.a);
                        }

                        if (!self.vertices[face.b]) {
                            console.log("this face.b can't index vertices: ", face.b);
                        }

                        if (!self.vertices[face.c]) {
                            console.log("this face.c can't index vertices: ", face.c);
                        }

                        if (!self.vertices[face.d]) {
                            console.log("this face.d can't index vertices: ", face.d);
                        }

                        face.normal.copy( normal );
                        face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone(), normal.clone() );

                        self.faces.push( face );
                        self.faceVertexUvs[ 0 ].push( [
                            new THREE.UV( t1 / noOfSegments, 1 - t2 / noOfSegments ),
                            new THREE.UV( t1 / noOfSegments, 1 - ( t2 + 1 ) / noOfSegments ),
                            new THREE.UV( ( t1 + 1 ) / noOfSegments, 1 - ( t2 + 1 ) / noOfSegments ),
                            new THREE.UV( ( t1 + 1 ) / noOfSegments, 1 - t2 / noOfSegments )
                        ] );

                    }

                }

            }


            var parametricPlane_X = function ( t1, t2 ) {
                x = t1;
                return x;
            };

            var parametricPlane_Y = function ( t1, t2 ) {
                y = t1;
                return y;
            };

            var parametricPlane_Z = function ( t1, t2 ) {
                z = t1 * t2;
                return z;
            };

            var equidistantSampler_T1 = function ( t1 ) {
                t1 = Math.sqrt(t1);
                return t1;
            };

            var equidistantSampler_T2 = function ( t2 ) {
                t2 = t2;
                return t2;
            };

            freePlane(this, parametricPlane_X, parametricPlane_Y, parametricPlane_Z, equidistantSampler_T1, equidistantSampler_T2);


            this.computeCentroids();

        };

        THREE.FreeWallGeometry.prototype = Object.create( THREE.Geometry.prototype );

    }
);

1 个答案:

答案 0 :(得分:2)

您可以使用Firebugs console.trace()查看崩溃站点之前的呼叫。

我不知道通常是否会出现这种情况,但在您的情况下,console.trace()会向您显示调用addSelf之前的最后一个函数调用是对函数{{}的调用1}}围绕computeCentroids中的第4939行定义。

在此函数中,three.js - 数组(您定义的自定义几何体的定义在定义时填充顶点)由verticesface.aface.bface.c

这些索引取自几何体的face.d数组(您自己也在自定义几何体定义中填充),这里可能会出现问题:

面由顶点组成,如果生成的顶点与您生成的面的顶点索引不匹配,则某些面顶点索引可能会超出顶点数组的边界。因此faces将返回null而不是顶点,从而使您难以找到错误。

PS。您可能希望在vertices[face.outOfBoundVerticeIndex]未定义的情况下调用console.trace,以避免使用跟踪调用充斥Firefox。这种趋势会让Firefox陷入困境。