UNPKG

6.6 kBMarkdownView Raw
1#### three.js Experiments
2
3[threejs](https://threejs.org) is a JS library that is much more fine-grained than [p5.js](https://p5js.org). I thought I'd try it out. I suspect that older machines without WebGL capability will have trouble with some of the [examples](https://threejs.org/examples/).
4
5This example is adapted from [WebGL Interactive Cubes](https://threejs.org/examples/#webgl_interactive_cubes), although I'm still having difficulty with getting the mouseover to work properly due to coordinate system issues when the window is scrolled or resized. I eventually need to detect the coordinate system change and recalibrate the mouseover.
6
7```javascript/playable
8var container = this.div;
9container.style.margin = 'auto';
10container.style.width = '250px';
11container.style.height = '250px';
12var bounds = container.getBoundingClientRect();
13
14var camera, scene, raycaster, renderer;
15
16var mouse = new THREE.Vector2(), INTERSECTED;
17var radius = 100, theta = 0;
18
19init();
20animate();
21
22function init() {
23
24 camera = new THREE.PerspectiveCamera( 70, container.clientWidth / container.clientHeight, 1, 10000 );
25
26 scene = new THREE.Scene();
27
28 var light = new THREE.DirectionalLight( 0xffffff, 1 );
29 light.position.set( 1, 1, 1 ).normalize();
30 scene.add( light );
31
32 var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
33
34 for ( var i = 0; i < 2000; i ++ ) {
35
36 var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
37
38 object.position.x = Math.random() * 800 - 400;
39 object.position.y = Math.random() * 800 - 400;
40 object.position.z = Math.random() * 800 - 400;
41
42 object.rotation.x = Math.random() * 2 * Math.PI;
43 object.rotation.y = Math.random() * 2 * Math.PI;
44 object.rotation.z = Math.random() * 2 * Math.PI;
45
46 object.scale.x = Math.random() + 0.5;
47 object.scale.y = Math.random() + 0.5;
48 object.scale.z = Math.random() + 0.5;
49
50 scene.add( object );
51
52 }
53
54 raycaster = new THREE.Raycaster();
55
56 renderer = new THREE.WebGLRenderer();
57 renderer.setClearColor( 0xf0f0f0 );
58 renderer.setPixelRatio( window.devicePixelRatio );
59 renderer.setSize( container.clientWidth, container.clientHeight );
60 renderer.sortObjects = false;
61 container.appendChild(renderer.domElement);
62
63
64 document.addEventListener( 'mousemove', onDocumentMouseMove, false );
65}
66
67
68function onDocumentMouseMove( event ) {
69 event.preventDefault();
70 mouse.x = ( (event.clientX - bounds.left) / container.clientWidth ) * 2 - 1;
71 mouse.y = - ( (event.clientY - bounds.top) / container.clientHeight ) * 2 + 1;
72}
73
74function animate() {
75 requestAnimationFrame( animate );
76 render();
77}
78
79function render() {
80
81 theta += 0.1;
82
83 camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
84 camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
85 camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
86 camera.lookAt( scene.position );
87
88 camera.updateMatrixWorld();
89
90 // find intersections
91
92 raycaster.setFromCamera( mouse, camera );
93
94 var intersects = raycaster.intersectObjects( scene.children );
95
96 if ( intersects.length > 0 ) {
97
98 if ( INTERSECTED != intersects[ 0 ].object ) {
99
100 if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
101
102 INTERSECTED = intersects[ 0 ].object;
103 INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
104 INTERSECTED.material.emissive.setHex( 0xff0000 );
105
106 }
107
108 }
109 else {
110 if (INTERSECTED) {
111 INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
112 }
113
114 INTERSECTED = null;
115 }
116 renderer.render( scene, camera );
117}
118```
119
120
121### TopoJSON and ThreeJS
122
123From https://bl.ocks.org/mbostock/2b85250396c17a79155302f91ec21224
124
125```javascript/playable
126var width = 480,
127 height = 480,
128 radius = 114,
129 mesh,
130 graticule,
131 scene = new THREE.Scene,
132 camera = new THREE.PerspectiveCamera(70, width / height, 1, 1000),
133 renderer = new THREE.WebGLRenderer({alpha: true});
134
135camera.position.z = 200;
136renderer.setPixelRatio(window.devicePixelRatio);
137renderer.setSize(width, height);
138this.div.appendChild(renderer.domElement);
139
140d3.json("https://unpkg.com/world-atlas@1/world/50m.json", function(error, topology) {
141 if (error) throw error;
142 scene.add(graticule = wireframe(graticule10(), new THREE.LineBasicMaterial({color: 0xaaaaaa})));
143 scene.add(mesh = wireframe(topojson.mesh(topology, topology.objects.land), new THREE.LineBasicMaterial({color: 0xff0000})));
144 d3.timer(function(t) {
145 graticule.rotation.x = mesh.rotation.x = Math.sin(t / 11000) * Math.PI / 3 - Math.PI / 2;
146 graticule.rotation.z = mesh.rotation.z = t / 10000;
147 renderer.render(scene, camera);
148 });
149});
150
151// Converts a point [longitude, latitude] in degrees to a THREE.Vector3.
152function vertex(point) {
153 var lambda = point[0] * Math.PI / 180,
154 phi = point[1] * Math.PI / 180,
155 cosPhi = Math.cos(phi);
156 return new THREE.Vector3(
157 radius * cosPhi * Math.cos(lambda),
158 radius * cosPhi * Math.sin(lambda),
159 radius * Math.sin(phi)
160 );
161}
162
163// Converts a GeoJSON MultiLineString in spherical coordinates to a THREE.LineSegments.
164function wireframe(multilinestring, material) {
165 var geometry = new THREE.Geometry;
166 multilinestring.coordinates.forEach(function(line) {
167 d3.pairs(line.map(vertex), function(a, b) {
168 geometry.vertices.push(a, b);
169 });
170 });
171 return new THREE.LineSegments(geometry, material);
172}
173
174// See https://github.com/d3/d3-geo/issues/95
175function graticule10() {
176 var epsilon = 1e-6,
177 x1 = 180, x0 = -x1, y1 = 80, y0 = -y1, dx = 10, dy = 10,
178 X1 = 180, X0 = -X1, Y1 = 90, Y0 = -Y1, DX = 90, DY = 360,
179 x = graticuleX(y0, y1, 2.5), y = graticuleY(x0, x1, 2.5),
180 X = graticuleX(Y0, Y1, 2.5), Y = graticuleY(X0, X1, 2.5);
181
182 function graticuleX(y0, y1, dy) {
183 var y = d3.range(y0, y1 - epsilon, dy).concat(y1);
184 return function(x) { return y.map(function(y) { return [x, y]; }); };
185 }
186
187 function graticuleY(x0, x1, dx) {
188 var x = d3.range(x0, x1 - epsilon, dx).concat(x1);
189 return function(y) { return x.map(function(x) { return [x, y]; }); };
190 }
191
192 return {
193 type: "MultiLineString",
194 coordinates: d3.range(Math.ceil(X0 / DX) * DX, X1, DX).map(X)
195 .concat(d3.range(Math.ceil(Y0 / DY) * DY, Y1, DY).map(Y))
196 .concat(d3.range(Math.ceil(x0 / dx) * dx, x1, dx).filter(function(x) { return Math.abs(x % DX) > epsilon; }).map(x))
197 .concat(d3.range(Math.ceil(y0 / dy) * dy, y1 + epsilon, dy).filter(function(y) { return Math.abs(y % DY) > epsilon; }).map(y))
198 };
199}
200```
201
202---
203
204[Back to Home](:@Home)
205