-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathearth-sphere.js
92 lines (74 loc) · 2.37 KB
/
earth-sphere.js
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
// Ensure ThreeJS is in global scope for the 'examples/'
global.THREE = require("three");
// Include any additional ThreeJS examples below
require("three/examples/js/controls/OrbitControls");
const canvasSketch = require("canvas-sketch");
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: "webgl"
};
const sketch = ({ context }) => {
// Create a renderer
const renderer = new THREE.WebGLRenderer({
canvas: context.canvas
});
// WebGL background color
renderer.setClearColor("#000", 1);
// Setup a camera
const camera = new THREE.PerspectiveCamera(50, 1, 0.01, 100);
camera.position.set(4, 4, -4);
camera.lookAt(new THREE.Vector3());
// Setup camera controller
const controls = new THREE.OrbitControls(camera, context.canvas);
// Setup your scene
const scene = new THREE.Scene();
// Setup a geometry
const geometry = new THREE.SphereGeometry(1, 32, 16);
// Setup a loader for textures
const loader = new THREE.TextureLoader();
const earthMap = loader.load("src/demos/earth.jpg");
const moonMap = loader.load("src/demos/moon.jpg");
// Setup a material
const material = new THREE.MeshBasicMaterial({
map: earthMap
});
// Setup a mesh with geometry + material
const earth = new THREE.Mesh(geometry, material);
scene.add(earth);
const moonMaterial = new THREE.MeshBasicMaterial({
map: moonMap
});
const moonAnchor = new THREE.Group();
const moon = new THREE.Mesh(geometry, moonMaterial);
moonAnchor.add(moon);
moon.position.x = -2;
moon.position.y = 0.5;
moon.scale.setScalar(0.25);
scene.add(moonAnchor);
// draw each frame
return {
// Handle resize events here
resize({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight, false);
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
},
// Update & render your scene here
render({ time, deltaTime }) {
earth.rotation.y = time * 0.15;
moon.rotation.y = time * 0.1;
moonAnchor.rotation.y = time * 0.2;
controls.update();
renderer.render(scene, camera);
},
// Dispose of events & renderer for cleaner hot-reloading
unload() {
controls.dispose();
renderer.dispose();
}
};
};
canvasSketch(sketch, settings);