-
Notifications
You must be signed in to change notification settings - Fork 182
/
Convex_Polyhedra.js
315 lines (249 loc) · 11.9 KB
/
Convex_Polyhedra.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// scene/demo-specific variables go here
let skewMatrix = new THREE.Matrix4();
let uniformScale = 1;
let transform_Folder;
let position_Folder;
let scale_Folder;
let skew_Folder;
let rotation_Folder;
let transform_PositionXController, transform_PositionXObject;
let transform_PositionYController, transform_PositionYObject;
let transform_PositionZController, transform_PositionZObject;
let transform_ScaleUniformController, transform_ScaleUniformObject;
let transform_ScaleXController, transform_ScaleXObject;
let transform_ScaleYController, transform_ScaleYObject;
let transform_ScaleZController, transform_ScaleZObject;
let transform_RotationXController, transform_RotationXObject;
let transform_RotationYController, transform_RotationYObject;
let transform_RotationZController, transform_RotationZObject;
let transform_SkewX_YController, transform_SkewX_YObject;
let transform_SkewX_ZController, transform_SkewX_ZObject;
let transform_SkewY_XController, transform_SkewY_XObject;
let transform_SkewY_ZController, transform_SkewY_ZObject;
let transform_SkewZ_XController, transform_SkewZ_XObject;
let transform_SkewZ_YController, transform_SkewZ_YObject;
let needChangePosition = false;
let needChangeScaleUniform = false;
let needChangeScale = false;
let needChangeSkew = false;
let needChangeRotation = false;
let material_TypeObject, material_TypeController;
let material_ColorObject, material_ColorController;
let material_RoughnessObject, material_RoughnessController;
let needChangeMaterialType = false;
let needChangeMaterialColor = false;
let needChangeMaterialRoughness = false;
let matType = 0;
let matColor;
let wallRadius = 50;
let shapeRadius = 10;
let convexPolyhedron;
// called automatically from within initTHREEjs() function (located in InitCommon.js file)
function initSceneData()
{
demoFragmentShaderFileName = 'Convex_Polyhedra_Fragment.glsl';
// scene/demo-specific three.js objects setup goes here
sceneIsDynamic = false;
cameraFlightSpeed = 100;
// pixelRatio is resolution - range: 0.5(half resolution) to 1.0(full resolution)
pixelRatio = mouseControl ? 0.75 : 0.75;
EPS_intersect = 0.01;
// set camera's field of view
worldCamera.fov = 50;
focusDistance = 120.0;
apertureChangeSpeed = 5;
// position and orient camera
cameraControlsObject.position.set(-32, 12, 116);
// look right
cameraControlsYawObject.rotation.y = -0.2436;
// look downward
cameraControlsPitchObject.rotation.x = -0.29;
// CONVEX POLYHEDRA
convexPolyhedron = new THREE.Object3D();
convexPolyhedron.position.set(25, -wallRadius + shapeRadius + 5, 0);
convexPolyhedron.scale.set(16, 16, 16);
pathTracingScene.add(convexPolyhedron);
// In addition to the default GUI on all demos, add any special GUI elements that this particular demo requires
transform_PositionXObject = { positionX: 0 };
transform_PositionYObject = { positionY: -wallRadius + shapeRadius + 5 };
transform_PositionZObject = { positionZ: 0 };
transform_ScaleUniformObject = { uniformScale: shapeRadius };
transform_ScaleXObject = { scaleX: shapeRadius };
transform_ScaleYObject = { scaleY: shapeRadius };
transform_ScaleZObject = { scaleZ: shapeRadius };
transform_RotationXObject = { rotationX: 0 };
transform_RotationYObject = { rotationY: 0 };
transform_RotationZObject = { rotationZ: 0 };
transform_SkewX_YObject = { skewX_Y: 0 };
transform_SkewX_ZObject = { skewX_Z: 0 };
transform_SkewY_XObject = { skewY_X: 0 };
transform_SkewY_ZObject = { skewY_Z: 0 };
transform_SkewZ_XObject = { skewZ_X: 0 };
transform_SkewZ_YObject = { skewZ_Y: 0 };
material_TypeObject = { Material_Preset: 'ClearCoat Diffuse' };
material_ColorObject = { Material_Color: [0, 1, 1] };
material_RoughnessObject = { Material_Roughness: 0.0 };
function handlePositionChange() { needChangePosition = true; }
function handleScaleUniformChange() { needChangeScaleUniform = true; }
function handleScaleChange() { needChangeScale = true; }
function handleRotationChange() { needChangeRotation = true; }
function handleSkewChange() { needChangeSkew = true; }
function handleMaterialTypeChange() { needChangeMaterialType = true; }
function handleMaterialColorChange() { needChangeMaterialColor = true; }
function handleMaterialRoughnessChange() { needChangeMaterialRoughness = true; }
material_TypeController = gui.add(material_TypeObject, 'Material_Preset', ['Diffuse', 'ClearCoat Diffuse', 'Transparent Refractive',
'Copper Metal', 'Aluminum Metal', 'Gold Metal']).onChange(handleMaterialTypeChange);
material_ColorController = gui.addColor(material_ColorObject, 'Material_Color').onChange(handleMaterialColorChange);
material_RoughnessController = gui.add(material_RoughnessObject, 'Material_Roughness', 0.0, 1.0, 0.01).onChange(handleMaterialRoughnessChange);
transform_Folder = gui.addFolder('All Objects Transform');
scale_Folder = transform_Folder.addFolder('Scale').close();
transform_ScaleUniformController = scale_Folder.add(transform_ScaleUniformObject, 'uniformScale', 1, 50, 1).onChange(handleScaleUniformChange);
transform_ScaleXController = scale_Folder.add(transform_ScaleXObject, 'scaleX', 1, 50, 1).onChange(handleScaleChange);
transform_ScaleYController = scale_Folder.add(transform_ScaleYObject, 'scaleY', 1, 50, 1).onChange(handleScaleChange);
transform_ScaleZController = scale_Folder.add(transform_ScaleZObject, 'scaleZ', 1, 50, 1).onChange(handleScaleChange);
rotation_Folder = transform_Folder.addFolder('Rotation').close();
transform_RotationXController = rotation_Folder.add(transform_RotationXObject, 'rotationX', 0, 359, 1).onChange(handleRotationChange);
transform_RotationYController = rotation_Folder.add(transform_RotationYObject, 'rotationY', 0, 359, 1).onChange(handleRotationChange);
transform_RotationZController = rotation_Folder.add(transform_RotationZObject, 'rotationZ', 0, 359, 1).onChange(handleRotationChange);
position_Folder = transform_Folder.addFolder('Position').close();
transform_PositionXController = position_Folder.add(transform_PositionXObject, 'positionX', -50, 50, 1).onChange(handlePositionChange);
transform_PositionYController = position_Folder.add(transform_PositionYObject, 'positionY', -50, 50, 1).onChange(handlePositionChange);
transform_PositionZController = position_Folder.add(transform_PositionZObject, 'positionZ', -50, 50, 1).onChange(handlePositionChange);
skew_Folder = transform_Folder.addFolder('Skew').close();
transform_SkewX_YController = skew_Folder.add(transform_SkewX_YObject, 'skewX_Y', -0.9, 0.9, 0.1).onChange(handleSkewChange);
transform_SkewX_ZController = skew_Folder.add(transform_SkewX_ZObject, 'skewX_Z', -0.9, 0.9, 0.1).onChange(handleSkewChange);
transform_SkewY_XController = skew_Folder.add(transform_SkewY_XObject, 'skewY_X', -0.9, 0.9, 0.1).onChange(handleSkewChange);
transform_SkewY_ZController = skew_Folder.add(transform_SkewY_ZObject, 'skewY_Z', -0.9, 0.9, 0.1).onChange(handleSkewChange);
transform_SkewZ_XController = skew_Folder.add(transform_SkewZ_XObject, 'skewZ_X', -0.9, 0.9, 0.1).onChange(handleSkewChange);
transform_SkewZ_YController = skew_Folder.add(transform_SkewZ_YObject, 'skewZ_Y', -0.9, 0.9, 0.1).onChange(handleSkewChange);
// jumpstart some of the gui controller-change handlers so that the pathtracing fragment shader uniforms are correct and up-to-date
handlePositionChange();
handleScaleUniformChange();
handleScaleChange();
handleRotationChange();
handleSkewChange();
handleMaterialTypeChange();
handleMaterialColorChange();
handleMaterialRoughnessChange();
// scene/demo-specific uniforms go here
pathTracingUniforms.uConvexPolyhedronInvMatrix = { value: new THREE.Matrix4() };
pathTracingUniforms.uMaterialType = { value: 4 };
pathTracingUniforms.uMaterialColor = { value: new THREE.Color(0.0, 1.0, 1.0) };
pathTracingUniforms.uRoughness = { value: 0.0 };
} // end function initSceneData()
let firstTimeFlag = true;
// called automatically from within the animate() function (located in InitCommon.js file)
function updateVariablesAndUniforms()
{
if (firstTimeFlag)
{
cameraIsMoving = true;
firstTimeFlag = false;
}
if (needChangePosition)
{
convexPolyhedron.position.set(transform_PositionXController.getValue(),
transform_PositionYController.getValue(),
transform_PositionZController.getValue());
cameraIsMoving = true;
needChangePosition = false;
}
if (needChangeScaleUniform)
{
uniformScale = transform_ScaleUniformController.getValue();
// convexPolyhedron.scale.set(uniformScale, uniformScale, uniformScale);
transform_ScaleXController.setValue(uniformScale);
transform_ScaleYController.setValue(uniformScale);
transform_ScaleZController.setValue(uniformScale);
cameraIsMoving = true;
needChangeScaleUniform = false;
}
if (needChangeScale)
{
convexPolyhedron.scale.set(transform_ScaleXController.getValue(),
transform_ScaleYController.getValue(),
transform_ScaleZController.getValue());
cameraIsMoving = true;
needChangeScale = false;
}
if (needChangeRotation)
{
convexPolyhedron.rotation.set(THREE.MathUtils.degToRad(transform_RotationXController.getValue()),
THREE.MathUtils.degToRad(transform_RotationYController.getValue()),
THREE.MathUtils.degToRad(transform_RotationZController.getValue()));
cameraIsMoving = true;
needChangeRotation = false;
}
if (needChangeSkew)
{
skewMatrix.set(
1, transform_SkewX_YController.getValue(), transform_SkewX_ZController.getValue(), 0,
transform_SkewY_XController.getValue(), 1, transform_SkewY_ZController.getValue(), 0,
transform_SkewZ_XController.getValue(), transform_SkewZ_YController.getValue(), 1, 0,
0, 0, 0, 1
);
cameraIsMoving = true;
needChangeSkew = false;
}
if (needChangeMaterialType)
{
matType = material_TypeController.getValue();
if (matType == 'Diffuse')
{
pathTracingUniforms.uMaterialType.value = 1;
pathTracingUniforms.uMaterialColor.value.setRGB(0.0, 0.8, 1.0);
}
else if (matType == 'ClearCoat Diffuse')
{
pathTracingUniforms.uMaterialType.value = 4;
pathTracingUniforms.uMaterialColor.value.setRGB(0.35, 0.0, 1.0);
}
else if (matType == 'Transparent Refractive')
{
pathTracingUniforms.uMaterialType.value = 2;
pathTracingUniforms.uMaterialColor.value.setRGB(1.0, 0.1, 0.0);
}
else if (matType == 'Copper Metal')
{
pathTracingUniforms.uMaterialType.value = 3;
pathTracingUniforms.uMaterialColor.value.setRGB(0.955008, 0.637427, 0.538163);
}
else if (matType == 'Aluminum Metal')
{
pathTracingUniforms.uMaterialType.value = 3;
pathTracingUniforms.uMaterialColor.value.setRGB(0.913183, 0.921494, 0.924524);
}
else if (matType == 'Gold Metal')
{
pathTracingUniforms.uMaterialType.value = 3;
pathTracingUniforms.uMaterialColor.value.setRGB(1.000000, 0.765557, 0.336057);
}
material_ColorController.setValue([ pathTracingUniforms.uMaterialColor.value.r,
pathTracingUniforms.uMaterialColor.value.g,
pathTracingUniforms.uMaterialColor.value.b ]);
cameraIsMoving = true;
needChangeMaterialType = false;
}
if (needChangeMaterialColor)
{
matColor = material_ColorController.getValue();
pathTracingUniforms.uMaterialColor.value.setRGB( matColor[0], matColor[1], matColor[2] );
cameraIsMoving = true;
needChangeMaterialColor = false;
}
if (needChangeMaterialRoughness)
{
pathTracingUniforms.uRoughness.value = material_RoughnessController.getValue();
cameraIsMoving = true;
needChangeMaterialRoughness = false;
}
if (cameraIsMoving)
{
convexPolyhedron.updateMatrixWorld(true); // need to update matrix because we manually multiply it by skewMatrix on the next line
convexPolyhedron.matrixWorld.multiply(skewMatrix); // multiply polyhedron's matrix by my custom skew(shear) matrix4
pathTracingUniforms.uConvexPolyhedronInvMatrix.value.copy(convexPolyhedron.matrixWorld).invert();
}
// INFO
cameraInfoElement.innerHTML = "FOV: " + worldCamera.fov + " / Aperture: " + apertureSize.toFixed(2) + " / FocusDistance: " + focusDistance + "<br>" + "Samples: " + sampleCounter;
} // end function updateVariablesAndUniforms()
init(); // init app and start animating