-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader-toy.html
206 lines (201 loc) · 6.18 KB
/
shader-toy.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
<html>
<head>
<script src='https://pagecdn.io/lib/three/110/three.min.js'></script>
<script id="frag1" type="x-shader/x-fragment">
uniform float iGlobalTime;
uniform vec2 iResolution;
uniform vec4 iMouse;
uniform sampler2D iChannel0;
varying vec2 fragCoord;
varying vec2 vUv;
vec2 cmul( vec2 a, vec2 b ) { return vec2( a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x ); }
vec2 csqr( vec2 a ) { return vec2( a.x*a.x - a.y*a.y, 2.*a.x*a.y ); }
vec3 dmul( vec3 a, vec3 b ) {
float r = length(a);
b.xy=cmul(normalize(a.xy), b.xy);
b.yz=cmul(normalize(a.yz), b.yz);
return r*b;
}
vec3 pow4( vec3 z){
z=dmul(z,z);return dmul(z,z);
}
vec3 pow3( vec3 z){
float r2 = dot(z,z);
vec2 a = z.xy;a=csqr(a)/dot( a,a);
vec2 b = z.yz;b=csqr(b)/dot( b,b);
vec2 c = z.xz;c=csqr(c)/dot( c,c);
z.xy = cmul(a,z.xy);
z.yz = cmul(b,z.yz);
z.xz = cmul(c,z.xz);
return r2*z;
}
mat2 rot(float a) {
return mat2(cos(a),sin(a),-sin(a),cos(a));
}
float zoom=4.;
float field(in vec3 p) {
float res = 0.;
vec3 c = p;
for (int i = 0; i < 10; ++i) {
p = abs(p) / dot(p,p) -1.;
p = dmul(p,p)+.7;
res += exp(-6. * abs(dot(p,c)-.15));
}
return max(0., res/3.);
}
vec3 raycast( in vec3 ro, vec3 rd )
{
float t = 6.0;
float dt = .05;
vec3 col= vec3(0.);
for( int i=0; i<64; i++ )
{
float c = field(ro+t*rd);
t+=dt/(.35+c*c);
c = max(5.0 * c - .9, 0.0);
col = .97*col+ .08*vec3(0.5*c*c*c, .6*c*c, c);
}
return col;
}
void main()
{
float time = iGlobalTime;
vec2 q = fragCoord.xy / iResolution.xy;
vec2 p = -1.0 + 2.0 * q;
p.x *= iResolution.x/iResolution.y;
vec2 m = vec2(0.);
if( iMouse.z>0.0 )m = iMouse.xy/iResolution.xy*3.14;
m-=.5;
vec3 ro = zoom*vec3(1.);
ro.yz*=rot(m.y);
ro.xz*=rot(m.x+ 0.1*time);
vec3 ta = vec3( 0.0 , 0.0, 0.0 );
vec3 ww = normalize( ta - ro );
vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
vec3 vv = normalize( cross(uu,ww));
vec3 rd = normalize( p.x*uu + p.y*vv + 4.0*ww );
vec3 col = raycast(ro,rd);
col = .5 *(log(1.+col));
col = clamp(col,0.,1.);
gl_FragColor = vec4( sqrt(col), 1.0 );
}
</script>
<script id="frag2" type="x-shader/x-fragment">
uniform float iGlobalTime;
uniform vec2 iResolution;
uniform vec4 iMouse;
uniform sampler2D iChannel0;
varying vec2 fragCoord;
varying vec2 vUv;
void main()
{
float k=0.;
vec3 d = vec3(fragCoord,1.0)/1.0-.5, o = d, c=k*d, p;
for( int i=0; i<99; i++ ){
p = o+sin(iGlobalTime*.1);
for (int j = 0; j < 10; j++)
p = abs(p.zyx-.4) -.7,k += exp(-6. * abs(dot(p,o)));
k/=3.;
o += d *.05*k;
c = .97*c + .1*k*vec3(k*k,k,1);
}
c = .4 *log(1.+c);
gl_FragColor.rgb = c;
}
</script>
<script id="general" type="x-shader/x-vertex">
attribute vec3 in_Position;
varying vec2 fragCoord;
varying vec2 vUv;
void main()
{
vUv = uv;
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0 );
gl_Position = projectionMatrix * mvPosition;
fragCoord = position.xy;
}
</script>
</head>
<body>
<script>
var myInstance = {
scene: new THREE.Scene(),
camera: new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000),
gameObjects: [],
gameObjectsDict: {},
};
(function(myInstance) {
myInstance.camera.position.x = 0;
myInstance.camera.position.y = 10;
myInstance.camera.position.z = 5;
var webgl = new THREE.WebGLRenderer();
webgl.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(webgl.domElement);
function onWindowResize() {
myInstance.camera.aspect = window.innerWidth / window.innerHeight;
myInstance.camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize, false);
renderer = webgl
var light = new THREE.HemisphereLight(0xeeeeee, 0x888888, 1);
light.position.set(0, 20, 0);
myInstance.scene.add(light);
counter = 0;
function render() {
myInstance.gameObjects.forEach(function(item) {
item.update(counter);
});
requestAnimationFrame(render);
renderer.render(myInstance.scene, myInstance.camera);
}
render();
})(myInstance);
(function() {
var gameObject = function(name, x, y, z, col, rx, ry) {
this.name = name
this.geometry = new THREE.BoxGeometry(3, 3, 3);
uniforms = {
iGlobalTime: {
type: "f",
value: 1.0
},
iResolution: {
type: "v2",
value: new THREE.Vector2()
},
};
uniforms.iResolution.value.x = 1; // window.innerWidth;
uniforms.iResolution.value.y = 1; // window.innerHeight;
this.material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById('general').textContent,
fragmentShader: document.getElementById('frag1').textContent
});
this.obj = new THREE.Mesh(this.geometry, this.material);
this.obj.startTime = Date.now();
this.obj.uniforms = uniforms;
this.rx = rx
this.ry = ry
this.obj.name = name
myInstance.scene.add(this.obj);
this.obj.position.x = x;
this.obj.position.y = y;
this.obj.position.z = z;
this.vel = [0, 0, 0]
}
gameObject.prototype.update = function() {
this.obj.rotation.x += this.rx
this.obj.rotation.y += this.ry
this.obj.position.x += this.vel[0]
this.obj.position.y += this.vel[1]
this.obj.position.z += this.vel[2]
var elapsedMilliseconds = Date.now() - this.obj.startTime;
var elapsedSeconds = elapsedMilliseconds / 1000.;
this.obj.uniforms.iGlobalTime.value = elapsedSeconds;
}
myInstance.gameObjectsDict['cube3'] = myInstance.gameObjects.push(new gameObject("cube3", 0, 10, -3, 0x0000ff, 0, 0.0001))
})();
</script>
</body>
</html>