-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
/
Copy paththreejs-lights.js
142 lines (101 loc) · 3.1 KB
/
threejs-lights.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as THREE from 'three';
import { OrbitControls } from '../../examples/jsm/controls/OrbitControls.js';
import { threejsLessonUtils } from './threejs-lesson-utils.js';
{
function makeCheckerTexture( repeats ) {
const data = new Uint8Array( [
0x88, 0x88, 0x88, 0xFF, 0xCC, 0xCC, 0xCC, 0xFF,
0xCC, 0xCC, 0xCC, 0xFF, 0x88, 0x88, 0x88, 0xFF
] );
const width = 2;
const height = 2;
const texture = new THREE.DataTexture( data, width, height );
texture.needsUpdate = true;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( repeats / 2, repeats / 2 );
return texture;
}
const makeScene = function () {
const cubeSize = 4;
const cubeGeo = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
const cubeMat = new THREE.MeshPhongMaterial( { color: '#8AC' } );
const sphereRadius = 3;
const sphereWidthDivisions = 32;
const sphereHeightDivisions = 16;
const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
const sphereMat = new THREE.MeshPhongMaterial( { color: '#CA8' } );
const planeSize = 40;
const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
const planeMat = new THREE.MeshPhongMaterial( {
map: makeCheckerTexture( planeSize ),
side: THREE.DoubleSide,
} );
return function ( renderInfo ) {
const { scene, camera, elem } = renderInfo;
const controls = new OrbitControls( camera, elem );
controls.enableDamping = true;
controls.enablePanning = false;
scene.background = new THREE.Color( 'black' );
{
const mesh = new THREE.Mesh( cubeGeo, cubeMat );
mesh.position.set( cubeSize + 1, cubeSize / 2, - cubeSize - 1 );
scene.add( mesh );
}
{
const mesh = new THREE.Mesh( sphereGeo, sphereMat );
mesh.position.set( - sphereRadius - 1, sphereRadius + 2, - sphereRadius + 1 );
scene.add( mesh );
}
{
const mesh = new THREE.Mesh( planeGeo, planeMat );
mesh.rotation.x = Math.PI * - .5;
scene.add( mesh );
}
return {
trackball: false,
lights: false,
update() {
controls.update();
},
};
};
}();
threejsLessonUtils.addDiagrams( {
directionalOnly: {
create( props ) {
const { scene, renderInfo } = props;
const result = makeScene( renderInfo );
{
const light = new THREE.DirectionalLight( 0xFFFFFF, 1 );
light.position.set( 5, 10, 0 );
scene.add( light );
}
{
const light = new THREE.AmbientLight( 0xFFFFFF, .6 );
scene.add( light );
}
return result;
},
},
directionalPlusHemisphere: {
create( props ) {
const { scene, renderInfo } = props;
const result = makeScene( renderInfo );
{
const light = new THREE.DirectionalLight( 0xFFFFFF, 1 );
light.position.set( 5, 10, 0 );
scene.add( light );
}
{
const skyColor = 0xB1E1FF; // light blue
const groundColor = 0xB97A20; // brownish orange
const intensity = .6;
const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
scene.add( light );
}
return result;
},
},
} );
}