webgl_loader_prwm.html 5.2 KB

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