2017-02-13 16 views
0

私はThree.jsと一般的なJavaScriptの新機能です。基本的なThree.jsのテンプレートシーンは、アニメーションできません

私は基本的なフランケンシュタインのテンプレート(主にLee Stemkoskiの例をベースにしています)を使ってThree.jsを使用しようとしていますが、キューブを無限に回転させることはできません。チュートリアルやその他の例を見ていますが、私はそれを動作させることはできません、任意のアイデア理由またはどのようにそれを解決するには?

および

このテンプレートシーンを改善する方法についてのご意見はありますか?事前に

おかげ

<!DOCTYPE html> 
<html lang="en"> 

    <head> 
     <title>three.js Template</title> 
     <meta charset=utf-8> 
     <link rel="stylesheet" type="text/css" href="css/styles.css"> 
     <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'> 
     <script src="js/three.js"></script> 
     <script src="js/Detector.js"></script> 
     <script src="js/Stats.js"></script> 
     <script src="js/OrbitControls.js"></script> 

     <script src="js/OBJLoader.js"></script> 
     <script src="js/MTLLoader.js"></script> 
     <script src="js/DDSLoader.js"></script> 

     <script src="js/THREEx.KeyboardState.js"></script> 
     <script src="js/THREEx.FullScreen.js"></script> 
     <script src="js/THREEx.WindowResize.js"></script> 

    </head> 

    <body> 

     <div id="info"> 
      <a href="http://threejs.org" target="_blank">three.js</a> Template Scene<br /> 
      from <a href="x">Base scene</a> 
     </div> 

     <div id="threeJSScene"></div> 

     <script> 



// MAIN // 

// standard global variables 
var container, scene, camera, renderer, controls, stats, animate; 
var keyboard = new THREEx.KeyboardState(); 
var clock = new THREE.Clock(); 

// initialization 
init(); 
// animation loop/game loop 
animate(); 

// FUNCTIONS // 


