webgl_loader_vox.html 3.6 KB

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