123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - loaders - PRWM loader</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- .notes {
- position:absolute;
- left: 12px;
- bottom: 10px;
- font-family: "Arial", "Helvetica Neue", "Helvetica", sans-serif;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <strong>Models</strong>:
- <a class="model" href="models/prwm/faceted-nefertiti.*.prwm">Faceted Nefertiti</a>,
- <a class="model" href="models/prwm/smooth-suzanne.*.prwm">Smooth Suzanne</a>,
- <a class="model" href="models/prwm/vive-controller.*.prwm">Vive Controller</a>
- </div>
- <div class="notes">
- 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>
- This platform endianness is <strong id="endianness"></strong>.<br>
- See your console for stats.
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { PRWMLoader } from './jsm/loaders/PRWMLoader.js';
- var container;
- var camera, scene, renderer;
- var mouseX = 0, mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- init();
- animate();
- function init() {
- document.getElementById( 'endianness' ).innerHTML = PRWMLoader.isBigEndianPlatform() ? 'big-endian' : 'little-endian';
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.z = 250;
- // scene
- scene = new THREE.Scene();
- var ambient = new THREE.AmbientLight( 0x101030 );
- scene.add( ambient );
- var directionalLight = new THREE.DirectionalLight( 0xffeedd );
- directionalLight.position.set( 0, 0, 1 );
- scene.add( directionalLight );
- // model
- var loader = new PRWMLoader();
- var material = new THREE.MeshPhongMaterial( {} );
- var busy = false;
- var mesh = null;
- var onProgress = function ( xhr ) {
- if ( xhr.lengthComputable ) {
- var percentComplete = xhr.loaded / xhr.total * 100;
- console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
- if ( xhr.loaded === xhr.total ) {
- console.log( 'File size: ' + ( xhr.total / 1024 ).toFixed( 2 ) + 'kB' );
- console.timeEnd( 'Download' );
- }
- }
- };
- var onError = function () {
- busy = false;
- };
- function loadGeometry( url ) {
- if ( busy ) return;
- busy = true;
- if ( mesh !== null ) {
- scene.remove( mesh );
- mesh.geometry.dispose();
- }
- console.log( '-- Loading', url );
- console.time( 'Download' );
- loader.load( url, function ( geometry ) {
- mesh = new THREE.Mesh( geometry, material );
- mesh.scale.set( 50, 50, 50 );
- scene.add( mesh );
- console.log( geometry.index ? 'indexed geometry' : 'non-indexed geometry' );
- console.log( '# of vertices: ' + geometry.attributes.position.count );
- console.log( '# of polygons: ' + ( geometry.index ? geometry.index.count / 3 : geometry.attributes.position.count / 3 ) );
- busy = false;
- }, onProgress, onError );
- }
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- //
- document.querySelectorAll( 'a.model' ).forEach( function ( anchor ) {
- anchor.addEventListener( 'click', function ( e ) {
- e.preventDefault();
- loadGeometry( anchor.href );
- } );
- } );
- //
- // * is automatically replaced by 'le' or 'be' depending on the client platform's endianness
- loadGeometry( './models/prwm/smooth-suzanne.*.prwm' );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- windowHalfX = window.innerWidth / 2;
- windowHalfY = window.innerHeight / 2;
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onDocumentMouseMove( event ) {
- mouseX = ( event.clientX - windowHalfX ) / 2;
- mouseY = ( event.clientY - windowHalfY ) / 2;
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- camera.position.x += ( mouseX - camera.position.x ) * .05;
- camera.position.y += ( - mouseY - camera.position.y ) * .05;
- camera.lookAt( scene.position );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|