-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
simple.html
178 lines (126 loc) · 4.71 KB
/
simple.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>simple</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body style="background-color:#000; margin:0; padding:0; overflow:hidden;">
<!-- 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": "./src/libs/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from './src/jsm/controls/OrbitControls.js';
import { RGBELoader } from './src/jsm/loaders/RGBELoader.js';
import { phy, math } from './build/Phy.module.js';
//const motor = 'PHYSX';
//const motor = 'OIMO';
const motor = 'HAVOK';
//const motor = 'JOLT';
let camera, scene, renderer, controler, text;
let needResize = false;
init();
animate();
// THREE
function init() {
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.6;
document.body.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set(12,1,32);
controler = new OrbitControls( camera, renderer.domElement );
controler.target.set( 0, 10, 0 );
controler.screenSpacePanning = true;
controler.update();
new RGBELoader()
.setPath( './assets/textures/equirectangular/' )
.load( 'factory.hdr', function ( texture ) {
texture.mapping = THREE.EquirectangularReflectionMapping;
//scene.background = texture;
scene.background = new THREE.Color( 0x33322c );
scene.environment = texture;
initEngine();
})
window.addEventListener( 'resize', () => { needResize = true; }, false );
text = document.createElement( 'div' );
text.style.cssText = "position:absolute; top:30px; left:30px; color:#7c806c; color:#CCCCCC; text-shadow: 1px 1px 1px #000; pointer-events:none; font-size:20px; font-family:Monospace;"
document.body.appendChild( text )
}
function resize() {
if(!needResize) return;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
needResize = false;
}
// LOOP
function animate( stamp = 0 ) {
requestAnimationFrame( animate );
resize();
//phy.doStep( stamp );
renderer.render( scene, camera );
}
// PHYSICS
function initEngine() {
// use compact version of physic engine in /compact folder
phy.init({ type:motor, worker:true, compact:true, callback:startDemo, renderer:renderer, scene:scene });
const envmap = phy.addEnvmap({cube:true})
let m = new THREE.Mesh( new THREE.SphereGeometry(0.5), new THREE.MeshBasicMaterial({color:new THREE.Color(100,100,100)}))
m.position.set(0-1,3,2)
envmap.scene.background = new THREE.Color(0.5,0.5,0.8)
envmap.scene.add( m );
envmap.blur = 0.5
envmap.render()
}
function startDemo() {
text.innerHTML = motor + " " + phy.startTime();
// load glb model
phy.load("./assets/models/minmac.glb", onLoad );
}
function onLoad() {
const texture = new THREE.TextureLoader().load( './assets/textures/minmac.jpg' );
const texture2 = new THREE.TextureLoader().load( './assets/textures/minmac_l.jpg' );
texture.flipY = false;
texture2.flipY = false;
const material = new THREE.MeshStandardMaterial( {
map:texture,
emissiveMap:texture2,
metalnessMap:texture2,
emissive:0x5E82BC,
emissiveIntensity:0.2,
roughness:0.33,
metalness:1,
});
const model = phy.getMesh('minmac');
phy.set({ substep:1, gravity:[0,-9.81,0], fps:60 });
// add static ground
phy.add({ type:'plane', size:[300,1,300], visible:false });
const option = { instance:'bx', type:'box', size:[2.5,3.4,2.8], mass:7.26, friction:0.4, restitution:0.2 };
let ar = [], pos, n=0;
let i = 100;
while(i--){
pos = [0, 1.7+3.4*n, 0];
if(n===0) phy.add( {...option, pos:pos, material:material, mesh: model.mac, meshScale:[10] });
else phy.add( {...option, pos:pos });
pos[0] = pos[0]+3.4;
phy.add( {...option, pos:pos });
pos[0] = pos[0]-(3.4*2);
phy.add( {...option, pos:pos });
n++
}
}
</script>
</body>
</html>