forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_loader_prwm.html
218 lines (142 loc) · 5.5 KB
/
webgl_loader_prwm.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!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: fixed;
text-align: left;
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 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>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { PRWMLoader } from 'three/addons/loaders/PRWMLoader.js';
let camera, scene, renderer;
let mouseX = 0, mouseY = 0;
let windowHalfX = window.innerWidth / 2;
let windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
document.getElementById( 'endianness' ).innerHTML = PRWMLoader.isBigEndianPlatform() ? 'big-endian' : 'little-endian';
const 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();
const ambient = new THREE.AmbientLight( 0x101030 );
scene.add( ambient );
const directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, 0, 1 );
scene.add( directionalLight );
// model
const loader = new PRWMLoader();
const material = new THREE.MeshPhongMaterial( {} );
let busy = false;
let mesh = null;
const onProgress = function ( xhr ) {
if ( xhr.lengthComputable ) {
const 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' );
}
}
};
const 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 );
//
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 );
}
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>