![](/img/trans.png)
[英]Three.js - Uncaught TypeError: Cannot read property 'normal' of undefined
[英]Three.js program returns: "Cannot read property '0' of undefined"
以下代碼用於an object according to
四元數數據an object according to
設置動畫:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 1, 0.3, 2 );
var material = new THREE.MeshBasicMaterial( { color: 0xf4b942 } );
var obj = new THREE.Mesh( geometry, material );
scene.add( obj );
var geometry = new THREE.BoxBufferGeometry(1, 0.3, 2 );
var edges = new THREE.EdgesGeometry(geometry);
var lines = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ color: 0x000000 }));
scene.add( lines );
camera.position.z = 5;
renderer.render(scene,camera);
var xpos_o = obj.rotation.x;
var ypos_o = obj.rotation.y;
var zpos_o = obj.rotation.z;
var xpos_lo = lines.rotation.x;
var ypos_lo = lines.rotation.y;
var zpos_lo = lines.rotation.z;
let i = 0; let m = 0;
var animate = function (quat_data) {
requestAnimationFrame( animate );
if (m % 5 == 0 || m == 0) {
if (m !== 0) {i++;}
if (i >= quat_data.length){
obj.rotation.x = xpos_o;
obj.rotation.y = ypos_o;
obj.rotation.z = zpos_o;
lines.rotation.x = xpos_lo;
lines.rotation.y = ypos_lo;
lines.rotation.z = zpos_lo;
i=0;
}
let quat = quat_data[i];
var quaternion = new THREE.Quaternion(quat[0],quat[1],quat[2],quat[3]);
obj.applyQuaternion(quaternion);
lines.applyQuaternion(quaternion);
renderer.render( scene, camera );
}
m++;
}
var animate1 = function() {
var quat_data1 = [ [0.681492531,-0.014533572,0.711492547,-0.170668778 ],
[-0.81103207,0.125979118,0.555748976,-0.132257921 ],
[-0.459080697,-0.267181198,-0.846533742,0.035103342 ],
[0.874097952,-0.166578614,-0.244525976,0.385231457 ],
...
animate(quat_data1);
}
var animate2 = function() {
var quat_data2 = [[-0.395306087,-0.567475272,-0.168275478,-0.702416024 ],
[0.591221013,-0.226393713,-0.208636898,0.745435603 ],
[-0.279990579,0.754847831,0.56444738,-0.182233852 ],
[-0.419803039,-0.623439075,-0.538698243,-0.380648559 ],
[0.878036311,0.088829796,-0.024437397,0.469642749 ],
...
animate(quat_data2);
}
var animate3 = function() {
var quat_data3 = [ [-0.17958507,0.232682609,-0.217296778,-0.930800793 ],
[-0.178598829,0.228014038,-0.2182292,-0.931927075 ],
[-0.177601473,0.223351625,-0.219152185,-0.933029522 ],
[-0.176574343,0.218685883,-0.220066047,-0.934113976 ],
[-0.175533915,0.214015872,-0.22097227,-0.935177153 ],
[-0.174483815,0.209350901,-0.221866773,-0.936217247 ],
[-0.173407996,0.204681945,-0.222753878,-0.937238325 ],
...
animate(quat_data3);
}
我省略了一些quaternion
數據並用省略號代替它以節省空間。 開始部分只是建立場景的幾何形狀。 animate 函數采用quaternion
數組。 requestAnimationFrame
每次迭代后,m 增加 1,每次達到 5 的倍數時,執行動畫代碼。 (這是因為我想將requestAnimationFrame
的 60 fps 轉換為 12 fps。)動畫代碼將 i 增加 1(移動到下一個quaternion
)並應用quaternion
。 如果 i 超過quaternion
數據數組的長度,則它會循環回到數據的開頭並重置對象的原始方向。
還有其他三個函數animate1
、 animate2
和animate3
,它們使用不同的quaternion
數據集來執行animate
函數。
當我單擊 html 代碼中的按鈕來執行animate1
,控制台在以下行中給我問題中所述的錯誤:
var quaternion = new THREE.Quaternion(quat[0],quat[1],quat[2],quat[3]);
問題是動畫( requestAnimationFrame
)在錯誤的位置和錯誤的回調目標執行。 回調函數只傳遞一個參數,即時間值。
var animate = function (quat_data) { requestAnimationFrame( animate ); // [...] let quat = quat_data[i]; var quaternion = new THREE.Quaternion(quat[0],quat[1],quat[2],quat[3]); // [...] }
您期望函數animate
的輸入是動畫數據數組,但它是單個時間值。
你必須這樣做:
var animate = function (quat_data) {
requestAnimationFrame( function() { animate(quat_data); } );
// [...]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.