Skip to content

Commit 61ca697

Browse files
committed
added modified earth example for TrackballCamera
1 parent f4a08e5 commit 61ca697

File tree

1 file changed

+338
-0
lines changed

1 file changed

+338
-0
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<title>three.js webgl - trackball camera - earth</title>
4+
5+
<style type="text/css">
6+
7+
body {
8+
background: #FFF;
9+
color: #EEE;
10+
padding: 0;
11+
margin: 0;
12+
font-weight: bold;
13+
overflow: hidden;
14+
15+
font-family: Monospace;
16+
font-size: 13px;
17+
text-align: center;
18+
}
19+
20+
#info {
21+
position: absolute;
22+
top: 0px;
23+
width: 100%;
24+
padding: 5px;
25+
z-index: 100;
26+
}
27+
28+
a { color: green; }
29+
b { color: green; }
30+
31+
</style>
32+
33+
<script type="text/javascript" src="../build/Three.js"></script>
34+
<script type="text/javascript" src="../src/extras/cameras/TrackballCamera.js"></script>
35+
<script type="text/javascript" src="js/Detector.js"></script>
36+
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
37+
<script type="text/javascript" src="js/Stats.js"></script>
38+
39+
</head>
40+
41+
<body>
42+
43+
<div id="info">
44+
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - earth [trackball camera]<br/><br/>
45+
<b>MOVE</b> mouse & press <b>LEFT/SHIFT:</b> rotate, <b>MIDDLE/ALT:</b> zoom, <b>RIGHT/CTRL:</b> pan</div>
46+
47+
<script type="text/javascript">
48+
49+
if ( !Detector.webgl ) {
50+
51+
Detector.addGetWebGLMessage();
52+
return;
53+
54+
}
55+
56+
var radius = 6371,
57+
tilt = 0.41,
58+
rotationSpeed = 0.1,
59+
60+
cloudsScale = 1.005,
61+
moonScale = 0.23,
62+
63+
height = window.innerHeight,
64+
width = window.innerWidth,
65+
66+
container, stats,
67+
68+
camera, scene, renderer,
69+
geometry, meshPlanet, meshClouds, meshMoon, meshMoon2, meshMoon3,
70+
dirLight, ambientLight,
71+
72+
time = new Date().getTime();
73+
74+
window.onload = function() {
75+
76+
init();
77+
animate();
78+
79+
}
80+
81+
function init() {
82+
83+
container = document.createElement( 'div' );
84+
document.body.appendChild( container );
85+
86+
87+
scene = new THREE.Scene();
88+
89+
90+
renderer = new THREE.WebGLRenderer( { clearAlpha: 1, clearColor: 0x000000 } );
91+
renderer.setSize( width, height );
92+
renderer.sortObjects = false;
93+
renderer.autoClear = false;
94+
container.appendChild( renderer.domElement );
95+
96+
97+
camera = new THREE.TrackballCamera({
98+
99+
fov: 25,
100+
aspect: width / height,
101+
near: 50,
102+
far: 1e7,
103+
104+
rotateSpeed: 0.6,
105+
zoomSpeed: 1.2,
106+
panSpeed: 0.2,
107+
108+
noZoom: false,
109+
noPan: false,
110+
111+
staticMoving: false,
112+
dynamicDampingFactor: 0.2,
113+
114+
minDistance: radius * 1.1,
115+
maxDistance: radius * 100,
116+
117+
keys: [ 16, 18, 17 ], // [ rotateKey, zoomKey, panKey ],
118+
119+
domElement: renderer.domElement,
120+
121+
});
122+
123+
camera.position.z = radius * 7;
124+
125+
126+
dirLight = new THREE.DirectionalLight( 0xFFFFFF );
127+
dirLight.position.set( -1, 0, 1 );
128+
dirLight.position.normalize();
129+
scene.addLight( dirLight );
130+
131+
ambientLight = new THREE.AmbientLight( 0xFFFFFF );
132+
scene.addLight( ambientLight );
133+
134+
var planetTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_atmos_2048.jpg" ),
135+
cloudsTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_clouds_1024.png" ),
136+
normalTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_normal_2048.jpg" ),
137+
specularTexture = THREE.ImageUtils.loadTexture( "textures/planets/earth_specular_2048.jpg" ),
138+
moonTexture = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
139+
140+
var shader = THREE.ShaderUtils.lib[ "normal" ],
141+
uniforms = THREE.UniformsUtils.clone( shader.uniforms );
142+
143+
uniforms[ "tNormal" ].texture = normalTexture;
144+
uniforms[ "uNormalScale" ].value = 0.85;
145+
146+
uniforms[ "tDiffuse" ].texture = planetTexture;
147+
uniforms[ "tSpecular" ].texture = specularTexture;
148+
149+
uniforms[ "enableAO" ].value = false;
150+
uniforms[ "enableDiffuse" ].value = true;
151+
uniforms[ "enableSpecular" ].value = true;
152+
153+
uniforms[ "uDirLightPos" ].value = dirLight.position;
154+
uniforms[ "uDirLightColor" ].value = dirLight.color;
155+
156+
uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
157+
158+
uniforms[ "uDiffuseColor" ].value.setHex( 0xffffff );
159+
uniforms[ "uSpecularColor" ].value.setHex( 0xaaaaaa );
160+
uniforms[ "uAmbientColor" ].value.setHex( 0x000000 );
161+
162+
uniforms[ "uShininess" ].value = 30;
163+
164+
var materialNormalMap = new THREE.MeshShaderMaterial({
165+
fragmentShader: shader.fragmentShader,
166+
vertexShader: shader.vertexShader,
167+
uniforms: uniforms
168+
});
169+
170+
171+
// planet
172+
173+
geometry = new THREE.Sphere( radius, 100, 50 );
174+
geometry.computeTangents();
175+
176+
meshPlanet = new THREE.Mesh( geometry, materialNormalMap );
177+
meshPlanet.rotation.y = 1.3;
178+
meshPlanet.rotation.z = tilt;
179+
scene.addObject( meshPlanet );
180+
181+
182+
// clouds
183+
184+
var materialClouds = new THREE.MeshLambertMaterial( { color: 0xffffff, map: cloudsTexture, transparent:true } );
185+
186+
meshClouds = new THREE.Mesh( geometry, materialClouds );
187+
meshClouds.scale.set( cloudsScale, cloudsScale, cloudsScale );
188+
meshClouds.rotation.z = tilt;
189+
scene.addObject( meshClouds );
190+
191+
192+
// moon
193+
194+
var materialMoon = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
195+
196+
meshMoon = new THREE.Mesh( geometry, materialMoon );
197+
meshMoon.position.set( radius * 5, 0, 0 );
198+
meshMoon.scale.set( moonScale, moonScale, moonScale );
199+
scene.addObject( meshMoon );
200+
201+
202+
// moon2
203+
204+
var materialMoon2 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
205+
206+
meshMoon2 = new THREE.Mesh( geometry, materialMoon );
207+
meshMoon2.position.set( -radius * 7, 0, radius * 3 );
208+
meshMoon2.scale.set( moonScale, moonScale, moonScale );
209+
scene.addObject( meshMoon2 );
210+
211+
212+
// moon3
213+
214+
var materialMoon3 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: moonTexture } );
215+
216+
meshMoon3 = new THREE.Mesh( geometry, materialMoon );
217+
meshMoon3.position.set( radius, 0, -radius * 10 );
218+
meshMoon3.scale.set( moonScale, moonScale, moonScale );
219+
scene.addObject( meshMoon3 );
220+
221+
222+
// stars
223+
224+
var i,
225+
vector,
226+
starsGeometry = new THREE.Geometry();
227+
228+
for ( i = 0; i < 1500; i++ ) {
229+
230+
vector = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
231+
vector.multiplyScalar( radius );
232+
233+
starsGeometry.vertices.push( new THREE.Vertex( vector ) );
234+
235+
}
236+
237+
var stars,
238+
starsMaterials = [
239+
new THREE.ParticleBasicMaterial( { color: 0x555555, size: 2, sizeAttenuation: false } ),
240+
new THREE.ParticleBasicMaterial( { color: 0x555555, size: 1, sizeAttenuation: false } ),
241+
new THREE.ParticleBasicMaterial( { color: 0x333333, size: 2, sizeAttenuation: false } ),
242+
new THREE.ParticleBasicMaterial( { color: 0x3a3a3a, size: 1, sizeAttenuation: false } ),
243+
new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 2, sizeAttenuation: false } ),
244+
new THREE.ParticleBasicMaterial( { color: 0x1a1a1a, size: 1, sizeAttenuation: false } )
245+
];
246+
247+
for ( i = 10; i < 30; i++ ) {
248+
249+
stars = new THREE.ParticleSystem( starsGeometry, starsMaterials[ i % 6 ] );
250+
251+
stars.rotation.x = Math.random() * 6;
252+
stars.rotation.y = Math.random() * 6;
253+
stars.rotation.z = Math.random() * 6;
254+
255+
var s = i * 10;
256+
stars.scale.set( s, s, s );
257+
258+
stars.matrixAutoUpdate = false;
259+
stars.updateMatrix();
260+
261+
scene.addObject( stars );
262+
263+
}
264+
265+
stats = new Stats();
266+
stats.domElement.style.position = 'absolute';
267+
stats.domElement.style.top = '0px';
268+
stats.domElement.style.zIndex = 100;
269+
container.appendChild( stats.domElement );
270+
271+
window.addEventListener( 'resize', onWindowResize, false );
272+
273+
};
274+
275+
function onWindowResize( event ) {
276+
277+
width = window.innerWidth;
278+
height = window.innerHeight;
279+
280+
renderer.setSize( width, height );
281+
282+
camera.aspect = width / height;
283+
camera.updateProjectionMatrix();
284+
285+
camera.screen.width = width;
286+
camera.screen.height = height;
287+
288+
camera.radius = ( width + height ) / 4;
289+
290+
};
291+
292+
function animate() {
293+
294+
requestAnimationFrame( animate );
295+
296+
render();
297+
stats.update();
298+
299+
};
300+
301+
function render() {
302+
303+
var t = new Date().getTime(),
304+
dt = ( t - time ) / 1000;
305+
time = t;
306+
307+
meshPlanet.rotation.y += rotationSpeed * dt;
308+
meshClouds.rotation.y += 1.25 * rotationSpeed * dt;
309+
310+
var angle = dt * rotationSpeed;
311+
312+
meshMoon.position = rotateY( meshMoon.position, angle * 2 );
313+
meshMoon.rotation.y += angle;
314+
315+
meshMoon2.position = rotateY( meshMoon2.position, -angle * 1.5 );
316+
meshMoon2.rotation.y += angle;
317+
318+
meshMoon3.position = rotateY( meshMoon3.position, angle );
319+
meshMoon3.rotation.y += angle;
320+
321+
renderer.clear();
322+
renderer.render( scene, camera );
323+
324+
};
325+
326+
function rotateY( vector, angle ) {
327+
328+
return new THREE.Vector3(
329+
Math.cos( angle ) * vector.x - Math.sin( angle ) * vector.z,
330+
0,
331+
Math.sin( angle ) * vector.x + Math.cos( angle ) * vector.z
332+
);
333+
334+
};
335+
336+
</script>
337+
</body>
338+
</html>

0 commit comments

Comments
 (0)