Skip to content

Commit

Permalink
When material is not set on mesh instance, warn and use default one. (#…
Browse files Browse the repository at this point in the history
…4524)

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed Aug 9, 2022
1 parent 26151cd commit 6bdc8dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
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

0 comments on commit 6bdc8dc

Please sign in to comment.