Three.js – 使用 CDN 的 STLLoader

huangapple go评论78阅读模式
英文:

Three.js - STLLoader with cdn

问题

我已决定尝试制作一个STL文件加载器来玩。现在我很生气。
我尝试了互联网上的一切,但仍然无法工作,主要是因为CDN错误。
我现在正在按照Three.js网站上的教程以及这个教程 进行操作。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>STL</title>
		<style>
			body { margin: 0; }
		</style>
	</head>
	<body>
		<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
		<script type="importmap">
			{
				"imports": {
					"three": "https://unpkg.com/three@0.154.0/build/three.module.js",
					"stl-loader": "https://cdn.jsdelivr.net/npm/stl-loader@1.0.0/STLLoader.min.js",
					"three/addons/": "https://unpkg.com/three@0.154.0/examples/jsm/"
				}
			}
		</script>
		<script type="module">
			import * as THREE from 'three';
			import { STLLoader } from 'stl-loader';

			const scene = new THREE.Scene();
			const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

			const renderer = new THREE.WebGLRenderer();
			renderer.setSize(window.innerWidth, window.innerHeight);
			document.body.appendChild(renderer.domElement);

			scene.background = new THREE.Color('#cc0000');

			const loadObject = () => {
				const loader = new STLLoader();
				loader.load("stl_file.stl", function (geometry) {
					group = new THREE.Group();
					scene.add(group);

					const material = new THREE.MeshPhongMaterial({ color: 0xaaaaaa, specular: 0x111111, shininess: 200 });
					mesh = new THREE.Mesh(geometry, material);
					mesh.position.set(0, 0, 0);
					mesh.scale.set(10, 10, 10);
					mesh.castShadow = true;
					mesh.receiveShadow = true;

					geometry.center();
					group.add(mesh);
				})
			}

			loadObject();
			camera.position.z = 5;

			renderer.render(scene, camera);
		</script>
	</body>
</html>

大部分的代码来自这里,正如我之前提到的。
从控制台出现错误信息:Uncaught SyntaxError: The requested module 'stl-loader' does not provide an export named 'STLLoader',但我知道这不是唯一的问题...

英文:

I've decided to try to make a stl loader for fun. Now I'm mad.
I've tried everything on the internet and still not work, mostly from errors of cdn.
Right now I'm following the tutorial on Three.js site and this one.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;STL&lt;/title&gt;
&lt;style&gt;
body { margin: 0; }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script async src=&quot;https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;importmap&quot;&gt;
{
&quot;imports&quot;: {
&quot;three&quot;: &quot;https://unpkg.com/three@0.154.0/build/three.module.js&quot;,
&quot;stl-loader&quot;: &quot;https://cdn.jsdelivr.net/npm/stl-loader@1.0.0/STLLoader.min.js&quot;,
&quot;three/addons/&quot;: &quot;https://unpkg.com/three@0.154.0/examples/jsm/&quot;
}
}
&lt;/script&gt;
&lt;script type=&quot;module&quot;&gt;
import * as THREE from &#39;three&#39;;
import { STLLoader } from &#39;stl-loader&#39;;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene.background = new THREE.Color(&#39;#cc0000&#39;);
const loadObject = () =&gt; {
const loader = new STLLoader()
loader.load(&quot;stl_file.stl&quot;, function (geometry) {
group = new THREE.Group()
scene.add(group)
const material = new THREE.MeshPhongMaterial({ color: 0xaaaaaa, specular: 0x111111, shininess: 200 })
mesh = new THREE.Mesh(geometry, material)
mesh.position.set(0, 0, 0)
mesh.scale.set(10, 10, 10)
mesh.castShadow = true
mesh.receiveShadow = true
geometry.center()
group.add(mesh)
})
} 
loadObject();
camera.position.z = 5;
renderer.render( scene, camera );
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Most of the code came from here as I mentioned earlier.
From console
Uncaught SyntaxError: The requested module &#39;stl-loader&#39; does not provide an export named &#39;STLLoader&#39;
but I know that it's not the only problem...

答案1

得分: 0

"STLLoader.min.js"文件不是ES6模块,因此无法使用ES6导入语法导入它。该文件应该具有与three npm包中的STLLoader相同的结构:

https://unpkg.com/three@0.154.0/examples/jsm/loaders/STLLoader.js

英文:

The file STLLoader.min.js is no ES6 module so you can't import it via ES6 import syntax. The file should have the same structure like the STLLoader from the three npm package:

https://unpkg.com/three@0.154.0/examples/jsm/loaders/STLLoader.js

huangapple
  • 本文由 发表于 2023年7月28日 03:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定