简体   繁体   English

Threejs 导入 OrbitControl.js

[英]Threejs importing OrbitControl.js

So I'm just using a normal html with no other libraries..like reactjs,vitejs,vuejs etc... I want it just to import as normal html and just put in the script tag.所以我只是使用没有其他库的普通html..如reactjs、vitejs、vuejs等......我希望它只是作为普通html导入并放入script标签。

So the thing is I don't know why I keep receiving the wrong import giving me something like this所以问题是我不知道为什么我一直收到错误的导入给我这样的东西

Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".
This problem occured only in my OrbitControls.js and I don't understand what it means.这个问题只发生在我的 OrbitControls.js 中,我不明白这是什么意思。

And this how it looks in my script terminal and file locations.这就是它在我的脚本终端和文件位置中的外观。

在此处输入图像描述

在此处输入图像描述

I tried to copy the unpkg link of the orbit control but also it doesn't work..I tried also to pass the raw file but still doesn't work.. Can anyone help me through this?我试图复制轨道控制的 unpkg 链接,但它也不起作用..我也尝试传递原始文件但仍然不起作用..有人可以帮我解决这个问题吗?

    <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js" integrity="sha512-WoO4Ih0CDOSLYafy22wZD/mcJ7k0ESLqtQsFa6zFKnEUrbtuGU+GkLtVhgt93xa2qewG5gKEC6CWlN8OaCTSVg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="OrbitControls.js" type="module"></script>

  <script type="module">
        import * as THREE from 'https://unpkg.com/three@0.141.0/build/three.module.js';
        import * as dat from './dat.gui.module.js'

        import { OrbitControls }  from 'OrbitControls.js'
        // console.log(dat)
        // const orbit = new OrbitControls()
        console.log(OrbitControls)
  </src> 

I also tried to get the link of the OrbitControls by doing something like this我也试图通过做这样的事情来获取 OrbitControls 的链接

import { OrbitControls } from 'https://unpkg.com/browse/three@0.141.0/examples/jsm/controls/OrbitControls.js'
console.log(OrbitControls)

When not working with a bundler, you have to define an import map when using the ES6 modules of three.js .如果不使用捆绑器,则必须在使用three.js的 ES6 模块时定义导入映射。 Put the following code into your HTML:将以下代码放入您的 HTML 中:

<script type="importmap">
    {
        "imports": {
            "three": "https://unpkg.com/three@0.141.0/build/three.module.js"
        }
    }
</script>

In your JS code, do this:在您的 JS 代码中,执行以下操作:

import * as THREE from 'three';
import { OrbitControls } from 'https://unpkg.com/three@0.141.0/examples/jsm/controls/OrbitControls.js';

You also have to remove the line <script src="OrbitControls.js" type="module"></script> .您还必须删除<script src="OrbitControls.js" type="module"></script>行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM