forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
104 lines (83 loc) · 2.78 KB
/
system.js
File metadata and controls
104 lines (83 loc) · 2.78 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
import { Component } from '../component.js';
import { ComponentSystem } from '../system.js';
import { RenderComponent } from './component.js';
import { RenderComponentData } from './data.js';
const _schema = [
{ name: 'rootBone', type: 'entity' },
'enabled'
];
// order matters here
const _properties = [
'material',
'meshInstances',
'asset',
'materialAssets',
'castShadows',
'receiveShadows',
'castShadowsLightmap',
'lightmapped',
'lightmapSizeMultiplier',
'renderStyle',
'type',
'layers',
'isStatic',
'batchGroupId'
];
/**
* @class
* @name RenderComponentSystem
* @augments ComponentSystem
* @classdesc Allows an Entity to render a mesh or a primitive shape like a box, capsule, sphere, cylinder, cone etc.
* @description Create a new RenderComponentSystem.
* @param {Application} app - The Application.
*/
class RenderComponentSystem extends ComponentSystem {
constructor(app) {
super(app);
this.id = 'render';
this.ComponentType = RenderComponent;
this.DataType = RenderComponentData;
this.schema = _schema;
this.defaultMaterial = app.scene.defaultMaterial;
this.on('beforeremove', this.onRemove, this);
}
initializeComponentData(component, _data, properties) {
if (_data.batchGroupId === null || _data.batchGroupId === undefined) {
_data.batchGroupId = -1;
}
// duplicate layer list
if (_data.layers && _data.layers.length) {
_data.layers = _data.layers.slice(0);
}
for (var i = 0; i < _properties.length; i++) {
if (_data.hasOwnProperty(_properties[i])) {
component[_properties[i]] = _data[_properties[i]];
}
}
super.initializeComponentData(component, _data, _schema);
}
cloneComponent(entity, clone) {
// copy properties
const data = {};
for (let i = 0; i < _properties.length; i++) {
data[_properties[i]] = entity.render[_properties[i]];
}
// mesh instances cannot be used this way, remove them and manually clone them later
delete data.meshInstances;
// clone component
const component = this.addComponent(clone, data);
// clone mesh instances
const srcMeshInstances = entity.render.meshInstances;
const meshes = srcMeshInstances.map((mi) => mi.mesh);
component._onSetMeshes(meshes);
// assign materials
for (let m = 0; m < srcMeshInstances.length; m++) {
component.meshInstances[m].material = srcMeshInstances[m].material;
}
}
onRemove(entity, component) {
component.onRemove();
}
}
Component._buildAccessors(RenderComponent.prototype, _schema);
export { RenderComponentSystem };