webgl_loader_vox.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  24. import { VOXLoader, VOXMesh } from 'three/addons/loaders/VOXLoader.js';
  25. let camera, controls, scene, renderer;
  26. init();
  27. function init() {
  28. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 10 );
  29. camera.position.set( 0.175, 0.075, 0.175 );
  30. scene = new THREE.Scene();
  31. scene.add( camera );
  32. // light
  33. const hemiLight = new THREE.HemisphereLight( 0xcccccc, 0x444444, 3 );
  34. scene.add( hemiLight );
  35. const dirLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  36. dirLight.position.set( 1.5, 3, 2.5 );
  37. scene.add( dirLight );
  38. const dirLight2 = new THREE.DirectionalLight( 0xffffff, 1.5 );
  39. dirLight2.position.set( - 1.5, - 3, - 2.5 );
  40. scene.add( dirLight2 );
  41. const loader = new VOXLoader();
  42. loader.load( 'models/vox/monu10.vox', function ( chunks ) {
  43. for ( let i = 0; i < chunks.length; i ++ ) {
  44. const chunk = chunks[ i ];
  45. // displayPalette( chunk.palette );
  46. const mesh = new VOXMesh( chunk );
  47. mesh.scale.setScalar( 0.0015 );
  48. scene.add( mesh );
  49. }
  50. } );
  51. // renderer
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.setAnimationLoop( animate );
  56. document.body.appendChild( renderer.domElement );
  57. // controls
  58. controls = new OrbitControls( camera, renderer.domElement );
  59. controls.minDistance = .1;
  60. controls.maxDistance = 0.5;
  61. //
  62. window.addEventListener( 'resize', onWindowResize );
  63. }
  64. /*
  65. function displayPalette( palette ) {
  66. const canvas = document.createElement( 'canvas' );
  67. canvas.width = 8;
  68. canvas.height = 32;
  69. canvas.style.position = 'absolute';
  70. canvas.style.top = '0';
  71. canvas.style.width = '100px';
  72. canvas.style.imageRendering = 'pixelated';
  73. document.body.appendChild( canvas );
  74. const context = canvas.getContext( '2d' );
  75. for ( let c = 0; c < 256; c ++ ) {
  76. const x = c % 8;
  77. const y = Math.floor( c / 8 );
  78. const hex = palette[ c + 1 ];
  79. const r = hex >> 0 & 0xff;
  80. const g = hex >> 8 & 0xff;
  81. const b = hex >> 16 & 0xff;
  82. context.fillStyle = `rgba(${r},${g},${b},1)`;
  83. context.fillRect( x, 31 - y, 1, 1 );
  84. }
  85. }
  86. */
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function animate() {
  93. controls.update();
  94. renderer.render( scene, camera );
  95. }
  96. </script>
  97. </body>
  98. </html>