three.js 导入多个stl
时间: 2023-09-10 17:10:55 浏览: 229
three.js的stl文件
文件并显示在场景中的示例代码:
```javascript
var scene = new THREE.Scene();
var loader = new THREE.STLLoader();
var materials = [
new THREE.MeshPhongMaterial({ color: 0xff5533, specular: 0x111111, shininess: 200 }),
new THREE.MeshPhongMaterial({ color: 0x00ffff, specular: 0x111111, shininess: 200 }),
new THREE.MeshPhongMaterial({ color: 0x00ff00, specular: 0x111111, shininess: 200 }),
];
var models = [
{ file: 'model1.stl', position: new THREE.Vector3(0, 0, 0), scale: 1.0, material: materials[0] },
{ file: 'model2.stl', position: new THREE.Vector3(50, 0, 0), scale: 0.5, material: materials[1] },
{ file: 'model3.stl', position: new THREE.Vector3(-50, 0, 0), scale: 2.0, material: materials[2] },
];
var count = models.length;
models.forEach(function(model) {
loader.load(model.file, function(geometry) {
var mesh = new THREE.Mesh(geometry, model.material);
mesh.position.copy(model.position);
mesh.scale.set(model.scale, model.scale, model.scale);
scene.add(mesh);
count--;
if (count === 0) {
render();
}
});
});
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 100);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
function render() {
renderer.render(scene, camera);
}
```
这段代码使用了 `THREE.STLLoader` 加载多个 STL 文件,并使用不同的材质和位置、缩放参数来显示这些模型。注意在所有模型都加载完成后才调用 `render()` 函数进行渲染。
阅读全文