-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
375 lines (301 loc) · 12.2 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
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Metaballs</title>
<script id="simple-vert-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec4 vColor;
uniform mat4 modelView;
uniform mat4 projection;
uniform mat4 rotation;
uniform mat4 translate;
uniform mat4 scale;
varying vec4 fColor;
void
main()
{
gl_Position = projection * modelView * translate * rotation * scale *vPosition;
fColor = vColor;
}
</script>
<script id="simple-frag-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 fColor;
void
main()
{
gl_FragColor = fColor;
}
</script>
<script id="metaball-vert-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec4 vColor;
uniform mat4 modelView;
uniform mat4 projection;
uniform mat4 rotation;
uniform mat4 translate;
uniform mat4 scale;
varying vec4 fColor;
varying vec4 fPosition;
void
main()
{
fPosition = translate * rotation * scale * vPosition;
gl_Position = projection * modelView * fPosition;
fColor = vColor;
}
</script>
<script id="metaball-frag-shader" type="x-shader/x-fragment">
precision mediump float;
const int numOfBalls = 9;
const float numOfRaySteps = 110.0;
uniform vec4 camEye;
uniform vec4 lightPosition;
uniform bool blinnSpecular;
uniform bool specularOn;
uniform float ambientIntensity;
uniform float diffuseIntensity;
uniform float specularIntensity;
uniform float shine;
uniform vec4 ballsPos[numOfBalls];
uniform vec4 ballsColors[numOfBalls];
uniform float ballsRadius[numOfBalls];
varying vec4 fPosition;
vec4 nearestPoint = vec4(0.0, 0.0, 0.0, 1.0);
vec4 farthestPoint = vec4(0.0, 0.0, 0.0, 1.0);
vec4 drawColor = vec4(0.0, 0.0, 0.0, 0.0);
int hittedBalls[numOfBalls];
int hasHitted = 0;
void calculatePoints(vec4 startPos, vec4 rayDir)
{
vec4 ballDir, ballPosInRay;
float ballRayDot, ballPosDist;
vec4 enterPoint, exitPoint;
float A, B, C; //Sides in the pythagoras theorem
for(int i = 0; i < numOfBalls; i++)
{
ballDir = ballsPos[i] - startPos;
ballRayDot = dot(rayDir, ballDir);
ballPosInRay = ballRayDot * rayDir + startPos;
ballPosDist = abs(distance(ballPosInRay, ballsPos[i]));
if(ballPosDist < ballsRadius[i])
{
A = distance(ballPosInRay, ballsPos[i]);
B = ballsRadius[i];
C = sqrt(A*A + B*B);
enterPoint = startPos + rayDir*(ballRayDot - C);
exitPoint = startPos + rayDir*(ballRayDot + C);
if(distance(enterPoint, startPos) < distance(nearestPoint, startPos) ||
hasHitted == 0)
{
nearestPoint = enterPoint;
}
if(distance(exitPoint, startPos) > distance(farthestPoint, startPos)||
hasHitted == 0)
{
farthestPoint = exitPoint;
}
hittedBalls[i] = 1;
hasHitted = 1;
}
else if(ballPosDist == ballsRadius[i])
{
if(distance(ballPosInRay, startPos) < distance(nearestPoint, startPos) ||
hasHitted == 0)
{
nearestPoint = ballPosInRay;
}
if(distance(ballPosInRay, startPos) > distance(farthestPoint, startPos)||
hasHitted == 0)
{
farthestPoint = ballPosInRay;
}
hittedBalls[i] = 1;
hasHitted = 1;
}
else
{
hittedBalls[i] = 0;
}
}
}
// #define M_PI 3.1415926535897932384626433832795;
float metaballFormula(float d)
{
// return d * d * d * (d * (d * 6.0 - 15.0) + 10.0);
// return d*d*d*d - d*d + 0.25;
// return (cos(d*3.1416*4.0 + 3.1416) + 1.0)/2.0;
return (1.0 - d*d)*(1.0 - d*d);
}
// float threshold = 0.12;
float threshold = 0.5;
void main()
{
vec4 startPos = fPosition;
vec4 rayDir = normalize(fPosition - camEye);
vec3 normal = vec3(0.0, 0.0, 0.0);
vec3 normalsTotal = vec3(0.0, 0.0, 0.0);
vec3 weightedNormal = vec3(0.0, 0.0, 0.0);
vec3 blendedNormal = vec3(0.0, 0.0, 0.0);
float balls_radius = 1.0;
calculatePoints(startPos, rayDir);
if(hasHitted == 1)
{
float rayLength = distance(nearestPoint, farthestPoint);
float rayStep = rayLength/numOfRaySteps;
vec4 actualPoint = nearestPoint;
float chargeSum = 0.0;
int shouldRender = 0;
for(float i = 0.0; i <= numOfRaySteps; i += 1.0)
{
actualPoint = actualPoint + rayDir*rayStep;
for(int j = 0; j < numOfBalls; j += 1)
{
if(hittedBalls[j] == 1)
{
// float dist = distance(actualPoint, ballsPos[j])/balls_radius;
float dist = distance(actualPoint, ballsPos[j])/ballsRadius[j];
normal = normalize(actualPoint.xyz - ballsPos[j].xyz);
// Check if the current point is inside the ball
if(dist < 1.0)
{
// float charge = metaballFormula(1.0 - dist);
float charge = metaballFormula(dist);
weightedNormal = charge * normal;
normalsTotal += weightedNormal;
chargeSum += charge;
drawColor += charge*ballsColors[j];
}
}
}
if(chargeSum >= threshold)
{
shouldRender = 1;
blendedNormal = normalsTotal / chargeSum;
blendedNormal = normalize(blendedNormal);
break;
}
else
{
chargeSum = 0.0;
normalsTotal = vec3(0.0, 0.0, 0.0);
drawColor = vec4(0.0, 0.0, 0.0, 0.0);
}
}
if(shouldRender == 1)
{
vec3 color = vec3(0.0, 0.0, 0.0);
vec3 dirToLight = lightPosition.xyz - actualPoint.xyz;
vec3 dirToCamera = camEye.xyz - actualPoint.xyz;
vec3 dirToLightNorm = normalize(dirToLight);
vec3 dirToCameraNorm = normalize(dirToCamera);
// Last component is the intensity of the color
vec3 ambientLightColor = vec3(1.0, 1.0, 0.8);
vec3 diffuseLightColor = vec3(1.0, 1.0, 0.8);
// float diffuseIntensity = 1.0;
vec3 specularLightColor = vec3(1.0, 1.0, 0.8);
// float specularIntensity = 3.0;
//float shine = 125.0;
vec3 ambient = vec3(0.0, 0.0, 0.0);
vec3 diffuse = vec3(0.0, 0.0, 0.0);
vec3 specular = vec3(0.0, 0.0, 0.0);
ambient = ambientLightColor * drawColor.rgb * ambientIntensity;
float lambertian = max( dot( dirToLightNorm, blendedNormal ), 0.0 );
if(lambertian >= 0.0)
{
diffuse = diffuseLightColor * drawColor.rgb * lambertian * diffuseIntensity;
if(specularOn)
{
if(blinnSpecular)
{
// Blinn
vec3 blinnHalfway = vec3(0.0, 0.0, 0.0);
blinnHalfway = normalize(dirToLight + dirToCamera);
float blinn = pow( max( dot(blendedNormal, blinnHalfway), 0.0 ), shine );
specular = specularLightColor * blinn * specularIntensity;
}
else
{
// Phong
vec3 phongReflected = vec3(0.0, 0.0, 0.0);
phongReflected = -reflect(dirToLightNorm, blendedNormal);
float phong = pow( max( dot(phongReflected, dirToCameraNorm), 0.0 ), shine );
specular = specularLightColor * phong * specularIntensity;
}
}
}
color = ambient + diffuse + specular;
gl_FragColor = vec4(color, 1.0);
}
else
{
discard;
}
}
else
{
discard;
}
}
</script>
<script type="text/javascript" src="./js/webgl-utils.js"></script>
<script type="text/javascript" src="./js/initshaders.js"></script>
<script type="text/javascript" src="./js/mv.js"></script>
<script type="text/javascript" src="./js/smallextlib_mv.js"></script>
<script type="text/javascript" src="./js/models.js"></script>
<script type="text/javascript" src="./js/renderobject.js"></script>
<script type="text/javascript" src="./js/main_metaballs.js"></script>
<script type="text/javascript" src="./js/controls.js"></script>
<script type="text/javascript" src="./js/jscolor.js"></script>
</head>
<body>
<div id="centerStripe"></div>
<div id="page">
<h1 id="title">METABALLS</h1>
<canvas id="glcanvas" width="640" height="400">
It seems that your browser doesn't support the HTML5 canvas element
</canvas>
<div id="fps"></div>
<div id="controls" align="center">
<!-- Specular Controls -->
<div id="specular-controls" class="controls-opt">
<span class="opt-title">Specular</span><br>
<input type="radio" name="specular" id="phong-spec" value="phong" checked> Phong Specular
<input type="radio" name="specular" id="blinn-spec" value="blinn"> Blinn Specular
<input type="radio" name="specular" id="off-spec" value="off"> Specular OFF<br>
<!-- <span>Shininess </span>
<input id="shine-slider" type="range" max="125.0" min="5.0" step="25.00" value="125.0"> -->
</div>
<!-- Animation Controls -->
<div id="animation-controls" class="controls-opt">
<span class="opt-title">Animation</span><br>
<input type="checkbox" id="anim-toggle" checked>Toggle</input><br>
<!-- <span>Speed </span>
<input id="anim-speed-slider" type="range" max="0.02" min="0.002" step="0.001" value="0.01"> -->
</div>
<!-- Camera Controls -->
<div id="camera-controls" class="controls-opt">
<span class="opt-title">Camera</span><br>
<input type="checkbox" id="cam-rotation">Camera Rotation</input>
</div>
<!-- Light Controls -->
<div id="light-controls" class="controls-opt">
<span class="opt-title">Light</span><br>
<input type="checkbox" id="light-rotation">Light Rotation</input><br>
<span>Ambient Light Intensity </span>
<input id="amb-light-slider" type="range" max="1.0" min="0.0" step="0.05" value="0.1"><br>
<span>Diffuse Light Intensity </span>
<input id="diff-light-slider" type="range" max="2.0" min="0.0" step="0.05" value="1.0"><br>
<span>Specular Light Intensity </span>
<input id="spec-light-slider" type="range" max="3.0" min="0.0" step="0.05" value="3.0">
</div>
<!-- Balls -->
<div id="balls-controls" class="controls-opt">
<span class="opt-title">Balls</span>
</div>
</div>
</div>
</center>
</body></html>