-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
228 lines (197 loc) · 7.61 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
function init() {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
100,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.x = 1;
camera.position.y = 10;
camera.position.z = 10;
camera.lookAt(new THREE.Vector3(0, 0, 0));
//#region Create Objects
//###############################################//
//-----------------Create Objects-----------------//
//###############################################//
let Light = getSpotLight(1, 'rgb(255, 255, 255)');//Change between(Point - Spot - Directional)
Light.position.set(-7.5, 10, 10);
let sphereLight = getSphere(0.1, 32, getMaterial('basic', 'rgb(250, 250, 0)'));
let sphereMaterial = getMaterial('standard', 'rgb(100, 0, 150)');
let sphere = getSphere(2, 32, sphereMaterial);
let planeMaterial = getMaterial('standard', 'rgb(128, 128, 128)');
let plane = getPlane(20, planeMaterial);
let boxGrid = getBoxGrid(10, 2);
//#endregion
//#region Add to Scene
//###############################################//
//-----------------Add to Scene-----------------//
//###############################################//
scene.add(camera);
//scene.add(box);
sphereLight.add(Light);
scene.add(sphereLight);
scene.add(plane);
//scene.add(boxGrid);
scene.add(sphere);
//#endregion
//#region GUI
//###############################################//
//-----------------GUI-----------------------------//
//###############################################//
const gui = new dat.GUI();
lightFolder = gui.addFolder('Light Controls');
gui.add(Light.position, 'x', -10, 10);
gui.add(Light.position, 'y', -10, 10);
gui.add(Light.position, 'z', -10, 10);
gui.add(Light, 'intensity', 0.1, 2);
const cameraPosition = gui.addFolder('Camera Position');
cameraPosition.add(camera.position, 'x', -10, 10);
cameraPosition.add(camera.position, 'y', -10, 10);
cameraPosition.add(camera.position, 'z', -10, 10);
//#endregion
//#region Create Objects
//###############################################//
//-----------------Cube Map-----------------//
//###############################################//
var path = './assets/cubemab/';
var format = '.jpg';
var urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
var reflectionCube = new THREE.CubeTextureLoader().load( urls );
reflectionCube.format = THREE.RGBFormat;
scene.background = reflectionCube;
var loader = new THREE.TextureLoader();
planeMaterial.map = loader.load('./assets/textures/concrete.jpg');
planeMaterial.bumpMap = loader.load('./assets/textures/checkerboard.jpg');
planeMaterial.roughnessMap = loader.load('./assets/textures/scratch.jpg');
planeMaterial.bumpScale = 0.01;
planeMaterial.metalness = 0.1;
planeMaterial.roughness = 0.7;
planeMaterial.envMap = reflectionCube;
sphereMaterial.roughnessMap = loader.load('./assets/textures/fingerprints.jpg');
sphereMaterial.envMap = reflectionCube;
var maps = [
'map',
'bumpMap',
'roughnessMap'
];
maps.forEach(function(mapName) {
var texture = planeMaterial[mapName];
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1, 1);
});
//#endregion
//#region Renderer
//Renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(color = 'rgb(40, 159, 167)');
renderer.shadowMap.enabled = true;
//Use OrbitControls library.
const controls = new THREE.OrbitControls(camera, renderer.domElement);
//Append the scene to the div element.
document.getElementById("container").appendChild(renderer.domElement);
//Update for each frame.
update(renderer, scene, camera, controls);
function update(renderer, scene, camera, controls) {
renderer.render(scene, camera);
controls.update();
requestAnimationFrame(function() {
update(renderer, scene, camera, controls);
});
}
//#endregion
}
init();
//###############################################//
//-----------------Custom functions-----------------//
//###############################################//
//Geometry functions
function getBox(w, h, d, material = getMaterial()) {
const geometry = new THREE.BoxGeometry(w, h, d);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y = h / 2;
mesh.castShadow = true; // Shadows
return mesh;
}
//Create sphere
function getSphere(radius = 1, Segments = 32, material = getMaterial()) {
const geometry = new THREE.SphereGeometry(radius, Segments, Segments);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(0, radius, 0);
mesh.castShadow = true;
return mesh;
}
//Material functions
function getMaterial(type = 'basic', color = 'rgb(128, 0, 0)') {
let materialOptions = {
color: color,
side: THREE.DoubleSide,
};
switch(type) {
case 'basic':
return new THREE.MeshBasicMaterial(materialOptions);
break;
case 'lambert':
return new THREE.MeshLambertMaterial(materialOptions);
break;
case 'phong':
return new THREE.MeshPhongMaterial(materialOptions);
break;
case 'standard':
return new THREE.MeshStandardMaterial(materialOptions);
break;
default:
return new THREE.MeshBasicMaterial(materialOptions);
break;
}
}
//Create a plane
function getPlane(length, material = getMaterial()) {
const geometry = new THREE.PlaneGeometry(length, length);
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -Math.PI / 2;
mesh.receiveShadow = true; // Shadows
return mesh;
}
//Box Grid
function getBoxGrid(count, spacing) {
const group = new THREE.Group();
for(let i = 0; i < count; i++) {
for(let j = 0; j < count; j++) {
let box = getBox(1, 1, 1, getMaterial('standard', 'rgb(200, 128, 128)'));
box.position.x = i * spacing;
box.position.z = j * spacing;
group.add(box);
}
}
group.position.x = -(count - 1) * (spacing / 2);
group.position.z = -(count - 1) * (spacing / 2);
return group;
}
//#region Light Functions
//Point Light
function getPointLight(intensity, color = 'rgb(255, 255, 255)') {
let light = new THREE.PointLight(color, intensity);
light.castShadow = true;
return light;
}
//Spot Light
function getSpotLight(intensity, color = 'rgb(255, 255, 255)') {
let light = new THREE.SpotLight(color, intensity);
light.castShadow = true;
// light.shadow.mapSize.width = 2048;
return light;
}
//Directional Light
function getDirectionalLight(intensity, color = 'rgb(255, 255, 255)') {
let light = new THREE.DirectionalLight(color, intensity);
light.castShadow = true;
return light;
}
//#endregion