jellyVertex.glsl
1uniform float uTime;
2uniform int uOctaves;
3uniform float uTimeFrequency;
4uniform float uAmplitude;
5uniform float uFrequency;
6
7attribute vec4 tangent;
8
9varying vec2 vUv;
10varying float vDisplacement;
11varying vec3 vPosition;
12varying vec3 varyNormal;
13
14// Permutation function for noise
15vec3 permute(vec3 x) {
16 return mod(((x*34.0)+1.0)*x, 289.0);
17}
18
19// Noise function
20float noise(vec3 p) {
21 vec3 i = floor(p);
22 vec3 f = fract(p);
23 vec3 u = f*f*(3.0-2.0*f);
24
25 return mix(mix(mix( dot(permute(i + vec3(0,0,0)), f - vec3(0,0,0)),
26 dot(permute(i + vec3(1,0,0)), f - vec3(1,0,0)), u.x),
27 mix( dot(permute(i + vec3(0,1,0)), f - vec3(0,1,0)),
28 dot(permute(i + vec3(1,1,0)), f - vec3(1,1,0)), u.x), u.y),
29 mix(mix( dot(permute(i + vec3(0,0,1)), f - vec3(0,0,1)),
30 dot(permute(i + vec3(1,0,1)), f - vec3(1,0,1)), u.x),
31 mix( dot(permute(i + vec3(0,1,1)), f - vec3(0,1,1)),
32 dot(permute(i + vec3(1,1,1)), f - vec3(1,1,1)), u.x), u.y), u.z);
33}
34
35float fractalNoise(vec3 p, int octaves) {
36 float noiseValue = 0.0;
37 float amplitude = uAmplitude;
38 float frequency = 1.0;
39 for (int i = 0; i < octaves; i++) {
40 noiseValue += amplitude * noise(p * frequency);
41 amplitude *= 0.5;
42 frequency *= 2.0;
43 }
44 float maxAmplitude = (1.0 - pow(0.5, float(octaves))) * uAmplitude;
45 return noiseValue / maxAmplitude;
46}
47
48
49
50float getDisplacement(vec3 p) {
51 return abs(fractalNoise(p * uFrequency + vec3(uTime * uTimeFrequency), uOctaves));
52}
53
54float getDisplacementEffect(float noiseValue) {
55 return sin(noiseValue) * cos(noiseValue);
56}
57
58
59void main() {
60 vec3 biTangent = cross(normal, tangent.xyz);
61 float shift = 0.01;
62 vec3 positionA = csm_Position + tangent.xyz * shift;
63 vec3 positionB = csm_Position + biTangent * shift;
64
65 vUv = uv;
66 varyNormal = normalize(normalMatrix * normal);
67 vPosition = position;
68 float displacement = getDisplacementEffect(getDisplacement(vPosition));
69 float displacementA = getDisplacementEffect(getDisplacement(positionA));
70 float displacementB = getDisplacementEffect( getDisplacement(positionB));
71 csm_Position += normal * displacement;
72 positionA += normal * displacementA;
73 positionB += normal * displacementB;
74
75 vec3 toA = normalize(positionA - csm_Position);
76 vec3 toB = normalize(positionB - csm_Position);
77 csm_Normal = normalize(cross(toA, toB));
78 vDisplacement = displacement;
79}
jellyFragment.glsl
1varying vec2 vUv;
2varying float vDisplacement;
3
4uniform vec3 uColorA;
5uniform vec3 uColorB;
6uniform float uOpacity;
7uniform float uRoughness;
8uniform float uMetalness;
9
10void main() {
11 float colorMix = smoothstep(-1.0, 1.0, vDisplacement);
12 csm_DiffuseColor.rgb = mix(uColorA,uColorB, colorMix);
13 csm_DiffuseColor.a = uOpacity;
14
15 csm_Roughness = uRoughness;
16 csm_Metalness = uMetalness;
17}