<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VTK Loader</title>
<script src="../../three-part/threejs/three.js"></script>
<script src="../../three-part/threejs/VTKLoader.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 = -30;
camera.position.y = 40;
camera.position.z = 30;
camera.lookAt(scene.position);
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);
// add ambient light
let ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
// init trackball control
let trackballControls = initTrackballControls(camera, renderer);
let clock = new THREE.Clock();
let loader = new THREE.VTKLoader();
loader.load("../assets/models/vtk/bunny.vtk", function (geometry) {
let material = new THREE.MeshNormalMaterial();
geometry.center();
geometry.computeVertexNormals();
let mesh = new THREE.Mesh(geometry, material);
mesh.scale.set(100, 100, 100);
scene.add(mesh);
});
// 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>
运行结果:
总结:
- 使用VTKLoader之前需要引入VTKLoader.js文件;
- VTKLoader加载模型的回调函数中的传入的是几何体。
网友评论