Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/dev/core/src/Materials/PBR/openPbrMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
...(this._eventInfo.defineNames || {}),
...(this._samplerDefines || {}),
});
const effect = this._prepareEffect(mesh, defines, undefined, undefined, localOptions.useInstances, localOptions.clipPlane, mesh.hasThinInstances)!;
const effect = this._prepareEffect(mesh, mesh, defines, undefined, undefined, localOptions.useInstances, localOptions.clipPlane)!;
if (this._onEffectCreatedObservable) {
onCreatedEffectParameters.effect = effect;
onCreatedEffectParameters.subMesh = null;
Expand Down Expand Up @@ -1735,7 +1735,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {

const previousEffect = subMesh.effect;
const lightDisposed = defines._areLightsDisposed;
let effect = this._prepareEffect(mesh, defines, this.onCompiled, this.onError, useInstances, null, subMesh.getRenderingMesh().hasThinInstances);
let effect = this._prepareEffect(mesh, subMesh.getRenderingMesh(), defines, this.onCompiled, this.onError, useInstances, null);

let forceWasNotReadyPreviously = false;

Expand Down Expand Up @@ -2118,14 +2118,14 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {

private _prepareEffect(
mesh: AbstractMesh,
renderingMesh: AbstractMesh,
defines: OpenPBRMaterialDefines,
onCompiled: Nullable<(effect: Effect) => void> = null,
onError: Nullable<(effect: Effect, errors: string) => void> = null,
useInstances: Nullable<boolean> = null,
useClipPlane: Nullable<boolean> = null,
useThinInstances: boolean
useClipPlane: Nullable<boolean> = null
): Nullable<Effect> {
this._prepareDefines(mesh, defines, useInstances, useClipPlane, useThinInstances);
this._prepareDefines(mesh, renderingMesh, defines, useInstances, useClipPlane);

if (!defines.isDirty) {
return null;
Expand Down Expand Up @@ -2359,11 +2359,13 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {

private _prepareDefines(
mesh: AbstractMesh,
renderingMesh: AbstractMesh,
defines: OpenPBRMaterialDefines,
useInstances: Nullable<boolean> = null,
useClipPlane: Nullable<boolean> = null,
useThinInstances: boolean = false
useClipPlane: Nullable<boolean> = null
): void {
const useThinInstances = renderingMesh.hasThinInstances;

const scene = this.getScene();
const engine = scene.getEngine();

Expand Down Expand Up @@ -2527,7 +2529,10 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
this.fogEnabled,
this.needAlphaTestingForMesh(mesh),
defines,
this._applyDecalMapAfterDetailMap
this._applyDecalMapAfterDetailMap,
this._useVertexPulling,
renderingMesh,
this._setVertexOutputInvariant
);
defines.UNLIT = this._unlit || ((this.pointsCloud || this.wireframe) && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind));
defines.DEBUGMODE = this._debugMode;
Expand Down
55 changes: 55 additions & 0 deletions packages/dev/core/src/Rendering/geometryBufferRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { BindMorphTargetParameters, BindSceneUniformBuffer, PrepareDefinesAndAtt

import "../Engines/Extensions/engine.multiRender";
import { ShaderLanguage } from "core/Materials/shaderLanguage";
import type { OpenPBRMaterial } from "../Materials/PBR/openPbrMaterial";

/** @internal */
interface ISavedTransformationMatrix {
Expand Down Expand Up @@ -695,6 +696,38 @@ export class GeometryBufferRenderer {
if (material.specularColor) {
defines.push("#define REFLECTIVITYCOLOR");
}
} else if (material.getClassName() === "OpenPBRMaterial") {
const pbrMaterial = material as OpenPBRMaterial;

defines.push("#define METALLICWORKFLOW");
metallicWorkflow = true;
defines.push("#define METALLIC");
defines.push("#define ROUGHNESS");
if (pbrMaterial._useRoughnessFromMetallicTextureGreen && pbrMaterial.baseMetalnessTexture) {
defines.push("#define ORMTEXTURE");
defines.push(`#define REFLECTIVITY_UV${pbrMaterial.baseMetalnessTexture.coordinatesIndex + 1}`);
needUv = true;
} else if (pbrMaterial.baseMetalnessTexture) {
defines.push("#define METALLIC_TEXTURE");
defines.push(`#define METALLIC_UV${pbrMaterial.baseMetalnessTexture.coordinatesIndex + 1}`);
needUv = true;
} else if (pbrMaterial.specularRoughnessTexture) {
defines.push("#define ROUGHNESS_TEXTURE");
defines.push(`#define ROUGHNESS_UV${pbrMaterial.specularRoughnessTexture.coordinatesIndex + 1}`);
needUv = true;
}

if (pbrMaterial.baseColorTexture) {
defines.push("#define ALBEDOTEXTURE");
defines.push(`#define ALBEDO_UV${pbrMaterial.baseColorTexture.coordinatesIndex + 1}`);
if (pbrMaterial.baseColorTexture.gammaSpace) {
defines.push("#define GAMMAALBEDO");
}
needUv = true;
}
if (pbrMaterial.baseColor) {
defines.push("#define ALBEDOCOLOR");
}
}
}

Expand Down Expand Up @@ -1201,6 +1234,28 @@ export class GeometryBufferRenderer {
if (material.specularColor !== null) {
effect.setColor3("reflectivityColor", material.specularColor);
}
} else if (material.getClassName() === "OpenPBRMaterial") {
// if it is a OpenPBR material:
const openpbrMaterial = material as OpenPBRMaterial;
if (openpbrMaterial._useRoughnessFromMetallicTextureGreen && openpbrMaterial.baseMetalnessTexture) {
effect.setTexture("reflectivitySampler", openpbrMaterial.baseMetalnessTexture);
effect.setMatrix("reflectivityMatrix", openpbrMaterial.baseMetalnessTexture.getTextureMatrix());
} else if (openpbrMaterial.baseMetalnessTexture) {
effect.setTexture("metallicSampler", openpbrMaterial.baseMetalnessTexture);
effect.setMatrix("metallicMatrix", openpbrMaterial.baseMetalnessTexture.getTextureMatrix());
} else if (openpbrMaterial.specularRoughnessTexture) {
effect.setTexture("roughnessSampler", openpbrMaterial.specularRoughnessTexture);
effect.setMatrix("roughnessMatrix", openpbrMaterial.specularRoughnessTexture.getTextureMatrix());
}
effect.setFloat("metallic", openpbrMaterial.baseMetalness);
effect.setFloat("glossiness", 1.0 - openpbrMaterial.specularRoughness);
if (openpbrMaterial.baseColorTexture !== null) {
effect.setTexture("albedoSampler", openpbrMaterial.baseColorTexture);
effect.setMatrix("albedoMatrix", openpbrMaterial.baseColorTexture.getTextureMatrix());
}
if (openpbrMaterial.baseColor !== null) {
effect.setColor3("albedoColor", openpbrMaterial.baseColor);
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/dev/core/src/Shaders/geometry.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ uniform vec2 vTangentSpaceParams;
#if defined(ORMTEXTURE) || defined(SPECULARGLOSSINESSTEXTURE) || defined(REFLECTIVITYTEXTURE)
uniform sampler2D reflectivitySampler;
varying vec2 vReflectivityUV;
#else
#ifdef METALLIC_TEXTURE
uniform sampler2D metallicSampler;
varying vec2 vMetallicUV;
#endif
#ifdef ROUGHNESS_TEXTURE
uniform sampler2D roughnessSampler;
varying vec2 vRoughnessUV;
#endif
#endif
#ifdef ALBEDOTEXTURE
varying vec2 vAlbedoUV;
Expand Down Expand Up @@ -141,6 +150,13 @@ void main() {
// pbr.useMetallnessFromMetallicTextureBlue = true;
metal *= texture2D(reflectivitySampler, vReflectivityUV).b;
roughness *= texture2D(reflectivitySampler, vReflectivityUV).g;
#else
#ifdef METALLIC_TEXTURE
metal *= texture2D(metallicSampler, vMetallicUV).r;
#endif
#ifdef ROUGHNESS_TEXTURE
roughness *= texture2D(roughnessSampler, vRoughnessUV).r;
#endif
#endif

#ifdef METALLIC
Expand Down
22 changes: 22 additions & 0 deletions packages/dev/core/src/Shaders/geometry.vertex.fx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ attribute vec3 normal;
varying vec2 vReflectivityUV;
varying vec2 vAlbedoUV;
#endif
#ifdef METALLIC_TEXTURE
varying vec2 vMetallicUV;
uniform mat4 metallicMatrix;
#endif
#ifdef ROUGHNESS_TEXTURE
varying vec2 vRoughnessUV;
uniform mat4 roughnessMatrix;
#endif

#ifdef UV1
attribute vec2 uv;
Expand Down Expand Up @@ -162,6 +170,13 @@ void main(void)
#endif
#ifdef REFLECTIVITY_UV1
vReflectivityUV = vec2(reflectivityMatrix * vec4(uvUpdated, 1.0, 0.0));
#else
#ifdef METALLIC_UV1
vMetallicUV = vec2(metallicMatrix * vec4(uvUpdated, 1.0, 0.0));
#endif
#ifdef ROUGHNESS_UV1
vRoughnessUV = vec2(roughnessMatrix * vec4(uvUpdated, 1.0, 0.0));
#endif
#endif
#ifdef ALBEDO_UV1
vAlbedoUV = vec2(albedoMatrix * vec4(uvUpdated, 1.0, 0.0));
Expand All @@ -179,6 +194,13 @@ void main(void)
#endif
#ifdef REFLECTIVITY_UV2
vReflectivityUV = vec2(reflectivityMatrix * vec4(uv2Updated, 1.0, 0.0));
#else
#ifdef METALLIC_UV2
vMetallicUV = vec2(metallicMatrix * vec4(uv2Updated, 1.0, 0.0));
#endif
#ifdef ROUGHNESS_UV2
vRoughnessUV = vec2(roughnessMatrix * vec4(uv2Updated, 1.0, 0.0));
#endif
#endif
#ifdef ALBEDO_UV2
vAlbedoUV = vec2(albedoMatrix * vec4(uv2Updated, 1.0, 0.0));
Expand Down
18 changes: 18 additions & 0 deletions packages/dev/core/src/ShadersWGSL/geometry.fragment.fx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ uniform vTangentSpaceParams: vec2f;
var reflectivitySamplerSampler: sampler;
var reflectivitySampler: texture_2d<f32>;
varying vReflectivityUV: vec2f;
#else
#ifdef METALLIC_TEXTURE
var metallicSamplerSampler: sampler;
var metallicSampler: texture_2d<f32>;
varying vMetallicUV: vec2f;
#endif
#ifdef ROUGHNESS_TEXTURE
var roughnessSamplerSampler: sampler;
var roughnessSampler: texture_2d<f32>;
varying vRoughnessUV: vec2f;
#endif
#endif
#ifdef ALBEDOTEXTURE
varying vAlbedoUV: vec2f;
Expand Down Expand Up @@ -144,6 +155,13 @@ fn main(input: FragmentInputs) -> FragmentOutputs {
// pbr.useMetallnessFromMetallicTextureBlue = true;
metal *= textureSample(reflectivitySampler, reflectivitySamplerSampler, input.vReflectivityUV).b;
roughness *= textureSample(reflectivitySampler, reflectivitySamplerSampler, input.vReflectivityUV).g;
#else
#ifdef METALLIC_TEXTURE
metal *= textureSample(metallicSampler, metallicSamplerSampler, input.vMetallicUV).r;
#endif
#ifdef ROUGHNESS_TEXTURE
roughness *= textureSample(roughnessSampler, roughnessSamplerSampler, input.vRoughnessUV).r;
#endif
#endif

#ifdef METALLIC
Expand Down
22 changes: 22 additions & 0 deletions packages/dev/core/src/ShadersWGSL/geometry.vertex.fx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ attribute normal: vec3f;
varying vReflectivityUV: vec2f;
varying vAlbedoUV: vec2f;
#endif
#ifdef METALLIC_TEXTURE
varying vMetallicUV: vec2f;
uniform metallicMatrix: mat4x4f;
#endif
#ifdef ROUGHNESS_TEXTURE
varying vRoughnessUV: vec2f;
uniform roughnessMatrix: mat4x4f;
#endif

#ifdef UV1
attribute uv: vec2f;
Expand Down Expand Up @@ -168,6 +176,13 @@ fn main(input : VertexInputs) -> FragmentInputs {
#endif
#ifdef REFLECTIVITY_UV1
vertexOutputs.vReflectivityUV = (uniforms.reflectivityMatrix * vec4f(uvUpdated, 1.0, 0.0)).xy;
#else
#ifdef METALLIC_UV1
vertexOutputs.vMetallicUV = (uniforms.metallicMatrix * vec4f(uvUpdated, 1.0, 0.0)).xy;
#endif
#ifdef ROUGHNESS_UV1
vertexOutputs.vRoughnessUV = (uniforms.roughnessMatrix * vec4f(uvUpdated, 1.0, 0.0)).xy;
#endif
#endif
#ifdef ALBEDO_UV1
vertexOutputs.vAlbedoUV = (uniforms.albedoMatrix * vec4f(uvUpdated, 1.0, 0.0)).xy;
Expand All @@ -185,6 +200,13 @@ fn main(input : VertexInputs) -> FragmentInputs {
#endif
#ifdef REFLECTIVITY_UV2
vertexOutputs.vReflectivityUV = (uniforms.reflectivityMatrix * vec4f(uv2Updated, 1.0, 0.0)).xy;
#else
#ifdef METALLIC_UV2
vertexOutputs.vMetallicUV = (uniforms.metallicMatrix * vec4f(uv2Updated, 1.0, 0.0)).xy;
#endif
#ifdef ROUGHNESS_UV2
vertexOutputs.vRoughnessUV = (uniforms.roughnessMatrix * vec4f(uv2Updated, 1.0, 0.0)).xy;
#endif
#endif
#ifdef ALBEDO_UV2
vertexOutputs.vAlbedoUV = (uniforms.albedoMatrix * vec4f(uv2Updated, 1.0, 0.0)).xy;
Expand Down