赞
踩
GeoSTLLLoader 类提供两个方法。
import { Mesh } from "three"; import { STLLoader } from "three/examples/jsm/loaders/STLLoader"; class GeoSTLLLoader { constructor(file, scene, material) { this.file = file; this.scene = scene; this.material = material; } load() { var loader = new STLLoader(); var material = this.material; var scene = this.scene; loader.load(this.file, function (geometry) { var mesh = new Mesh(geometry, material); scene.add(mesh); }) } parse() { var loader = new STLLoader(); var geometry = loader.parse(this.file); return geometry; } } export { GeoSTLLLoader };
测试代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>第一个three.js文件_WebGL三维场景</title> <style> body { margin: 0; overflow: hidden; /* 隐藏body窗口区域滚动条 */ } </style> <!--引入three.js三维引擎--> <script src="../three.js-master/build/three.js"></script> <script src="../three.js-master/examples/js/controls/OrbitControls.js"></script> <script src="../three.js-master/examples/js/loaders/STLLoader.js"></script> </head> <body> <script> /** * 创建场景对象Scene */ var scene = new THREE.Scene(); var loader = new THREE.STLLoader(); loader.load("plane.stl", function (geometry) { var material = new THREE.MeshLambertMaterial({ color: 0x0000ff, }); //材质对象Material var mesh = new THREE.Mesh(geometry, material); scene.add(mesh); }); //点光源 var point = new THREE.DirectionalLight(0xffffff); point.position.set(1000, 50, 1000); scene.add(point); scene.add(new THREE.AmbientLight(0x444444)); /** * 光源设置 */ //点光源 var point = new THREE.PointLight(0xffffff); point.position.set(400, 200, 300); //点光源位置 scene.add(point); //点光源添加到场景中 point.castShadow = true; //环境光 var ambient = new THREE.AmbientLight(0x444444); scene.add(ambient); // 辅助坐标系 参数250表示坐标系大小,可以根据场景大小去设置 var axisHelper = new THREE.AxesHelper(250); scene.add(axisHelper); // console.log(scene) // console.log(scene.children) /** * 相机设置 */ var width = window.innerWidth; //窗口宽度 var height = window.innerHeight; //窗口高度 var k = width / height; //窗口宽高比 var s = 200; //三维场景显示范围控制系数,系数越大,显示的范围越大 //创建相机对象 var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000); camera.position.set(200, 300, 200); //设置相机位置 camera.lookAt(scene.position); //设置相机方向(指向的场景对象) /** * 创建渲染器对象 */ var renderer = new THREE.WebGLRenderer(); renderer.setSize(width, height); //设置渲染区域尺寸 renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色 document.body.appendChild(renderer.domElement); //body元素中插入canvas对象 renderer.shadowMap.enabled = true; //执行渲染操作 指定场景、相机作为参数 // renderer.render(scene, camera); function render() { renderer.render(scene, camera); //执行渲染操作 // mesh.rotateY(0.01);//每次绕y轴旋转0.01弧度 requestAnimationFrame(render); //请求再次执行渲染函数render } render(); var controls = new THREE.OrbitControls(camera, renderer.domElement); //创建控件对象 </script> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。