Hi I’m afraid I’m struggling with a basic implementation of ThreeJS. I have it all working great using NPM and it’s all neatly packaged in my main.js file. I load a .glb 3d model and GLTFLoader and OrbitControls are all nicely imported and everything is great. My problem now is that I need to load my 3d model from a database so I don’t need the whole javascript neatly packaged in my JS file. I can do this by importing the correct script like this:
<script type="module">
import * as THREE from './js/three.module.js';
import { GLTFLoader } from './examples/jsm/loaders/GLTFLoader.js';
import { OrbitControls } from './examples/jsm/controls/OrbitControls.js';
// rest of my script goes here
This again works fine but the problem is the CMS I’m using has a problem with type=“module” so is there a different way to implement it without using that bit?
According to your lib folder structure suggested in your code above, it would be something like this,
<script src="./js/three.min.js"></script>
<script src="./examples/js/loaders/GLTFLoader.js"></script>
<script src="./examples/js/controls/OrbitControls.js"></script>
<script>
// rest of your script goes here
</script>
The libs in this version,
are being loaded using script tags and not es6 import statements.
are in the /examples/js folder and not the /examples/jsm folder
the three lib is either three.js or three.min.js and not the three.module.js
Thanks very much for your reply. Unfortunately it didn’t work. I get two warnings and an error in Firefox:
THREE.GLTFLoader: As part of the transition to ES6 Modules, the files in ‘examples/js’ were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation.
THREE.OrbitControls: As part of the transition to ES6 Modules, the files in ‘examples/js’ were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation.
Uncaught ReferenceError: OrbitControls is not defined
Paste your code where you declare your ObitControls variable and I will tell you what you did wrong. You left it out of your original code sample.
I guess its in the bit where you say
// rest of my script goes here
And the warnings, well they are warnings and to stop those particular warnings, you can use type="module"
@cannon303 If you want to keep using modules in your project, you could also use a compiler like Babel or a bundler like Webpack that will automatically convert your code to an older js version, compatible with your CMS.
It’s Craft CMS. You can write javascript right in page where you want it to take effect and it will automatically move it to the bottom of the page and only pull it in if it is specifically required. Unfortunately the best it will do is throw your script into a <script>...</script> tag and not add the type=“module” bit.