webgl_loader_vox.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, VOXMesh } 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. // displayPalette( chunk.palette );
  39. const mesh = new VOXMesh( chunk );
  40. mesh.scale.setScalar( 0.0015 );
  41. scene.add( mesh );
  42. }
  43. } );
  44. // renderer
  45. renderer = new THREE.WebGLRenderer();
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. document.body.appendChild( renderer.domElement );
  49. // controls
  50. controls = new OrbitControls( camera, renderer.domElement );
  51. controls.minDistance = .1;
  52. controls.maxDistance = 0.5;
  53. //
  54. window.addEventListener( 'resize', onWindowResize );
  55. }
  56. /*
  57. function displayPalette( palette ) {
  58. const canvas = document.createElement( 'canvas' );
  59. canvas.width = 8;
  60. canvas.height = 32;
  61. canvas.style.position = 'absolute';
  62. canvas.style.top = '0';
  63. canvas.style.width = '100px';
  64. canvas.style.imageRendering = 'pixelated';
  65. document.body.appendChild( canvas );
  66. const context = canvas.getContext( '2d' );
  67. for ( let c = 0; c < 256; c ++ ) {
  68. const x = c % 8;
  69. const y = Math.floor( c / 8 );
  70. const hex = palette[ c + 1 ];
  71. const r = hex >> 0 & 0xff;
  72. const g = hex >> 8 & 0xff;
  73. const b = hex >> 16 & 0xff;
  74. context.fillStyle = `rgba(${r},${g},${b},1)`;
  75. context.fillRect( x, 31 - y, 1, 1 );
  76. }
  77. }
  78. */
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. controls.update();
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>