webgl_loader_vox.html 4.0 KB

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