Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor material instance #5910

Merged
merged 13 commits into from
Dec 25, 2019
94 changes: 47 additions & 47 deletions cocos/core/3d/builtin/effects.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cocos/core/3d/builtin/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class BuiltinResMgr {
const type = selectJointsMediumType(device);
missingSkinningMtl.initialize({
effectName: 'builtin-unlit',
defines: { USE_COLOR: true, CC_USE_SKINNING: type },
defines: { USE_COLOR: true, USE_SKINNING: type },
});
missingSkinningMtl.setProperty('color', cc.color('#ff00ff'));
resources[missingSkinningMtl._uuid] = missingSkinningMtl;
Expand Down
4 changes: 2 additions & 2 deletions cocos/core/3d/framework/batched-skinning-model-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class SkinningModelUnit {
if (!comp) { return; }
this.mesh = comp.mesh;
this.skeleton = comp.skeleton;
this.material = comp.getSharedMaterial(0);
this.material = comp.getMaterial(0);
}
get copyFrom () {
return null;
Expand Down Expand Up @@ -164,7 +164,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {

public cookMaterials () {
if (!this._batchMaterial) {
this._batchMaterial = this.getSharedMaterial(0);
this._batchMaterial = this.getMaterial(0);
}
const mat = this.getMaterial(0);
if (!mat || !this._batchMaterial || !this._batchMaterial.effectAsset) {
Expand Down
11 changes: 11 additions & 0 deletions cocos/core/3d/framework/deprecated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { removeProperty } from '../../utils';
import { ModelComponent } from './model-component';

removeProperty(ModelComponent.prototype, 'ModelComponent.prototype', [
{
name: 'enableDynamicBatching',
},
{
name: 'receiveShadows',
},
]);
3 changes: 3 additions & 0 deletions cocos/core/3d/framework/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ cc.LightComponent = LightComponent;
cc.DirectionalLightComponent = DirectionalLightComponent;
cc.SphereLightComponent = SphereLightComponent;
cc.SpotLightComponent = SpotLightComponent;

/** deprecated */
import './deprecated';
173 changes: 45 additions & 128 deletions cocos/core/3d/framework/model-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,13 @@ const ModelShadowCastingMode = Enum({
@executeInEditMode
export class ModelComponent extends RenderableComponent {

/**
* @en The mesh of the model
* @zh 模型网格。
*/
@property({
type: Mesh,
tooltip: '模型网格',
})
get mesh () {
return this._mesh;
}
public static ShadowCastingMode = ModelShadowCastingMode;

set mesh (val) {
const old = this._mesh;
this._mesh = val;
this._onMeshChanged(old);
this._updateModels();
if (this.node.activeInHierarchy) {
this._attachToScene();
}
}
@property
protected _mesh: Mesh | null = null;

@property
protected _shadowCastingMode = ModelShadowCastingMode.OFF;

/**
* @en The shadow casting mode
Expand All @@ -101,72 +87,34 @@ export class ModelComponent extends RenderableComponent {
}

/**
* @en Does this model receive shadows?
* @zh 是否接受阴影?
* @en The mesh of the model
* @zh 模型网格。
*/
// @property
get receiveShadows () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

涉及到对外 API 改动,必须写 deprecated 代码来确保兼容性,并做好必要的数据迁移

return this._receiveShadows;
@property({
type: Mesh,
tooltip: '模型网格',
})
get mesh () {
return this._mesh;
}

set receiveShadows (val) {
this._receiveShadows = val;
this._updateReceiveShadow();
set mesh (val) {
const old = this._mesh;
this._mesh = val;
this._onMeshChanged(old);
this._updateModels();
if (this.node.activeInHierarchy) {
this._attachToScene();
}
}

get model () {
return this._model;
}

/**
* @zh 是否启用动态合批?
*/
@property({
tooltip: '是否启用动态合批',
})
set enableDynamicBatching (enable: boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

if (this._enableDynamicBatching === enable) { return; }
this._enableDynamicBatching = enable;
if (this._mesh) {
if (enable) { this._mesh.createFlatBuffers(); }
else { this._mesh.destroyFlatBuffers(); }
}
if (this._model) {
this._model.isDynamicBatching = enable;
this._model.onPipelineChange(); // update material
for (let i = 0; i < this._model.subModels.length; ++i) {
const subModel = this._model.subModels[i];
for (let p = 0; p < subModel.passes.length; ++p) {
const pass = subModel.passes[p];
if (enable) { pass.createBatchedBuffer(); }
else { pass.clearBatchedBuffer(); }
}
}
}
}

get enableDynamicBatching (): boolean {
return this._enableDynamicBatching;
}

public static ShadowCastingMode = ModelShadowCastingMode;

protected _modelType: typeof Model;

protected _model: Model | null = null;

@property
protected _enableDynamicBatching = false;

@property
protected _mesh: Mesh | null = null;

@property
private _shadowCastingMode = ModelShadowCastingMode.OFF;

@property
private _receiveShadows = false;

constructor () {
super();
this._modelType = Model;
Expand All @@ -175,7 +123,6 @@ export class ModelComponent extends RenderableComponent {
public onLoad () {
this._updateModels();
this._updateCastShadow();
this._updateReceiveShadow();
}

public onEnable () {
Expand Down Expand Up @@ -217,21 +164,6 @@ export class ModelComponent extends RenderableComponent {
if (this._model) {
this._model.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition);
this._model.enabled = true;

if (this._enableDynamicBatching) {
if (!this._mesh.hasFlatBuffers) {
this._mesh.createFlatBuffers();
}
for (let i = 0; i < this._model.subModels.length; ++i) {
const subModel = this._model.subModels[i];
for (let p = 0; p < subModel.passes.length; ++p) {
const pass = subModel.passes[p];
if (!pass.batchedBuffer) {
pass.createBatchedBuffer();
}
}
}
}
}
}

Expand Down Expand Up @@ -261,14 +193,13 @@ export class ModelComponent extends RenderableComponent {
}

protected _updateModelParams () {
if (!this._mesh || !this._model) {
return;
}
if (!this._mesh || !this._model) { return; }
this.node.hasChangedFlags = this._model.transform.hasChangedFlags = TransformBit.POSITION;
this._model.isDynamicBatching = this._enableDynamicBatching; // should pass this in before create PSO
const batching = this._model.isDynamicBatching = this._isBatchingEnabled();
if (batching) { this._mesh.createFlatBuffers(); }
const meshCount = this._mesh ? this._mesh.subMeshCount : 0;
for (let i = 0; i < meshCount; ++i) {
const material = this.getSharedMaterial(i);
const material = this.getRenderMaterial(i);
const renderingMesh = this._mesh.renderingMesh;
if (renderingMesh) {
const subMeshData = renderingMesh.getSubmesh(i);
Expand All @@ -280,36 +211,22 @@ export class ModelComponent extends RenderableComponent {
}

protected _onMaterialModified (idx: number, material: Material | null) {
if (this._model == null) {
return;
}
if (!this._model || !this._model.inited) { return; }
this._onRebuildPSO(idx, material || this._getBuiltinMaterial());

if (this._enableDynamicBatching) {
if (material) {
for (let p = 0; p < material.passes.length; ++p) {
const pass = material.passes[p];
if (!pass.batchedBuffer) {
pass.createBatchedBuffer();
}
}
}
}
}

protected _onRebuildPSO (idx: number, material: Material) {
if (this._model) {
this._model.setSubModelMaterial(idx, material);
}
if (!this._model || !this._model.inited) { return; }
const batching = this._model.isDynamicBatching = this._isBatchingEnabled();
if (batching && this._mesh) { this._mesh.createFlatBuffers(); }
this._model.setSubModelMaterial(idx, material);
}

protected _onMeshChanged (old: Mesh | null) {
}

protected _clearMaterials () {
if (this._model == null) {
return;
}
if (!this._model) { return; }
for (let i = 0; i < this._model.subModelNum; ++i) {
this._onMaterialModified(i, null);
}
Expand All @@ -320,10 +237,9 @@ export class ModelComponent extends RenderableComponent {
return builtinResMgr.get<Material>('missing-material');
}

protected _onVisiblityChange (val) {
if (this._model) {
this._model.visFlags = val;
}
protected _onVisiblityChange (val: number) {
if (!this._model) { return; }
this._model.visFlags = val;
}

private _updateCastShadow () {
Expand All @@ -337,15 +253,16 @@ export class ModelComponent extends RenderableComponent {
}
}

private _updateReceiveShadow () {
if (!this.enabledInHierarchy || !this._model) {
return;
private _isBatchingEnabled () {
if (!this._model || !this._mesh) { return false; }
for (let i = 0; i < this._materials.length; ++i) {
const mat = this._materials[i];
if (!mat) { continue; }
for (let p = 0; p < mat.passes.length; ++p) {
const pass = mat.passes[p];
if (pass.batchedBuffer) { return true; }
}
}
// for (let i = 0; i < this._model.subModelNum; ++i) {
// const subModel = this._model.getSubModel(i);
// if (subModel._defines['CC_USE_SHADOW_MAP'] != undefined) {
// this.getMaterial(i).define('CC_USE_SHADOW_MAP', this._receiveShadows);
// }
// }
return false;
}
}
Loading