webgl_loader_vox.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - vox</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <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>)
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { VOXLoader } from './jsm/loaders/VOXLoader.js';
  17. let camera, controls, scene, renderer;
  18. init();
  19. animate();
  20. function init() {
  21. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10 );
  22. camera.position.set( 0.175, 0.075, 0.175 );
  23. scene = new THREE.Scene();
  24. scene.add( camera );
  25. // light
  26. const hemiLight = new THREE.HemisphereLight( 0x888888, 0x444444, 1 );
  27. scene.add( hemiLight );
  28. const dirLight = new THREE.DirectionalLight( 0xffffff, 0.75 );
  29. dirLight.position.set( 1.5, 3, 2.5 );
  30. scene.add( dirLight );
  31. const dirLight2 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  32. dirLight2.position.set( - 1.5, - 3, - 2.5 );
  33. scene.add( dirLight2 );
  34. const loader = new VOXLoader();
  35. loader.load( 'models/vox/monu10.vox', function ( chunks ) {
  36. for ( let i = 0; i < chunks.length; i ++ ) {
  37. const chunk = chunks[ i ];
  38. const geometry = buildGeometry( chunk );
  39. const material = new THREE.MeshStandardMaterial( {
  40. vertexColors: geometry.hasAttribute( 'color' )
  41. } );
  42. const mesh = new THREE.Mesh( geometry, material );
  43. mesh.scale.setScalar( 0.0015 );
  44. scene.add( mesh );
  45. }
  46. } );
  47. // renderer
  48. renderer = new THREE.WebGLRenderer();
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. document.body.appendChild( renderer.domElement );
  52. // controls
  53. controls = new OrbitControls( camera, renderer.domElement );
  54. controls.minDistance = .1;
  55. controls.maxDistance = 0.5;
  56. //
  57. window.addEventListener( 'resize', onWindowResize, false );
  58. }
  59. function buildGeometry( chunk ) {
  60. const data = chunk.data;
  61. const size = chunk.size;
  62. const palette = chunk.palette;
  63. // displayPalette( palette );
  64. const vertices = [];
  65. const colors = [];
  66. const nx = [ 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1 ];
  67. const px = [ 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0 ];
  68. const py = [ 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 ];
  69. const ny = [ 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0 ];
  70. const nz = [ 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0 ];
  71. const pz = [ 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1 ];
  72. function add( tile, x, y, z, r, g, b ) {
  73. x -= size.x / 2;
  74. y -= size.z / 2;
  75. z += size.y / 2;
  76. for ( let i = 0; i < 18; i += 3 ) {
  77. vertices.push( tile[ i + 0 ] + x, tile[ i + 1 ] + y, tile[ i + 2 ] + z );
  78. colors.push( r, g, b );
  79. }
  80. }
  81. // Store data in a volume for sampling
  82. const offsety = size.x;
  83. const offsetz = size.x * size.y;
  84. const array = new Uint8Array( size.x * size.y * size.z );
  85. for ( let j = 0; j < data.length; j += 4 ) {
  86. const x = data[ j + 0 ];
  87. const y = data[ j + 1 ];
  88. const z = data[ j + 2 ];
  89. const index = x + ( y * offsety ) + ( z * offsetz );
  90. array[ index ] = 255;
  91. }
  92. // Construct geometry
  93. let hasColors = false;
  94. for ( let j = 0; j < data.length; j += 4 ) {
  95. const x = data[ j + 0 ];
  96. const y = data[ j + 1 ];
  97. const z = data[ j + 2 ];
  98. const c = data[ j + 3 ];
  99. const hex = palette[ c ];
  100. const r = ( hex >> 0 & 0xff ) / 0xff;
  101. const g = ( hex >> 8 & 0xff ) / 0xff;
  102. const b = ( hex >> 16 & 0xff ) / 0xff;
  103. if ( r > 0 || g > 0 || b > 0 ) hasColors = true;
  104. const index = x + ( y * offsety ) + ( z * offsetz );
  105. if ( array[ index + 1 ] === 0 || x === size.x - 1 ) add( px, x, z, - y, r, g, b );
  106. if ( array[ index - 1 ] === 0 || x === 0 ) add( nx, x, z, - y, r, g, b );
  107. if ( array[ index + offsety ] === 0 || y === size.y - 1 ) add( ny, x, z, - y, r, g, b );
  108. if ( array[ index - offsety ] === 0 || y === 0 ) add( py, x, z, - y, r, g, b );
  109. if ( array[ index + offsetz ] === 0 || z === size.z - 1 ) add( pz, x, z, - y, r, g, b );
  110. if ( array[ index - offsetz ] === 0 || z === 0 ) add( nz, x, z, - y, r, g, b );
  111. }
  112. const geometry = new THREE.BufferGeometry();
  113. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  114. geometry.computeVertexNormals();
  115. if ( hasColors ) geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  116. return geometry;
  117. }
  118. /*
  119. function displayPalette( palette ) {
  120. const canvas = document.createElement( 'canvas' );
  121. canvas.width = 8;
  122. canvas.height = 32;
  123. canvas.style.position = 'absolute';
  124. canvas.style.top = '0';
  125. canvas.style.width = '100px';
  126. canvas.style.imageRendering = 'pixelated';
  127. document.body.appendChild( canvas );
  128. const context = canvas.getContext( '2d' );
  129. for ( let c = 0; c < 256; c ++ ) {
  130. const x = c % 8;
  131. const y = Math.floor( c / 8 );
  132. const hex = palette[ c + 1 ];
  133. const r = hex >> 0 & 0xff;
  134. const g = hex >> 8 & 0xff;
  135. const b = hex >> 16 & 0xff;
  136. context.fillStyle = `rgba(${r},${g},${b},1)`;
  137. context.fillRect( x, 31 - y, 1, 1 );
  138. }
  139. }
  140. */
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function animate() {
  147. requestAnimationFrame( animate );
  148. controls.update();
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>