123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- precision mediump float;
- attribute vec4 a_position;
- attribute vec2 a_texCoord;
- attribute vec3 a_triangleCoord;
- uniform mat4 u_projectionMatrix;
- uniform vec2 u_meshSize;
- uniform vec2 u_textureSize;
- uniform mat4 transform;
- uniform float amount;
- uniform float randomness;
- uniform vec3 flipAxis;
- varying float v_depth;
- varying vec2 v_uv;
- const float PI2 = 1.5707963267948966;
- mat4 perspectiveMatrix(float p)
- {
- float perspective = - 1.0 / p;
- return mat4(
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 1.0, perspective,
- 0.0, 0.0, 0.0, 1.0
- );
- }
- vec3 rotateVectorByQuaternion(vec3 v, vec4 q)
- {
- vec3 dest = vec3(0.0);
- float x = v.x, y = v.y, z = v.z;
- float qx = q.x, qy = q.y, qz = q.z, qw = q.w;
-
- float ix = qw * x + qy * z - qz * y,
- iy = qw * y + qz * x - qx * z,
- iz = qw * z + qx * y - qy * x,
- iw = -qx * x - qy * y - qz * z;
-
- dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
- dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
- dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
- return dest;
- }
- vec4 axisAngleToQuaternion(vec3 axis, float angle)
- {
- vec4 dest = vec4(0.0);
- float halfAngle = angle / 2.0;
- float s = sin(halfAngle);
- dest.x = axis.x * s;
- dest.y = axis.y * s;
- dest.z = axis.z * s;
- dest.w = cos(halfAngle);
- return dest;
- }
- float random(vec2 scale)
- {
-
- return fract(sin(dot(vec2(a_triangleCoord.x, a_triangleCoord.y), scale)) * 4000.0);
- }
- void main()
- {
-
-
- vec2 u_meshSize = u_meshSize.yx;
- vec4 pos = a_position;
- float aspect = u_textureSize.x / u_textureSize.y;
- float cx = a_triangleCoord.x / u_meshSize.y - 0.5 + 0.5 / u_meshSize.y;
- float cy = a_triangleCoord.y / u_meshSize.x - 0.5 + 0.5 / u_meshSize.x;
- vec3 centroid = vec3(cx, cy, 0.0);
- float r = random(vec2(10.0, 80.0));
- float rr = mix(0.0, PI2, amount * (1.0 + randomness * r));
- vec4 rotation = vec4(flipAxis, rr);
- vec4 qRotation = axisAngleToQuaternion(normalize(rotation.xyz), rotation.w);
- vec3 newPosition = rotateVectorByQuaternion((pos.xyz - centroid)* vec3(aspect, 1., 1.0), qRotation) * vec3(1.0 / aspect, 1.0, 1.0) + centroid;
- pos.xyz = newPosition;
- gl_Position = u_projectionMatrix * transform * pos;
-
- v_depth = abs(rr)/ PI2;
- v_uv = a_texCoord;
- }
|