【发布时间】:2022-08-19 09:32:54
【问题描述】:
我正在使用three.js制作水,但我不明白出了什么问题,给了我错误..
Uncaught TypeError: Error resolving module specifier “three”. Relative module specifiers must start with “./”, “../” or “/”.
如下图所示。
我用来创建水的 HTML 代码是:
<!DOCTYPE html>
<html>
<head>
<meta charset=\"utf-8\">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
<script type=\"importmap\">
{
\"imports\": {
\"three\": \"./build/three.module.js\",
\"three.js-master/examples/jsm/controls/OrbitControls\":\"./jsm/controls/OrbitControls.js\",
\"three.js-master/examples/jsm/libs/stats.module\":\"./jsm/libs/stats.module.js\",
}
}
</script>
</head>
<body>
<script type=\"module\" src=\"https://cdn.jsdelivr.net/npm/three@0.143/build/three.min.js\"></script>
<script type=\"module\" src=\"https://cdn.jsdelivr.net/npm/three@0.143/examples/js/controls/OrbitControls.js\"></script>
</script>
<script type=\"module\">
import { Water } from \'http://localhost/Lines/jsm/objects/Water.js\';
import * as THREE from \'http://localhost/Lines/build/three.module.js\';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 0, 50 );
camera.lookAt( 0, 0, 0 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const WaterGeometry = new THREE.PlaneGeometry( 45, 45 );
const texture = new THREE.TextureLoader().load( \'https://i.imgur.com/mK9cHLq.jpeg\' );
const material = new THREE.MeshBasicMaterial( { map: texture, side: THREE.DoubleSide } );
const controls = new THREE.OrbitControls( camera, renderer.domElement );
let water = new Water
(
WaterGeometry,
{
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load
( \'textures/waternormals.jpg\', function ( texture )
{
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}
),
sunDirection: new THREE.Vector3(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 3.7,
fog: scene.fog !== undefined
}
);
controls.update();
scene.add( water );
function animate()
{
requestAnimationFrame( animate );
water.rotation.x += 0.00;
water.rotation.y += 0.00;
water.rotation.z += 0.01;
renderer.render( scene, camera );
};
animate();
</script>
</body>
</html>
我不明白为什么我的代码会抛出该错误。很确定在我的文件夹中导入的 JavaScript 代码 Water.js 没有问题,而我的 HTML 代码就是问题所在。
我无法弄清楚出了什么问题,或者我可能根本不知道 Javascript 是如何工作的以及她的真实本性。
为什么我的代码会抛出这种错误。请赐教。
谢谢!
-
这回答了你的问题了吗? Failed to resolve module specifier three
-
@Marquizzo我试图理解并复制您链接上的部分代码,但我仍然遇到同样的错误。我现在真的很沮丧,因为我不知道为什么我的代码会抛出那个错误。自从我遇到这种情况已经一周了,每天看到我的代码我都感到沮丧。我认为three.js 很酷,但似乎她很难使用....尤其是导入一些东西。该死的,为什么我对此不聪明..诅咒这个。我什至不知道如何以不会引发该错误的方式编写 HTML 代码。我用你在链接中所说的提示在答案中编写了代码。
标签: three.js