X Tutup
Skip to content

Commit bfc9ffd

Browse files
improve and fix high level api
1 parent df33186 commit bfc9ffd

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

src/framework/components/model/component.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pc.extend(pc, function () {
4141
this.on("set_model", this.onSetModel, this);
4242
this.on("set_material", this.onSetMaterial, this);
4343
this.on("set_mapping", this.onSetMapping, this);
44-
this.on("set_batchGroup", this.onSetBatchGroup, this);
4544

4645
// override materialAsset property to return a pc.Asset instead
4746
Object.defineProperty(this, 'materialAsset', {
@@ -285,9 +284,6 @@ pc.extend(pc, function () {
285284
this.data.lightmapSizeMultiplier = newValue;
286285
},
287286

288-
onSetBatchGroup: function (name, oldValue, newValue) {
289-
},
290-
291287
onSetIsStatic: function (name, oldValue, newValue) {
292288
var i, m;
293289
if (this.data.model) {

src/framework/components/model/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pc.extend(pc, function() {
2020
this.lightmapped = false;
2121
this.lightmapSizeMultiplier = 1;
2222
this.isStatic = false;
23-
this.batchGroup = -1;
23+
this.batchGroupId = -1;
2424

2525
// non-serialized
2626
this.material = null;

src/framework/components/model/system.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pc.extend(pc, function () {
1313
'material',
1414
'model',
1515
'mapping',
16-
'batchGroup'
16+
'batchGroupId'
1717
];
1818

1919
/**
@@ -73,7 +73,7 @@ pc.extend(pc, function () {
7373
data.material = this.defaultMaterial;
7474

7575
// order matters here
76-
properties = ['enabled', 'material', 'materialAsset', 'asset', 'castShadows', 'receiveShadows', 'castShadowsLightmap', 'lightmapped', 'lightmapSizeMultiplier', 'type', 'mapping', 'isStatic', 'batchGroup'];
76+
properties = ['enabled', 'material', 'materialAsset', 'asset', 'castShadows', 'receiveShadows', 'castShadowsLightmap', 'lightmapped', 'lightmapSizeMultiplier', 'type', 'mapping', 'isStatic', 'batchGroupId'];
7777

7878
ModelComponentSystem._super.initializeComponentData.call(this, component, data, properties);
7979
},
@@ -101,7 +101,7 @@ pc.extend(pc, function () {
101101
lightmapSizeMultiplier: entity.model.lightmapSizeMultiplier,
102102
isStatic: entity.model.isStatic,
103103
enabled: entity.model.enabled,
104-
batchGroup: entity.model.batchGroup,
104+
batchGroupId: entity.model.batchGroupId,
105105
mapping: pc.extend({}, entity.model.mapping)
106106
};
107107

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mat4 getModelMatrix() {
2+
return getBoneMatrix(vertex_boneIndices);
3+
}
4+
5+
vec4 getPosition() {
6+
dModelMatrix = getModelMatrix();
7+
vec4 posW = dModelMatrix * vec4(vertex_position, 1.0);
8+
dPositionW = posW.xyz;
9+
return matrix_viewProjection * posW;
10+
}
11+
12+
vec3 getWorldPosition() {
13+
return dPositionW;
14+
}
15+

src/scene/batching.js

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ pc.extend(pc, function () {
88
this.isDynamic = isDynamic;
99
};
1010

11-
var BatchGroup = function (isDynamic, maxAabbSize) {
11+
var BatchGroup = function (id, name, isDynamic, maxAabbSize) {
1212
this.isDynamic = isDynamic;
1313
this.maxAabbSize = maxAabbSize;
14-
this.name = "Group";
14+
this.id = id;
15+
this.name = name;
1516
};
1617

1718
// Modified SkinInstance for batching
@@ -101,25 +102,55 @@ pc.extend(pc, function () {
101102
this.scene = scene;
102103
this._init = false;
103104

104-
this.batchGroups = {};
105+
this._batchGroups = {};
106+
this._batchGroupNameToId = {};
107+
this._batchGroupCounter = 0;
105108

106109
this._stats = {
107110
time: 0
108111
};
109112
};
110113

114+
Batching.prototype.addGroup = function(name, isDynamic, maxAabbSize) {
115+
if (this._batchGroupNameToId[name]) {
116+
// #ifdef DEBUG
117+
console.error("batch group " + name + " already exists");
118+
// #endif
119+
return;
120+
}
121+
var id = this._batchGroupCounter;
122+
var group;
123+
this._batchGroups[id] = group = new pc.BatchGroup(id, name, isDynamic, maxAabbSize);
124+
this._batchGroupNameToId[name] = id;
125+
this._batchGroupCounter++;
126+
return group;
127+
};
128+
129+
Batching.prototype.removeGroup = function(name) {
130+
if (!this._batchGroupNameToId[name]) {
131+
// #ifdef DEBUG
132+
console.error("batch group " + name + " doesn't exist");
133+
// #endif
134+
return;
135+
}
136+
var id = this._batchGroupNameToId[name];
137+
delete this._batchGroups[id];
138+
delete this._batchGroupNameToId[name];
139+
};
140+
141+
Batching.prototype.removeGroup = function(name, isDynamic, maxAabbSize) {
142+
this._batchGroups[name] = new pc.BatchGroup(name, isDynamic, maxAabbSize);
143+
};
144+
111145
Batching.prototype._collectAndRemoveModels = function(node, groupMeshInstances) {
112146
if (!node.enabled) return;
113-
if (!node.model) return;
114-
if (node.model.batchGroup < 0) return;
115-
if (!node.model.model) return;
116-
if (!node.model.enabled) return;
117147

118-
var arr = groupMeshInstances[node.model.batchGroup];
119-
if (!arr) arr = groupMeshInstances[node.model.batchGroup] = [];
120-
arr = arr.concat(node.model.meshInstances);
121-
122-
this.scene.removeModel(node.model);
148+
if (node.model && node.model.batchGroupId >= 0 && node.model.model && node.model.enabled) {
149+
var arr = groupMeshInstances[node.model.batchGroupId];
150+
if (!arr) arr = groupMeshInstances[node.model.batchGroupId] = [];
151+
groupMeshInstances[node.model.batchGroupId] = arr.concat(node.model.meshInstances);
152+
this.scene.removeModel(node.model.model);
153+
}
123154

124155
for(var i = 0; i < node._children.length; i++) {
125156
this._collectAndRemoveModels(node._children[i], groupMeshInstances);
@@ -165,12 +196,12 @@ pc.extend(pc, function () {
165196
}
166197
}
167198

168-
var group, lists, groupData, j, batch;
199+
var group, lists, groupData, batch;
169200
for(var groupId in groupMeshInstances) {
170201
if (!groupMeshInstances.hasOwnProperty(groupId)) continue;
171202
group = groupMeshInstances[groupId];
172203

173-
groupData = this.batchGroups[groupId];
204+
groupData = this._batchGroups[groupId];
174205
if (!groupData) {
175206
// #ifdef DEBUG
176207
console.error("batch group " + groupId + " not found");
@@ -179,7 +210,7 @@ pc.extend(pc, function () {
179210
}
180211

181212
lists = this.prepare(group, groupData.isDynamic, groupData.maxAabbSize);
182-
for(j=0; j<lists.length; j++) {
213+
for(i=0; i<lists.length; i++) {
183214
batch = this.create(lists[i], groupData.isDynamic);
184215
this.scene.addModel(batch.model);
185216
this._registerEntities(batch, group);
@@ -678,7 +709,7 @@ pc.extend(pc, function () {
678709

679710
return {
680711
Batch: Batch,
681-
SkinBatchInstance: SkinBatchInstance,
712+
BatchGroup: BatchGroup,
682713
Batching: Batching
683714
};
684715
}());

0 commit comments

Comments
 (0)
X Tutup