<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>STL Loader</title>
<script src="../../three-part/threejs/three.js"></script>
<script src="../../three-part/threejs/STLLoader.js"></script>
<script src="../../three-part/utils/stats.min.js"></script>
<script src="../../three-part/utils/dat.gui.min.js"></script>
<script src="../controls/TrackballControls.js"></script>
<script src="../util/util.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
init();
function init() {
// show FPS
let stats = initStats();
// resize
window.addEventListener('resize', onResize, false);
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.x = 50;
camera.position.y = 50;
camera.position.z = 30;
camera.lookAt(new THREE.Vector3(0, 15, 0));
let renderer = new THREE.WebGLRenderer();
renderer.setClearColor(new THREE.Color(0x000000));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.getElementById("container").appendChild(renderer.domElement);
// init trackball control
let trackballControls = initTrackballControls(camera, renderer);
let clock = new THREE.Clock();
let loader = new THREE.STLLoader();
loader.load('../assets/models/head/SolidHead_2_lowPoly_42k.stl',
// onLoad callback
function (geometry) {
let material = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 1,
roughness: 0.5,
});
let mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -0.5 * Math.PI;
mesh.scale.set(0.3, 0.3, 0.3);
mesh.position.set(-10, 0, 10);
scene.add(mesh);
},
// onProgress callback
function (xhr) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
// onError callback
function (err) {
console.error('An error happened');
}
);
// add spotlight
let spotLight = new THREE.SpotLight(0xffffff, 1, 180, Math.PI / 4);
spotLight.shadow.mapSize.set(2048, 2048);
spotLight.position.set(0, 60, 0);
spotLight.castShadow = true;
spotLight.angle = 1.2;
spotLight.intensity = 3;
scene.add(spotLight);
// add a plane
let planeGeometry = new THREE.PlaneGeometry(100, 100, 1, 1);
let planeMaterial = new THREE.MeshLambertMaterial({
color: 0xffffff
});
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = true;
plane.rotation.x = -0.5 * Math.PI;
scene.add(plane);
// attributes which can be modified in GUI
const controls = {
};
// init GUI
initGUI();
renderScene();
function initGUI(){
let gui = new dat.GUI();
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function renderScene(){
trackballControls.update(clock.getDelta());
stats.update();
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
}
</script>
</body>
</html>
运行结果:
总结:
- 使用STLLoader之前需要引入STLLoader.js文件;
- STLLoader回调函数中的geometry为几何体,需要自己制定材质。
网友评论