webgl_loader_prwm.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - PRWM loader</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. <style>
  9. .notes {
  10. position: fixed;
  11. text-align: left;
  12. bottom: 10px;
  13. font-family: "Arial", "Helvetica Neue", "Helvetica", sans-serif;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info">
  19. <strong>Models</strong>:
  20. <a class="model" href="models/prwm/faceted-nefertiti.*.prwm">Faceted Nefertiti</a>,
  21. <a class="model" href="models/prwm/smooth-suzanne.*.prwm">Smooth Suzanne</a>,
  22. <a class="model" href="models/prwm/vive-controller.*.prwm">Vive Controller</a>
  23. <div class="notes">
  24. The parsing of PRWM file is especially fast when the endianness of the file is the same as the endianness of the client platform.
  25. The loader will automatically replace the <strong>*</strong> in the model url by either <strong>le</strong> or <strong>be</strong> depending on the client platform's endianness to download the most appropriate file. <a href="https://github.com/kchapelier/PRWM" target="_blank" rel="noopener noreferrer">Specifications and implementations</a><br><br>
  26. This platform endianness is <strong id="endianness"></strong>.<br>
  27. See your console for stats.
  28. </div>
  29. </div>
  30. <!-- Import maps polyfill -->
  31. <!-- Remove this when import maps will be widely supported -->
  32. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  33. <script type="importmap">
  34. {
  35. "imports": {
  36. "three": "../build/three.module.js",
  37. "three/addons/": "./jsm/"
  38. }
  39. }
  40. </script>
  41. <script type="module">
  42. import * as THREE from 'three';
  43. import { PRWMLoader } from 'three/addons/loaders/PRWMLoader.js';
  44. let camera, scene, renderer;
  45. let mouseX = 0, mouseY = 0;
  46. let windowHalfX = window.innerWidth / 2;
  47. let windowHalfY = window.innerHeight / 2;
  48. init();
  49. animate();
  50. function init() {
  51. document.getElementById( 'endianness' ).innerHTML = PRWMLoader.isBigEndianPlatform() ? 'big-endian' : 'little-endian';
  52. const container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  55. camera.position.z = 250;
  56. // scene
  57. scene = new THREE.Scene();
  58. const ambient = new THREE.AmbientLight( 0x101030 );
  59. scene.add( ambient );
  60. const directionalLight = new THREE.DirectionalLight( 0xffeedd );
  61. directionalLight.position.set( 0, 0, 1 );
  62. scene.add( directionalLight );
  63. // model
  64. const loader = new PRWMLoader();
  65. const material = new THREE.MeshPhongMaterial( {} );
  66. let busy = false;
  67. let mesh = null;
  68. const onProgress = function ( xhr ) {
  69. if ( xhr.lengthComputable ) {
  70. const percentComplete = xhr.loaded / xhr.total * 100;
  71. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  72. if ( xhr.loaded === xhr.total ) {
  73. console.log( 'File size: ' + ( xhr.total / 1024 ).toFixed( 2 ) + 'kB' );
  74. console.timeEnd( 'Download' );
  75. }
  76. }
  77. };
  78. const onError = function () {
  79. busy = false;
  80. };
  81. function loadGeometry( url ) {
  82. if ( busy ) return;
  83. busy = true;
  84. if ( mesh !== null ) {
  85. scene.remove( mesh );
  86. mesh.geometry.dispose();
  87. }
  88. console.log( '-- Loading', url );
  89. console.time( 'Download' );
  90. loader.load( url, function ( geometry ) {
  91. mesh = new THREE.Mesh( geometry, material );
  92. mesh.scale.set( 50, 50, 50 );
  93. scene.add( mesh );
  94. console.log( geometry.index ? 'indexed geometry' : 'non-indexed geometry' );
  95. console.log( '# of vertices: ' + geometry.attributes.position.count );
  96. console.log( '# of polygons: ' + ( geometry.index ? geometry.index.count / 3 : geometry.attributes.position.count / 3 ) );
  97. busy = false;
  98. }, onProgress, onError );
  99. }
  100. //
  101. renderer = new THREE.WebGLRenderer( { antialias: true } );
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. container.appendChild( renderer.domElement );
  105. document.addEventListener( 'mousemove', onDocumentMouseMove );
  106. //
  107. document.querySelectorAll( 'a.model' ).forEach( function ( anchor ) {
  108. anchor.addEventListener( 'click', function ( e ) {
  109. e.preventDefault();
  110. loadGeometry( anchor.href );
  111. } );
  112. } );
  113. //
  114. // * is automatically replaced by 'le' or 'be' depending on the client platform's endianness
  115. loadGeometry( './models/prwm/smooth-suzanne.*.prwm' );
  116. window.addEventListener( 'resize', onWindowResize );
  117. }
  118. function onWindowResize() {
  119. windowHalfX = window.innerWidth / 2;
  120. windowHalfY = window.innerHeight / 2;
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. function onDocumentMouseMove( event ) {
  126. mouseX = ( event.clientX - windowHalfX ) / 2;
  127. mouseY = ( event.clientY - windowHalfY ) / 2;
  128. }
  129. //
  130. function animate() {
  131. requestAnimationFrame( animate );
  132. render();
  133. }
  134. function render() {
  135. camera.position.x += ( mouseX - camera.position.x ) * .05;
  136. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  137. camera.lookAt( scene.position );
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>