我尝试更新的3组件ArrowHelper
用three.js
。现在,ArrowHelper
仅在绘画函数的每次调用中分配新的动画,就可以成功更新动画3 。
例如,在我的main
函数中,我首先分配如下3 ArrowHelper
:
// Parameters for vector
var directionMainVectorX = new THREE.Vector3(1, 0, 0);
var directionMainVectorY = new THREE.Vector3(0, 1, 0);
var directionMainVectorZ = new THREE.Vector3(0, 0, 1);
var originMainVector = new THREE.Vector3(0, 0, 0);
var lengthVector = 20;
var widthVector = 3;
var headLengthVector = 1.5;
var headWidthVector = 1.5;
var hexVector = 0x11ff00;
mainVectorX = new THREE.ArrowHelper(directionMainVectorX, originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
mainVectorY = new THREE.ArrowHelper(directionMainVectorY, originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
mainVectorZ = new THREE.ArrowHelper(directionMainVectorZ, originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
然后,进入drawVector()
函数,我有:
function drawVector() {
// Parameters for ArrowHelper
var headLengthVector = 1.5;
var headWidthVector = 1.5;
var hexVector = 0x11ff00;
var widthVector = 3;
// SOLUTION : Allocating each time a new vector
mainVectorX = new THREE.ArrowHelper(directionMainVectorX.normalize(), originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
mainVectorY = new THREE.ArrowHelper(directionMainVectorY.normalize(), originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
mainVectorZ = new THREE.ArrowHelper(directionMainVectorZ.normalize(), originMainVector, lengthVector, hexVector, headLengthVector, headWidthVector);
// Width for mainVector
mainVectorX.line.material.linewidth = widthVector;
mainVectorY.line.material.linewidth = widthVector;
mainVectorZ.line.material.linewidth = widthVector;
// Add arrows to scene reference
scene.add(mainVectorX);
scene.add(mainVectorY);
scene.add(mainVectorZ);
}
最后,我的render
功能:
function render() {
rotateCamera();
drawVector();
controls.update();
renderer.render(scene, camera);
scene.remove(mainVectorX);
scene.remove(mainVectorY);
scene.remove(mainVectorZ);
}
使用以上所有代码,一切正常。
我的问题是我不想使用“ allocating at each call of drawVector() function
”。
这就是为什么我只分配一次3的原因ArrowHelper
(我在main
代码的开头进行分配),然后通过使用intodrawVector()
函数来更新它们的组件,如下所示:
function drawVector() {
// Parameters for ArrowHelper
var headLengthVector = 1.5;
var headWidthVector = 1.5;
var hexVector = 0x11ff00;
var widthVector = 3;
// Compute coordinates of 3vectors
// Compute directions
directionMainVectorX.set(1, 0, 0);
directionMainVectorY.set(0, 1, 0);
directionMainVectorZ.set(0, 0, 1);
// Update mainVector
// SOLUTION : update the parameters of vector
mainVectorX.position.set(originMainVector);
mainVectorY.position.set(originMainVector);
mainVectorZ.position.set(originMainVector);
mainVectorX.setDirection(directionMainVectorX.normalize());
mainVectorY.setDirection(directionMainVectorY.normalize());
mainVectorZ.setDirection(directionMainVectorZ.normalize());
mainVectorX.setLength(lengthVector, headLengthVector, headWidthVector);
mainVectorX.setColor(hexVector)
mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
mainVectorY.setColor(hexVector)
mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
mainVectorY.setColor(hexVector)
// Add arrows to scene reference
scene.add(mainVectorX);
scene.add(mainVectorY);
scene.add(mainVectorZ);
}
我在上一篇文章中看到了这种方法: update ArrowHelper
但是不幸的是,什么都没有出现,我错过了一些东西,但是我不知道...
如果有人可以帮助我,那就太好了。
Vector3.set
需要3个参数-x, y, z
不是另一个向量,因此您应该使用mainVectorX.position.set(originMainVector.x,originMainVector.y,originMainVector.z);
或mainVectorX.position.copy(originMainVector);
将您的drawVector(现在可以称为updateVector :)重写为
function drawVector() {
// Parameters for ArrowHelper
var headLengthVector = 1.5;
var headWidthVector = 1.5;
var hexVector = 0x11ff00;
var widthVector = 3;
if(!mainVectorX.parent){
// Add transported arrow to scene reference
scene.add(mainVectorX);
scene.add(mainVectorY);
scene.add(mainVectorZ);
}
mainVectorX.position.copy(originMainVector);
mainVectorY.position.copy(originMainVector);
mainVectorZ.position.copy(originMainVector);
mainVectorX.setDirection(directionMainVectorX.normalize());
mainVectorY.setDirection(directionMainVectorY.normalize());
mainVectorZ.setDirection(directionMainVectorZ.normalize());
mainVectorX.setLength(lengthVector, headLengthVector, headWidthVector);
mainVectorX.setColor(hexVector);
mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
mainVectorY.setColor(hexVector);
mainVectorY.setLength(lengthVector, headLengthVector, headWidthVector);
mainVectorY.setColor(hexVector);
}
当然,您应该删除
scene.remove(mainVectorX);
scene.remove(mainVectorY);
scene.remove(mainVectorZ);
从渲染功能
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句