-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
204 lines (154 loc) · 5.46 KB
/
index.html
File metadata and controls
204 lines (154 loc) · 5.46 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="container"></div>
<script id="vertexShader" type="x-shader/x-vertex">
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform float u_time;
uniform vec3 u_colors [3];
uniform sampler2D uTex;
// The Book of Shaders by Patricio Gonzalez Vivo & Jen Lowe
vec3 rgb2hsb( in vec3 c ){
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz),
vec4(c.gb, K.xy),
step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r),
vec4(c.r, p.yzx),
step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)),
d / (q.x + e),
q.x);
}
float idw(in vec2 _st) {
vec2 loc = vec2(0.3,0.3);
vec2 loc2 = vec2(0.7, 0.7);
vec3 pts;
float prop = 0.0;
float dist = 0.0;
float weigthed_prop = 0.0;
float weigthed_prop_sum = 0.0;
float weigth_sum = 0.0;
for (int i = 0; i < NB_POINTS; i++) {
float pix_size = 1.0 / float(NB_POINTS);
vec4 item = texture2D(uTex,vec2((0.5 + float(i)) * pix_size,0));
loc = vec2(item[0], item[1]);
//loc = vec2(float(i) / float(NB_POINTS), 0.5);
prop = float(i) / float(NB_POINTS);
prop = item[2];
dist = distance(_st, loc);
if (dist < 0.05) {
weigthed_prop_sum = weigthed_prop_sum + (prop / (dist * dist));
weigth_sum = weigth_sum + (1.0 / (dist * dist));
}
}
return (weigthed_prop_sum / weigth_sum);
}
vec3 prop2color(in float _prop) {
return vec3(0.0, 0.0, _prop);
}
vec3 three_color_gradient(in float _value, in vec3 _rgb_a, in vec3 _rgb_b, in vec3 _rgb_c) {
vec3 _hsb_a = rgb2hsb(_rgb_a);
vec3 _hsb_b = rgb2hsb(_rgb_b);
vec3 _hsb_c = rgb2hsb(_rgb_c);
if (_value < 0.5) {
return mix(_rgb_a, _rgb_b, 2.0 * _value );
} else {
return mix(_rgb_b, _rgb_c, 2.0 * (_value - 0.5));
}
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
float prop_min = 0.0;
float prop_max = 1.0;
float prop = idw(st);
vec3 color = three_color_gradient(smoothstep(prop_min, prop_max, prop), u_colors[0], u_colors[1], u_colors[2]);
gl_FragColor=vec4(color,1.0);
}
</script>
<script src="bundle.js"></script>
<script>
var container;
var camera, scene, renderer;
var uniforms;
var x, y, z;
init();
animate();
function init () {
container = document.getElementById('container');
var particles = 4000;
camera = new THREE.Camera();
camera.position.z = 1;
var points = new Float32Array( particles * 3 );
for ( var i = 0, i3 = 0; i < particles; i ++, i3 += 3 ) {
x = Math.random();
y = Math.random();
z = x * 0.8 + y * 0.2 + (Math.random() - 0.5) * 0.3;
z = Math.min(Math.max(z, 0.0), 1.0);
points[ i3 + 0 ] = (x);
points[ i3 + 1 ] = ( y) ;
points[ i3 + 2 ] = ( z);
}
//console.log(points);
scene = new THREE.Scene();
var geometry = new THREE.PlaneBufferGeometry(2, 2);
var texture = new THREE.DataTexture(points , particles, 1, THREE.RGBFormat, THREE.FloatType, THREE.UVMapping);
texture.needsUpdate = true;
uniforms = {
u_time: {type: 'f', value: 1.0},
u_resolution: {type: 'v2', value: new THREE.Vector2()},
u_mouse: {type: 'v2', value: new THREE.Vector2()},
u_colors: {
type: 'v3v', value: [ new THREE.Color(0x9e0142), new THREE.Color(0xffffbf), new THREE.Color(0x5e4fa2)]
},
uTex : { type: "t", value: texture } // regular texture
}
var material = new THREE.ShaderMaterial({
defines: {
NB_POINTS: particles
},
uniforms: uniforms,
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent
});
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
container.appendChild(renderer.domElement);
onWindowResize();
window.addEventListener('resize', onWindowResize, false);
document.onmousemove = function (e) {
uniforms.u_mouse.value.x = e.pageX;
uniforms.u_mouse.value.y = e.pageY;
}
}
function onWindowResize (event) {
renderer.setSize(window.innerWidth, window.innerHeight);
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}
function animate () {
setTimeout( function() {
requestAnimationFrame( animate );
}, 1000 / 1.0 );
render();
}
function render () {
uniforms.u_time.value += 0.05;
renderer.render(scene, camera);
}
</script>
</body>
</html>