【问题标题】:three.js: THREE.Vector3.sub() acting weirdlythree.js: THREE.Vector3.sub() 行为怪异
【发布时间】:2012-08-31 09:44:46
【问题描述】:

为什么在这种情况下 THREE.Vector3.sub 返回 (0,0,0)?

p0 = new THREE.Vector3( 0, 100, 50 );
p1 = new THREE.Vector3( 0, 50, 100 );
dummy = new THREE.Vector3(0,0,0);
p1_relative_to_p0 = dummy.sub(p1, p0);
console.log(p1_relative_to_p0);

这是 THREE.Vector3 原型的子函数:

sub: function ( a, b ) {
    this.x = a.x - b.x;
    this.y = a.y - b.y;
    this.z = a.z - b.z;
    return this;
},

控制台输出:

三个.Vector3 x: 0 是:0 z: 0

为什么不是输出 (0, 50, -50) ?

代码可以在这里看到: https://dl.dropbox.com/u/2070405/webgl_lines_splines_jon.html

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    您成为console.log 警告的受害者。在 Chrome 中,记录的对象在您展开时进行评估,而不是在您记录时进行评估。

    由于return this,确实p1_relative_to_p0 === dummy。您稍后将更新dummy,因此还要更新p1_relative_to_p0,因为对象在JavaScript 中是共享的。展开对象时,您实际上是在读取 dummy 的内容,同时这些内容已设置为 0, 0, 0

    尝试设置断点而不是日志来停止执行以观察正确的值。

    【讨论】:

    • 或者试试这个console.log(p1_relative_to_p0.x, p1_relative_to_p0.y, p1_relative_to_p0.z )
    【解决方案2】:

    我试图复制 here 的行为,但它完全按照您的预期工作。您是否有可能只是不小心从错误区域读取了值?

    【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 2011-12-17
    • 2015-08-21
    • 2019-11-03
    • 2012-08-05
    • 2012-12-03
    相关资源
    最近更新 更多