【问题标题】:Three.js just drawing a white screenThree.js 只是画了一个白屏
【发布时间】:2021-04-07 08:23:39
【问题描述】:

我正在尝试按照此处的教程进行操作:https://threejs.org/docs/#manual/en/introduction/Creating-a-scene,而我的浏览器只显示了一个白色窗口。我尝试将文件分离为 js 和 html 文件。

我已经尝试过的:

  • 在脚本标签中添加/删除nomodule参数
  • 运行本地实时服务器

这是我的代码:

我的 index.html 文件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        <script nomodule src="js/main.js"></script>
        <script>
            // Our Javascript will go here.
        </script>
    </body>
</html>

我的 js 文件名为 main.js,位于“js”文件夹中。我通过 npm 安装了 Three.js。

import * as THREE from 'three'
//import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth, window.innerHeight, 0.1, 1000);

const renderer = new THREE.WebGLRenderer();

renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({color:0x00ff00});
const cube = new THREE.Mesh(geometry, material);

scene.add(cube);

camera.position.z = 5;

const animate = function () {
    requestAnimationFrame(animate);

    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;

    renderer.render(scene, camera);
};

animate();

反之示例代码:

<!DOCTYPE html>
<html>
    <head>
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        <script src="js/three.js"></script>
        <script>
            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

            const renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            document.body.appendChild( renderer.domElement );

            const geometry = new THREE.BoxGeometry();
            const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
            const cube = new THREE.Mesh( geometry, material );
            scene.add( cube );

            camera.position.z = 5;

            const animate = function () {
                requestAnimationFrame( animate );

                cube.rotation.x += 0.01;
                cube.rotation.y += 0.01;

                renderer.render( scene, camera );
            };

            animate();
        </script>
    </body>
</html>

【问题讨论】:

  • 这能回答你的问题吗? What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript?您的脚本被忽略,因为您添加了nomodule
  • 不,不幸的是它没有。如果我删除nomodule,我会在控制台中收到一个错误:Uncaught SyntaxError: import declarations may only appear at top level of a module 我看到 nomodule 参数会让这个错误消失,但似乎有一个不同的问题
  • type=“module” 添加到脚本标签而不是nomodule 时会发生什么?
  • 在这种情况下,Firefox 向我显示一条错误消息,指出“同源规则”禁止从文件中读取外部资源。 TypeError: Cross-origin script load denied by Cross-Origin Resource Sharing policy.
  • 你读过:How to run things locally

标签: javascript html three.js


【解决方案1】:

您需要在 js/ 文件夹中包含“three.js”。

“开始之前”部分: https://threejs.org/docs/#manual/en/introduction/Creating-a-scene

“将以下 HTML 保存到您计算机上的文件中,并在 js/ 目录中保存三个.js 的副本,然后在浏览器中打开它。”

【讨论】:

    【解决方案2】:

    这会渲染一个绿色的旋转立方体:

    <!DOCTYPE html>
    <html>
        <head>
            <title>My first three.js app</title>
            <style>
                body { margin: 0; }
            </style>
        </head>
        <body>
            <script type="module">
                import * as THREE from 'https://unpkg.com/three/build/three.module.js';
    
                const scene = new THREE.Scene();
                const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
    
                const renderer = new THREE.WebGLRenderer();
                renderer.setSize( window.innerWidth, window.innerHeight );
                document.body.appendChild( renderer.domElement );
    
                const geometry = new THREE.BoxGeometry();
                const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
                const cube = new THREE.Mesh( geometry, material );
                scene.add( cube );
    
                camera.position.z = 5;
    
                const animate = function () {
                    requestAnimationFrame( animate );
    
                    cube.rotation.x += 0.01;
                    cube.rotation.y += 0.01;
    
                    renderer.render( scene, camera );
                };
    
                animate();
            </script>
        </body>
    </html>
    

    【讨论】:

    • 现在可以了。非常感谢。不过,我很困惑为什么它不能与我单独的 JavaScript 文件一起使用。
    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 2021-07-27
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2011-08-09
    相关资源
    最近更新 更多