-
Notifications
You must be signed in to change notification settings - Fork 4
/
lavafountain.html
195 lines (151 loc) · 6.88 KB
/
lavafountain.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
<!DOCTYPE html>
<html>
<head>
<title>Lava!</title>
<script type="text/javascript" src="three.js"></script>
</head>
<body>
<div id="container"></div>
<script type="x-shader/x-vertex" id="vertexShader">
uniform float time;
varying vec3 transformedNormal;
varying vec3 lighting;
varying vec4 vPosition;
void main() {
vPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
transformedNormal = normalMatrix * normal;
}
</script>
<script type="x-shader/x-fragment" id="cubeFragmentShader">
uniform float time;
varying vec3 transformedNormal;
varying vec3 lighting;
varying vec4 vPosition;
void main() {
vec3 directionalVector = vec3(0.0, 5.0, 0.0);
vec3 lightDirection = normalize(directionalVector - vPosition.xyz);
vec3 lightColor = vec3(sin(time), cos(time), tan(time));//vec3(0.15, 0.9, 0.45);
vec3 ambientLight = vec3(0.16, 0.16, 0.16);
float directionalLightWeighting = max(dot(normalize(transformedNormal), lightDirection), 0.0);
vec3 lightWeighting = ambientLight + (lightColor * directionalLightWeighting);
vec4 fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script type="x-shader/x-fragment" id="sphereFragmentShader">
uniform float time;
varying vec3 transformedNormal;
varying vec3 lighting;
varying vec4 vPosition;
void main() {
vec3 directionalVector = vec3(0.0, 105.0, 0.0);
vec3 lightDirection = normalize(directionalVector - vPosition.xyz);
vec3 normal = normalize(transformedNormal);
vec3 directionalLightColor = vec3(0.80, 0.80, 0.0);
vec3 ambientLight = vec3(0.05, 0.05, 0.05);
float directionalLightWeighting = max(dot(normal, lightDirection), 0.0);
vec3 specularLightColor = directionalLightColor;
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), 20.0);
vec3 lightWeighting = ambientLight +
(specularLightColor * specularLightWeighting) +
(directionalLightColor * directionalLightWeighting);
vec4 fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script type="text/javascript" id="display-logic">
Math.randomRange = function (min, max) {
return Math.random() * (max - min) + min;
};
var WIDTH = 1000,
HEIGHT = 600;
VIEW_ANGLE = 45,
ASPECT_RATIO = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000,
PARTICLE_COUNT = 3000;
var camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT_RATIO, NEAR, FAR);
var scene = new THREE.Scene();
camera.position.z = 300;
var renderer = new THREE.WebGLRenderer();
renderer.setSize(WIDTH, HEIGHT);
renderer.setClearColor(0x000000);
// var ambientLight = new THREE.AmbientLight(0x666666);
// scene.add(ambientLight);
var frame = 0
particles = [],
geometry = new THREE.Geometry()
randomPosition = new THREE.Vector3(Math.random() * 600 - 400, Math.random() * 300 - 150, Math.random() * 500 - 250);
var resetParticleAt = function(i) {
geometry.vertices[i].y = -140;
geometry.vertices[i].x = Math.randomRange(-15, 15);
particles[i].velocity.x = Math.randomRange(-0.05, 0.05);
particles[i].velocity.y = Math.randomRange(1, 4);
};
var assembleGeometry = function() {
for (var i = 0; i < PARTICLE_COUNT; i++) {
var particle = {
velocity:
new THREE.Vector3(Math.randomRange(-0.05, 0.05), Math.randomRange(1, 1.55), 0),
acceleration:
new THREE.Vector3(Math.randomRange(-0.013, 0.013), Math.randomRange(-0.005, 0.05), 0)
};
particles.push(particle);
var x = Math.randomRange(-15,15);
geometry.vertices.push(new THREE.Vector3(x, -140, Math.randomRange(-80, 80)));
}
};
assembleGeometry();
var particleMaterial = new THREE.ParticleSystemMaterial({
color: '#DF3A01',
map: THREE.ImageUtils.loadTexture('http://i.imgur.com/IZXm8UR.png'),
size: 42,
blending: THREE.AdditiveBlending,
transparent: true,
opacity: 1.0
}),
particleSystem = new THREE.ParticleSystem(geometry, particleMaterial);
particleSystem.sortParticles = true;
scene.add(particleSystem);
var gravity = new THREE.Vector3(0, -0.06, 0);
var clock = new THREE.Clock(true),
i = 0;
clock.getDelta(),
startTime = new Date().getTime();
var render = function() {
requestAnimationFrame(render);
// particleSystem.rotation.y = frame / 90;
// particleSystem.rotation.z = frame / 500;
// particleMaterial.color.setRGB(i * Math.random(),i,i);
for (i = 0; i < PARTICLE_COUNT; i++) {
if (geometry.vertices[i].y > 145 || geometry.vertices[i].y < -140) {
resetParticleAt(i);
}
if (geometry.vertices[i].x > 200 || geometry.vertices[i].x < -200) {
resetParticleAt(i);
}
var particle = particles[i],
delta = clock.getDelta();
particle.velocity.add(particle.acceleration);
particle.velocity.add(gravity);
// geometry.vertices[i].add((particle.velocity.multiplyScalar(delta)));
geometry.vertices[i].add(particle.velocity);
// if (i === 60) console.log((particle.velocity.multiplyScalar(delta)) + (.5 * particle.acceleration.multiplyScalar(delta * delta)));
// geometry.vertices[i].x = Math.random() * (i - 100) * ((Math.round(Math.random())) % 2 === 0 ? 1 : -1);
// geometry.vertices[i].y += particles[i].velocity.y;
}
renderer.render(scene, camera);
if (i > 256) {
i = 0;
}
i++;
frame++;
};
render();
document.getElementById('container').appendChild(renderer.domElement);
</script>
</body>
</html>