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
138 lines (113 loc) · 4.47 KB
/
system.js
File metadata and controls
138 lines (113 loc) · 4.47 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
import { extend } from '../../../core/core.js';
import { Asset } from '../../../asset/asset.js';
import { Component } from '../component.js';
import { ComponentSystem } from '../system.js';
import { ModelComponent } from './component.js';
import { ModelComponentData } from './data.js';
const _schema = ['enabled'];
/**
* @class
* @name ModelComponentSystem
* @augments ComponentSystem
* @classdesc Allows an Entity to render a model or a primitive shape like a box,
* capsule, sphere, cylinder, cone etc.
* @description Create a new ModelComponentSystem.
* @param {Application} app - The Application.
*/
class ModelComponentSystem extends ComponentSystem {
constructor(app) {
super(app);
this.id = 'model';
this.ComponentType = ModelComponent;
this.DataType = ModelComponentData;
this.schema = _schema;
this.defaultMaterial = app.scene.defaultMaterial;
this.on('beforeremove', this.onRemove, this);
}
initializeComponentData(component, _data, properties) {
// order matters here
properties = [
'material',
'materialAsset',
'asset',
'castShadows',
'receiveShadows',
'castShadowsLightmap',
'lightmapped',
'lightmapSizeMultiplier',
'type',
'mapping',
'layers',
'isStatic',
'batchGroupId'
];
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, ['enabled']);
}
cloneComponent(entity, clone) {
var data = {
type: entity.model.type,
asset: entity.model.asset,
castShadows: entity.model.castShadows,
receiveShadows: entity.model.receiveShadows,
castShadowsLightmap: entity.model.castShadowsLightmap,
lightmapped: entity.model.lightmapped,
lightmapSizeMultiplier: entity.model.lightmapSizeMultiplier,
isStatic: entity.model.isStatic,
enabled: entity.model.enabled,
layers: entity.model.layers,
batchGroupId: entity.model.batchGroupId,
mapping: extend({}, entity.model.mapping)
};
// if original has a different material
// than the assigned materialAsset then make sure we
// clone that one instead of the materialAsset one
var materialAsset = entity.model.materialAsset;
if (!(materialAsset instanceof Asset) && materialAsset != null) {
materialAsset = this.app.assets.get(materialAsset);
}
var material = entity.model.material;
if (!material ||
material === this.defaultMaterial ||
!materialAsset ||
material === materialAsset.resource) {
data.materialAsset = materialAsset;
}
var component = this.addComponent(clone, data);
// clone the original model if the original model component is of type asset but
// has no specified asset
if (entity.model.model && entity.model.type === 'asset' && !entity.model.asset) {
component.model = entity.model.model.clone();
component._clonedModel = true;
}
if (!data.materialAsset)
component.material = material;
// TODO: we should copy all relevant meshinstance properties here
if (entity.model.model) {
var meshInstances = entity.model.model.meshInstances;
var meshInstancesClone = component.model.meshInstances;
for (var i = 0; i < meshInstances.length; i++) {
meshInstancesClone[i].mask = meshInstances[i].mask;
meshInstancesClone[i].material = meshInstances[i].material;
meshInstancesClone[i].layer = meshInstances[i].layer;
meshInstancesClone[i].receiveShadow = meshInstances[i].receiveShadow;
}
}
}
onRemove(entity, component) {
component.onRemove();
}
}
Component._buildAccessors(ModelComponent.prototype, _schema);
export { ModelComponentSystem };