-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.html
235 lines (222 loc) · 7.31 KB
/
index.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<title>gpu-physics.js by schteppe</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="twitter:creator" content="@schteppe" />
<meta property="og:title" content="gpu-physics.js by schteppe" />
<meta property="og:description" content="Play around with a massive amount of rigid bodies in this demo." />
<meta property="og:url" content="https://schteppe.github.io/gpu-physics.js/" />
<meta property="og:type" content="website" />
<meta property="og:image" content="https://schteppe.github.io/gpu-physics.js/gpu-physics.jpg" />
<style>
body {
color: #000000;
font-family: "Lucida Grande", sans-serif;
font-size: 12px;
font-weight: normal;
text-align: center;
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
bottom: 0px;
width: 100%;
padding: 5px;
}
a {
color: #000000;
font-weight: bold;
text-decoration: none;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info">
<b>gpu physics</b> by <a href="http://twitter.com/schteppe">@schteppe</a> | <a href="https://www.youtube.com/watch?v=PfCZEQxTvqA">video</a> | <a href="https://github.com/schteppe/gpu-physics.js">github</a>
</div>
<script id="sharedShaderCode" type="x-shader/x-fragment">
// Convert an index to an UV-coordinate
vec2 indexToUV(float index, vec2 res){
vec2 uv = vec2(mod(index/res.x,1.0), floor( index/res.y ) / res.x);
return uv;
}
// Rotate a vector by a quaternion
vec3 vec3_applyQuat(vec3 v, vec4 q){
float ix = q.w * v.x + q.y * v.z - q.z * v.y;
float iy = q.w * v.y + q.z * v.x - q.x * v.z;
float iz = q.w * v.z + q.x * v.y - q.y * v.x;
float iw = -q.x * v.x - q.y * v.y - q.z * v.z;
return vec3(
ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y,
iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z,
iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x
);
}
</script>
<!--
Render body instances vertex shader
Copy of THREE.ShaderLib.phong + modifications to render instances
-->
<script id="renderBodiesVertex" type="x-shader/x-vertex">
uniform sampler2D bodyPosTex;
uniform sampler2D bodyQuatTex;
attribute float bodyIndex;
#define PHONG
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
#include <uv_pars_vertex>
#include <uv2_pars_vertex>
#include <displacementmap_pars_vertex>
#include <envmap_pars_vertex>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <uv_vertex>
#include <uv2_vertex>
#include <color_vertex>
vec2 bodyUV = indexToUV(bodyIndex,bodyTextureResolution);
#ifdef USE_COLOR
vColor = vec3((floor(bodyUV*3.0)+1.0)/3.0,0);
#endif
#include <beginnormal_vertex>
#include <morphnormal_vertex>
#include <skinbase_vertex>
#include <skinnormal_vertex>
vec3 bodyPos = texture2D(bodyPosTex,bodyUV).xyz;
vec4 bodyQuat = texture2D(bodyQuatTex,bodyUV).xyzw;
objectNormal.xyz = vec3_applyQuat(objectNormal.xyz, bodyQuat);
#include <defaultnormal_vertex>
#ifndef FLAT_SHADED
vNormal = normalize( transformedNormal );
#endif
#include <begin_vertex>
transformed.xyz = vec3_applyQuat(transformed.xyz, bodyQuat);
transformed.xyz += bodyPos;
#include <displacementmap_vertex>
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
vViewPosition = - mvPosition.xyz;
#include <worldpos_vertex>
#include <envmap_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}
</script>
<!--
Render body instances vertex shader
Copy of THREE.ShaderLib.phong + modifications to render body instances with correct transform
-->
<script id="renderParticlesVertex" type="x-shader/x-vertex">
uniform sampler2D particleWorldPosTex;
uniform sampler2D quatTex;
attribute float particleIndex;
#define PHONG
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
#include <uv_pars_vertex>
#include <uv2_pars_vertex>
#include <displacementmap_pars_vertex>
#include <envmap_pars_vertex>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <uv_vertex>
#include <uv2_vertex>
#include <color_vertex>
vec2 particleUV = indexToUV(particleIndex,resolution);
#ifdef USE_COLOR
vColor = vec3((floor(particleUV*3.0)+1.0)/3.0,0);
#endif
#include <beginnormal_vertex>
#include <morphnormal_vertex>
#include <skinbase_vertex>
#include <skinnormal_vertex>
vec4 particlePosAndBodyId = texture2D(particleWorldPosTex,particleUV);
vec2 bodyUV = indexToUV(particlePosAndBodyId.w,bodyTextureResolution);
vec4 bodyQuat = texture2D(quatTex,bodyUV).xyzw;
objectNormal.xyz = vec3_applyQuat(objectNormal.xyz, bodyQuat);
#include <defaultnormal_vertex>
#ifndef FLAT_SHADED
vNormal = normalize( transformedNormal );
#endif
#include <begin_vertex>
vec3 particlePos = particlePosAndBodyId.xyz;
transformed.xyz = vec3_applyQuat(transformed.xyz, bodyQuat);
transformed.xyz += particlePos;
#include <displacementmap_vertex>
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
vViewPosition = - mvPosition.xyz;
#include <worldpos_vertex>
#include <envmap_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}
</script>
<!--
Render depth. This is for rendering shadows.
-->
<script id="renderBodiesDepth" type="x-shader/x-vertex">
uniform sampler2D bodyPosTex;
uniform sampler2D bodyQuatTex;
attribute float bodyIndex;
#include <common>
#include <uv_pars_vertex>
#include <displacementmap_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <uv_vertex>
#include <skinbase_vertex>
#include <begin_vertex>
vec2 bodyUV = indexToUV(bodyIndex,bodyTextureResolution);
vec3 bodyPos = texture2D(bodyPosTex,bodyUV).xyz;
vec4 bodyQuat = texture2D(bodyQuatTex,bodyUV).xyzw;
transformed.xyz = vec3_applyQuat(transformed.xyz, bodyQuat);
transformed.xyz += bodyPos;
#include <displacementmap_vertex>
#include <morphtarget_vertex>
#include <skinning_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
}
</script>
<script src="lib/three.min.js"></script>
<script src="lib/stats.min.js"></script>
<script src="lib/dat.gui.min.js"></script>
<script src="lib/three.orbitcontrols.js"></script>
<script src="lib/three.transformcontrols.js"></script>
<script src="build/gp.js"></script>
<script src="main.js"></script>
</body>
</html>