webgl_loader_vox.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. <meta property="og:title" content="three.js webgl - loaders - vox">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_vox.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_vox.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <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>)
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { VOXLoader } from 'three/addons/loaders/VOXLoader.js';
  29. let camera, controls, scene, renderer;
  30. init();
  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( 0xcccccc, 0x444444, 3 );
  38. scene.add( hemiLight );
  39. const dirLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  40. dirLight.position.set( 1.5, 3, 2.5 );
  41. scene.add( dirLight );
  42. const dirLight2 = new THREE.DirectionalLight( 0xffffff, 1.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 ( result ) {
  47. const mesh = result.scene.children[ 0 ];
  48. mesh.position.y = 0;
  49. mesh.scale.setScalar( 0.0015 );
  50. scene.add( mesh );
  51. } );
  52. // renderer
  53. renderer = new THREE.WebGLRenderer( { antialias: true } );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.setAnimationLoop( animate );
  57. document.body.appendChild( renderer.domElement );
  58. // controls
  59. controls = new OrbitControls( camera, renderer.domElement );
  60. controls.minDistance = .1;
  61. controls.maxDistance = 0.5;
  62. //
  63. window.addEventListener( 'resize', onWindowResize );
  64. }
  65. /*
  66. function displayPalette( palette ) {
  67. const canvas = document.createElement( 'canvas' );
  68. canvas.width = 8;
  69. canvas.height = 32;
  70. canvas.style.position = 'absolute';
  71. canvas.style.top = '0';
  72. canvas.style.width = '100px';
  73. canvas.style.imageRendering = 'pixelated';
  74. document.body.appendChild( canvas );
  75. const context = canvas.getContext( '2d' );
  76. for ( let c = 0; c < 256; c ++ ) {
  77. const x = c % 8;
  78. const y = Math.floor( c / 8 );
  79. const hex = palette[ c + 1 ];
  80. const r = hex >> 0 & 0xff;
  81. const g = hex >> 8 & 0xff;
  82. const b = hex >> 16 & 0xff;
  83. context.fillStyle = `rgba(${r},${g},${b},1)`;
  84. context.fillRect( x, 31 - y, 1, 1 );
  85. }
  86. }
  87. */
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. function animate() {
  94. controls.update();
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>
粤ICP备19079148号