function init() 
{ 

    // SCENE // 

    scene = new THREE.Scene(); 
    //Add fog to the scene 
// scene.fog = new THREE.FogExp2(0xcccccc, 0.001); 

    // CAMERA // 

    // set the view size in pixels (custom or according to window size) 
    // var SCREEN_WIDTH = 400, SCREEN_HEIGHT = 300; 
    var SCREEN_WIDTH = window.innerWidth, 
     SCREEN_HEIGHT = window.innerHeight; 
    // camera attributes 
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH/SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000; 
    // set up camera 
    camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR); 
    // add the camera to the scene 
    scene.add(camera); 
    // the camera defaults to position (0,0,0) 
    // so pull it back (z = 400) and up (y = 100) and set the angle towards the scene origin 
    camera.position.set(0,150,400); 
    camera.lookAt(scene.position); 



    // RENDERER // 

    // create and start the renderer; choose antialias setting. 
    if (Detector.webgl) 
     renderer = new THREE.WebGLRenderer({alpha:true, antialias:true}); 
    else 
     renderer = new THREE.CanvasRenderer(); 

    // Configure renderer size 
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); 

    //Change BG Color 
    //renderer.setClearColor(0xAA20AA); 

    //Configure pixel aspect ratio 
    renderer.setPixelRatio(window.devicePixelRatio); 

    //Enable shadows 
    renderer.shadowMapEnabled = true; 
    renderer.shadowMap.type = THREE.PCFSoftShadowMap; 

    // Modify gamma 
    // renderer.gammaInput = true; 
    // renderer.gammaOutput = true; 

    //Attach div element to variable to contain the renderer 
    container = document.getElementById('threeJSScene'); 
    // alternatively: to create the div at runtime, use: 
    // container = document.createElement('div'); 
    // document.body.appendChild(container); 

    // attach renderer to the *container* div 
    container.appendChild(renderer.domElement); 


    // EVENTS // 

    // automatically resize renderer 
    THREEx.WindowResize(renderer, camera); 
    // toggle full-screen on given key press 
    THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) }); 


    // CONTROLS // 

    controls = new THREE.OrbitControls(camera, renderer.domElement); 
       controls.addEventListener('change', render); // remove when using animation loop 
       // enable animation loop when using damping or autorotation 
       //controls.enableDamping = true; 
       //controls.dampingFactor = 0.25; 
       controls.enableZoom = true; 
       //controls.update(); ----------> // required if controls.enableDamping = true, or if controls.autoRotate = true 


    // STATS // 

    // displays current and past frames per second attained by scene 
    stats = new Stats(); 
    stats.domElement.style.position = 'absolute'; 
    stats.domElement.style.bottom = '0px'; 
    stats.domElement.style.zIndex = 100; 
    container.appendChild(stats.domElement); 


    // LIGHT // 

    // Add ambient light to Scene - Color(Blue) - Intensity 
    var Ambientlight = new THREE.AmbientLight (0x506699, 1); 
    scene.add(Ambientlight); 

    // Add light to Scene - Color(Red) - Intensity - Distance - decay 
    var light1 = new THREE.PointLight (0xff0000, 2, 400, 2); 
    light1.position.set(-60,150,-30); 
    light1.castShadow = true; 
    light1.shadowCameraVisible = true; 
    light1.shadow.mapSize.width = 1024 * 2; 
    light1.shadow.mapSize.height = 1024 * 2; 
    light1.shadowDarkness = 0.95; 
    light1.shadow.camera.near = 20;  
    light1.shadow.camera.far = 10000;  
    scene.add(light1); 

    // spotlight #1 -- yellow, dark shadow 
    var spotlight = new THREE.SpotLight(0xffff00); 
    spotlight.position.set(-60,150,-30); 
    spotlight.shadowCameraVisible = true; 
    spotlight.shadowDarkness = 0.95; 
    spotlight.intensity = 2; 
    // must enable shadow casting ability for the light 
    spotlight.castShadow = true; 
    scene.add(spotlight); 


    // GEOMETRY // 

    // Create a Cube Mesh // 
    var geometry = new THREE.BoxGeometry(50, 50, 50); 

    // Create a basic material 
    var material = new THREE.MeshStandardMaterial({ 
    color: "#ffffff", 
    side: THREE.DoubleSide, 
    //transparent: true, 
    //opacity: 0.5, 
    //wireframe: true, 
    //wireframeLinewidth: 5, 
    map: new THREE.TextureLoader().load('img/pattern.jpg'), 
    normalMap: new THREE.TextureLoader().load('img/pattern_NRM.png') 
    }); 

    //Join the two attribute (Geometry and material) 
    var mesh = new THREE.Mesh(geometry, material); 
    mesh.castShadow = true; 
    mesh.receiveShadow = true; 
    mesh.position.set(0, 50, 0); // Chance object position 
    //Add geometry to the scene 

    scene.add (mesh); 

    // Create a TorusKnot // 
    var TorusknotGeometry = new THREE.TorusKnotGeometry(15, 5, 60, 25); 
    var Torusknot = new THREE.Mesh(TorusknotGeometry, material); // We are using the same material created for the cube 
    Torusknot.castShadow = true; 
    Torusknot.receiveShadow = true; 
    Torusknot.position.set (0,100,0); 
    scene.add (Torusknot); 

    // Create a cube for the ground // 
    var groundGeometry = new THREE.BoxGeometry(200,200,10); 
    var ground = new THREE.Mesh(groundGeometry, material); 
    ground.castShadow = true; 
    ground.receiveShadow = true; 
    ground.position.set (0,0,0); 
    ground.rotation.x = 1.57; 

    scene.add (ground); 


    // Load in the mesh and add it to the scene. 
     var loader = new THREE.JSONLoader(); 
     loader.load("models/treehouse_logo.js", function(log){ 
     var materiallogo = new THREE.MeshLambertMaterial({color: 0x55B663}); 
     logo = new THREE.Mesh(log, materiallogo); 
     logo.scale.set (50,50,50); 
     logo.position.y = -1; 
     logo.castShadow = true; 
     logo.receiveShadow = true; 
     scene.add(logo); 
     }); 

    // FLOOR // 

    // note: 4x4 checkboard pattern scaled so that each square is 25 by 25 pixels. 
    var floorTexture = new THREE.ImageUtils.loadTexture('img/checkerboard.jpg'); 
    floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; 
    floorTexture.repeat.set(10, 10); 
    // DoubleSide: render texture on both sides of mesh 
    var floorMaterial = new THREE.MeshBasicMaterial({ map: floorTexture, side: THREE.DoubleSide }); 
    var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 1, 1); 
    var floor = new THREE.Mesh(floorGeometry, floorMaterial); 
    floor.castShadow = true; 
    floor.receiveShadow = true; 
    floor.position.y = -0.5; 
    floor.rotation.x = Math.PI/2; 
    scene.add(floor); 



    // create a set of coordinate axes to help orient user 
    // specify length in pixels in each direction 
    var axes = new THREE.AxisHelper(100); 
    scene.add(axes); 

    // SKY // 

    // recommend either a skybox or fog effect (can't use both at the same time) 
    // without one of these, the scene's background color is determined by webpage background 
    // make sure the camera's "far" value is large enough so that it will render the skyBox! 
    var skyBoxGeometry = new THREE.CubeGeometry(10000, 10000, 10000); 
    // BackSide: render faces from inside of the cube, instead of from outside (default). 
    var skyBoxMaterial = new THREE.MeshBasicMaterial({ color: 0x9999ff, side: THREE.BackSide }); 
    var skyBox = new THREE.Mesh(skyBoxGeometry, skyBoxMaterial); 
    // scene.add(skyBox); 
} 
function update() 
{ 
    controls.update(); 
    stats.update(); 

}   
//Animate function    
function animate() 
{ 

    requestAnimationFrame(animate); 

    render();  
    update(); 
} 

