无法导入 three.js glTF 模型
Can't import three.js glTF model
我正在尝试将 3D 模块导入 three.js and I was reading here and here。但这一切都是黑色的。我尝试将相机的 z
位置移动到 5
,但仍然是黑色。我刚开始 Three.js。我在在线 Three.js 观众中加载了模型,它加载正常。这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import 3D Model into Three JS</title>
<link rel="stylesheet" href="../common.css">
</head>
<body>
<div id="container">
</div>
</body>
<script src="../three.js-master/build/three.js"></script>
<script type="module">
// import { THREE } from '../three.js-master/build/three.js';
import { GLTFLoader } from '../three.js-master/examples/jsm/loaders/GLTFLoader.js';
const container = document.querySelector('div#container');
const path_to_model = './Mini-Game Variety Pack/Models/gltf/tree_forest.gltf.glb';
const loader = new GLTFLoader();
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);
loader.load(path_to_model, function (gltf)
{
console.log('Adding glTF model to scene...');
scene.add(gltf.scene);
console.log('Model added.');
console.log('Moving camera 5z...');
camera.position.z = 5;
console.log('Camera moved.');
}, undefined, function (error)
{
console.error(error);
});
container.appendChild(renderer.domElement);
function animate()
{
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</html>
我的文件夹是这样的:
- 三个JS/
- common.css
- 三个.js-master/
- 导入模型/
- index.html
- Mini-Game 百变包/
- 型号/
- gltf/
- tree_forest.glb
- tree_forest.gltf.glb
(未显示所有文件,仅显示必要文件)
我从 here 下载了所有模型,但使用的是 tree_forest
。
当我加载页面时,我看到的是:
我在 Mac 上,我在 VSCode 上使用五台服务器。
<script src="../three.js-master/build/three.js"></script>
<script type="module">
// import { THREE } from '../three.js-master/build/three.js';
import { GLTFLoader } from '../three.js-master/examples/jsm/loaders/GLTFLoader.js';
将全局脚本与 ES6 模块混合并不是一个好主意,并且会很快导致未定义的行为。我建议您像这样组织导入,直到应用程序运行为止:
import * as THREE from 'https://cdn.skypack.dev/three@0.129.0/build/three.module.js';
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js';
您还应该为场景添加一些灯光,否则您只会看到黑屏。试试看:
const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
hemiLight.position.set( 0, 10, 0 );
scene.add( hemiLight );
const dirLight = new THREE.DirectionalLight( 0xffffff );
dirLight.position.set( 0, 0, 10 );
scene.add( dirLight );
我正在尝试将 3D 模块导入 three.js and I was reading here and here。但这一切都是黑色的。我尝试将相机的 z
位置移动到 5
,但仍然是黑色。我刚开始 Three.js。我在在线 Three.js 观众中加载了模型,它加载正常。这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import 3D Model into Three JS</title>
<link rel="stylesheet" href="../common.css">
</head>
<body>
<div id="container">
</div>
</body>
<script src="../three.js-master/build/three.js"></script>
<script type="module">
// import { THREE } from '../three.js-master/build/three.js';
import { GLTFLoader } from '../three.js-master/examples/jsm/loaders/GLTFLoader.js';
const container = document.querySelector('div#container');
const path_to_model = './Mini-Game Variety Pack/Models/gltf/tree_forest.gltf.glb';
const loader = new GLTFLoader();
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);
loader.load(path_to_model, function (gltf)
{
console.log('Adding glTF model to scene...');
scene.add(gltf.scene);
console.log('Model added.');
console.log('Moving camera 5z...');
camera.position.z = 5;
console.log('Camera moved.');
}, undefined, function (error)
{
console.error(error);
});
container.appendChild(renderer.domElement);
function animate()
{
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</html>
我的文件夹是这样的:
- 三个JS/
- common.css
- 三个.js-master/
- 导入模型/
- index.html
- Mini-Game 百变包/
- 型号/
- gltf/
- tree_forest.glb
- tree_forest.gltf.glb
- gltf/
- 型号/
(未显示所有文件,仅显示必要文件)
我从 here 下载了所有模型,但使用的是 tree_forest
。
当我加载页面时,我看到的是:
我在 Mac 上,我在 VSCode 上使用五台服务器。
<script src="../three.js-master/build/three.js"></script>
<script type="module">
// import { THREE } from '../three.js-master/build/three.js';
import { GLTFLoader } from '../three.js-master/examples/jsm/loaders/GLTFLoader.js';
将全局脚本与 ES6 模块混合并不是一个好主意,并且会很快导致未定义的行为。我建议您像这样组织导入,直到应用程序运行为止:
import * as THREE from 'https://cdn.skypack.dev/three@0.129.0/build/three.module.js';
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js';
您还应该为场景添加一些灯光,否则您只会看到黑屏。试试看:
const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
hemiLight.position.set( 0, 10, 0 );
scene.add( hemiLight );
const dirLight = new THREE.DirectionalLight( 0xffffff );
dirLight.position.set( 0, 0, 10 );
scene.add( dirLight );