2
0

webgl_loader_prwm.html 5.8 KB

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