2015-11-21 59 views
0

我是Three.js和一般3D編程的新手,但我試圖讓this three.js example的馬從固定攝像頭在屏幕上移動。翻譯自定義幾何三,js

的代碼,「負載」馬是在這裏:

var loader = new THREE.JSONLoader(); 
loader.load("models/animated/horse.js", function(geometry) { 

    mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ 
      vertexColors: THREE.FaceColors, 
      morphTargets: true 
      })); 
      mesh.scale.set(1.5, 1.5, 1.5); 
      scene.add(mesh); 

      mixer = new THREE.AnimationMixer(mesh); 

      var clip = THREE.AnimationClip.CreateFromMorphTargetSequence('gallop', geometry.morphTargets, 30); 
       mixer.addAction(new THREE.AnimationAction(clip).warpToDuration(1)); 

      }); 

在我的渲染功能,我可以在不增加對象的X和Z poisition以同樣的方式,我可以用相機的:

var radius = 600; 
var theta = 0; 
var prevTime = Date.now(); 

function render() { 

theta += 0.1; 
camera.position.x = radius * Math.sin(THREE.Math.degToRad(90)); 
camera.position.z = radius * Math.cos(THREE.Math.degToRad(90)); 

mesh.position.x = theta; 
mesh.position.z = theta; 
} 

即使mesh.position.x在Javascript控制檯中輸入時,我也會得到一個Uncaught TypeError: Cannot set property 'x' of undefined。我如何翻譯這個對象?

+0

是你的'camera'還是'mesh'定義爲全局變量? – gaitat

+0

@gaitat是的,他們是:/ – trembling

+2

'if(mesh){mesh.position.x = theta; 「工作? – WestLangley

回答

0

loader.load()是一種異步方法。這就是您指定回調函數的原因。在引用它之前,您需要確保模型已加載。

一種解決方案是在渲染循環使用此模式:

if (mesh) { mesh.position.x = theta; } 

另一種解決方案是發起animate()在回調函數。

three.js r.73