// Render the scene - Always goes at the end   
function render() 
{ 
    renderer.render(scene, camera); 

} 
</script> 


    </body> 
</html> 
+0

デベロッパーコンソールを見ましたか?エラーメッセージが表示されますか? –

+0

[こちら](ここではhttps://jsfiddle.net/2pha/ne7gjdnq/)はthree.jsでfiddlesに使用するテンプレートです – 2pha

+0

私は試しましたが、アニメーション機能でコードを追加するとすぐに黒、どのように私は私のシーンにそれを追加する? – Evoinsec

答えて

0

キューブを回転するには、フレームごとにキューブの回転に値を追加する必要があります。あなたが以前にそれをしたときにこれがうまくいかなかったのは、cubeがinit関数で定義されており、render関数に関数への参照がないからです。

だからあなたの修正は2つのことが必要です。

  • は、両方の方法は、「見る」
  • はあなたのキューブの回転にすべてのフレーム

をいくつかの値を追加することができますことをスコープにあなたのキューブを定義しますinit関数の内部でキューブをmeshと定義しているので、cubeに名前を変更し、varを削除して:

を削除してください

varを削除すると、cubeは、initではなくdomwindowに定義されたグローバル変数になります。したがって、レンダリング機能はcubeを見ることができます。だから、あなたはそれを回転させるだけです!

function render() 
{ 
    cube.rotation.x += 0.01; 
    cube.rotation.y += 0.01; 
    renderer.render(scene, camera); 
} 

私は助けてくれることを願っています! あなたがスコープについてもっと知りたいのであれば、このリンクには良い読書を与えてくれるので、かなり助けになりました。 https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/

+0

素晴らしい、すばらしい説明です、ありがとうございました。問題を修正しました。オブジェクトにVarを使用しないでください。 – Evoinsec

+0

グローバル変数は一般的に悪い習慣ですが、スコープ付き変数に慣れるために時間を費やしています。それはあなたの人生を本当に簡単にします:D –

+0

申し訳ありません、あなたが私の前に投稿するリンクを読むつもりです。 – Evoinsec

0

私は+ Ctrlキーを押しながらコードをf'd、あなたは一度、キューブの回転を参照しません。それはなぜそれが回転していないか説明するでしょう。 を回転させることはできません。シーンの要素の値を変更するものを実際に書き込む必要があります。

何かの回転を連続的に変更するには、ループするコードの部分を参照またはインクリメントする必要があります。

+0

私はすでにこれを試してみましたが、うまくいきませんでした。 function animate(){ requestAnimationFrame(animate); mesh.rotation.x + = 0。05; \t render(); \t update(); } シーンが黒くなる – Evoinsec

+0

動作させるにはどうすればいいですか? – Evoinsec

+0

が黒くなったら、ブラウザのインス​​ペクタを開き、ページをリロードして、コンソールに何が表示されるか教えてください。黒い場合はおそらくエラーがあるので、エラーが解決されるまでコードの内容は重要ではありません。回転コードとは何の関係もないかもしれません。 – OtterFamily

関連する問題