意外的空数组

时间:2018-11-01 04:17:59

标签: javascript three.js

很抱歉,冗长的代码,但是我没有给出一个重现此问题的最小示例。由以下函数返回的两个数组的每个单元格都包含一个数字,如预期的那样:

function VNtorus(R, r, nx, ny) {
    var Vertices = new Array(nx);
    var Normals = new Array(nx);
    for (var i = 0; i < nx; i++) {
        Vertices[i] = new Array(ny);
        Normals[i] = new Array(ny);
        var u = i / nx * 2 * Math.PI;
        var cos_u = Math.cos(u);
        var sin_u = Math.sin(u);
        var cx = R * cos_u;
        var cy = R * sin_u;
        for (var j = 0; j < ny; j++) {
            var v = j / ny * 2 * Math.PI;
            var rcos_v = r * Math.cos(v);
            var rsin_v = r * Math.sin(v);
            Vertices[i][j] = new THREE.Vector3(
                cx + rcos_v * cos_u,
                cy + rcos_v * sin_u,
                rsin_v
            );
            Normals[i][j] = new THREE.Vector3(
                rcos_v * cos_u,
                rcos_v * sin_u,
                rsin_v
            );
        }
    }
    return {
        vertices : Vertices,
        normals : Normals
    }
}

如果需要,此代码的上下文位于:Cannot render a mesh with THREE js

但是,当我如下调用此函数时,对于两个结果数组,第一个数组为空:

var a = 3; var c = 0.7; var mu = 1.7;
var b = Math.sqrt(a * a - c * c);
var bb = b * Math.sqrt(mu * mu - c * c);
var omega = (a * mu + bb) / c;
var Omega = new THREE.Vector3(omega, 0, 0)
var inversion = function (M) {
    var OmegaM = Omega.sub(M);
    var k = OmegaM.dot(OmegaM);
    return Omega.addScaledVector(OmegaM, k);
}
var d = (a - c) * (mu - c) + bb;
var r = c * c * (mu - c) / ((a + c) * (mu - c) + bb) / d;
var R = c * c * (a - c) / ((a - c) * (mu + c) + bb) / d;
var omegaT = omega - (b * b * (omega - c)) /
    ((a - c) * (mu + omega) - b * b) / ((a + c) * (omega - c) + b * b);
var tmesh = VNtorus(10, 3, 64, 32);
var tvertices = tmesh.vertices;
var tnormals = tmesh.normals;
console.log(tvertices)
// ...

0: Array(32)
length: 32
__proto__: Array(0)
1: Array(32)
0: p {x: 0.008700136926854488, y: 0.0008568886953253847, z: 0}
1: p {x: 0.008532966235351477, y: 0.0008404238193190658, z: 0.0017055250840917766}
....

我真的在这里迷路了。我不是Java语言方面的专家,并且可能在某个地方犯了新手错误。

1 个答案:

答案 0 :(得分:0)

我已经使用jquery.mon.js和three.js为您创建了一个代码段,然后单击按钮,我正在调用您的代码,它正在为我生成包含64个元素的数组。

您可能要验证您的three.js CDN。

(64) [Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32)]

function myFunction(elmnt, clr) {
            var a = 3; var c = 0.7; var mu = 1.7;
            var b = Math.sqrt(a * a - c * c);
            var bb = b * Math.sqrt(mu * mu - c * c);
            var omega = (a * mu + bb) / c;
            var Omega = new THREE.Vector3(omega, 0, 0)
            var inversion = function (M) {
                var OmegaM = Omega.sub(M);
                var k = OmegaM.dot(OmegaM);
                return Omega.addScaledVector(OmegaM, k);
            }
            var d = (a - c) * (mu - c) + bb;
            var r = c * c * (mu - c) / ((a + c) * (mu - c) + bb) / d;
            var R = c * c * (a - c) / ((a - c) * (mu + c) + bb) / d;
            var omegaT = omega - (b * b * (omega - c)) /
                ((a - c) * (mu + omega) - b * b) / ((a + c) * (omega - c) + b * b);
            var tmesh = VNtorus(10, 3, 64, 32);
            var tvertices = tmesh.vertices;
            var tnormals = tmesh.normals;
            console.log(tvertices)
        }

        function VNtorus(R, r, nx, ny) {
            var Vertices = new Array(nx);
            var Normals = new Array(nx);
            for (var i = 0; i < nx; i++) {
                Vertices[i] = new Array(ny);
                Normals[i] = new Array(ny);
                var u = i / nx * 2 * Math.PI;
                var cos_u = Math.cos(u);
                var sin_u = Math.sin(u);
                var cx = R * cos_u;
                var cy = R * sin_u;
                for (var j = 0; j < ny; j++) {
                    var v = j / ny * 2 * Math.PI;
                    var rcos_v = r * Math.cos(v);
                    var rsin_v = r * Math.sin(v);
                    Vertices[i][j] = new THREE.Vector3(
                        cx + rcos_v * cos_u,
                        cy + rcos_v * sin_u,
                        rsin_v
                    );
                    Normals[i][j] = new THREE.Vector3(
                        rcos_v * cos_u,
                        rcos_v * sin_u,
                        rsin_v
                    );
                }
            }
            return {
                vertices: Vertices,
                normals: Normals
            }
        }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script src="https://threejs.org/build/three.js"></script>
 <button onclick="myFunction()">Click me</button </body> </body> </html>