-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet.html
233 lines (179 loc) · 6.36 KB
/
planet.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<!-- It is not really possible to reasonably split this file so that js and html are not mixed -->
<html lang="en">
<head>
<meta charset="utf-8"/>
<script src="js/three.r108.js"></script>
<title>That is some pro JS code here!</title>
<script>
// I took this task from Computer Graphics - Basic II - Task 4.1 - Procedural Generation
// My own solution
var width = 500;
var height = 500;
var renderer, scene, camera;
var planet;
var lightPosition;
var planetVertexShader, planetFragmentShader;
function millis() {
return (new Date()).getTime();
}
function toRad(degree) {
return Math.PI * 2 * degree / 360;
}
function onLoad() {
var canvasContainer = document.getElementById('PizzaPlanet');
planetVertexShader = document.getElementById('vertexShader').textContent;
planetFragmentShader = document.getElementById('fragmentShader').textContent;
renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(width, height);
canvasContainer.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(80, width / height, 1, 30);
camera.position.z = 12;
camera.position.y = 0;
camera.up = new THREE.Vector3(0,1,0);
camera.lookAt(new THREE.Vector3(0,0,0));
scene.add(camera);
lightPosition = new THREE.Vector3(50, 40, 20);
planet = addPlanet();
draw();
}
function draw() {
requestAnimationFrame(draw);
var k = 0.90 * window.innerWidth;
width = k;
height = k;
renderer.setSize(width, height);
planet.rotation.set(0, toRad((millis() / 150) % 360), 0);
scene.children[1].children[0].material.uniforms.time.value = Math.sin(millis() / 2000) * 0.2;
renderer.setClearColor(0x00000, 0);
renderer.render(scene, camera);
}
function addPlanet() {
var planet = new THREE.Object3D();
var sphere = createSphere();
sphere.scale.set(7, 7, 7);
planet.add(sphere);
scene.add(planet);
return planet;
}
function createSphere() {
var geometry = new THREE.SphereBufferGeometry(1, 30, 30);
var colorWater = new THREE.Color(0xC44833);
var colorAtmosphere = new THREE.Color(0x66d5ed);
var color1 = new THREE.Color(0xF0D993);
var color2 = new THREE.Color(0xE0B455);
var color3 = new THREE.Color(0x1C6D35);
var time = Math.sin(millis() / 50);
var material = new THREE.ShaderMaterial({
uniforms: {
lightPosition: {
value: lightPosition
},
colors: {
value: [color1, color2, color3]
},
colorWater: {
value: colorWater
},
colorAtmosphere: {
value: colorAtmosphere
},
time: {
value: time
},
},
vertexShader: planetVertexShader,
fragmentShader: planetFragmentShader
});
var sphere = new THREE.Mesh(geometry, material); //We create our cube with that material
return sphere;
}
</script>
<script id="vertexShader" type="x-shader/x-vertex">
varying vec3 interpolatedLocalPosition; //We interpolate the local position of fragment
varying vec3 interpolatedPosition;
varying vec3 interpolatedNormal;
void main() {
interpolatedLocalPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
interpolatedPosition = (modelViewMatrix * vec4(position, 1.0)).xyz;
interpolatedNormal = normalize(normalMatrix * normal);
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform float time;
uniform vec3 colors[3];
uniform vec3 colorWater;
uniform vec3 colorAtmosphere;
uniform vec3 lightPosition;
varying vec3 interpolatedLocalPosition;
varying vec3 interpolatedPosition;
varying vec3 interpolatedNormal;
//Source: https://www.shadertoy.com/view/Xsl3Dl
vec3 hash(vec3 p) {
p = vec3( dot(p, vec3(127.1,311.7, 74.7)),
dot(p, vec3(269.5,163.3,226.1)),
dot(p, vec3(113.5,271.9,124.6)));
return -1.0 + 2.0*fract(sin(p)*43758.5453123);
}
//Source: https://www.shadertoy.com/view/Xsl3Dl
float noise(in vec3 p) {
vec3 i = floor( p );
vec3 f = fract( p );
//vec3 u = f*f*(3.0-2.0*f);
vec3 u = f*f*(3.0-2.0*f);
return mix( mix( mix( dot( hash( i + vec3(0.0,0.0,0.0) ), f - vec3(0.0,0.0,0.0) ),
dot( hash( i + vec3(1.0,0.0,0.0) ), f - vec3(1.0,0.0,0.0) ), u.x),
mix( dot( hash( i + vec3(0.0,1.0,0.0) ), f - vec3(0.0,1.0,0.0) ),
dot( hash( i + vec3(1.0,1.0,0.0) ), f - vec3(1.0,1.0,0.0) ), u.x), u.y),
mix( mix( dot( hash( i + vec3(0.0,0.0,1.0) ), f - vec3(0.0,0.0,1.0) ),
dot( hash( i + vec3(1.0,0.0,1.0) ), f - vec3(1.0,0.0,1.0) ), u.x),
mix( dot( hash( i + vec3(0.0,1.0,1.0) ), f - vec3(0.0,1.0,1.0) ),
dot( hash( i + vec3(1.0,1.0,1.0) ), f - vec3(1.0,1.0,1.0) ), u.x), u.y), u.z );
}
void main() {
float f = 0.0;
f += 0.7 * noise(2.7 * interpolatedLocalPosition);
f += 0.2 * noise(5.0 * interpolatedLocalPosition);
f += 0.1 * noise(10.0* interpolatedLocalPosition);
float a = 0.86;
a -= 0.5;
f = (f + a) / (2.0 * a);
vec3 color = colors[1];
float water_tresh = 0.35 + time;
if (f < water_tresh)
color = (0.4+f/2.0) * colorWater;
else if (f < 0.55)
color = colors[0];
else if (f < 0.8)
{
float k = (f - 0.55) / (0.8 - 0.55);
k = pow(abs(k), 0.9);
color = (1.0 - k) * colors[0] + k * colors[1];
}
else
{
float k = (f - 0.8) / (1.0 - 0.8);
k = pow(abs(k), 0.4);
color = (1.0 - k) * colors[1] + k * colors[2];
}
vec3 n = normalize(interpolatedNormal);
vec3 l = normalize(lightPosition - interpolatedPosition);
vec3 v = normalize(- interpolatedPosition);
vec3 r = reflect(-l, n);
float shininess = 15.0;
vec3 curColor;
curColor += color * max(0.3, dot(n, l));
if (f < water_tresh)
curColor += 0.5 * pow(max(0.0, dot(v, r)), shininess);
vec3 colorAtm = max(0.0, min(0.3, 0.7 - pow(max(0.0, dot(n, v)), 0.6))) * colorAtmosphere;
vec3 curColorAtm = colorAtm * max(0.0, dot(n, l));
gl_FragColor = vec4(curColor + curColorAtm, 1.0);
}
</script>
</head>
<body onload="onLoad()">
<div id="PizzaPlanet"></div>
</body>
</html>