forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.js
More file actions
891 lines (720 loc) · 29.5 KB
/
Copy pathcomponent.js
File metadata and controls
891 lines (720 loc) · 29.5 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
import { RefCountedObject } from '../../../core/ref-counted-object.js';
import { LAYERID_WORLD, RENDERSTYLE_SOLID } from '../../../scene/constants.js';
import { BatchGroup } from '../../../scene/batching/batch-group.js';
import { MeshInstance } from '../../../scene/mesh-instance.js';
import { SkinInstance } from '../../../scene/skin-instance.js';
import { MorphInstance } from '../../../scene/morph-instance.js';
import { getShapePrimitive } from '../../../scene/procedural.js';
import { GraphNode } from '../../../scene/graph-node.js';
import { Asset } from '../../../asset/asset.js';
import { AssetReference } from '../../../asset/asset-reference.js';
import { Component } from '../component.js';
import { EntityReference } from '../../utils/entity-reference.js';
// Class used as an entry in the ref-counted skin instance cache
class SkinInstanceCachedObject extends RefCountedObject {
constructor(skin, skinInstance) {
super();
this.skin = skin;
this.skinInstance = skinInstance;
}
}
/**
* @component
* @class
* @name RenderComponent
* @augments Component
* @classdesc Enables an Entity to render a {@link Mesh} or a primitive shape. This component attaches {@link MeshInstance} geometry to the Entity.
* @description Create a new RenderComponent.
* @param {RenderComponentSystem} system - The ComponentSystem that created this Component.
* @param {Entity} entity - The Entity that this Component is attached to.
* @property {string} type The type of the render. Can be one of the following:
* * "asset": The component will render a render asset
* * "box": The component will render a box (1 unit in each dimension)
* * "capsule": The component will render a capsule (radius 0.5, height 2)
* * "cone": The component will render a cone (radius 0.5, height 1)
* * "cylinder": The component will render a cylinder (radius 0.5, height 1)
* * "plane": The component will render a plane (1 unit in each dimension)
* * "sphere": The component will render a sphere (radius 0.5)
* @property {Asset|number} asset The render asset for the render component (only applies to type 'asset') - can also be an asset id.
* @property {Asset[]|number[]} materialAssets The material assets that will be used to render the meshes. Each material corresponds to the respective mesh instance.
* @property {Material} material The material {@link Material} that will be used to render the meshes (not used on renders of type 'asset').
* @property {boolean} castShadows If true, attached meshes will cast shadows for lights that have shadow casting enabled.
* @property {boolean} receiveShadows If true, shadows will be cast on attached meshes.
* @property {boolean} castShadowsLightmap If true, the meshes will cast shadows when rendering lightmaps.
* @property {boolean} lightmapped If true, the meshes will be lightmapped after using lightmapper.bake().
* @property {number} lightmapSizeMultiplier Lightmap resolution multiplier.
* @property {boolean} isStatic Mark meshes as non-movable (optimization).
* @property {BoundingBox} aabb If set, the bounding box is used as a bounding box for visibility culling of attached mesh instances. This is an optimization,
* allowing oversized bounding box to be specified for skinned characters in order to avoid per frame bounding box computations based on bone positions.
* @property {MeshInstance[]} meshInstances An array of meshInstances contained in the component. If meshes are not set or loaded for component it will return null.
* @property {number} batchGroupId Assign meshes to a specific batch group (see {@link BatchGroup}). Default value is -1 (no group).
* @property {number[]} layers An array of layer IDs ({@link Layer#id}) to which the meshes should belong.
* Don't push/pop/splice or modify this array, if you want to change it - set a new one instead.
* @property {Entity} rootBone A reference to the entity to be used as the root bone for any skinned meshes that are rendered by this component.
* @property {number} renderStyle Set rendering of all {@link MeshInstance}s to the specified render style. Can be one of the following:
* * {@link RENDERSTYLE_SOLID}
* * {@link RENDERSTYLE_WIREFRAME}
* * {@link RENDERSTYLE_POINTS}
*/
class RenderComponent extends Component {
constructor(system, entity) {
super(system, entity);
this._type = 'asset';
this._castShadows = true;
this._receiveShadows = true;
this._castShadowsLightmap = true;
this._lightmapped = false;
this._lightmapSizeMultiplier = 1;
this._isStatic = false;
this._batchGroupId = -1;
this._meshInstances = [];
this._layers = [LAYERID_WORLD]; // assign to the default world layer
this._renderStyle = RENDERSTYLE_SOLID;
// bounding box which can be set to override bounding box based on mesh
this._aabb = null;
// area - used by lightmapper
this._area = null;
// the entity that represents the root bone if this render component has skinned meshes
this._rootBone = new EntityReference(this, 'rootBone');
this._rootBone.on('set:entity', this._onSetRootBone, this);
// render asset reference
this._assetReference = new AssetReference(
'asset',
this,
system.app.assets, {
add: this._onRenderAssetAdded,
load: this._onRenderAssetLoad,
remove: this._onRenderAssetRemove,
unload: this._onRenderAssetUnload
},
this
);
// material used to render meshes other than asset type
// it gets priority when set to something else than defaultMaterial, otherwise materialASsets[0] is used
this._material = system.defaultMaterial;
// material asset references
this._materialReferences = [];
entity.on('remove', this.onRemoveChild, this);
entity.on('insert', this.onInsertChild, this);
}
// map of SkinInstances allowing those to be shared between render components
// (specifically a single glb with multiple render components)
// maps rootBone to an array of SkinInstanceCachedObject
// this allows to find if a skin instance already exists for a rootbone, and a specific skin
static _skinInstanceCache = new Map();
// #if _DEBUG
// function that logs out the state of the skin instances cache
static logCachedSkinInstances() {
console.log("CachedSkinInstances");
RenderComponent._skinInstanceCache.forEach(function (array, rootBone) {
console.log(rootBone.name + ': Array(' + array.length + ")");
for (let i = 0; i < array.length; i++) {
console.log(" " + i + ": RefCount " + array[i].getRefCount());
}
});
}
// #endif
// returns already created skin instance from skin, for use on the rootBone
// ref count of existing skinInstance is increased
static getCachedSkinInstance(skin, rootBone) {
let skinInstance = null;
// get an array of cached object for the rootBone
const cachedObjArray = RenderComponent._skinInstanceCache.get(rootBone);
if (cachedObjArray) {
// find matching skin
const cachedObj = cachedObjArray.find((element) => element.skin === skin);
if (cachedObj) {
cachedObj.incRefCount();
skinInstance = cachedObj.skinInstance;
}
}
return skinInstance;
}
// adds skin instance to the cache, and increases ref count on it
static addCachedSkinInstance(skin, rootBone, skinInstance) {
// get an array for the rootBone
let cachedObjArray = RenderComponent._skinInstanceCache.get(rootBone);
if (!cachedObjArray) {
cachedObjArray = [];
RenderComponent._skinInstanceCache.set(rootBone, cachedObjArray);
}
// find entry for the skin
let cachedObj = cachedObjArray.find((element) => element.skin === skin);
if (!cachedObj) {
cachedObj = new SkinInstanceCachedObject(skin, skinInstance);
cachedObjArray.push(cachedObj);
}
cachedObj.incRefCount();
}
// removes skin instance from the cache. This decreases ref count, and when that reaches 0 it gets destroyed
static removeCachedSkinInstance(skinInstance) {
if (skinInstance) {
const rootBone = skinInstance.rootBone;
if (rootBone) {
// an array for boot bone
const cachedObjArray = RenderComponent._skinInstanceCache.get(rootBone);
if (cachedObjArray) {
// actual skin instance
const cachedObjIndex = cachedObjArray.findIndex((element) => element.skinInstance === skinInstance);
if (cachedObjIndex >= 0) {
// dec ref on the object
const cachedObj = cachedObjArray[cachedObjIndex];
cachedObj.decRefCount();
// last reference, needs to be destroyed
if (cachedObj.getRefCount() === 0) {
cachedObjArray.splice(cachedObjIndex, 1);
// if the array is empty
if (!cachedObjArray.length) {
RenderComponent._skinInstanceCache.delete(rootBone);
}
// destroy the skin instance
if (skinInstance) {
skinInstance.destroy();
cachedObj.skinInstance = null;
}
}
}
}
}
}
}
_onSetRootBone(entity) {
if (entity) {
this._onRootBoneChanged();
}
}
_onRootBoneChanged() {
// remove existing skin instances and create new ones, connected to new root bone
this._clearSkinInstances();
this._cloneSkinInstances();
}
destroyMeshInstances() {
var meshInstances = this._meshInstances;
if (meshInstances) {
this.removeFromLayers();
// destroy mesh instances separately to allow them to be removed from the cache
this._clearSkinInstances();
for (var i = 0; i < meshInstances.length; i++) {
meshInstances[i].destroy();
}
this._meshInstances.length = 0;
}
}
addToLayers() {
const layers = this.system.app.scene.layers;
for (var i = 0; i < this._layers.length; i++) {
const layer = layers.getLayerById(this._layers[i]);
if (layer) {
layer.addMeshInstances(this._meshInstances);
}
}
}
removeFromLayers() {
if (this._meshInstances && this._meshInstances.length) {
var layer, layers = this.system.app.scene.layers;
for (var i = 0; i < this._layers.length; i++) {
layer = layers.getLayerById(this._layers[i]);
if (layer) {
layer.removeMeshInstances(this._meshInstances);
}
}
}
}
onRemoveChild() {
this.removeFromLayers();
}
onInsertChild() {
if (this._meshInstances && this.enabled && this.entity.enabled) {
this.addToLayers();
}
}
onRemove() {
this.destroyMeshInstances();
this.asset = null;
this.materialAsset = null;
this.entity.off('remove', this.onRemoveChild, this);
this.entity.off('insert', this.onInsertChild, this);
}
onLayersChanged(oldComp, newComp) {
this.addToLayers();
oldComp.off("add", this.onLayerAdded, this);
oldComp.off("remove", this.onLayerRemoved, this);
newComp.on("add", this.onLayerAdded, this);
newComp.on("remove", this.onLayerRemoved, this);
}
onLayerAdded(layer) {
var index = this.layers.indexOf(layer.id);
if (index < 0) return;
layer.addMeshInstances(this._meshInstances);
}
onLayerRemoved(layer) {
var index = this.layers.indexOf(layer.id);
if (index < 0) return;
layer.removeMeshInstances(this._meshInstances);
}
onEnable() {
var app = this.system.app;
var scene = app.scene;
this._rootBone.onParentComponentEnable();
scene.on("set:layers", this.onLayersChanged, this);
if (scene.layers) {
scene.layers.on("add", this.onLayerAdded, this);
scene.layers.on("remove", this.onLayerRemoved, this);
}
var isAsset = (this._type === 'asset');
if (this._meshInstances && this._meshInstances.length) {
this.addToLayers();
} else if (isAsset && this.asset) {
this._onRenderAssetAdded();
}
// load materials
for (var i = 0; i < this._materialReferences.length; i++) {
if (this._materialReferences[i].asset) {
this.system.app.assets.load(this._materialReferences[i].asset);
}
}
if (this._batchGroupId >= 0) {
app.batcher.insert(BatchGroup.RENDER, this.batchGroupId, this.entity);
}
}
onDisable() {
var app = this.system.app;
var scene = app.scene;
scene.off("set:layers", this.onLayersChanged, this);
if (scene.layers) {
scene.layers.off("add", this.onLayerAdded, this);
scene.layers.off("remove", this.onLayerRemoved, this);
}
if (this._batchGroupId >= 0) {
app.batcher.remove(BatchGroup.RENDER, this.batchGroupId, this.entity);
}
this.removeFromLayers();
}
/**
* @function
* @name RenderComponent#hide
* @description Stop rendering {@link MeshInstance}s without removing them from the scene hierarchy.
* This method sets the {@link MeshInstance#visible} property of every MeshInstance to false.
* Note, this does not remove the mesh instances from the scene hierarchy or draw call list.
* So the render component still incurs some CPU overhead.
*/
hide() {
if (this._meshInstances) {
for (var i = 0; i < this._meshInstances.length; i++) {
this._meshInstances[i].visible = false;
}
}
}
/**
* @function
* @name RenderComponent#show
* @description Enable rendering of the render {@link MeshInstance}s if hidden using {@link RenderComponent#hide}.
* This method sets all the {@link MeshInstance#visible} property on all mesh instances to true.
*/
show() {
if (this._meshInstances) {
for (var i = 0; i < this._meshInstances.length; i++) {
this._meshInstances[i].visible = true;
}
}
}
_onRenderAssetAdded() {
if (!this._assetReference.asset) return;
if (this._assetReference.asset.resource) {
this._onRenderAssetLoad();
} else if (this.enabled && this.entity.enabled) {
this.system.app.assets.load(this._assetReference.asset);
}
}
_onRenderAssetLoad() {
// remove existing instances
this.destroyMeshInstances();
if (this._assetReference.asset) {
var render = this._assetReference.asset.resource;
render.off('set:meshes', this._onSetMeshes, this);
render.on('set:meshes', this._onSetMeshes, this);
if (render.meshes) {
this._onSetMeshes(render.meshes);
}
}
}
_onSetMeshes(meshes) {
this._cloneMeshes(meshes);
}
_clearSkinInstances() {
for (var i = 0; i < this._meshInstances.length; i++) {
const meshInstance = this._meshInstances[i];
// remove it from the cache
RenderComponent.removeCachedSkinInstance(meshInstance.skinInstance);
meshInstance.skinInstance = null;
}
}
_cloneSkinInstances() {
if (this._meshInstances.length && this._rootBone.entity instanceof GraphNode) {
var j, skin, skinInst;
for (var i = 0; i < this._meshInstances.length; i++) {
var meshInstance = this._meshInstances[i];
var mesh = meshInstance.mesh;
// if skinned but does not have instance created yet
if (mesh.skin && !mesh.skinInstance) {
skin = mesh.skin;
const rootBone = this._rootBone.entity;
// try and get skin instance from the cache
skinInst = RenderComponent.getCachedSkinInstance(skin, rootBone);
// don't have skin instance for this skin
if (!skinInst) {
skinInst = new SkinInstance(skin);
skinInst.rootBone = rootBone;
// add it to the cache
RenderComponent.addCachedSkinInstance(skin, rootBone, skinInst);
// Resolve bone IDs to actual graph nodes
var bones = [];
for (j = 0; j < skin.boneNames.length; j++) {
var boneName = skin.boneNames[j];
var bone = this._rootBone.entity.findByName(boneName);
if (!bone) {
console.error("Failed to find bone [" + boneName + "] in the entity hierarchy, RenderComponent on " + this.entity.name + ", rootBone: " + this._rootBone.entity.name);
bone = this.entity;
}
bones.push(bone);
}
skinInst.bones = bones;
}
meshInstance.skinInstance = skinInst;
}
}
}
}
_cloneMeshes(meshes) {
if (meshes && meshes.length) {
// cloned mesh instances
var meshInstances = [];
for (var i = 0; i < meshes.length; i++) {
// mesh instance
var mesh = meshes[i];
var material = this._materialReferences[i] && this._materialReferences[i].asset && this._materialReferences[i].asset.resource;
var meshInst = new MeshInstance(mesh, material || this.system.defaultMaterial, this.entity);
meshInstances.push(meshInst);
// morph instance
if (mesh.morph) {
meshInst.morphInstance = new MorphInstance(mesh.morph);
}
}
this.meshInstances = meshInstances;
// try to create skin instances if rootBone has been set, otherwise this executes when rootBone is set later
this._cloneSkinInstances();
}
}
_onRenderAssetUnload() {
// when unloading asset, only remove asset mesh instances (type could have been already changed to 'box' or similar)
if (this._type === 'asset') {
this.destroyMeshInstances();
}
}
_onRenderAssetRemove() {
if (this._assetReference.asset && this._assetReference.asset.resource) {
this._assetReference.asset.resource.off('set:meshes', this._onSetMeshes, this);
}
this._onRenderAssetUnload();
}
_onMaterialAdded(index, component, asset) {
if (asset.resource) {
this._onMaterialLoad(index, component, asset);
} else {
if (this.enabled && this.entity.enabled) {
this.system.app.assets.load(asset);
}
}
}
_onMaterialLoad(index, component, asset) {
if (this._meshInstances[index]) {
this._meshInstances[index].material = asset.resource;
}
}
_onMaterialRemove(index, component, asset) {
if (this._meshInstances[index]) {
this._meshInstances[index].material = this.system.defaultMaterial;
}
}
_onMaterialUnload(index, component, asset) {
if (this._meshInstances[index]) {
this._meshInstances[index].material = this.system.defaultMaterial;
}
}
get renderStyle() {
return this._renderStyle;
}
set renderStyle(renderStyle) {
if (this._renderStyle !== renderStyle) {
this._renderStyle = renderStyle;
MeshInstance._prepareRenderStyleForArray(this._meshInstances, renderStyle);
}
}
get aabb() {
return this._aabb;
}
set aabb(value) {
this._aabb = value;
// set it on meshInstances
var mi = this._meshInstances;
if (mi) {
for (var i = 0; i < mi.length; i++) {
mi[i].setOverrideAabb(this._aabb);
}
}
}
get type() {
return this._type;
}
set type(value) {
if (this._type !== value) {
this._area = null;
this._type = value;
this.destroyMeshInstances();
if (value !== 'asset') {
var material = this._material;
if (!material || material === this.system.defaultMaterial) {
material = this._materialReferences[0] &&
this._materialReferences[0].asset &&
this._materialReferences[0].asset.resource;
}
var primData = getShapePrimitive(this.system.app.graphicsDevice, value);
this._area = primData.area;
this.meshInstances = [new MeshInstance(primData.mesh, material || this.system.defaultMaterial, this.entity)];
}
}
}
get meshInstances() {
return this._meshInstances;
}
set meshInstances(value) {
this.destroyMeshInstances();
this._meshInstances = value;
if (this._meshInstances) {
const mi = this._meshInstances;
for (var i = 0; i < mi.length; i++) {
// if mesh instance was created without a node, assign it here
if (!mi[i].node) {
mi[i].node = this.entity;
}
mi[i].castShadow = this._castShadows;
mi[i].receiveShadow = this._receiveShadows;
mi[i].isStatic = this._isStatic;
mi[i].renderStyle = this._renderStyle;
mi[i].setLightmapped(this._lightmapped);
mi[i].setOverrideAabb(this._aabb);
}
if (this.enabled && this.entity.enabled) {
this.addToLayers();
}
}
}
get lightmapped() {
return this._lightmapped;
}
set lightmapped(value) {
if (value !== this._lightmapped) {
this._lightmapped = value;
var mi = this._meshInstances;
if (mi) {
for (var i = 0; i < mi.length; i++) {
mi[i].setLightmapped(value);
}
}
}
}
get castShadows() {
return this._castShadows;
}
set castShadows(value) {
if (this._castShadows !== value) {
var i, layer, mi = this._meshInstances;
if (mi) {
var layers = this.layers;
var scene = this.system.app.scene;
if (this._castShadows && !value) {
for (i = 0; i < layers.length; i++) {
layer = scene.layers.getLayerById(this.layers[i]);
if (layer) {
layer.removeShadowCasters(mi);
}
}
}
for (i = 0; i < mi.length; i++) {
mi[i].castShadow = value;
}
if (!this._castShadows && value) {
for (i = 0; i < layers.length; i++) {
layer = scene.layers.getLayerById(layers[i]);
if (layer) {
layer.addShadowCasters(mi);
}
}
}
}
this._castShadows = value;
}
}
get receiveShadows() {
return this._receiveShadows;
}
set receiveShadows(value) {
if (this._receiveShadows !== value) {
this._receiveShadows = value;
var mi = this._meshInstances;
if (mi) {
for (var i = 0; i < mi.length; i++) {
mi[i].receiveShadow = value;
}
}
}
}
get castShadowsLightmap() {
return this._castShadowsLightmap;
}
set castShadowsLightmap(value) {
this._castShadowsLightmap = value;
}
get lightmapSizeMultiplier() {
return this._lightmapSizeMultiplier;
}
set lightmapSizeMultiplier(value) {
this._lightmapSizeMultiplier = value;
}
get isStatic() {
return this._isStatic;
}
set isStatic(value) {
if (this._isStatic !== value) {
this._isStatic = value;
var mi = this._meshInstances;
if (mi) {
for (var i = 0; i < mi.length; i++) {
mi[i].isStatic = value;
}
}
}
}
get layers() {
return this._layers;
}
set layers(value) {
var i, layer, layers = this.system.app.scene.layers;
if (this._meshInstances) {
// remove all meshinstances from old layers
for (i = 0; i < this._layers.length; i++) {
layer = layers.getLayerById(this._layers[i]);
if (layer) {
layer.removeMeshInstances(this._meshInstances);
}
}
}
// set the layer list
this._layers.length = 0;
for (i = 0; i < value.length; i++) {
this._layers[i] = value[i];
}
// don't add into layers until we're enabled
if (!this.enabled || !this.entity.enabled || !this._meshInstances) return;
// add all mesh instances to new layers
for (i = 0; i < this._layers.length; i++) {
layer = layers.getLayerById(this._layers[i]);
if (layer) {
layer.addMeshInstances(this._meshInstances);
}
}
}
get batchGroupId() {
return this._batchGroupId;
}
set batchGroupId(value) {
if (this._batchGroupId !== value) {
var batcher = this.system.app.batcher;
if (this.entity.enabled && this._batchGroupId >= 0) {
batcher.remove(BatchGroup.RENDER, this.batchGroupId, this.entity);
}
if (this.entity.enabled && value >= 0) {
batcher.insert(BatchGroup.RENDER, value, this.entity);
}
if (value < 0 && this._batchGroupId >= 0 && this.enabled && this.entity.enabled) {
// re-add render to scene, in case it was removed by batching
this.addToLayers();
}
this._batchGroupId = value;
}
}
get material() {
return this._material;
}
set material(value) {
if (this._material !== value) {
this._material = value;
if (this._meshInstances && this._type !== 'asset') {
for (var i = 0; i < this._meshInstances.length; i++) {
this._meshInstances[i].material = value;
}
}
}
}
get materialAssets() {
return this._materialReferences.map(function (ref) {
return ref.id;
});
}
set materialAssets(value) {
var i;
value = value || [];
if (this._materialReferences.length > value.length) {
for (i = value.length; i < this._materialReferences.length; i++) {
this._materialReferences[i].id = null;
}
this._materialReferences.length = value.length;
}
for (i = 0; i < value.length; i++) {
if (!this._materialReferences[i]) {
this._materialReferences.push(
new AssetReference(
i,
this,
this.system.app.assets, {
add: this._onMaterialAdded,
load: this._onMaterialLoad,
remove: this._onMaterialRemove,
unload: this._onMaterialUnload
},
this
)
);
}
if (value[i]) {
var id = value[i] instanceof Asset ? value[i].id : value[i];
if (this._materialReferences[i].id !== id) {
this._materialReferences[i].id = id;
}
if (this._materialReferences[i].asset) {
this._onMaterialAdded(i, this, this._materialReferences[i].asset);
}
} else {
this._materialReferences[i].id = null;
if (this._meshInstances[i]) {
this._meshInstances[i].material = this.system.defaultMaterial;
}
}
}
}
get asset() {
return this._assetReference.id;
}
set asset(value) {
var id = (value instanceof Asset ? value.id : value);
if (this._assetReference.id === id) return;
if (this._assetReference.asset && this._assetReference.asset.resource) {
this._onRenderAssetRemove();
}
this._assetReference.id = id;
if (this._assetReference.asset) {
this._onRenderAssetAdded();
}
}
}
export { RenderComponent };