webgl_loader_prwm.html 5.3 KB

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