23.06.2017 Views

Web_Designer_UK_Issue_263_2017

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Tutorials<br />

Create a <strong>Web</strong>GL data visualisation<br />

composer = new THREE.<br />

EffectComposer(renderer);<br />

composer.addPass(renderPass);<br />

composer.addPass(filmPass);<br />

composer.addPass(badTVPass);<br />

composer.addPass(rgbPass);<br />

composer.addPass(copyPass);<br />

copyPass.renderToScreen = true;<br />

params();<br />

group = new THREE.Group();<br />

scene.add(group);<br />

6. Light the scene<br />

Lighting from above is added into the scene to<br />

ensure that objects can be seen. An Earth texture<br />

is loaded that will be wrapped around the spheres.<br />

The sphere geometry is created, the material is<br />

made with the Earth texture and the Earth is added<br />

to the group object.<br />

7. A second Earth<br />

Another model of the Earth is created just slightly<br />

larger than the first, but this time it has a slightly lower<br />

opacity to the texture. This just makes it a little more<br />

interesting with the blending of textures as they rotate<br />

around in the 3D scene. Notice how their blending is<br />

set to be an additive blend.<br />

material = new THREE.MeshBasicMaterial({<br />

map: planetTexture, transparent: true,<br />

opacity: 0.95, side: THREE.DoubleSide,<br />

blending: THREE.AdditiveBlending<br />

});<br />

Listen for events<br />

Event listeners are special browser actions that<br />

monitor certain types of activity such as user<br />

input or the browser resizing. The event usually<br />

calls a function with the activity to perform when<br />

the event is detected. JavaScript was originally<br />

set up as an event-driven language.<br />

earth = new THREE.Mesh(geometry, material);<br />

earth.scale.x = earth.scale.y = earth.<br />

scale.z = 1.02;<br />

group.add(earth);<br />

var mesh = new THREE.Object3D();<br />

8. Lines around the Earth<br />

An icosahedron model is added just around the outside<br />

of the Earth, to make it look a little more technical in its<br />

aesthetic look. This icosahedron is given a line material,<br />

so that only the edges of the model are visible. Another<br />

image is loaded here that will be used to add stars to<br />

the scene.<br />

9. Two thousand stars<br />

The for loop creates 2,000 stars in the scene by adding<br />

them at random positions in X, Y and Z space. A new<br />

material is created that gets the star texture. As this<br />

scene is about data, these look more like floating points<br />

than stars as a deliberate nod to the data on display.<br />

for (i = 0; i < 2000; i++) {<br />

var vertex = new THREE.Vector3();<br />

vertex.x = 400 * Math.random() - 200;<br />

vertex.y = 400 * Math.random() - 200;<br />

vertex.z = 400 * Math.random() - 200;<br />

geometry.vertices.push(vertex); }<br />

material = new THREE.PointsMaterial({<br />

size: 2, map: sprite, transparent: true,<br />

opacity: 0.5, alphaTest: 0.5<br />

});<br />

10. Add the data<br />

Here the particles get added into the group and then<br />

another for loop is created. This time the for loop is going<br />

to get the latitude, longitude and magnitude data of the<br />

earthquake. This data will be loaded at the end of the<br />

tutorial from a JSON file and stored in a global variable.<br />

particles1 = new THREE.Points(geometry,<br />

material);<br />

particles1.sortParticles = true;<br />

group.add(particles1);<br />

for (var i = 0; i < data.features.length;<br />

i++) {<br />

var geometry = new THREE.Geometry();<br />

var lat = data.features[i].geometry.<br />

coordinates[1];<br />

var lon = data.features[i].geometry.<br />

coordinates[0];<br />

var mag = data.features[i].properties.mag;<br />

var radius = 100;<br />

11. Check the magnitude<br />

If the magnitude is greater than zero, then the latitude<br />

and longitude are mapped on the sphere as X, Y, Z<br />

co-ordinates. This point becomes the starting point for a<br />

line originating at the point of the earthquake. This vertex<br />

is saved into the geometry.<br />

12. Show the magnitude<br />

Now the original vertex is cloned and multiplied out<br />

based on the value of the magnitude of the quake. This<br />

becomes a line with the addition of a line material that is<br />

drawn between the two points. The colour of the line is<br />

altered based on the value of the magnitude.<br />

var vertex2 = vertex.clone();<br />

vertex2.multiplyScalar((nmag * 0.4) + 1);<br />

geometry.vertices.push(vertex2);<br />

var myCol = new THREE.Color(0xffffff);<br />

myCol.setHSL((mag / 5), 0.9, 0.6);<br />

var line = new THREE.Line(geometry, new<br />

THREE.LineBasicMaterial({ color: myCol,<br />

linewidth: 1 }));<br />

group.add(line);<br />

}<br />

}<br />

13. Listen to the browser<br />

The whole group is rotated slightly to simulate that the<br />

Earth is on an angle. The document gets a number of<br />

event listeners added for the mouse, and touch input so<br />

that the screen can be zoomed into and looked at based<br />

on the mouse or touch position.<br />

Top left<br />

The stars are now added to give much more depth to<br />

the scene<br />

Top right<br />

With the parsed data from the JSON file the height and<br />

location of the earthquakes can be added into the scene<br />

Right<br />

Now the shaders are all rendered using the effects<br />

composer, it adds a little extra distortion to the scene<br />

54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!