forked from sghall/webgl-globes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_2_threejs_sphere.html
87 lines (66 loc) · 2.64 KB
/
example_2_threejs_sphere.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
<!DOCTYPE html>
<html>
<head>
<title>THREE.js Sphere Wrapped with Canvas</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="loading">Loading...</div>
<script src="node_modules/three/three.js"></script>
<script src="node_modules/d3/d3.js"></script>
<script src="node_modules/topojson/build/topojson.js"></script>
<script>
/*eslint-disable */
var projection = d3.geo.equirectangular()
.translate([512, 256])
.scale(163);
d3.json('data/world.json', function (err, data) {
d3.select("#loading").transition().duration(500)
.style("opacity", 0).remove();
var countries = topojson.feature(data, data.objects.countries);
var canvas = d3.select("body").append("canvas")
.style("display", "none")
.attr("width", "1024px")
.attr("height", "512px");
var context = canvas.node().getContext("2d");
var path = d3.geo.path()
.projection(projection).context(context);
context.strokeStyle = "#333";
context.lineWidth = 0.25;
context.fillStyle = "#fff";
context.beginPath();
path(countries);
context.fill();
context.stroke();
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 6000);
camera.position.z = 500;
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var light = new THREE.HemisphereLight('#fff', '#666', 1.5);
light.position.set(0, 500, 0);
scene.add(light);
var waterMaterial = new THREE.MeshBasicMaterial({color: '#555', transparent: true});
var sphere = new THREE.SphereGeometry(200, 100, 100);
var baseLayer = new THREE.Mesh(sphere, waterMaterial);
var mapTexture = new THREE.Texture(canvas.node());
mapTexture.needsUpdate = true;
var mapMaterial = new THREE.MeshBasicMaterial({map: mapTexture, transparent: true});
var mapLayer = new THREE.Mesh(sphere, mapMaterial);
var root = new THREE.Object3D();
root.add(baseLayer);
root.add(mapLayer);
scene.add(root);
function render() {
root.rotation.y += 0.02;
requestAnimationFrame(render);
renderer.render(scene, camera);
}
render();
});
</script>
</body>
</html>