Uncaught TypeError: Failed to resolve module specifier “three/examples/jsm/controls/OrbitControls“.

在这里插入图片描述
做three.js项目遇到这个问题。
如何解决呢?
我的方法:
//index.html

<head>
    <script type="importmap">
        {
            "imports": {
                "three": "https://unpkg.com/three@0.138.0/build/three.module.js",
                "OrbitControls": "https://unpkg.com/three@0.138.0/examples/jsm/controls/OrbitControls.js"
            }
        }
    </script>
</head>
<body>
  <script type="module" src="js/main.js"></script>
</body>

//main .js

// import * as THREE from "three";
import * as THREE from '../../node_modules/three/src/Three.js';
// 导入轨道控制器
// import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { OrbitControls } from "../../node_modules/three/examples/jsm/controls/OrbitControls.js";
// 导入动画库
// import gsap from "gsap";
import { gsap } from '../../node_modules/gsap/index.js';

// console.log(THREE);

// 目标:监听页面尺寸变化,修改渲染画面

// 1、创建场景
const scene = new THREE.Scene();

// 2、创建相机
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// 设置相机位置
camera.position.set(0, 0, 10);
scene.add(camera);

// 添加物体
// 创建几何体
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
// 根据几何体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);

// 修改物体的位置
// cube.position.set(5, 0, 0);
// cube.position.x = 3;
// 缩放
// cube.scale.set(3, 2, 1);
// cube.scale.x = 5;
// 旋转
cube.rotation.set(Math.PI / 4, 0, 0, "XZY");

// 将几何体添加到场景中
scene.add(cube);

console.log(cube);

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// console.log(renderer);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

// // 使用渲染器,通过相机将场景渲染进来
// renderer.render(scene, camera);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼,让控制器更有真实效果,必须在动画循环里调用.update()。
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// 设置时钟
const clock = new THREE.Clock();

// 设置动画
var animate1 = gsap.to(cube.position, {
  x: 5,
  duration: 5,
  ease: "power1.inOut",
  //   设置重复的次数,无限次循环-1
  repeat: -1,
  //   往返运动
  yoyo: true,
  //   delay,延迟2秒运动
  delay: 2,
  onComplete: () => {
    console.log("动画完成");
  },
  onStart: () => {
    console.log("动画开始");
  },
});
gsap.to(cube.rotation, { x: 2 * Math.PI, duration: 5, ease: "power1.inOut" });

window.addEventListener("dblclick", () => {
  //   console.log(animate1);
  if (animate1.isActive()) {
    //   暂停
    animate1.pause();
  } else {
    //   恢复
    animate1.resume();
  }
});

function render() {
  controls.update();
  renderer.render(scene, camera);
  //   渲染下一帧的时候就会调用render函数
  requestAnimationFrame(render);
}

render();

// 监听画面变化,更新渲染画面
window.addEventListener("resize", () => {
  //   console.log("画面变化了");
  // 更新摄像头
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();

  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});

还有一个方法:
(网上找到的,还没验证)

npm install -g parcel-bundler

parcel *.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用[1]:Not using node.js : Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../" - 卑面派对 - 博客园 引用[2]:错误信息 Uncaught TypeError: Failed to resolve module specifier “three”. Relative references must start with either “/”, “./”, or “…/”. 引用[3]:html <head> <script type="importmap"> { "imports": { "three": "https://unpkg.com/[email protected]/build/three.module.js", "OrbitControls": "https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js" } } </script> </head> <body> <script type="module" src="js/test.js"></script> </body> // test.js import * as THREE from 'three'; import { OrbitControls } from 'OrbitControls'; scene = new THREE.Scene(); renderer = new THREE.WebGLRenderer(); controls = new OrbitControls(camera, renderer.domElement);。 问题:新建工程 Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../". 回答: 根据你提供的引用内容,错误信息"Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../"通常是由于模块的路径引用错误导致的。在你的新建工程中,你可能没有正确引入Vue模块。请确保你已经正确安装了Vue,并在你的代码中使用正确的路径引用Vue模块。你可以尝试使用相对路径或绝对路径来引用Vue模块,例如"./vue"或"/vue"。如果问题仍然存在,请检查你的引用路径是否正确,并确保你的Vue模块已经正确安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值