gpt4 book ai didi

javascript - GLTF 加载程序试用版返回类型错误 : Failed to resolve module specifier "three". 相对引用必须以 "/"、 "./"或 "../"开头

转载 作者:行者123 更新时间:2023-12-05 04:35:25 24 4
gpt4 key购买 nike

我是 three.js 的新手,正在尝试将模型加载到我的 Canvas 中。一旦我导入 GLTFLoader,我就会在控制台中看到上面的读数。到底是怎么回事?语法和相对路径看起来不错。我真的不明白发生了什么事。我添加了另一个文件夹来放入 master three.js 文件夹,但它不起作用。

import * as THREE from '../master/three.js-master/build/three.module.js'
import {GLTFLoader} from '../master/three.js-master/examples/jsm/loaders/GLTFLoader.js'
const canvas = document.querySelector('.webgl')
const scene = new THREE.Scene()

const loader = new GLTFLoader()
loader.load('../assets/scene.gltf', function(gltf){
console.log(gltf);
const root = gltf.scene;
scene.add(root);
}, function(xhr){
console.log((xhr.loaded/xhr.total * 100) + "% loaded")
}, function(error){
console.log('error');
})

// const geometry = new THREE.BoxGeometry(1,1,1)
// const material = new THREE.MeshBasicMaterial({
// color: 'green'
// })

const boxMesh = new THREE.Mesh(geometry,material)
scene.add(boxMesh)

//Boiler Plate

const sizes = {
width: window.innerWidth,
height: window.innerHeight,
}

const camera = new THREE.PerspectiveCamera(75, sizes.width/sizes.height, 0.1, 100)
camera.position.set(0,1,2)
scene.add(camera)

const renderer = new THREE.WebGLRenderer({
canvas: canvas
})

renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
renderer.shadowMap.enabled = true
// renderer.outputEncoding = true
renderer.render(scene, camera)


console.log('working')
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Landing Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas class="webgl"></canvas>
<script type = "module" src="scripts.js"></script>
<script>

</script>
</body>
</html>

最佳答案

因为 r137 在浏览器中使用像 GLTFLoader 这样的 ES6 模块需要导入映射。在 canvas 正下方添加以下部分。

<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>

<script type="importmap">
{
"imports": {
"three": "../master/three.js-master/build/three.module.js"
}
}
</script>

您的 scripts.js 中的 three.js 导入语句如下所示:

import * as THREE from 'three'

关于javascript - GLTF 加载程序试用版返回类型错误 : Failed to resolve module specifier "three". 相对引用必须以 "/"、 "./"或 "../"开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71037149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com