0

我只是 webGL 和 three.js 的新手,我刚刚从 JSFiddle 中挑选了一个例子,

http://jsfiddle.net/BlackSRC/XWCRM/1/

但是当我测试它时,我什么也没得到,代码看起来像这样:

<!DOCTYPE html>
<html>


<head>
    <title>ThreeJS</title>

</head>
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/mrdoob-three.js-2524525/build/three.js"></script>
<script>
    function onLoadComplete() {
        init();
        animate();
    }

    var camera, scene, renderer;
    var geometry, material, mesh;



    function init() {

        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
        camera.position.z = 1000;

        scene = new THREE.Scene();

        geometry = new THREE.CubeGeometry(200, 200, 200);
        material = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });

        mesh = new THREE.Mesh(geometry, material);
        scene.add(mesh);

        renderer = new THREE.CanvasRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

    }

    function animate() {

        // note: three.js includes requestAnimationFrame shim
        requestAnimationFrame(animate);

        mesh.rotation.x += 0.01;
        mesh.rotation.y += 0.02;

        renderer.render(scene, camera);

    }
</script>

<body onload="onLoadComplete();">

我错过了什么吗?感谢您的帮助!

4

2 回答 2

0

单击检查并检查是否存在与加载模型路径相关的任何错误,或者可能是一些构造函数错误。

  • 您可能希望将其更改为 geometry = CubeGeometry(...)
  • 还可以使用实时服务器加载三个 js 模型,因为它们本质上是动态的。
于 2021-08-18T19:53:08.087 回答
0

您的代码没有问题,但是您使用的是本地js文件。所以它要求js代码在body dom渲染之前运行,所以调用时document.body.appendChild(renderer.domElement);会导致错误:

test.html:45 未捕获的类型错误:无法读取 null(...) 的属性“appendChild”

并导致空白页。但是当我使用未存储在本地的远程 js 文件进行测试时,它运行良好。所以为了避免这个问题,让js在dom之后加载。

下面是代码,我只是把正文前的js代码,并使用网上的js文件。这里在js代码之后放body也可以,因为js是从远程加载的,当js运行时,body已经渲染好了。

 <!DOCTYPE html>
<html>
<head>
    <title>ThreeJS</title>

<body onload="onLoadComplete();">
</head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://threejs.org/build/three.js"></script>
<script src="https://threejs.org/examples/js/renderers/CanvasRenderer.js"></script>
<script src="https://threejs.org/examples/js/renderers/Projector.js"></script>
<script>
    function onLoadComplete() {
        init();
        animate();
    }

    var camera, scene, renderer;
    var geometry, material, mesh;



    function init() {

        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
        camera.position.z = 1000;

        scene = new THREE.Scene();

        geometry = new THREE.CubeGeometry(200, 200, 200);
        material = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });

        mesh = new THREE.Mesh(geometry, material);
        scene.add(mesh);

        renderer = new THREE.CanvasRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

    }

    function animate() {

        // note: three.js includes requestAnimationFrame shim
        requestAnimationFrame(animate);

        mesh.rotation.x += 0.01;
        mesh.rotation.y += 0.02;

        renderer.render(scene, camera);

    }
</script>

</html>

于 2016-11-25T05:26:11.220 回答