-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathheroes2.html
More file actions
391 lines (286 loc) · 9.84 KB
/
heroes2.html
File metadata and controls
391 lines (286 loc) · 9.84 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<!DOCTYPE html>
<html lang="en">
<head>
<title>HEROES LAB</title>
<link rel="shortcut icon" href="favicon.ico">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta http-equiv="Permissions-Policy" content="interest-cohort=()">
<meta http-equiv="Pragma" content="no-cache">
</head>
<body style="background-color:#333; margin:0; padding:0; overflow:hidden;">
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"uil": "./src/libs/uil.module.js",
"tween": "./src/libs/tween.esm.js",
"three": "./three/build/three.module.js",
"three/addons/": "./three/examples/jsm/",
"3TH/": "./src/3TH/"
}
}
</script>
<script type="module" >
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { Avatar } from '3TH/character/Avatar.js';
import { Reflector } from '3TH/objects/Reflector.js';
import { Pool } from '3TH/Pool.js';
import { Gui } from '3TH/GuiAvatar.js';
import { Logo } from '3TH/Logo.js';
const env = 'river';
let camera, controls, scene, renderer, logo;
let hero, hero2, hero3, hero4, hero5, gui, water, dirLight, floor, mirror;
let clone = 30;
let heroes = [];
const t = { now:0, delta:0, then:0, interval: 0, tmp:0, n:0, dt:0 }
const vs = { w:window.innerWidth, h:window.innerHeight, r:window.innerWidth / window.innerHeight}
//const clock = new THREE.Clock();
let needResize = false;
init();
animate();
function init() {
scene = new THREE.Scene();
window.mouse = new THREE.Vector2();
const geometry = new THREE.PlaneGeometry( 5, 5 );
geometry.rotateX(-Math.PI*0.5)
floor = new THREE.Mesh( geometry, new THREE.ShadowMaterial( { opacity:0.3, depthWrite:false, transparent:true } ) );
floor.renderOrder = -1;
floor.receiveShadow = true;
floor.castShadow = false;
/*floor.material.onBeforeCompile = function ( shader ) {
Shader.modify( shader );
shader.fragmentShader = shader.fragmentShader.replace(
`gl_FragColor = vec4( color, opacity * ( 1.0 - getShadowMask() ) );`,`gl_FragColor = vec4( color, shadow * ( 1.0 - getShadowMask() ) );`)
}*/
scene.add( floor );
//const mesh2 = new THREE.GridHelper( 5, 5, 0x101010, 0x050505 );
//scene.add( mesh2 );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( vs.w, vs.h );
renderer.setClearColor( 0x1e1c1c, 1 );//212121
//renderer.useLegacyLights = false
//renderer.physicallyCorrectLights = true
renderer.shadowMap.enabled = true;
//renderer.shadowMap.type = THREE.PCFSoftShadowMap;
//renderer.shadowMap.type = THREE.BasicShadowMap;
//renderer.shadowMap.type = THREE.VSMShadowMap;
//renderer.toneMapping = THREE.CustomToneMapping//
//renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.toneMappingExposure = 1.0;
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild( renderer.domElement );
document.body.addEventListener( 'mousemove', move, false );
document.body.addEventListener( 'mouseleave', out, false );
/*const hemiLight = new THREE.HemisphereLight( 0xFFFFFF, 0x303030, 0.0 );
//const hemiLight = new THREE.HemisphereLight( 0xFF0000, 0x00FF00, 0.9 );
hemiLight.position.set( 0, 20, 0 );
scene.add( hemiLight );*/
dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
dirLight.position.set( - 2.5, 5, 5 );
dirLight.castShadow = true;
dirLight.shadow.camera.top = 4;
dirLight.shadow.camera.bottom = - 4;
dirLight.shadow.camera.left = - 4;
dirLight.shadow.camera.right = 4;
dirLight.shadow.camera.near = 4;
dirLight.shadow.camera.far = 10;
dirLight.shadow.radius = 4;
dirLight.shadow.bias = -0.0005;
//dirLight.shadow.normalBias = 0.0075
//console.log( dirLight.shadow )//.radius = 4;
dirLight.shadow.mapSize.setScalar( 2048*2 )
scene.add( dirLight );
/*let map = new THREE.TextureLoader().load('./assets/textures/light.jpg');
map.encoding = THREE.sRGBEncoding
//map.magFilter = THREE.LinearFilter;
//map.minFilter = THREE.LinearFilter;
const spotLight = new THREE.SpotLight( 0xffffff, 0.3 );
spotLight.position.set( 2.5, 1, -5 );
spotLight.angle = Math.PI / 6;
spotLight.penumbra = 1;
spotLight.decay = 2;
spotLight.distance = 10;
spotLight.map = map
scene.add( spotLight )*/
camera = new THREE.PerspectiveCamera( 60, vs.r, 0.01, 200 );
camera.position.set( 0, 1.2, 3 );
controls = new OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 1, 0 );
controls.update();
//
window.addEventListener( 'resize', onWindowResize );
document.body.addEventListener( 'dragover', function(e){ e.preventDefault() }, false );
document.body.addEventListener( 'dragend', function(e){ e.preventDefault() }, false );
document.body.addEventListener( 'dragleave', function(e){ e.preventDefault()}, false );
document.body.addEventListener( 'drop', drop, false );
logo = new Logo()
Pool.log = function (msg){ logo.log( msg ) };
preLoadEnvmap()
}
function preLoadEnvmap(){
const path = './assets/textures/equirectangular/'
const asset = [ env + '.hdr' ]
Pool.load( asset, next, path, 'loading envmap...' )
}
function next(){
scene.environment = Pool.get( env, 'hdr' )
hero = new Avatar( { compact:false, quality:1, compact:true } );
//hero = new Avatar( { compact:true, material:false, morph:false } );
scene.add( hero );
hero.setPosition(0,0,0)
console.time("man time");
//
hero.onReady = function () {
console.timeEnd("man time");
hero2 = new Avatar({ type:'woman' });
//hero2 = new Avatar({ type:'woman', compact:true, material:false, morph:false });
scene.add( hero2 );
//hero2.position.x = -0.5
hero2.setPosition(0,0,-1)
console.time("woman time");
window.gui = new Gui( hero, hero2, cloneTest, addReflection, playAnim, upMaterial );
hero2.onReady = function () {
console.timeEnd("woman time");
//console.log('ok')
//animate();
//addReflection()
//hero2.loadAnimationFbx('./assets/animation/AnimationLibrary3.fbx')
}
//window.gui = new Gui( hero, null, cloneTest );
//cloneTest( true )
}
}
function playAnim( s ){
if(hero) hero.play(s);
if(hero2) hero2.play(s)
/*if(hero3) hero3.play(s);
if(hero4) hero4.play(s);
if(hero5) hero5.play(s);*/
}
function upMaterial( s ){
if( hero ) hero3.setMaterial(s);
if( hero2 ) hero4.setMaterial(s);
if( hero3 ) hero3.setMaterial(s);
if( hero4 ) hero4.setMaterial(s);
if( hero5 ) hero5.setMaterial(s);
}
function addReflection( b ){
if(b){
scene.remove( floor );
if(!mirror){
mirror = new Reflector( {
clipBias: 0.003,
textureSize: window.innerHeight * window.devicePixelRatio*0.5,
//textureWidth: window.innerWidth * window.devicePixelRatio*0.25,
//textureHeight: window.innerHeight * window.devicePixelRatio*0.25,
color: 0x505050,//0x090909
reflect:1.0,
encoding:true,
});
}
//mirror.position.y = -0.01
scene.add( mirror );
} else {
if( mirror ) scene.remove(mirror)
scene.add( floor )
}
}
function rand ( low, high ) { return low + Math.random() * ( high - low ); }
function randInt( low, high ) { return low + Math.floor( Math.random() * ( high - low + 1 ) ); }
function cloneTest( b ){
var i = clone, h, n, model = 'brown', anim, s, r, d;
if( b ){
while(i--){
r = rand(-Math.PI, Math.PI);
d = rand(1, 5);
n = randInt(0,3);
n = randInt(0,2);
switch( n ){
case 0: anim = 'idle'; break;
case 1: anim = 'Jog Forward'; break;
case 2: anim = 'Standard Run'; break;
}
h = hero.clone({type:randInt(0,1) ? 'man': 'woman'} );
//h.position.set( rand(-5, 5), 0, rand(-5, 5) )
h.position.set( d * Math.sin(r), 0, d * Math.cos(r) )
h.rotation.y = r;
s = 1.2-d/5;
h.scale.set( s, s, s );
scene.add( h )
h.updateMatrix();
h.play( anim );
//h.addExo(true)
//h.addHelper(true)
heroes.push( h );
}
} else {
i = heroes.length;
while(i--){
h = heroes[i];
h.dispose();
}
heroes = [];
}
}
function drop( e ) {
e.preventDefault();
//if ( !e.dataTransfer.items) return
const file = e.dataTransfer.files[0]
const reader = new FileReader();
const name = file.name;
const type = name.substring(name.lastIndexOf('.')+1, name.length )
const finalName = name.substring( name.lastIndexOf('/')+1, name.lastIndexOf('.') )
reader.readAsArrayBuffer( file )
reader.onload = function ( e ) {
if( type==='fbx' ) hero.directFbx(e.target.result, finalName)
if( type==='glb' ) hero.directGlbTest(e.target.result, finalName)
}.bind(this);
}
function onWindowResize() {
vs.w = window.innerWidth;
vs.h = window.innerHeight;
vs.r = vs.w / vs.h;
needResize = true
}
function resize()
{
if( !needResize ) return
camera.aspect = vs.r;
camera.updateProjectionMatrix();
renderer.setSize( vs.w, vs.h );
if( window.gui ) window.gui.resize();
needResize = false
}
function move(e) {
window.mouse.set( ( e.clientX / vs.w ) * 2 - 1, - ( e.clientY / vs.h ) * 2 + 1 );
}
function out(e) {
window.mouse.set( 0, 0 );
}
function animate( stamp = 0 ) {
requestAnimationFrame( animate );
t.now = stamp !== undefined ? stamp : Date.now();
t.dt = (t.now - t.then)
t.delta = t.dt * 0.001;
t.then = t.now;
if(t.delta > 1) t.delta = 0;
if ( t.now - 1000 > t.tmp && hero2 ){ t.tmp = t.now; logo.log( t.n ); t.n = 0; }; t.n++;
resize();
if(hero){
hero.update( t.delta );
let i = heroes.length;
if(hero2) hero2.update( t.delta );
while(i--) heroes[i].update( t.delta );
}
//if(water) water.material.uniforms[ 'time' ].value += 0.0002;
renderer.render( scene, camera );
}
</script>
</body>
</html>