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.
 
 
 
 
 

150 lines
3.6 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - loaders - vox</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">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - vox loader (<a href="https://ephtracy.github.io/" target="_blank" rel="noopener">Magica Voxel</a>)
</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 { VOXLoader, VOXMesh } from 'three/addons/loaders/VOXLoader.js';
let camera, controls, scene, renderer;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.set( 0.175, 0.075, 0.175 );
scene = new THREE.Scene();
scene.add( camera );
// light
const hemiLight = new THREE.HemisphereLight( 0x888888, 0x444444, 1 );
scene.add( hemiLight );
const dirLight = new THREE.DirectionalLight( 0xffffff, 0.75 );
dirLight.position.set( 1.5, 3, 2.5 );
scene.add( dirLight );
const dirLight2 = new THREE.DirectionalLight( 0xffffff, 0.5 );
dirLight2.position.set( - 1.5, - 3, - 2.5 );
scene.add( dirLight2 );
const loader = new VOXLoader();
loader.load( 'models/vox/monu10.vox', function ( chunks ) {
for ( let i = 0; i < chunks.length; i ++ ) {
const chunk = chunks[ i ];
// displayPalette( chunk.palette );
const mesh = new VOXMesh( chunk );
mesh.scale.setScalar( 0.0015 );
scene.add( mesh );
}
} );
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = .1;
controls.maxDistance = 0.5;
//
window.addEventListener( 'resize', onWindowResize );
}
/*
function displayPalette( palette ) {
const canvas = document.createElement( 'canvas' );
canvas.width = 8;
canvas.height = 32;
canvas.style.position = 'absolute';
canvas.style.top = '0';
canvas.style.width = '100px';
canvas.style.imageRendering = 'pixelated';
document.body.appendChild( canvas );
const context = canvas.getContext( '2d' );
for ( let c = 0; c < 256; c ++ ) {
const x = c % 8;
const y = Math.floor( c / 8 );
const hex = palette[ c + 1 ];
const r = hex >> 0 & 0xff;
const g = hex >> 8 & 0xff;
const b = hex >> 16 & 0xff;
context.fillStyle = `rgba(${r},${g},${b},1)`;
context.fillRect( x, 31 - y, 1, 1 );
}
}
*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>