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

When material is not set on mesh instance, warn and use default one. #4524

Merged
merged 1 commit into from
Aug 9, 2022
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
8 changes: 8 additions & 0 deletions src/scene/mesh-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from './constants.js';

import { GraphNode } from './graph-node.js';
import { getDefaultMaterial } from './materials/default-material.js';
import { LightmapCache } from './lightmapper/lightmap-cache.js';

/** @typedef {import('../graphics/texture.js').Texture} Texture */
Expand Down Expand Up @@ -747,6 +748,13 @@ class MeshInstance {
viewUniformFormat, viewBindGroupFormat);
}

ensureMaterial(device) {
if (!this.material) {
Debug.warn(`Mesh attached to entity '${this.node.name}' does not have a material, using a default one.`);
this.material = getDefaultMaterial(device);
}
}

// Parameter management
clearParameters() {
this.parameters = {};
Expand Down
5 changes: 1 addition & 4 deletions src/scene/renderer/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
} from '../constants.js';
import { Material } from '../materials/material.js';
import { LightTextureAtlas } from '../lighting/light-texture-atlas.js';
import { getDefaultMaterial } from '../materials/default-material.js';

import { ShadowRenderer } from './shadow-renderer.js';
import { StaticMeshes } from './static-meshes.js';
Expand Down Expand Up @@ -1291,9 +1290,7 @@ class ForwardRenderer {
}
// #endif

if (!drawCall.material)
drawCall.material = getDefaultMaterial(device);

drawCall.ensureMaterial(device);
const material = drawCall.material;

const objDefs = drawCall._shaderDefs;
Expand Down
2 changes: 2 additions & 0 deletions src/scene/renderer/shadow-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ class ShadowRenderer {
for (let i = 0; i < count; i++) {
const meshInstance = visibleCasters[i];
const mesh = meshInstance.mesh;

meshInstance.ensureMaterial(device);
const material = meshInstance.material;

// set basic material states/parameters
Expand Down