-
Notifications
You must be signed in to change notification settings - Fork 3
/
noise.sphere.html
165 lines (145 loc) · 5.91 KB
/
noise.sphere.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
<script src="../js/gl_utils.js"></script>
<body bgcolor=black>
<canvas id='canvas1' width='800' height='800'>
</canvas>
</body>
<script id="vs" type="x-shader/x-vertex">
attribute vec3 aPosition;
varying vec3 vPosition;
void main() {
gl_Position = vec4(aPosition, 1.0);
vPosition = aPosition;
}
</script>
<script id="fs" type="x-shader/x-fragment">
precision mediump float;
uniform float uTime;
varying vec3 vPosition;
uniform vec3 uCursor;
// Noise
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }
vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
vec3 fade(vec3 t) { return t*t*t*(t*(t*6.0-15.0)+10.0); }
float noise(vec3 P) {
vec3 i0 = mod289(floor(P)), i1 = mod289(i0 + vec3(1.0));
vec3 f0 = fract(P), f1 = f0 - vec3(1.0), f = fade(f0);
vec4 ix = vec4(i0.x, i1.x, i0.x, i1.x), iy = vec4(i0.yy, i1.yy);
vec4 iz0 = i0.zzzz, iz1 = i1.zzzz;
vec4 ixy = permute(permute(ix) + iy), ixy0 = permute(ixy + iz0), ixy1 = permute(ixy + iz1);
vec4 gx0 = ixy0 * (1.0 / 7.0), gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
vec4 gx1 = ixy1 * (1.0 / 7.0), gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
gx0 = fract(gx0); gx1 = fract(gx1);
vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0), sz0 = step(gz0, vec4(0.0));
vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1), sz1 = step(gz1, vec4(0.0));
gx0 -= sz0 * (step(0.0, gx0) - 0.5); gy0 -= sz0 * (step(0.0, gy0) - 0.5);
gx1 -= sz1 * (step(0.0, gx1) - 0.5); gy1 -= sz1 * (step(0.0, gy1) - 0.5);
vec3 g0 = vec3(gx0.x,gy0.x,gz0.x), g1 = vec3(gx0.y,gy0.y,gz0.y),
g2 = vec3(gx0.z,gy0.z,gz0.z), g3 = vec3(gx0.w,gy0.w,gz0.w),
g4 = vec3(gx1.x,gy1.x,gz1.x), g5 = vec3(gx1.y,gy1.y,gz1.y),
g6 = vec3(gx1.z,gy1.z,gz1.z), g7 = vec3(gx1.w,gy1.w,gz1.w);
vec4 norm0 = taylorInvSqrt(vec4(dot(g0,g0), dot(g2,g2), dot(g1,g1), dot(g3,g3)));
vec4 norm1 = taylorInvSqrt(vec4(dot(g4,g4), dot(g6,g6), dot(g5,g5), dot(g7,g7)));
g0 *= norm0.x; g2 *= norm0.y; g1 *= norm0.z; g3 *= norm0.w;
g4 *= norm1.x; g6 *= norm1.y; g5 *= norm1.z; g7 *= norm1.w;
vec4 nz = mix(vec4(dot(g0, vec3(f0.x, f0.y, f0.z)), dot(g1, vec3(f1.x, f0.y, f0.z)),
dot(g2, vec3(f0.x, f1.y, f0.z)), dot(g3, vec3(f1.x, f1.y, f0.z))),
vec4(dot(g4, vec3(f0.x, f0.y, f1.z)), dot(g5, vec3(f1.x, f0.y, f1.z)),
dot(g6, vec3(f0.x, f1.y, f1.z)), dot(g7, vec3(f1.x, f1.y, f1.z))), f.z);
return 2.2 * mix(mix(nz.x,nz.z,f.y), mix(nz.y,nz.w,f.y), f.x);
}
float noise(vec2 P) { return noise(vec3(P, 0.0)); }
float turbulence(vec3 P) {
float f = 0., s = 1.;
for (int i = 0 ; i < 9 ; i++) {
f += abs(noise(s * P)) / s;
s *= 2.;
P = vec3(.866 * P.x + .5 * P.z, P.y + 100., -.5 * P.x + .866 * P.z);
}
return f;
}
vec3 clouds(float x, float y) {
float L = turbulence(vec3(x, y, uTime * .1));
return vec3(noise(vec3(.5, .5, L) * 1.2));
}
struct Sphere {
vec3 center;
float radius;
vec3 color;
};
struct Ray {
vec3 origin;
vec3 direction;
};
struct Light {
vec3 position;
float ambience;
vec3 specular;
vec3 diffuse;
};
Sphere spheres[1];
Ray rays[1];
Light light[1];
void initialize() {
float x = vPosition.x;
float y = vPosition.y;
float z = vPosition.z;
float focalLength = 2.0;
vec3 color = vec3(0.0, 0.0, 0.0);
// Create spheres
spheres[0].center = vec3(0.0, 0.0, 0.0);
spheres[0].radius = 0.7;
spheres[0].color = vec3(1.0, .2, 0.3);
// Create ray
rays[0].origin = vec3(0.0, 0.0, 4.0);
rays[0].direction = normalize(vec3(x-0.5, 0.5-y, -focalLength));
// Create Light source
light[0].position = vec3(uCursor.x, -uCursor.y, 0.9);
light[0].ambience = 0.01;
}
vec3 checkIntersectSphere(Sphere sphere, Ray ray, Light light) {
vec3 sphereCenter = sphere.center;
vec3 colorOfSphere = sphere.color;
float radius = sphere.radius;
vec3 cameraSource = ray.origin;
vec3 cameraDirection = ray.direction;
vec3 lightSource = light.position;
float ambience = light.ambience;
vec3 color = vec3(.0, .0, .0);
vec3 sphereBaseColor = vec3(0.3, 0.3, 1.);
vec3 cloudEffect = clouds(vPosition.x, vPosition.y);
vec3 distanceFromCenter = (cameraSource - sphereCenter);
float B = 2.0 * dot(cameraDirection, distanceFromCenter);
float C = dot(distanceFromCenter, distanceFromCenter) - pow(radius, 2.0);
float delta = pow(B, 2.0) - 4.0 * C;
float t = 0.0;
if (delta > 0.0) {
float sqRoot = sqrt(delta);
float t1 = (-B + sqRoot) / 2.0;
float t2 = (-B - sqRoot) / 2.0;
t = min(t1, t2);
}
if (delta == 0.0) {
t = -B / 2.0;
}
if (t > 0.0) {
vec3 surfacePoint = cameraSource + (t * cameraDirection);
vec3 surfaceNormal = normalize(surfacePoint - sphereCenter);
sphereBaseColor = colorOfSphere * (ambience + ((1.0 - ambience) * max(0.0, dot(surfaceNormal, lightSource))));
color = vec3(cloudEffect * sphereBaseColor) + vec3(0.5, 0.2, 0.1);
}
return color;
}
void main() {
vec3 color;
float x = vPosition.x;
float y = vPosition.y;
initialize();
color = checkIntersectSphere(spheres[0], rays[0], light[0]);
gl_FragColor = vec4(color, 1.);
}
</script>
<script>
start_gl("canvas1", getStringFromDOMElement('vs'), getStringFromDOMElement('fs'));
</script>