webgl_loader_prwm.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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:absolute;
  11. left: 12px;
  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>
  24. <div class="notes">
  25. The parsing of PRWM file is especially fast when the endianness of the file is the same as the endianness of the client platform. 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. <script type="module">
  30. import {
  31. AmbientLight,
  32. DirectionalLight,
  33. PerspectiveCamera,
  34. Mesh,
  35. MeshPhongMaterial,
  36. Scene,
  37. WebGLRenderer,
  38. } from "../build/three.module.js";
  39. import { PRWMLoader } from './jsm/loaders/PRWMLoader.js';
  40. var container;
  41. var camera, scene, renderer;
  42. var mouseX = 0, mouseY = 0;
  43. var windowHalfX = window.innerWidth / 2;
  44. var windowHalfY = window.innerHeight / 2;
  45. init();
  46. animate();
  47. function init() {
  48. document.getElementById( 'endianness' ).innerHTML = PRWMLoader.isBigEndianPlatform() ? 'big-endian' : 'little-endian';
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  52. camera.position.z = 250;
  53. // scene
  54. scene = new Scene();
  55. var ambient = new AmbientLight( 0x101030 );
  56. scene.add( ambient );
  57. var directionalLight = new DirectionalLight( 0xffeedd );
  58. directionalLight.position.set( 0, 0, 1 );
  59. scene.add( directionalLight );
  60. // model
  61. var loader = new PRWMLoader();
  62. var material = new MeshPhongMaterial( {} );
  63. var busy = false;
  64. var mesh = null;
  65. var onProgress = function ( xhr ) {
  66. if ( xhr.lengthComputable ) {
  67. var percentComplete = xhr.loaded / xhr.total * 100;
  68. console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
  69. if ( xhr.loaded === xhr.total ) {
  70. console.log( 'File size: ' + ( xhr.total / 1024 ).toFixed( 2 ) + 'kB' );
  71. console.timeEnd( 'Download' );
  72. }
  73. }
  74. };
  75. var onError = function () {
  76. busy = false;
  77. };
  78. function loadGeometry( url ) {
  79. if ( busy ) return;
  80. busy = true;
  81. if ( mesh !== null ) {
  82. scene.remove( mesh );
  83. mesh.geometry.dispose();
  84. }
  85. console.log( '-- Loading', url );
  86. console.time( 'Download' );
  87. loader.load( url, function ( geometry ) {
  88. mesh = new Mesh( geometry, material );
  89. mesh.scale.set( 50, 50, 50 );
  90. scene.add( mesh );
  91. console.log( geometry.index ? 'indexed geometry' : 'non-indexed geometry' );
  92. console.log( '# of vertices: ' + geometry.attributes.position.count );
  93. console.log( '# of polygons: ' + ( geometry.index ? geometry.index.count / 3 : geometry.attributes.position.count / 3 ) );
  94. busy = false;
  95. }, onProgress, onError );
  96. }
  97. //
  98. renderer = new WebGLRenderer( { antialias: true } );
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. container.appendChild( renderer.domElement );
  102. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  103. //
  104. document.querySelectorAll( 'a.model' ).forEach( function ( anchor ) {
  105. anchor.addEventListener( 'click', function ( e ) {
  106. e.preventDefault();
  107. loadGeometry( anchor.href );
  108. } );
  109. } );
  110. //
  111. // * is automatically replaced by 'le' or 'be' depending on the client platform's endianness
  112. loadGeometry( './models/prwm/smooth-suzanne.*.prwm' );
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. }
  115. function onWindowResize() {
  116. windowHalfX = window.innerWidth / 2;
  117. windowHalfY = window.innerHeight / 2;
  118. camera.aspect = window.innerWidth / window.innerHeight;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. }
  122. function onDocumentMouseMove( event ) {
  123. mouseX = ( event.clientX - windowHalfX ) / 2;
  124. mouseY = ( event.clientY - windowHalfY ) / 2;
  125. }
  126. //
  127. function animate() {
  128. requestAnimationFrame( animate );
  129. render();
  130. }
  131. function render() {
  132. camera.position.x += ( mouseX - camera.position.x ) * .05;
  133. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  134. camera.lookAt( scene.position );
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>