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