基本Three.js模板场景,无法动画

时间:2017-02-13 23:37:42

标签: three.js

我是Three.js和javascript的新手。

我的问题,我一直在尝试创建一些基本的frankenstein模板之王(主要基于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>

2 个答案:

答案 0 :(得分:0)

我按了ctrl + f'd代码,你没有引用一次立方体的旋转。这可以解释为什么它不会旋转。你不能将它旋转,你需要实际写一些会改变场景中元素值的东西。

为了不断改变某些东西的旋转,你需要在循环的代码部分引用或增加它的旋转。

答案 1 :(得分:0)

要旋转立方体,您需要在每帧的立方体旋转中添加一些值。之前这样做不起作用的原因是你的init函数中定义了cuberender函数没有引用它。

所以你的修复需要两件事:

  • 在两个方法都可以“看到”
  • 的范围内定义您的多维数据集
  • 为每个框架的立方体旋转添加一些值

在init函数内部,您将多维数据集定义为mesh,因此将其重命名为cube并删除var

//Join the two attribute ( Geometry and material ) 
//var mesh = new THREE.Mesh( geometry, material);  // Old  
cube = new THREE.Mesh( geometry, material); // new

删除var会导致cube成为dom window而不是init函数定义的全局变量。所以你的渲染功能可以“看到”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/