/
Animation.js
116 lines (107 loc) · 3.16 KB
/
Animation.js
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
import * as THREE from 'three'
const noise = `
// 2D Random
float random (in vec2 st) {
return fract(sin(dot(st.xy, vec2(6.,15.))) + sin(dot(st.xy, vec2(2.8765,23.23))));
}
// 2D Noise based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
float noise (in vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// Four corners in 2D of a tile
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
// Smooth Interpolation
// Cubic Hermine Curve. Same as SmoothStep()
vec2 u = f*f*(3.0-2.0*f);
// u = smoothstep(0.,1.,f)
// Mix 4 coorners porcentages
return mix(a, b, u.x) +
(c - a)* u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
`
const vs = `
precision highp float;
uniform float time;
uniform float sineTime;
attribute vec4 color;
varying vec3 vPosition;
// varying vec2 vUv;
${noise}
void main(){
float PI2 = 6.28;
// vUv = uv;
vec3 offset = vec3(0.0, 0.0, 0.0);
offset.z += noise(position.xy*vec2(.01)+vec2(0.0,time*.2))* 100.0;
offset.z = min(offset.z, cos(uv.x*PI2*.2)*250.0);
offset.z -= 50.0;
vPosition = offset + position;
if ( vPosition.z < -1.0 ){
vPosition.z = -1.0;
}
gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );
}
`
const fs = `
precision highp float;
uniform float time;
varying vec3 vPosition;
// varying vec2 vUv;
void main() {
float PI2 = 6.28;
float v = 7.0;
vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
if ( vPosition.z < 0.0 ) { // water level
color = vec4(0.1, 0.1, 0.1, 0.2);
} else if (mod(vPosition.z, v) < v/7.0 ) {
color = vec4(0.11, 0.56, 0.65, 1.0) * v/7.0;
} else {
color = vec4(0.11, 0.56, 0.65, 0.5);
}
color.a = min(color.a, clamp(0.0, 1.0, 4.0*cos( min(1.0, length(vPosition.xy)/1000.0) * 3.14)));
gl_FragColor = color;
}
`
export default class Animation {
constructor () {
let geometryBase = new THREE.PlaneGeometry(500, 500, 100, 100)
let geometry = new THREE.BufferGeometry().fromGeometry(geometryBase)
let length = geometry.attributes.position.count
let barycentric = new THREE.BufferAttribute(new Float32Array(length * 3), 3)
for (let i = 0, i3 = 0; i < length; i++, i3 += 3) {
barycentric.setX(i3 + 0, 1)
barycentric.setY(i3 + 0, 0)
barycentric.setZ(i3 + 0, 0)
barycentric.setX(i3 + 1, 0)
barycentric.setY(i3 + 1, 1)
barycentric.setZ(i3 + 1, 0)
barycentric.setX(i3 + 2, 0)
barycentric.setY(i3 + 2, 0)
barycentric.setZ(i3 + 2, 1)
}
geometry.addAttribute('aBarycentric', barycentric)
// material
let material = new THREE.ShaderMaterial({
uniforms: {
time: { value: 1.0 },
sineTime: { value: 1.0 }
// mouse: { value: this.mouse },
},
vertexShader: vs,
fragmentShader: fs,
extensions: {
derivatives: true
},
wireframe: true,
transparent: true
})
this.mesh = new THREE.Mesh(geometry, material)
}
update (time) {
this.mesh.material.uniforms.time.value = time
}
}