You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.0 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - 3MF</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
background-color: #333;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
<a href="http://3mf.io" target="_blank" rel="noopener">3MF File format</a>
<div>3MF loader test by <a href="https://github.com/technohippy" target="_blank" rel="noopener">technohippy</a></div>
<div>Files from <a href="https://github.com/3MFConsortium/3mf-samples" target="_blank" rel="noopener">3mf-samples</a></div>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
let camera, scene, renderer, object, loader, controls;
const params = {
asset: 'cube_gears'
};
const assets = [
'cube_gears',
'facecolors',
'multipletextures',
'vertexcolors'
];
init();
function init() {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x333333 );
scene.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 500 );
// Z is up for objects intended to be 3D printed.
camera.up.set( 0, 0, 1 );
camera.position.set( - 100, - 250, 100 );
scene.add( camera );
controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render );
controls.minDistance = 50;
controls.maxDistance = 400;
controls.enablePan = false;
controls.update();
const pointLight = new THREE.PointLight( 0xffffff, 0.8 );
camera.add( pointLight );
const manager = new THREE.LoadingManager();
manager.onLoad = function () {
const aabb = new THREE.Box3().setFromObject( object );
const center = aabb.getCenter( new THREE.Vector3() );
object.position.x += ( object.position.x - center.x );
object.position.y += ( object.position.y - center.y );
object.position.z += ( object.position.z - center.z );
controls.reset();
scene.add( object );
render();
};
loader = new ThreeMFLoader( manager );
loadAsset( params.asset );
window.addEventListener( 'resize', onWindowResize );
//
const gui = new GUI();
gui.add( params, 'asset', assets ).onChange( function ( value ) {
loadAsset( value );
} );
}
function loadAsset( asset ) {
loader.load( 'models/3mf/' + asset + '.3mf', function ( group ) {
if ( object ) {
object.traverse( function ( child ) {
if ( child.material ) child.material.dispose();
if ( child.material && child.material.map ) child.material.map.dispose();
if ( child.geometry ) child.geometry.dispose();
} );
scene.remove( object );
}
//
object = group;
} );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>