-
Notifications
You must be signed in to change notification settings - Fork 4
/
NodeList.ts
455 lines (373 loc) · 10.9 KB
/
NodeList.ts
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// solufa element class
import * as THREE from "three";
import BaseNode from "./BaseNode";
import createMaterial from "./createMaterial";
import createGeometry from "./createGeometry";
import errorMessage from "../utils/errorMessage";
import { setCoreObject as setObject } from "./adminCoreObject";
import RdrNode from "./RdrNode";
import VpNode from "./VpNode";
// 以下二つは廃止予定
import physics from "./physics";
import getAsset from "./getAsset";
const tmpVec = new THREE.Vector3; // for translate
class GomlNode extends BaseNode {
private _coreObject;
private _lightHelper;
private _cameraHelper;
// coreObject = three.js object
get coreObject() {
return this._coreObject;
}
set coreObject( object ) {
this._coreObject = object;
setObject( this );
}
public setAttrHook( name, value ) {
switch ( name ) {
case "display":
this.coreObject.visible = value !== false;
break;
case "rotateOrder":
this.coreObject.rotation.order = value;
break;
case "renderOrder":
this.coreObject.renderOrder = value;
break;
case "castShadow":
case "receiveShadow":
this.coreObject[ name ] = !!value;
break;
}
}
public getAttrHook( name ) {
switch ( name ) {
case "display":
return this.coreObject.visible;
case "rotateOrder":
return this.coreObject.rotation.order;
case "renderOrder":
return this.coreObject.renderOrder;
case "castShadow":
case "receiveShadow":
return this.coreObject[ name ];
default:
return super.getAttrHook( name );
}
}
public getScene() {
let scene = this;
while ( scene.tagName !== "scene" ) {
scene = scene.parentNode;
}
return scene;
}
public appendHook( childNode ) {// m.redrawとかでMithrilがtextNodeを生成することがある
if ( childNode.coreObject instanceof THREE.Object3D ) {
this.coreObject.add( childNode.coreObject );
}
}
public removeHook( childNode ) {
if ( childNode.coreObject instanceof THREE.Object3D ) {
this.coreObject.remove( childNode.coreObject );
}
}
public setHelper( type, object ) {
if ( object ) {
const helper = new THREE[ type + "Helper" ]( object );
this.coreObject.add( helper );
if ( type === "Camera" ) {
this._cameraHelper = helper;
} else {
this._lightHelper = helper;
}
} else {
let helper;
if ( type === "Camera" ) {
helper = this._cameraHelper;
delete this._cameraHelper;
} else {
helper = this._lightHelper;
delete this._lightHelper;
}
this.coreObject.remove( helper );
}
}
// translate系は現在のpositionとrotateが基準なのでstyleで表現できない
public translate( x, y, z ) {
tmpVec.set( x, y, z );
let length = tmpVec.length();
this.coreObject.translateOnAxis( tmpVec.normalize(), length );
}
public translateX( distance ) {
this.coreObject.translateX( distance );
}
public translateY( distance ) {
this.coreObject.translateY( distance );
}
public translateZ( distance ) {
this.coreObject.translateZ( distance );
}
}
// three.js lightクラスの省略名を保持
let lightType = {};
for ( let key in THREE ) {
if ( /.+?Light$/.test( key ) ) {
lightType[ key.slice( 0, 3 ) ] = key;
}
}
export default {
// 仕様が不安定なので廃止予定
asset: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
if ( name === "src" ) {
if ( !/\//.test( value ) ) {
errorMessage( "src attribute of asset must be full, absolute or relative path." );
} else {
getAsset( this, value );
}
}
}
constructor( gomlDoc ) {
super( "asset", gomlDoc );
this.coreObject = new THREE.Object3D;
}
},
body: class extends BaseNode {
constructor( gomlDoc ) {
super( "body", gomlDoc );
}
},
cam: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
if ( /^(fov|near|far)$/.test( name ) ) {
this.coreObject[ name ] = value;
this.coreObject.updateProjectionMatrix();
}
}
constructor( gomlDoc ) {
super( "cam", gomlDoc );
this.coreObject = new THREE.PerspectiveCamera;
}
},
head: class extends BaseNode {
constructor( gomlDoc ) {
super( "head", gomlDoc );
}
},
light: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
switch ( name ) {
case "init":
if ( this.coreObject ) { break; }
let param = value.value || [];
this.coreObject = new THREE[ lightType[ value.type ] ]( param[ 0 ], param[ 1 ], param[ 2 ], param[ 3 ], param[ 4 ], param[ 5 ] );
break;
case "helper":
if ( !this.coreObject ) { break; }
this.setHelper( lightType[ this.getAttribute( "init" ).type ], value && this.coreObject );
if ( this.getAttribute( "castShadow" ) ) {
this.setHelper( "Camera", value && this.coreObject.shadow.camera );
}
break;
case "castShadow":
if ( typeof value === "object" ) {
const shadow = this.coreObject.shadow;
for ( let key in value ) {
if ( key === "mapSize" ) {
shadow.mapSize.width =
shadow.mapSize.height = value[ key ];
} else if ( key === "bias" ) {
shadow.bias = value[ key ];
} else {
shadow.camera[ key ] = value[ key ];
}
}
}
break;
}
}
constructor( gomlDoc ) {
super( "light", gomlDoc );
}
},
line: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
switch ( name ) {
case "geo":
this.coreObject.geometry = createGeometry( value );
break;
case "mtl":
this.coreObject.material = createMaterial( value );
break;
}
}
constructor( gomlDoc ) {
super( "line", gomlDoc );
this.coreObject = new THREE.Line;
}
},
mesh: class extends GomlNode {
private _physicsStyleNeedsUpdate = false;
get physicsStyleNeedsUpdate() {
return this._physicsStyleNeedsUpdate;
}
set physicsStyleNeedsUpdate( bool ) {
this._physicsStyleNeedsUpdate = bool;
if ( bool ) { physics.styleUpdate( this ); }
}
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
switch ( name ) {
case "geo":
this.coreObject.geometry = createGeometry( value );
break;
case "mtl":
this.coreObject.material = createMaterial( value );
break;
}
}
public appendedHook() {
if ( this.getAttribute( "physics" ) ) {
physics.addElement( this, this.getAttribute( "physics" ) );
}
}
public removedHook() {
if ( this.getAttribute( "physics" ) ) {
physics.removeElement( this );
}
}
constructor( gomlDoc ) {
super( "mesh", gomlDoc );
this.coreObject = new THREE.Mesh;
}
},
// あまり覚えてないけどモデルローダーで使う予定だったはず。asset elementとともに廃止
meshes: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
let n = 0;
switch ( name ) {
case "geos":
let mtls = this.getAttribute( "mtls" );
if ( !Array.isArray( value ) || !mtls ) {
return;
}
while ( n < value.length ) {
this.coreObject.add( new THREE.Mesh( createGeometry( value[ n ] ), createMaterial( mtls[ n ] ) ) );
n = n + 1;
}
break;
case "mtls":
let geos = this.getAttribute( "geos" );
while ( n < value.length ) {
createMaterial( value[ n ] ); // txrをロードしておく
if ( Array.isArray( geos ) && !this.childNodes.length ) {
this.coreObject.add( new THREE.Mesh( createGeometry( geos[ n ] ), createMaterial( value[ n ] ) ) );
}
n = n + 1;
}
break;
}
}
constructor( gomlDoc ) {
super( "meshes", gomlDoc );
this.coreObject = new THREE.Object3D;
}
},
// 3D用のdivとして扱える
obj: class extends GomlNode {
constructor( gomlDoc ) {
super( "obj", gomlDoc );
this.coreObject = new THREE.Object3D;
}
},
ocam: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
if ( /^(zoom|near|far)$/.test( name ) ) {
this.coreObject[ name ] = value;
this.coreObject.updateProjectionMatrix();
} else if ( name === "width" ) {
this.coreObject.left = value / - 2;
this.coreObject.right = value / 2;
this.coreObject.updateProjectionMatrix();
} else if ( name === "height" ) {
this.coreObject.top = value / 2;
this.coreObject.bottom = value / - 2;
this.coreObject.updateProjectionMatrix();
}
}
constructor( gomlDoc ) {
super( "ocam", gomlDoc );
this.coreObject = new THREE.OrthographicCamera( -1, 1, 1, -1 );
}
},
points: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
switch ( name ) {
case "geo":
this.coreObject.geometry = createGeometry( value );
break;
case "mtl":
this.coreObject.material = createMaterial( value );
break;
}
}
constructor( gomlDoc ) {
super( "points", gomlDoc );
this.coreObject = new THREE.Points;
}
},
rdr: RdrNode,
rdrs: class extends BaseNode {
constructor( gomlDoc ) {
super( "rdrs", gomlDoc );
}
},
scene: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
if ( name === "physicsWorld" ) {
physics.createWorld( this, value );
}
}
constructor( gomlDoc ) {
super( "scene", gomlDoc );
this.coreObject = new THREE.Scene;
}
},
scenes: class extends BaseNode {
constructor( gomlDoc ) {
super( "scenes", gomlDoc );
}
},
sprite: class extends GomlNode {
public setAttrHook( name: string, value ): void {
super.setAttrHook( name, value );
switch ( name ) {
case "mtl":
this.coreObject.material = createMaterial( value );
break;
}
}
constructor( gomlDoc ) {
super( "sprite", gomlDoc );
this.coreObject = new THREE.Sprite;
}
},
vp: VpNode,
vps: class extends BaseNode {
public appendHook( childNode ) {
childNode.setSize( this.parentNode.canvas.width, this.parentNode.canvas.height );
}
constructor( gomlDoc ) {
super( "vps", gomlDoc );
}
},
};