-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
14ca743
refactor material instance
zxg0622 6fc6ddf
revert setMaterial signature
zxg0622 5f97078
fix some defects
zxg0622 e50c04e
material instance using inheritance (#5)
YunHsiao cf9cfda
more fixes (#6)
YunHsiao 59c2ef0
fix some defects
zxg0622 48dd734
fix batching (#7)
YunHsiao ccd98a8
better format (#8)
YunHsiao 1881339
fix
zxg0622 0e2806f
fix batching & skybox (#9)
YunHsiao 9d17374
fix skinning (#10)
YunHsiao c86209b
change sharedMaterial decorator
zxg0622 f6b3c2d
add modelcomponent property deprecated
zxg0622 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 () { | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -175,7 +123,6 @@ export class ModelComponent extends RenderableComponent { | |
public onLoad () { | ||
this._updateModels(); | ||
this._updateCastShadow(); | ||
this._updateReceiveShadow(); | ||
} | ||
|
||
public onEnable () { | ||
|
@@ -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(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
@@ -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 () { | ||
|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
涉及到对外 API 改动,必须写 deprecated 代码来确保兼容性,并做好必要的数据迁移