-
Notifications
You must be signed in to change notification settings - Fork 4
/
Shaders-FractalTorus.html
192 lines (158 loc) · 5.38 KB
/
Shaders-FractalTorus.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
<html>
<head>
<title>LSD Shader</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="js/three.js"></script>
<script src="js/OrbitControls.js"></script>
<script id="vert" type="shader">
uniform float time;
varying vec3 norm;
varying vec2 vUv;
varying vec3 pos;
void main() {
norm = normal;
vUv = uv;
pos = position;
vec3 p1 = position + ( 0.2 * normal * (sin(time * 13.0) + 0.1) );
vec3 p2 = vec3( p1.x , p1.y + 0.1 * sin(time * 13.0) * sin(p1.x * 9.0 + time * 50.0) ,
p1.z + 0.08 * sin( -time * 21.0) * sin(p1.y * 9.0 + time * 90.0) );
gl_Position = projectionMatrix * modelViewMatrix * vec4(p2, 1.0);
}
</script>
<script id="frag" type="shader">
uniform float time;
uniform int width;
uniform int height;
uniform sampler2D tex;
varying vec3 norm;
varying vec2 vUv;
varying vec3 pos;
const int maxIterations=6;//a nice value for fullscreen is 8
float circleSize=1.0/(3.0*pow(2.0,float(maxIterations)));
//generic rotation formula
vec2 rot(vec2 uv,float a){
return vec2(uv.x*cos(a)-uv.y*sin(a),uv.y*cos(a)+uv.x*sin(a));
}
void main() {
/* A simple fractal from
* https://www.shadertoy.com/view/Mss3Wf
*/
//normalize stuff
vec2 uv = vec2(width, height);
uv=-.5*(pos.xy * 600.0)/uv.x;
//global rotation and zoom
uv=rot(uv,time);
uv*=sin(time)*0.5+1.5;
//mirror, rotate and scale 6 times...
float s=0.3;
for(int i=0;i<maxIterations;i++){
uv=abs(uv)-s;
uv=rot(uv,time * 20.0);
s=s/2.1;
}
//draw a circle
vec3 pointLight = vec3( 3.0 * cos(20.0 * time), 2.5 * sin(20.0 * time), 3.0);
vec3 lightRay = normalize(pointLight - pos);
float lightness = pow( abs(dot( norm, lightRay )), 5.5 ) + 0.7;
float blip = 1.0 - pow( sin( gl_FragCoord.y / 5.0) + 0.9, 6.0) ;
vec3 tColor = texture2D( tex, vec2( vUv.x * sin( 1.8 * time ), vUv.y * cos ( 6.89 * time) ) ).rgb;
vec3 col = length(uv) > circleSize? vec3(0.0, 0.0, 0.0) : tColor *lightness;
gl_FragColor = vec4(col, 1.0);
}
</script>
<script>
// @authors torinmb, PWhiddy
var scene, camera, renderer, controls;
var group, shaderMat;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
init();
animate();
function init() {
scene = new THREE.Scene();
camera = camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 1000 );
renderer = new THREE.WebGLRenderer();
group = new THREE.Group();
var geo = new THREE.TorusGeometry( 1, 0.5, 50, 100);
geo.computeFaceNormals();
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.minDistance = 10;
controls.zoomSpeed = 0.5;
controls.rotateSpeed = 0.5;
var dim = 512;
var size = dim * dim;
var data = new Uint8Array( size * 3);
for (var i = 0; i < size; i++) {
data[ i * 3 + 0] = ( Math.sin( i / 2000 ) * 255 );
data[ i * 3 + 1] = Math.cos(i % 50) * 255;
data[ i * 3 + 2] = (Math.cos(i / 10000) * 1000) % 255;
}
map = new THREE.DataTexture( data, dim, dim, THREE.RGBFormat );
map.needsUpdate = true;
shaderMat = new THREE.ShaderMaterial( {
uniforms: { time: { type: "f", value: 2.22 },
width: { type: "i", value: 3000 },
height: { type: "i", value: 3000 },
tex: { type: "t", value: map }
},
side: THREE.DoubleSide,
vertexShader: document.getElementById( 'vert' ).textContent,
fragmentShader: document.getElementById( 'frag' ).textContent
} );
var distance = 1000;
for ( var i = 0; i < 250; i ++ ) {
var mesh = new THREE.Mesh( geo, shaderMat );
mesh.position.x = Math.random() * distance - distance/2;
mesh.position.y = Math.random() * distance - distance/2;
mesh.position.z = Math.random() * distance - distance/2;
mesh.rotation.x = Math.random() * 2 * Math.PI;
mesh.rotation.y = Math.random() * 2 * Math.PI;
mesh.scale.set( 70, 70, 70 );
mesh.matrixAutoUpdate = false;
mesh.updateMatrix();
group.add( mesh );
}
}
var light = new THREE.AmbientLight( {color: 0xffffff} );
var light2 = new THREE.PointLight( {color: 0x004455});
scene.add(light, light2, group);
var renerer = new THREE.WebGLRenderer({antialias: true});
renderer.sortObjects = false;
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement);
camera.position.z = 10;
function onDocumentMouseMove(event) {
mouseX = ( event.clientX - windowHalfX ) * 10;
mouseY = ( event.clientY - windowHalfY ) * 10;
//console.log(mouseX + " " + mouseY);
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
controls.update();
var time = Date.now() * 0.001;
var rx = Math.sin( time * 0.7 ) * 0.5,
ry = Math.sin( time * 0.3 ) * 0.5,
rz = Math.sin( time * 0.2 ) * 0.5;
camera.lookAt( scene.position );
group.rotation.x = rx;
group.rotation.y = ry;
group.rotation.z = rz;
shaderMat.uniforms.time.value += 0.0005;
renderer.render( scene, camera );
}
</script>
</body>
</html>