【问题标题】:Three.js - Uncaught ReferenceError: THREE is not definedThree.js - 未捕获的 ReferenceError:未定义三个
【发布时间】:2017-04-26 15:32:10
【问题描述】:

当我运行我的 javascript 代码时。我收到以下错误“未捕获的 ReferenceError:未定义三个”。 提到的行是:

var renderer = new THREE.WebGLRenderer();

// I have attached the three.js library in the script tag. I don't know what seems to be problem.

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set = (0, 0, 10);
camera.lookAt(camera.position);
scene.add(camera);

var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0.0, 1.0, 0.0));
geometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0));
geometry.vertices.push(new THREE.Vector3(1.0, -1.0, 0.0));
geometry.faces.push(new THREE.Face3(0, 1, 2));

var material = new THREE.BasicMeshMaterial({
    color: 0xFFFFFF,
    side: THREE.DoubleSide
});

var mesh = new THREE.Mesh(geometry, material);
mesh.position.set(-1.5, 0.0, 4.0);
scene.add(mesh);

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

render();

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    有效检查文件three.js的路径 就我而言,它是在鲤鱼中

    【讨论】:

    【解决方案2】:

    对于任何对此进行检查的人,我都遇到了同样的问题。意识到我把 <script src="./libs/three.js"></script> 放在我的 html 文件中实际使用三个的脚本之后搞砸了。

    【讨论】:

      【解决方案3】:

      我的错误是包含来自/src/Three.js 文件夹的三个.js 文件。确保使用./build/three.js

      【讨论】:

        【解决方案4】:

        我有类似的问题,在标题中指定字符编码对我有用

        <head>
        	<title>Cube by Charden</title>
        	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        	.
          .
          .
          .
          
        </head>

        【讨论】:

          【解决方案5】:

          很难查明错误,因为您只提供了尝试使用 Three.js 的代码,但您没有显示的是如何加载模块以及所有其他初始化脚本的执行顺序。所以我强烈建议通过jsfiddle 分享一个简单的示例,或使用主帖中的代码片段功能。)

          为了确保 three.min.js 已加载:

          • 检查浏览器控制台是否有任何错误
          • 检查浏览器控制台中的网络部分,看看three.min.js 是否在那里监听。

          index.html

          <html>
          <head>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
          </head>
            <body>
            </body>
           </html>
          <script>
            var container = document.createElement( 'div' );
            document.body.appendChild( container );
            scene = new THREE.Scene();
              ....
          </script>

          【讨论】:

            【解决方案6】:

            之前需要包含三个.js

            <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
            

            【讨论】:

            • 添加 about 脚本 src 后,我现在收到此错误! “未捕获的 TypeError:THREE.BasicMeshMaterial 不是构造函数”
            • 应该是THREE.MeshBasicMaterial()
            猜你喜欢
            • 2023-01-23
            • 2016-11-03
            • 2011-01-05
            • 2016-01-02
            • 2013-10-06
            • 2016-12-17
            • 1970-01-01
            相关资源
            最近更新 更多