【发布时间】:2020-06-26 14:22:44
【问题描述】:
我目前遇到的问题是我无法将 STL 文件加载到通过 vue.js 创建的 three.js 场景中。
代码如下所示:
<template>
<div class="GCodeViewer" >
<div id="canvas"></div>
</div>
</template>
<script>
import * as THREE from "three";
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js';
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
export default {
name: "GCodeViewer",
data: function() {
return {
show: this.tabs,
scene: null,
renderer: null,
camera: null,
controls: null,
points: [],
lightHolder: null,
height: 0,
object: null,
nozzle: null,
raycaster: null,
mouse: null,
gcode: ""
};
},
methods: {
init() {
var self = this;
var container = document.getElementById("canvas");
this.renderer = new THREE.WebGLRenderer({
clearColor: 0x000000,
clearAlpha: 1
});
var width = (window.innerWidth - 100) / 2;
var height = window.innerHeight / 2;
this.renderer.setSize(width, height);
this.renderer.setClearColor(0xffffff, 1);
container.appendChild(this.renderer.domElement);
//this.raycaster = new THREE.Raycaster();
//this.mouse = new THREE.Vector2();
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0x50555f);
var zylinder_mesh = null
var loader = new STLLoader();
loader.load( 'stl/example.stl', function ( geometry ) {
var material = new THREE.MeshPhongMaterial( {
ambient: 0xff5533,
color: 0xff5533,
specular: 0x111111,
shininess: 200 }
);
mesh = new THREE.Mesh( geometry, material );
mesh.position.set(0, 100, 0);
self.scene.add(mesh)
}, undefined, function ( error ) {console.error( error );} );
console.log(this.scene.children)
this.points.push(new THREE.Vector3(0, 0, 0));
this.camera = new THREE.PerspectiveCamera(40, width / height, 1, 10000);
this.camera.position.y = 250;
this.camera.position.z = 0;
this.camera.position.x = -300;
this.camera.lookAt(new THREE.Vector3(0, 0, 0));
var ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
this.scene.add(ambientLight);
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.BasicShadowMap;
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
},
animate() {
this.controls.update();
requestAnimationFrame(this.animate);
this.renderer.render(this.scene, this.camera);
}
}
mounted() {
this.init();
this.animate();
//window.addEventListener("mousedown", this.onMouseDown, false);
}
};
</script>
在 init() 函数中创建网格并将它们添加到场景中时一切正常。但是,加载 stl 文件给我以下错误(这是找不到文件的指示)
GCodeViewer.vue?5fe3:78 RangeError: Invalid typed array length: 6861101076
at new Float32Array (<anonymous>)
at parseBinary (STLLoader.js?518e:196)
at STLLoader.parse (STLLoader.js?518e:393)
at Object.eval [as onLoad] (STLLoader.js?518e:90)
at XMLHttpRequest.eval (three.module.js?5a89:36216)
eval @ GCodeViewer.vue?5fe3:78
eval @ STLLoader.js?518e:96
eval @ three.module.js?5a89:36216
load (async)
load @ three.module.js?5a89:36194
load @ STLLoader.js?518e:86
...
我有些需要找到正确加载 stl 文件的方法。 任何帮助将不胜感激!
最好的问候 多米尼克
【问题讨论】:
-
将STL拖放到three.js editor时是否显示?
-
是的,stl显示正确
-
在这种情况下,问题似乎出在应用程序级别。您能否使用浏览器的开发工具确保您的后端实际提供 STL 文件?有时会出现类似问题,因为 Web 服务器确实返回了无效的 HTTP 响应(如 HTML 页面)。
-
我认为使用 devtools 我无法找到该文件(在源选项卡下)。我的建议是webpack不把它和vue组件捆绑在一起,即使它和./stl/example.stl下的vue组件“GCodeViewer”在同一个子目录下。
-
我建议您发布您的解决方案作为答案。
标签: javascript vue.js three.js stl