| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <!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>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { VOXLoader } from './jsm/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 ];
- const geometry = buildGeometry( chunk );
- const material = new THREE.MeshStandardMaterial( {
- vertexColors: geometry.hasAttribute( 'color' )
- } );
- const mesh = new THREE.Mesh( geometry, material );
- 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, false );
- }
- function buildGeometry( chunk ) {
- const data = chunk.data;
- const size = chunk.size;
- const palette = chunk.palette;
- // displayPalette( palette );
- const vertices = [];
- const colors = [];
- const nx = [ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1 ];
- const px = [ 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0 ];
- const py = [ 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 ];
- const ny = [ 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0 ];
- const nz = [ 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0 ];
- const pz = [ 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1 ];
- function add( tile, x, y, z, r, g, b ) {
- x -= size.x / 2;
- y -= size.z / 2;
- z += size.y / 2;
- for ( let i = 0; i < 18; i += 3 ) {
- vertices.push( tile[ i + 0 ] + x, tile[ i + 1 ] + y, tile[ i + 2 ] + z );
- colors.push( r, g, b );
- }
- }
- // Store data in a volume for sampling
- const offsety = size.x;
- const offsetz = size.x * size.y;
- const array = new Uint8Array( size.x * size.y * size.z );
- for ( let j = 0; j < data.length; j += 4 ) {
- const x = data[ j + 0 ];
- const y = data[ j + 1 ];
- const z = data[ j + 2 ];
- const index = x + ( y * offsety ) + ( z * offsetz );
- array[ index ] = 255;
- }
- // Construct geometry
- let hasColors = false;
- for ( let j = 0; j < data.length; j += 4 ) {
- const x = data[ j + 0 ];
- const y = data[ j + 1 ];
- const z = data[ j + 2 ];
- const c = data[ j + 3 ];
- const hex = palette[ c ];
- const r = ( hex >> 0 & 0xff ) / 0xff;
- const g = ( hex >> 8 & 0xff ) / 0xff;
- const b = ( hex >> 16 & 0xff ) / 0xff;
- if ( r > 0 || g > 0 || b > 0 ) hasColors = true;
- const index = x + ( y * offsety ) + ( z * offsetz );
- if ( array[ index + 1 ] === 0 || x === size.x - 1 ) add( px, x, z, - y, r, g, b );
- if ( array[ index - 1 ] === 0 || x === 0 ) add( nx, x, z, - y, r, g, b );
- if ( array[ index + offsety ] === 0 || y === size.y - 1 ) add( ny, x, z, - y, r, g, b );
- if ( array[ index - offsety ] === 0 || y === 0 ) add( py, x, z, - y, r, g, b );
- if ( array[ index + offsetz ] === 0 || z === size.z - 1 ) add( pz, x, z, - y, r, g, b );
- if ( array[ index - offsetz ] === 0 || z === 0 ) add( nz, x, z, - y, r, g, b );
- }
- const geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
- geometry.computeVertexNormals();
- if ( hasColors ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- return geometry;
- }
- /*
- 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>
|