-
Notifications
You must be signed in to change notification settings - Fork 1
/
RotatingEarth.html
208 lines (164 loc) · 5.56 KB
/
RotatingEarth.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
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>WebGL Rotating Earth</title>
<style>
#container {
background: #000
}
body {
background-color: #ffffff;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="Three.js"></script>
<!-- Shaders -->
<script id="vertexShader" type="x-shader/x-vertex">
varying vec2 vUv;
varying vec3 vNormal;
varying vec3 v_P;
void main()
{
vUv = uv; //Texture position
vNormal = normalMatrix * normal; // Calculate surface normal
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
v_P = gl_Position.xyz;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D spectexture;
uniform sampler2D cloudtexture;
uniform vec3 sunDirection;
varying vec2 vUv;
varying vec3 vNormal;
varying vec3 v_P;
void main()
{
// Values from textures using the texture coordinate from the vertex shader
vec3 colorDay = texture2D(texture1, vUv).rgb;
vec3 colorNight = texture2D(texture2, vUv).rgb;
vec3 colorSpec = texture2D(spectexture, vUv).rgb;
// Rather than a RGB colour for the clouds we just want a numerical value
// In this case we use the red channel although this is obviosuly not ideal
float cloudamount = texture2D(cloudtexture, vUv).r;
//Calculate the diffuse component of the reflected light from the sun
float sphere_diffuse = max(dot(normalize(vNormal), sunDirection), 0.0);
// Calculate the amount of atmospheric reflection absed on viewer position
float atmos_amount = max(dot(normalize(vNormal), vec3(0.0,0.0,1.0)), 0.0);
vec3 R = 2.0 * ( dot(normalize(vNormal),sunDirection)) * normalize(vNormal) - sunDirection;
vec3 E = vec3(0.0,0.0,1.0);
//Calculate amount of specular light reflected
vec3 specular_value = colorSpec * pow( max(dot(R,E), 0.0), 6.0 );
vec3 lights;
if (colorNight.r < 0.6) {
lights = vec3(colorNight.r/3.0,colorNight.g/3.0,colorNight.b/1.5);
} else {
lights = vec3(colorNight.r*1.0,colorNight.g*0.8,colorNight.b*0.4);
}
colorNight=lights;
// compute cosine sun to normal so -1 is away from sun and +1 is toward sun.
float cosineAngleSunToNormal = dot(normalize(vNormal), sunDirection);
// sharpen the edge beween the transition
//cosineAngleSunToNormal = clamp( cosineAngleSunToNormal * 30.0, -1.0, 1.0);
sphere_diffuse = clamp( sphere_diffuse * 10.0, -1.0, 1.0);
// convert to 0 to 1 for mixing
float mixAmount = cosineAngleSunToNormal * 0.5 + 0.5;
// Select day or night texture based on mix.
vec3 blue = vec3(0.2,0.4,0.8);
vec3 color = mix( colorNight+vec3(0.1,0.1,0.2)*cloudamount, colorDay+(specular_value/1.3)+vec3(1.0,1.0,1.0)*cloudamount+blue*(1.0-atmos_amount), sphere_diffuse);
gl_FragColor = vec4( color , 1.0 );
}
</script>
<!-- End Shaders -->
<script type="text/javascript">
var sphere;
var rotationamount = 0;
var camheight = 0;
var x = 1.0;
var z = 0.0;
// set the scene size
var WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
// set some camera attributes
var VIEW_ANGLE = 45,
ASPECT = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000;
// get the DOM element to attach to
// - assume we've got jQuery to hand
var $container = $('#container');
// create a WebGL renderer, camera
// and a scene
var renderer = new THREE.WebGLRenderer();
var camera = new THREE.PerspectiveCamera( VIEW_ANGLE,
ASPECT,
NEAR,
FAR );
var scene = new THREE.Scene();
// start the renderer
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
$container.append(renderer.domElement);
var pointLight = new THREE.PointLight(0xFFFFFF);
//pointLight.intensity(1.0)
// set its position
pointLight.position.x = 1.0;
pointLight.position.y = 0;
pointLight.position.z = 0.0;
var uniforms = {
sunDirection: { type: "v3", value: new THREE.Vector3(pointLight.position.x,0,pointLight.position.z) },
texture1: { type: "t", value: THREE.ImageUtils.loadTexture( "earth4096.jpg" ) },
texture2: { type: "t", value: THREE.ImageUtils.loadTexture( "EarthNight.jpg" ) },
spectexture: { type: "t", value: THREE.ImageUtils.loadTexture( "EarthSpec.jpg" ) },
cloudtexture: { type: "t", value: THREE.ImageUtils.loadTexture( "clouds4096.jpg" ) }
};
var vertShader = document.getElementById('vertexShader').innerHTML;
var fragShader = document.getElementById('fragmentShader').innerHTML;
// create the sphere's material
var shaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: vertShader,
fragmentShader: fragShader
});
// set up the sphere vars
var radius = 50, segments = 64, rings = 64;
// create a new mesh with sphere geometry -
// we will cover the sphereMaterial next!
var earth = new THREE.Mesh(
new THREE.SphereGeometry(radius, segments, rings),
shaderMaterial);
// add to the scene
scene.add(pointLight);
// add the sphere to the scene
scene.add(earth);
scene.add(camera);
function render(){
renderer.render(scene, camera);
}
function update(){
earth.rotation.y = rotationamount;
rotationamount += 0.001
camera.lookAt(earth.position)
camheight = 120;
uniforms["sunDirection"].value = new THREE.Vector3(Math.sin( -rotationamount * 2.9),0.0,Math.cos( rotationamount * 2.9));
camera.position.z = camheight;
camera.position.y = camera.position.z/2.0;
}
function animate()
{
requestAnimationFrame( animate );
render();
update();
}
animate();
</script>
</html>