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
153 lines (131 loc) · 5.35 KB
/
system.js
File metadata and controls
153 lines (131 loc) · 5.35 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
pc.extend(pc, function () {
var _schema = [
'enabled',
'type',
'asset',
'materialAsset',
'castShadows',
'receiveShadows',
'castShadowsLightmap',
'lightmapped',
'lightmapSizeMultiplier',
'isStatic',
'material',
'model',
'mapping',
'batchGroupId'
];
/**
* @name pc.ModelComponentSystem
* @description Create a new ModelComponentSystem
* @class Allows an Entity to render a model or a primitive shape like a box,
* capsule, sphere, cylinder, cone etc.
* @param {pc.Application} app The Application.
* @extends pc.ComponentSystem
*/
var ModelComponentSystem = function ModelComponentSystem (app) {
this.id = 'model';
this.description = "Renders a 3D model at the location of the Entity.";
app.systems.add(this.id, this);
this.ComponentType = pc.ModelComponent;
this.DataType = pc.ModelComponentData;
this.schema = _schema;
var gd = app.graphicsDevice;
this.box = pc.createBox(gd, {
halfExtents: new pc.Vec3(0.5, 0.5, 0.5)
});
this.capsule = pc.createCapsule(gd, {
radius: 0.5,
height: 2
});
this.sphere = pc.createSphere(gd, {
radius: 0.5
});
this.cone = pc.createCone(gd, {
baseRadius: 0.5,
peakRadius: 0,
height: 1
});
this.cylinder = pc.createCylinder(gd, {
radius: 0.5,
height: 1
});
this.plane = pc.createPlane(gd, {
halfExtents: new pc.Vec2(0.5, 0.5),
widthSegments: 1,
lengthSegments: 1
});
this.defaultMaterial = new pc.StandardMaterial();
this.on('beforeremove', this.onRemove, this);
};
ModelComponentSystem = pc.inherits(ModelComponentSystem, pc.ComponentSystem);
pc.Component._buildAccessors(pc.ModelComponent.prototype, _schema);
pc.extend(ModelComponentSystem.prototype, {
initializeComponentData: function (component, data, properties) {
data.material = this.defaultMaterial;
if (data.batchGroupId === null || data.batchGroupId === undefined)
data.batchGroupId = -1;
// order matters here
properties = ['enabled', 'material', 'materialAsset', 'asset', 'castShadows', 'receiveShadows', 'castShadowsLightmap', 'lightmapped', 'lightmapSizeMultiplier', 'type', 'mapping', 'isStatic', 'batchGroupId'];
ModelComponentSystem._super.initializeComponentData.call(this, component, data, properties);
},
removeComponent: function (entity) {
var data = entity.model.data;
entity.model.asset = null;
if (data.type !== 'asset' && data.model) {
this.app.scene.removeModel(data.model);
entity.removeChild(data.model.getGraph());
data.model = null;
}
ModelComponentSystem._super.removeComponent.call(this, entity);
},
cloneComponent: function (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,
batchGroupId: entity.model.batchGroupId,
mapping: pc.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 pc.Asset) && materialAsset != null) {
materialAsset = this.app.assets.get(materialAsset);
}
var material = entity.model.material;
if (!material ||
material === pc.ModelHandler.DEFAULT_MATERIAL ||
!materialAsset ||
material === materialAsset.resource) {
data.materialAsset = materialAsset;
}
var component = this.addComponent(clone, data);
if (!data.materialAsset)
component.material = material;
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: function(entity, component) {
component.remove();
}
});
return {
ModelComponentSystem: ModelComponentSystem
};
}());