2013-12-10 2 views
1

안녕하세요 저는 webGL과 자바 스크립트를 배우고 있습니다. 내가 만든 한three.js에 새로 태어난이 공을 three.js에서 움직이는 법

이 그들은

http://goo.gl/gOiHX4

공은 나머지 '참여'하는 것과 동일한 개체이야 ... WebGL을 현장 일을 three.js를, 그리고 실제로 생각하는 올 그래서 나는 다른 블렌더를 블렌더로 만들 것이다.

그래서

는 tribunal.js, 나는 ball.js하고 구조의 나머지 부분이 있다고 어떻게 것 I 모드이 경우 3D 환경을 따라 ball.js?

어쩌면 구조를 둘러싼 원과 비슷할 것입니다. 상수 루프. 너무 코드

페이스트 빈 : 위치, 회전 및/또는 배율 :

http://paste.ubuntu.com/6549663/

<!doctype html> 
<html lang="en"> 
<head> 
    <title>My 3D webGL experiment</title> 
    <meta charset="utf-8"> 
</head> 
<body style="margin: 0;"> 

    <script src="js/three.min.js"></script> 
    <script src="js/OrbitControls.js"></script> 

    <script> 

    // Set up the scene, camera, and renderer as global variables. 
    var scene, camera, renderer; 

    init(); 
    animate(); 

    // Sets up the scene. 
    function init() { 

     // Create the scene and set the scene size. 
     scene = new THREE.Scene(); 
     var WIDTH = window.innerWidth, 
      HEIGHT = window.innerHeight; 

     // Create a renderer and add it to the DOM. 
     renderer = new THREE.WebGLRenderer({antialias:true}); 
     renderer.setSize(WIDTH, HEIGHT); 
     document.body.appendChild(renderer.domElement); 

     // Create a camera, zoom it out from the model a bit, and add it to the scene. 
     camera = new THREE.PerspectiveCamera(45, WIDTH/HEIGHT, 0.1, 20000); 
     camera.position.set(90,80,0); 
     scene.add(camera); 

     // Create an event listener that resizes the renderer with the browser window. 
     window.addEventListener('resize', function() { 
     var WIDTH = window.innerWidth, 
      HEIGHT = window.innerHeight; 
     renderer.setSize(WIDTH, HEIGHT); 
     camera.aspect = WIDTH/HEIGHT; 
     camera.updateProjectionMatrix(); 
     }); 

     // Set the background color of the scene. 
     renderer.setClearColorHex(0xB5DBDB, 1); 

     // Create a light, set its position, and add it to the scene. 
     var light = new THREE.PointLight(0xf44fff); 
     light.position.set(200,200,200); 
     scene.add(light); 

     // Load in the mesh and add it to the scene. 
     var loader = new THREE.JSONLoader(); 
     loader.load("models/tribunal.js", function(geometry){ 
     var material = new THREE.MeshLambertMaterial({color: 0xCC0000}); 
     mesh = new THREE.Mesh(geometry, material); 
     scene.add(mesh); 
     }); 

     // Add OrbitControls so that we can pan around with the mouse. 
     controls = new THREE.OrbitControls(camera, renderer.domElement); 

    } 


    // Renders the scene and updates the render as needed. 
    function animate() { 

     // Read more about requestAnimationFrame at http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/ 
     requestAnimationFrame(animate); 

     // Render the scene. 
     renderer.render(scene, camera); 
     controls.update(); 

    } 

    </script> 

</body> 
</html> 

답변

2

가 three.js를에서는, 물체의 움직임이 속성을 변경함으로써 달성 될 수있다. 이러한 속성은 단순한 숫자가 아니므로 필요에 맞게 변경하면 내장 함수를 사용하여 변경 사항을 올바르게 처리해야합니다. 예를 들어, 메시의 위치가 너무 같은 집합() 함수를 사용하여 변경 될 수있는 벡터로 정의한다 : 문서에 기재된 벡터의 값을 변경하기위한 다양한 방법이있다

mesh.position.set(0, 0, 0); // Standard [ x, y, z ] coordinate system 

및 암호. 개체의 속성을 개체 자체로 생각하고 해당 개체와 해당 부모 개체에 대해 사용할 수있는 방법을 익히십시오.

질문에 대답하려면 : 일정 기간 동안 객체를 계속 이동하려면 animate() 함수 내에 더 많은 코드가 필요합니다.

mesh.position.translateX(1); 

팁 : 다음 코드는 x 축을 따라 긍정적 인 방향으로 애니메이션() 함수가 호출 될 때마다 메쉬 1 단위 이동합니다

  • 운동의 많은 종류가 있습니다 , 그러나 그들은 주로 위치, 회전 및/또는 규모의 조합입니다.

  • 하위 개체는 상위 개체의 영향을받습니다. 메쉬 B가 메쉬 A에 부착 된 경우, 메쉬 A에 적용된 이동은 메쉬 B도 이동합니다.

  • animate() 루프 내의 객체에 대한 변수 참조가 전역이어야하므로 animate() 루프는 사용자가 말하는 객체를 알고 있습니다.

  • 위치, 배율 및 회전이 변경되면 카메라의 절두체 (또는 시야) 밖으로 개체를 빠르게 이동할 수 있습니다.

  • 애니메이션을 디버깅하는 데 도움이되는 OrbitControls.js 및 console.log()를 사용하십시오.

관련 문제