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

Add MeshInstance.transparent to optimize access to it #5516

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scene/composition/layer-composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class LayerComposition extends EventHandler {
function moveByBlendType(dest, src, moveTransparent) {
for (let s = 0; s < src.length;) {

if (src[s].material?.transparent === moveTransparent) {
if (src[s].transparent === moveTransparent) {

// add it to dest
dest.push(src[s]);
Expand Down
10 changes: 10 additions & 0 deletions src/scene/materials/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ class Material {
return this._blendState.blend;
}

_updateTransparency() {
const transparent = this.transparent;
const meshInstances = this.meshInstances;
for (let i = 0; i < meshInstances.length; i++) {
meshInstances[i].transparent = transparent;
}
}

// called when material changes transparency, for layer composition to add it to appropriate
// queue (opaque or transparent)
_markBlendDirty() {
Expand All @@ -252,6 +260,7 @@ class Material {
*/
set blendState(value) {
if (this._blendState.blend !== value.blend) {
this._updateTransparency();
this._markBlendDirty();
}
this._blendState.copy(value);
Expand Down Expand Up @@ -298,6 +307,7 @@ class Material {
const blend = type !== BLEND_NONE;
if (this._blendState.blend !== blend) {
this._blendState.blend = blend;
this._updateTransparency();
this._markBlendDirty();
}
this._updateMeshInstanceKeys();
Expand Down
8 changes: 8 additions & 0 deletions src/scene/mesh-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ class MeshInstance {
*/
castShadow = false;

/**
* True if the material of the mesh instance is transparent. Optimization to avoid accessing the
* material. Updated by the material instance itself.
*
* @ignore
*/
transparent = false;

/**
* @type {import('./materials/material.js').Material}
* @private
Expand Down