webgl_loader_vox.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, 0x000000, 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 loader = new VOXLoader();
  32. loader.load( 'models/vox/monu10.vox', function ( chunks ) {
  33. const geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
  34. const material = new THREE.MeshStandardMaterial();
  35. const color = new THREE.Color();
  36. const matrix = new THREE.Matrix4();
  37. for ( let i = 0; i < chunks.length; i ++ ) {
  38. const chunk = chunks[ i ];
  39. const size = chunk.size;
  40. const data = chunk.data;
  41. const palette = chunk.palette;
  42. // displayPalette( palette );
  43. const mesh = new THREE.InstancedMesh( geometry, material, data.length / 4 );
  44. mesh.scale.setScalar( 0.0015 );
  45. scene.add( mesh );
  46. for ( let j = 0, k = 0; j < data.length; j += 4, k ++ ) {
  47. const x = data[ j + 0 ] - size.x / 2;
  48. const y = data[ j + 1 ] - size.y / 2;
  49. const z = data[ j + 2 ] - size.z / 2;
  50. const c = data[ j + 3 ];
  51. const hex = palette[ c ];
  52. const r = ( hex >> 0 & 0xff ) / 0xff;
  53. const g = ( hex >> 8 & 0xff ) / 0xff;
  54. const b = ( hex >> 16 & 0xff ) / 0xff;
  55. mesh.setColorAt( k, color.setRGB( r, g, b ) );
  56. mesh.setMatrixAt( k, matrix.setPosition( x, z, - y ) );
  57. }
  58. }
  59. } );
  60. // renderer
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. const container = document.createElement( 'div' );
  65. document.body.appendChild( container );
  66. container.appendChild( renderer.domElement );
  67. // controls
  68. controls = new OrbitControls( camera, renderer.domElement );
  69. controls.minDistance = .1;
  70. controls.maxDistance = 0.5;
  71. //
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. /*
  75. function displayPalette( palette ) {
  76. const canvas = document.createElement( 'canvas' );
  77. canvas.width = 8;
  78. canvas.height = 32;
  79. canvas.style.position = 'absolute';
  80. canvas.style.top = '0';
  81. canvas.style.width = '100px';
  82. canvas.style.imageRendering = 'pixelated';
  83. document.body.appendChild( canvas );
  84. const context = canvas.getContext( '2d' );
  85. for ( let c = 0; c < 256; c ++ ) {
  86. const x = c % 8;
  87. const y = Math.floor( c / 8 );
  88. const hex = palette[ c + 1 ];
  89. const r = hex >> 0 & 0xff;
  90. const g = hex >> 8 & 0xff;
  91. const b = hex >> 16 & 0xff;
  92. context.fillStyle = `rgba(${r},${g},${b},1)`;
  93. context.fillRect( x, 31 - y, 1, 1 );
  94. }
  95. }
  96. */
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. controls.update();
  105. renderer.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>