-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestObjects.js
263 lines (225 loc) · 6.94 KB
/
TestObjects.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
/*jshint esversion: 11 */
// @ts-check
/**
* CS559 3D World Framework Code
*
* Test Objects - these examples are for use in testing the framework
* and are less generally useful
*
* @module TestObjects
*/
// we need to have the BaseClass definition
import { GrObject } from "./GrObject.js";
// a global variable to keep track of how many objects we create
// this allows us to give unique names
let testobjsctr = 0;
import * as T from "../CS559-Three/build/three.module.js";
function degreesToRadians(deg) {
return (deg * Math.PI) / 180;
}
/**
* A simple object that is like a dump truck (with a hinge), but just made of
* boxes.
* A simple way to test a parametric object
*
* It's also a simple example of a hierarchical object
*/
export class HingeCube extends GrObject {
constructor() {
const group = new T.Group();
const geometry = new T.BoxGeometry(1, 0.5, 1);
const mesh1 = new T.Mesh(
geometry,
new T.MeshStandardMaterial({ color: 0xa0a000 })
);
mesh1.position.y = 0.25;
const mesh2 = new T.Mesh(
geometry,
new T.MeshStandardMaterial({ color: 0xffff00 })
);
mesh2.position.y = 0.25;
mesh2.position.z = 0.5;
// set group with origin at pivot point
group.add(mesh1);
const g2 = new T.Group();
g2.position.set(0, 0.5, -0.5);
g2.add(mesh2);
group.add(g2);
super(`HingeCube-${testobjsctr++}`, group, [
["x", -5, 5, 2],
["z", -5, 5, 2],
["theta", -180, 180, 0],
["tilt", 0, 90, 0]
]);
this.group = group;
this.mesh1 = mesh1;
this.mesh2 = mesh2;
this.g2 = g2;
}
update(paramValues) {
this.group.position.x = paramValues[0];
this.group.position.z = paramValues[1];
this.group.rotation.y = degreesToRadians(paramValues[2]);
this.g2.rotation.x = degreesToRadians(-paramValues[3]);
}
}
// for faking deferred loading
// from https://flaviocopes.com/javascript-sleep/
const sleep = milliseconds => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
/**
* test for an object that is created slowly (like loading an OBJ)
*
* the catch is that we need to have an object to install in the world
* (since we can't defer that), but we don't have "the" object
*
* the trick: make a Group - when the deferred object finally arrives,
* stick it in the group
*
* here, we fake OBJ loading with sleep
*/
export class DelayTest extends GrObject {
constructor() {
const group = new T.Group();
super("Delay-Test", group);
this.group = group;
// use sleep, rather than OBJ loader
sleep(1500).then(function() {
group.add(
new T.Mesh(
new T.TorusKnotGeometry(),
new T.MeshStandardMaterial({ color: "red" })
)
);
});
}
}
/**
* Better delayed object - put a proxy object in its place, and then remove it
*/
export class BetterDelayTest extends GrObject {
constructor() {
const group = new T.Group();
super("Delay-Test", group);
this.group = group;
// make a cube that will be there temporarily
const tempCube = new T.Mesh(
new T.BoxGeometry(),
new T.MeshStandardMaterial()
);
group.add(tempCube);
// use sleep, rather than OBJ loader
sleep(2000).then(function() {
group.remove(tempCube);
group.add(
new T.Mesh(
new T.TorusKnotGeometry(),
new T.MeshStandardMaterial({ color: "purple" })
)
);
});
}
}
/**
* test for changing an object's material after some delay
*/
export class MaterialDelayTest extends GrObject {
constructor() {
const group = new T.Group();
super("Delay-Test", group);
this.material = new T.MeshStandardMaterial({ color: "white" });
this.geometry = new T.TorusGeometry();
this.mesh = new T.Mesh(this.geometry, this.material);
group.add(this.mesh);
group.position.x = -3;
const self = this;
// use sleep, rather than OBJ loader
sleep(1000).then(function() {
// note: we can't use "this" because this isn't lexically scoped
self.material.setValues({ color: "red" });
self.material.needsUpdate = true;
});
}
}
export class CheckSign extends GrObject {
/**
*
* @param {Object} props
* @param {number} [props.checks=4] - number of squares per side
* @param {string} [props.colortype="vertex"] - vertex,face,none
* @param {number} [props.x]
* @param {number} [props.y]
* @param {number} [props.z]
* @param {number} [props.scale=1]
* @param {THREE.Color | string | Number} [props.materialcolor]
*/
constructor(props = {}) {
const group = new T.Group();
super("CheckSign1", group);
// let geometry = new T.Geometry();
const geometry = new T.BufferGeometry();
const nchecks = props.checks ?? 4;
const nverts = nchecks + 1;
const scale = props.scale > 0.0001 ? props.scale : 1; // disallow 0
let colortype;
switch (props.colortype && props.colortype[0]) {
case "v":
colortype = T.VertexColors;
break;
case "f":
colortype = T.FaceColors;
break;
case "n":
colortype = T.NoColors;
break;
default:
console.log(`no or bad colortype - assuming vertex`);
colortype = T.VertexColors;
}
const vertexIndex = []
for (let i = 0; i < nverts + 1; i++) {
for (let j = 0; j < nverts; j++) {
vertexIndex.push([i, j, 0]);
}
}
const vertices = []
const colors = []
for (let i = 0; i < nchecks; i++) {
for (let j = 0; j < nchecks; j++) {
vertices.push(...vertexIndex[i * nverts + j])
vertices.push(...vertexIndex[i * nverts + j + 1])
vertices.push(...vertexIndex[(i + 1) * nverts + j])
vertices.push(...vertexIndex[i * nverts + j + 1])
vertices.push(...vertexIndex[(i + 1) * nverts + j + 1])
vertices.push(...vertexIndex[(i + 1) * nverts + j])
const faceColor1 = (new T.Color('red')).toArray()
colors.push(...faceColor1);
colors.push(...faceColor1);
colors.push(...faceColor1);
colors.push(1, 0, 0);
colors.push(1, 1, 1);
colors.push(0, 0, 1);
}
}
geometry.setAttribute('position', new T.BufferAttribute(Float32Array.from(vertices), 3))
geometry.setAttribute('color', new T.BufferAttribute(Float32Array.from(colors), 3))
geometry.computeVertexNormals();
const materialProps = {
side: T.DoubleSide,
vertexColors: colortype
};
if (props.materialcolor) materialProps["color"] = props.materialcolor;
const material = new T.MeshStandardMaterial(materialProps);
const mesh = new T.Mesh(geometry, material);
// center at 0,0
mesh.scale.set(scale, scale, scale);
// warning - scale does not affect translation!
mesh.translateX(scale * (-nchecks / 2));
mesh.translateY(scale * (-nchecks / 2));
group.add(mesh);
group.position.x = Number(props.x) || 0;
group.position.y = Number(props.y) || 0;
group.position.z = Number(props.z) || 0;
}
}