Three.js - 在HTML中插入单独的js文件

时间:2016-08-05 15:44:55

标签: javascript html three.js

我现在正在使用three.js并使用HTML呈现3D立方体。 HTML代码如下所示:

<div id="render" class="center-block">

<script>

var camera;
var scene;
var renderer;
var mesh;
var geometry;
var material1;
var material2;
var material3;
var material4;
var material5;
var material6;
var materials;
var meshFaceMaterial;

init();
animate();

function init() {
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.set( 0, 0, 0 );

    var light = new THREE.DirectionalLight( 0xffffff );
    light.position.set( 0, 1, 1 ).normalize();
    scene.add(light);

    geometry = new THREE.CubeGeometry( 10, 10, 10);
    material1 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material2 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material3 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material4 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material5 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );
    material6 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('http://vignette1.wikia.nocookie.net/scifiminibuilders/images/8/88/Your_Picture_Here.png/revision/latest?cb=20130507015051') } );

    materials = [material1, material2, material3, material4, material5, material6];

    meshFaceMaterial = new THREE.MeshFaceMaterial( materials );

    mesh = new THREE.Mesh(geometry, meshFaceMaterial );
    mesh.position.z = -50;
    scene.add( mesh );

    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.getElementById('render').appendChild( renderer.domElement );
    window.addEventListener( 'resize', onWindowResize, false );

    render();
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    render();
}

function animate() {
    mesh.rotation.x += .016;
    mesh.rotation.y += .015;
    mesh.rotation.y += .014;

    render();
    requestAnimationFrame( animate );
}

function render() {
    renderer.render( scene, camera );
}

</script>
</div>

脚本太多了。我想从中创建一个单独的.js文件,但不知道如何使用three.js完成它。有什么建议吗?

修改

据我所知,该脚本的工作方式类似于canvas标签,而不是在HTML中输入标签。这可能会因制作一个单独的.js文件而受到干扰......

2 个答案:

答案 0 :(得分:0)

您只需要添加对其他.js文件的引用。我猜你已经在THREE.js页面上找到了参考资料。所以你可以复制它并引用第二个文件。

<script src="THREE.js"></script>
<script src="other_script.js"></script>

并将所有额外代码放入other_script.js(或您使用的任何文件名)。

答案 1 :(得分:0)

您很可能需要将函数包装在文档就绪标记中,如下所示:

$(function(){
init();
animate();
})

这样一来,在你尝试使用它里面的函数之前,外部js文件已经被加载了......基本上它必须在你的函数触发时在DOM中可用。