From 455540902f7631cdcb9067a1e2d6341c75474d67 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 9 Apr 2024 16:23:48 +0800 Subject: [PATCH 01/89] fix: multi material error --- package.json | 2 +- packages/shaderlab/src/GSLPBRMaterial.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 56b25733..409984dd 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ }, "pnpm": { "overrides": { - "@galacean/engine": "^1.2.0-alpha.5" + "@galacean/engine": "^1.2.0-beta.1" } } } diff --git a/packages/shaderlab/src/GSLPBRMaterial.ts b/packages/shaderlab/src/GSLPBRMaterial.ts index 5de52a36..e8ed8b44 100644 --- a/packages/shaderlab/src/GSLPBRMaterial.ts +++ b/packages/shaderlab/src/GSLPBRMaterial.ts @@ -1,8 +1,9 @@ -import { PBRBaseMaterial, ShaderFactory, Shader, Engine, Vector3 } from "@galacean/engine"; -import { pbr_include_fragment_list, pbrSource } from "./shaders"; +import { Engine, PBRBaseMaterial, Shader, ShaderFactory, Vector3 } from "@galacean/engine"; +import { pbrSource, pbr_include_fragment_list } from "./shaders"; export class GSLPBRMaterial extends PBRBaseMaterial { private static _registered = false; + static registerIncludes() { if (this._registered) return; @@ -14,7 +15,8 @@ export class GSLPBRMaterial extends PBRBaseMaterial { constructor(engine: Engine) { GSLPBRMaterial.registerIncludes(); - super(engine, Shader.create(pbrSource)); + const shader = Shader.find("pbr.gs") || Shader.create(pbrSource); + super(engine, shader); const shaderData = this.shaderData; shaderData.setFloat("material_Metal", 1); From 2083f73b0c6155331880d1da5feb86b219b55825 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 24 Apr 2024 17:26:47 +0800 Subject: [PATCH 02/89] feat: refactor fog --- .../shaderlab/src/shaders/FogFragment.glsl | 4 ---- .../src/shaders/FogFragmentDeclaration.glsl | 18 -------------- packages/shaderlab/src/shaders/fog.glsl | 24 +++++++++++++++++++ packages/shaderlab/src/shaders/globals.glsl | 10 -------- packages/shaderlab/src/shaders/index.ts | 12 ++++------ packages/shaderlab/src/shaders/pbr.gs | 20 ++++++++++++++-- 6 files changed, 47 insertions(+), 41 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/FogFragment.glsl delete mode 100644 packages/shaderlab/src/shaders/FogFragmentDeclaration.glsl create mode 100644 packages/shaderlab/src/shaders/fog.glsl delete mode 100644 packages/shaderlab/src/shaders/globals.glsl diff --git a/packages/shaderlab/src/shaders/FogFragment.glsl b/packages/shaderlab/src/shaders/FogFragment.glsl deleted file mode 100644 index de650ed2..00000000 --- a/packages/shaderlab/src/shaders/FogFragment.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#if SCENE_FOG_MODE != 0 - float fogIntensity = ComputeFogIntensity(length(v.v_positionVS)); - gl_FragColor.rgb = mix(scene_FogColor.rgb, gl_FragColor.rgb, fogIntensity); -#endif diff --git a/packages/shaderlab/src/shaders/FogFragmentDeclaration.glsl b/packages/shaderlab/src/shaders/FogFragmentDeclaration.glsl deleted file mode 100644 index fc6f1fc0..00000000 --- a/packages/shaderlab/src/shaders/FogFragmentDeclaration.glsl +++ /dev/null @@ -1,18 +0,0 @@ -#if SCENE_FOG_MODE != 0 - vec4 scene_FogColor; - vec4 scene_FogParams;// (-1/(end-start), end/(end-start), density/ln(2),density/sprt(ln(2))); - - float ComputeFogIntensity(float fogDepth){ - #if SCENE_FOG_MODE == 1 - // (end-z) / (end-start) = z * (-1/(end-start)) + (end/(end-start)) - return clamp(fogDepth * scene_FogParams.x + scene_FogParams.y, 0.0, 1.0); - #elif SCENE_FOG_MODE == 2 - // exp(-z * density) = exp2((-z * density)/ln(2)) = exp2(-z * density/ln(2)) - return clamp(exp2(-fogDepth * scene_FogParams.z), 0.0, 1.0); - #elif SCENE_FOG_MODE == 3 - // exp(-(z * density)^2) = exp2(-(z * density)^2/ln(2)) = exp2(-(z * density/sprt(ln(2)))^2) - float factor = fogDepth * scene_FogParams.w; - return clamp(exp2(-factor * factor), 0.0, 1.0); - #endif - } -#endif diff --git a/packages/shaderlab/src/shaders/fog.glsl b/packages/shaderlab/src/shaders/fog.glsl new file mode 100644 index 00000000..0a69ad64 --- /dev/null +++ b/packages/shaderlab/src/shaders/fog.glsl @@ -0,0 +1,24 @@ +#if SCENE_FOG_MODE != 0 + vec4 scene_FogColor; + vec4 scene_FogParams; // (-1/(end-start), end/(end-start), density/ln(2),density/sprt(ln(2))); + + vec4 fog(vec4 color, vec3 positionVS){ + float fogDepth = length(positionVS); + + #if SCENE_FOG_MODE == 1 + // (end-z) / (end-start) = z * (-1/(end-start)) + (end/(end-start)) + float fogIntensity = clamp(fogDepth * scene_FogParams.x + scene_FogParams.y, 0.0, 1.0); + #elif SCENE_FOG_MODE == 2 + // exp(-z * density) = exp2((-z * density)/ln(2)) = exp2(-z * density/ln(2)) + float fogIntensity = clamp(exp2(-fogDepth * scene_FogParams.z), 0.0, 1.0); + #elif SCENE_FOG_MODE == 3 + // exp(-(z * density)^2) = exp2(-(z * density)^2/ln(2)) = exp2(-(z * density/sprt(ln(2)))^2) + float factor = fogDepth * scene_FogParams.w; + float fogIntensity = clamp(exp2(-factor * factor), 0.0, 1.0); + #endif + + color.rgb = mix(scene_FogColor.rgb, color.rgb, fogIntensity); + + return color; + } +#endif diff --git a/packages/shaderlab/src/shaders/globals.glsl b/packages/shaderlab/src/shaders/globals.glsl deleted file mode 100644 index 1317ac17..00000000 --- a/packages/shaderlab/src/shaders/globals.glsl +++ /dev/null @@ -1,10 +0,0 @@ -#include "common.glsl" -#include "common_vert.glsl" -#include "blendShape_input.glsl" -#include "ShadowVertexDeclaration.glsl" - -// fragment uniforms -#include "FogFragmentDeclaration.glsl" -#include "light_frag_define.glsl" -#include "pbr_frag_define.glsl" -#include "pbr_helper.glsl" \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index e104c581..d4de44c4 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -9,8 +9,7 @@ import color_vert from "./color_vert.glsl"; import common_vert from "./common_vert.glsl"; import common from "./common.glsl"; import direct_irradiance_frag_define from "./direct_irradiance_frag_define.glsl"; -import FogFragment from "./FogFragment.glsl"; -import FogFragmentDeclaration from "./FogFragmentDeclaration.glsl"; +import fog from "./fog.glsl"; import FogVertex from "./FogVertex.glsl"; import ibl_frag_define from "./ibl_frag_define.glsl"; import light_frag_define from "./light_frag_define.glsl"; @@ -31,9 +30,9 @@ import uv_vert from "./uv_vert.glsl"; import varying from "./varying.glsl"; import worldpos_vert from "./worldpos_vert.glsl"; import pbr from "./pbr.gs"; -import globals from "./globals.glsl"; import pbr_vert1 from "./pbr_vert1.glsl"; import pbr_vert2 from "./pbr_vert2.glsl"; +import input from "./input.glsl"; interface IShaderFragment { includeKey: string; @@ -52,8 +51,7 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: common_vert, includeKey: "common_vert.glsl" }, { source: common, includeKey: "common.glsl" }, { source: direct_irradiance_frag_define, includeKey: "direct_irradiance_frag_define.glsl" }, - { source: FogFragment, includeKey: "FogFragment.glsl" }, - { source: FogFragmentDeclaration, includeKey: "FogFragmentDeclaration.glsl" }, + { source: fog, includeKey: "fog.glsl" }, { source: FogVertex, includeKey: "FogVertex.glsl" }, { source: ibl_frag_define, includeKey: "ibl_frag_define.glsl" }, { source: light_frag_define, includeKey: "light_frag_define.glsl" }, @@ -73,8 +71,8 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: uv_vert, includeKey: "uv_vert.glsl" }, { source: varying, includeKey: "varying.glsl" }, { source: worldpos_vert, includeKey: "worldpos_vert.glsl" }, - { source: globals, includeKey: "globals.glsl" }, { source: pbr_vert1, includeKey: "pbr_vert1.glsl" }, - { source: pbr_vert2, includeKey: "pbr_vert2.glsl" } + { source: pbr_vert2, includeKey: "pbr_vert2.glsl" }, + { source: input, includeKey: "input.glsl" } ]; export { pbr as pbrSource, pbr_include_fragment_list }; diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index c2cba60e..234329b7 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -69,7 +69,20 @@ Shader "pbr.gs" { #include "varying.glsl" } - #include "globals.glsl" + #include "common.glsl" + #include "common_vert.glsl" + #include "blendShape_input.glsl" + #include "ShadowVertexDeclaration.glsl" + + // fragment uniforms + #include "fog.glsl" + #include "light_frag_define.glsl" + #include "pbr_frag_define.glsl" + #include "pbr_helper.glsl" + + + // new + #include "input.glsl" VertexShader = pbrVert; FragmentShader = pbrFrag; @@ -92,7 +105,10 @@ Shader "pbr.gs" { void pbrFrag(_galacean_v2f v) { #include "pbr_frag.glsl" - #include "FogFragment.glsl" + + #if SCENE_FOG_MODE != 0 + gl_FragColor = fog(gl_FragColor, v.v_positionVS); + #endif #ifndef ENGINE_IS_COLORSPACE_GAMMA gl_FragColor = linearToGamma(gl_FragColor); From f7000ef9034d5966ff1444a1bf4ebccb7e2504f9 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 24 Apr 2024 17:27:06 +0800 Subject: [PATCH 03/89] refactor: init input --- packages/shaderlab/src/shaders/input.glsl | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 packages/shaderlab/src/shaders/input.glsl diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/input.glsl new file mode 100644 index 00000000..4e68209f --- /dev/null +++ b/packages/shaderlab/src/shaders/input.glsl @@ -0,0 +1,33 @@ +struct VertexData{ + + +} + +struct SurfaceData{ + +} + +struct A2v{ + +} + +struct V2f{ + +} + +struct Temp_a2v{ + +} + +struct Temp_v2f{ + +} + +void intVertexData(in Temp_a2v a2v, out VertexData vertexData){ + +} + + +void initSurfaceData(in Temp_v2f v2f, out SurfaceData surfaceData){ + +} \ No newline at end of file From b5537a37f38c0e6f57a04217e6f7f9eed32a1921 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 25 Apr 2024 18:37:04 +0800 Subject: [PATCH 04/89] refactor: shadow and shadow fade --- .../src/shaders/ShadowVertexDeclaration.glsl | 172 +++++++++++- .../shaderlab/src/shaders/common_vert.glsl | 3 - .../direct_irradiance_frag_define.glsl | 2 - packages/shaderlab/src/shaders/index.ts | 12 +- packages/shaderlab/src/shaders/input.glsl | 249 +++++++++++++++++- packages/shaderlab/src/shaders/pbr.gs | 6 +- .../src/shaders/temp/transformAttributes.glsl | 69 +++++ .../src/shaders/temp/transformVarying.glsl | 32 +++ .../src/shaders/transform_declare.glsl | 5 +- 9 files changed, 526 insertions(+), 24 deletions(-) create mode 100644 packages/shaderlab/src/shaders/temp/transformAttributes.glsl create mode 100644 packages/shaderlab/src/shaders/temp/transformVarying.glsl diff --git a/packages/shaderlab/src/shaders/ShadowVertexDeclaration.glsl b/packages/shaderlab/src/shaders/ShadowVertexDeclaration.glsl index 97c27df0..1a5d0f91 100644 --- a/packages/shaderlab/src/shaders/ShadowVertexDeclaration.glsl +++ b/packages/shaderlab/src/shaders/ShadowVertexDeclaration.glsl @@ -4,6 +4,176 @@ #ifdef SCENE_IS_CALCULATE_SHADOWS #if SCENE_SHADOW_CASCADED_COUNT==1 - #include "ShadowCoord.glsl" + + mat4 scene_ShadowMatrices[SCENE_SHADOW_CASCADED_COUNT + 1]; + vec4 scene_ShadowSplitSpheres[4]; + + mediump int computeCascadeIndex(vec3 positionWS) { + vec3 fromCenter0 = positionWS - scene_ShadowSplitSpheres[0].xyz; + vec3 fromCenter1 = positionWS - scene_ShadowSplitSpheres[1].xyz; + vec3 fromCenter2 = positionWS - scene_ShadowSplitSpheres[2].xyz; + vec3 fromCenter3 = positionWS - scene_ShadowSplitSpheres[3].xyz; + + mediump vec4 comparison = vec4( + (dot(fromCenter0, fromCenter0) < scene_ShadowSplitSpheres[0].w), + (dot(fromCenter1, fromCenter1) < scene_ShadowSplitSpheres[1].w), + (dot(fromCenter2, fromCenter2) < scene_ShadowSplitSpheres[2].w), + (dot(fromCenter3, fromCenter3) < scene_ShadowSplitSpheres[3].w)); + comparison.yzw = clamp(comparison.yzw - comparison.xyz,0.0,1.0);//keep the nearest + mediump vec4 indexCoefficient = vec4(4.0,3.0,2.0,1.0); + mediump int index = 4 - int(dot(comparison, indexCoefficient)); + return index; + } + + vec3 getShadowCoord() { + #if SCENE_SHADOW_CASCADED_COUNT == 1 + mediump int cascadeIndex = 0; + #else + mediump int cascadeIndex = computeCascadeIndex(v.v_pos); + #endif + + #ifdef GRAPHICS_API_WEBGL2 + mat4 shadowMatrix = scene_ShadowMatrices[cascadeIndex]; + #else + mat4 shadowMatrix; + #if SCENE_SHADOW_CASCADED_COUNT == 4 + if (cascadeIndex == 0) { + shadowMatrix = scene_ShadowMatrices[0]; + } else if (cascadeIndex == 1) { + shadowMatrix = scene_ShadowMatrices[1]; + } else if (cascadeIndex == 2) { + shadowMatrix = scene_ShadowMatrices[2]; + } else if (cascadeIndex == 3) { + shadowMatrix = scene_ShadowMatrices[3]; + } else { + shadowMatrix = scene_ShadowMatrices[4]; + } + #endif + #if SCENE_SHADOW_CASCADED_COUNT == 2 + if (cascadeIndex == 0) { + shadowMatrix = scene_ShadowMatrices[0]; + } else if (cascadeIndex == 1) { + shadowMatrix = scene_ShadowMatrices[1]; + } else { + shadowMatrix = scene_ShadowMatrices[2]; + } + #endif + #if SCENE_SHADOW_CASCADED_COUNT == 1 + if (cascadeIndex == 0) { + shadowMatrix = scene_ShadowMatrices[0]; + } else { + shadowMatrix = scene_ShadowMatrices[1]; + } + #endif + #endif + + vec4 shadowCoord = shadowMatrix * vec4(v.v_pos, 1.0); + return shadowCoord.xyz; + } + + #endif +#endif + + +#ifdef SCENE_IS_CALCULATE_SHADOWS + // intensity, null, fadeScale, fadeBias + vec4 scene_ShadowInfo; + vec4 scene_ShadowMapSize; + + #ifdef GRAPHICS_API_WEBGL2 + mediump sampler2DShadow scene_ShadowMap; + #define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) textureLod(textureName, coord3 , 0.0) + #define TEXTURE2D_SHADOW_PARAM(shadowMap) mediump sampler2DShadow shadowMap + #else + sampler2D scene_ShadowMap; + #ifdef ENGINE_NO_DEPTH_TEXTURE + const vec4 bitShift = vec4(1.0, 1.0/256.0, 1.0/(256.0*256.0), 1.0/(256.0*256.0*256.0)); + /** + * Unpack depth value. + */ + float unpack(in vec4 rgbaDepth) { + return dot(rgbaDepth, bitShift); + } + #define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) (unpack(texture2D(textureName, coord3.xy)) < coord3.z ? 0.0 : 1.0) + #else + #define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) ((texture2D(textureName, coord3.xy)).r < coord3.z ? 0.0 : 1.0) + #endif + #define TEXTURE2D_SHADOW_PARAM(shadowMap) mediump sampler2D shadowMap + #endif + + #if SCENE_SHADOW_TYPE == 2 + float sampleShadowMapFiltered4(TEXTURE2D_SHADOW_PARAM(shadowMap), vec3 shadowCoord, vec4 shadowMapSize) { + float attenuation; + vec4 attenuation4; + vec2 offset=shadowMapSize.xy/2.0; + vec3 shadowCoord0=shadowCoord + vec3(-offset,0.0); + vec3 shadowCoord1=shadowCoord + vec3(offset.x,-offset.y,0.0); + vec3 shadowCoord2=shadowCoord + vec3(-offset.x,offset.y,0.0); + vec3 shadowCoord3=shadowCoord + vec3(offset,0.0); + attenuation4.x = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord0); + attenuation4.y = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord1); + attenuation4.z = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord2); + attenuation4.w = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord3); + attenuation = dot(attenuation4, vec4(0.25)); + return attenuation; + } + #endif + + #if SCENE_SHADOW_TYPE == 3 + #include "shadow_sample_tent.glsl" + + float sampleShadowMapFiltered9(TEXTURE2D_SHADOW_PARAM(shadowMap), vec3 shadowCoord, vec4 shadowmapSize) { + float attenuation; + float fetchesWeights[9]; + vec2 fetchesUV[9]; + sampleShadowComputeSamplesTent5x5(shadowmapSize, shadowCoord.xy, fetchesWeights, fetchesUV); + attenuation = fetchesWeights[0] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[0].xy, shadowCoord.z)); + attenuation += fetchesWeights[1] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[1].xy, shadowCoord.z)); + attenuation += fetchesWeights[2] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[2].xy, shadowCoord.z)); + attenuation += fetchesWeights[3] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[3].xy, shadowCoord.z)); + attenuation += fetchesWeights[4] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[4].xy, shadowCoord.z)); + attenuation += fetchesWeights[5] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[5].xy, shadowCoord.z)); + attenuation += fetchesWeights[6] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[6].xy, shadowCoord.z)); + attenuation += fetchesWeights[7] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[7].xy, shadowCoord.z)); + attenuation += fetchesWeights[8] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[8].xy, shadowCoord.z)); + return attenuation; + } #endif + + + float getShadowFade(vec3 positionWS){ + vec3 camToPixel = positionWS - camera_Position; + float distanceCamToPixel2 = dot(camToPixel, camToPixel); + return saturate( distanceCamToPixel2 * scene_ShadowInfo.z + scene_ShadowInfo.w ); + } + + + float sampleShadowMap() { + #if SCENE_SHADOW_CASCADED_COUNT == 1 + vec3 shadowCoord = v.v_shadowCoord; + #else + vec3 shadowCoord = getShadowCoord(); + #endif + + float attenuation = 1.0; + if(shadowCoord.z > 0.0 && shadowCoord.z < 1.0) { + #if SCENE_SHADOW_TYPE == 1 + attenuation = SAMPLE_TEXTURE2D_SHADOW(scene_ShadowMap, shadowCoord); + #endif + + #if SCENE_SHADOW_TYPE == 2 + attenuation = sampleShadowMapFiltered4(scene_ShadowMap, shadowCoord, scene_ShadowMapSize); + #endif + + #if SCENE_SHADOW_TYPE == 3 + attenuation = sampleShadowMapFiltered9(scene_ShadowMap, shadowCoord, scene_ShadowMapSize); + #endif + attenuation = mix(1.0, attenuation, scene_ShadowInfo.x); + } + + float shadowFade = getShadowFade(v.v_pos); + attenuation = mix(1.0, mix(attenuation, 1.0, shadowFade), scene_ShadowInfo.x); + + return attenuation; + } #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/common_vert.glsl b/packages/shaderlab/src/shaders/common_vert.glsl index e6e2c60d..1a89b7e0 100644 --- a/packages/shaderlab/src/shaders/common_vert.glsl +++ b/packages/shaderlab/src/shaders/common_vert.glsl @@ -22,7 +22,4 @@ #endif #endif -#include "transform_declare.glsl" -#include "camera_declare.glsl" - vec4 material_TilingOffset; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/direct_irradiance_frag_define.glsl b/packages/shaderlab/src/shaders/direct_irradiance_frag_define.glsl index c9358096..6bc18c17 100644 --- a/packages/shaderlab/src/shaders/direct_irradiance_frag_define.glsl +++ b/packages/shaderlab/src/shaders/direct_irradiance_frag_define.glsl @@ -1,5 +1,3 @@ -#include "ShadowFragmentDeclaration.glsl" - void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Material material, inout ReflectedLight reflectedLight) { float attenuation = 1.0; diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index d4de44c4..82fa04a7 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -4,7 +4,7 @@ import begin_position_vert from "./begin_position_vert.glsl"; import blendShape_input from "./blendShape_input.glsl"; import blendShape_vert from "./blendShape_vert.glsl"; import brdf from "./brdf.glsl"; -import camera_declare from "./camera_declare.glsl"; +// import camera_declare from "./camera_declare.glsl"; import color_vert from "./color_vert.glsl"; import common_vert from "./common_vert.glsl"; import common from "./common.glsl"; @@ -20,8 +20,8 @@ import pbr_frag from "./pbr_frag.glsl"; import pbr_helper from "./pbr_helper.glsl"; import position_vert from "./position_vert.glsl"; import shadow_sample_tent from "./shadow_sample_tent.glsl"; -import ShadowCoord from "./ShadowCoord.glsl"; -import ShadowFragmentDeclaration from "./ShadowFragmentDeclaration.glsl"; +// import ShadowCoord from "./ShadowCoord.glsl"; +// import ShadowFragmentDeclaration from "./ShadowFragmentDeclaration.glsl"; import ShadowVertex from "./ShadowVertex.glsl"; import ShadowVertexDeclaration from "./ShadowVertexDeclaration.glsl"; import skinning_vert from "./skinning_vert.glsl"; @@ -46,7 +46,7 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: blendShape_input, includeKey: "blendShape_input.glsl" }, { source: blendShape_vert, includeKey: "blendShape_vert.glsl" }, { source: brdf, includeKey: "brdf.glsl" }, - { source: camera_declare, includeKey: "camera_declare.glsl" }, + // { source: camera_declare, includeKey: "camera_declare.glsl" }, { source: color_vert, includeKey: "color_vert.glsl" }, { source: common_vert, includeKey: "common_vert.glsl" }, { source: common, includeKey: "common.glsl" }, @@ -62,8 +62,8 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: pbr_helper, includeKey: "pbr_helper.glsl" }, { source: position_vert, includeKey: "position_vert.glsl" }, { source: shadow_sample_tent, includeKey: "shadow_sample_tent.glsl" }, - { source: ShadowCoord, includeKey: "ShadowCoord.glsl" }, - { source: ShadowFragmentDeclaration, includeKey: "ShadowFragmentDeclaration.glsl" }, + // { source: ShadowCoord, includeKey: "ShadowCoord.glsl" }, + // { source: ShadowFragmentDeclaration, includeKey: "ShadowFragmentDeclaration.glsl" }, { source: ShadowVertex, includeKey: "ShadowVertex.glsl" }, { source: ShadowVertexDeclaration, includeKey: "ShadowVertexDeclaration.glsl" }, { source: skinning_vert, includeKey: "skinning_vert.glsl" }, diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/input.glsl index 4e68209f..29a70361 100644 --- a/packages/shaderlab/src/shaders/input.glsl +++ b/packages/shaderlab/src/shaders/input.glsl @@ -1,33 +1,262 @@ -struct VertexData{ +// struct VertexData{ +// vec4 position; +// vec3 normal; +// vec4 tangent; +// } +struct SurfaceData{ + vec3 position; + vec3 normal; + vec3 viewDir; + float dotNV; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatNormal; + float clearCoatDotNV; + #endif + #ifdef MATERIAL_ENABLE_ANISOTROPY + vec3 anisotropicT; + vec3 anisotropicB; + vec3 anisotropicN; + float anisotropy; + #endif + + vec3 diffuseColor; + float roughness; + vec3 specularColor; + float opacity; + float f0; + #ifdef MATERIAL_ENABLE_CLEAR_COAT + float clearCoat; + float clearCoatRoughness; + #endif } -struct SurfaceData{ +struct Attributes{ + vec3 POSITION; -} + #ifdef RENDERER_HAS_BLENDSHAPE + #ifndef RENDERER_BLENDSHAPE_USE_TEXTURE + vec3 POSITION_BS0; + vec3 POSITION_BS1; + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + vec3 NORMAL_BS0; + vec3 NORMAL_BS1; + vec3 TANGENT_BS0; + vec3 TANGENT_BS1; + #else + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + vec3 POSITION_BS2; + vec3 POSITION_BS3; + + #ifdef RENDERER_BLENDSHAPE_HAS_NORMAL + vec3 NORMAL_BS0; + vec3 NORMAL_BS1; + vec3 NORMAL_BS2; + vec3 NORMAL_BS3; + #endif + + #ifdef RENDERER_BLENDSHAPE_HAS_TANGENT + vec3 TANGENT_BS0; + vec3 TANGENT_BS1; + vec3 TANGENT_BS2; + vec3 TANGENT_BS3; + #endif + + #else + vec3 POSITION_BS2; + vec3 POSITION_BS3; + vec3 POSITION_BS4; + vec3 POSITION_BS5; + vec3 POSITION_BS6; + vec3 POSITION_BS7; + #endif + #endif + #endif + #endif + + + #ifdef RENDERER_HAS_UV + vec2 TEXCOORD_0; + #endif -struct A2v{ + #ifdef RENDERER_HAS_UV1 + vec2 TEXCOORD_1; + #endif + #ifdef RENDERER_HAS_SKIN + vec4 JOINTS_0; + vec4 WEIGHTS_0; + #endif + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 COLOR_0; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 NORMAL; + #endif + + #ifdef RENDERER_HAS_TANGENT + vec4 TANGENT; + #endif + #endif } -struct V2f{ +struct Varyings{ + vec2 v_uv; + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 v_color; + #endif + #if SCENE_FOG_MODE != 0 + vec3 v_positionVS; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 v_normal; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + mat3 v_TBN; + #endif + #endif + #endif + + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT==1 + vec3 v_shadowCoord; + #endif + #endif + + #ifdef RENDERER_HAS_UV1 + vec2 v_uv1; + #endif + #ifdef MATERIAL_NEED_WORLD_POS + vec3 v_pos; + #endif } -struct Temp_a2v{ - +struct Temp_Attributes{ + #ifdef RENDERER_HAS_BLENDSHAPE + #ifndef RENDERER_BLENDSHAPE_USE_TEXTURE + vec3 POSITION_BS0; + vec3 POSITION_BS1; + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + vec3 NORMAL_BS0; + vec3 NORMAL_BS1; + vec3 TANGENT_BS0; + vec3 TANGENT_BS1; + #else + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + vec3 POSITION_BS2; + vec3 POSITION_BS3; + + #ifdef RENDERER_BLENDSHAPE_HAS_NORMAL + vec3 NORMAL_BS0; + vec3 NORMAL_BS1; + vec3 NORMAL_BS2; + vec3 NORMAL_BS3; + #endif + + #ifdef RENDERER_BLENDSHAPE_HAS_TANGENT + vec3 TANGENT_BS0; + vec3 TANGENT_BS1; + vec3 TANGENT_BS2; + vec3 TANGENT_BS3; + #endif + + #else + vec3 POSITION_BS2; + vec3 POSITION_BS3; + vec3 POSITION_BS4; + vec3 POSITION_BS5; + vec3 POSITION_BS6; + vec3 POSITION_BS7; + #endif + #endif + #endif + #endif + + vec3 POSITION; + + #ifdef RENDERER_HAS_UV + vec2 TEXCOORD_0; + #endif + + #ifdef RENDERER_HAS_UV1 + vec2 TEXCOORD_1; + #endif + + #ifdef RENDERER_HAS_SKIN + vec4 JOINTS_0; + vec4 WEIGHTS_0; + #endif + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 COLOR_0; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 NORMAL; + #endif + + #ifdef RENDERER_HAS_TANGENT + vec4 TANGENT; + #endif + #endif } -struct Temp_v2f{ +struct Temp_Varyings{ + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 v_color; + #endif + + #if SCENE_FOG_MODE != 0 + vec3 v_positionVS; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 v_normal; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + mat3 v_TBN; + #endif + #endif + #endif + + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT==1 + vec3 v_shadowCoord; + #endif + #endif + vec2 v_uv; + + #ifdef RENDERER_HAS_UV1 + vec2 v_uv1; + #endif + #ifdef MATERIAL_NEED_WORLD_POS + vec3 v_pos; + #endif } -void intVertexData(in Temp_a2v a2v, out VertexData vertexData){ +void intVertexData(in Temp_Attributes attributes, out Temp_Varyings varyings){ + vec4 position = vec4( attributes.POSITION , 1.0 ); + + #if SCENE_FOG_MODE != 0 + vec4 positionVS = renderer_MVMat * position; + varyings.v_positionVS = positionVS.xyz / positionVS.w; + #endif + + } -void initSurfaceData(in Temp_v2f v2f, out SurfaceData surfaceData){ +void initSurfaceData(in Temp_Varyings varyings, out SurfaceData surfaceData){ } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 234329b7..3ce4eee6 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -56,7 +56,9 @@ Shader "pbr.gs" { } SubShader "Default" { - Pass "Pass0" { + UsePass "pbr/Default/ShadowCaster" + + Pass "Forward Pass" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW @@ -70,6 +72,8 @@ Shader "pbr.gs" { } #include "common.glsl" + #include "transform_declare.glsl" + #include "common_vert.glsl" #include "blendShape_input.glsl" #include "ShadowVertexDeclaration.glsl" diff --git a/packages/shaderlab/src/shaders/temp/transformAttributes.glsl b/packages/shaderlab/src/shaders/temp/transformAttributes.glsl new file mode 100644 index 00000000..b994f4da --- /dev/null +++ b/packages/shaderlab/src/shaders/temp/transformAttributes.glsl @@ -0,0 +1,69 @@ +temp_attributes.POSITION = attr.POSITION; + + #ifdef RENDERER_HAS_BLENDSHAPE + #ifndef RENDERER_BLENDSHAPE_USE_TEXTURE + temp_attributes.POSITION_BS0 = attr.POSITION_BS0; + temp_attributes.POSITION_BS1 = attr.POSITION_BS1; + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + temp_attributes.NORMAL_BS0 = attr.NORMAL_BS0; + temp_attributes.NORMAL_BS1 = attr.NORMAL_BS1; + temp_attributes.TANGENT_BS0 = attr.TANGENT_BS0; + temp_attributes.TANGENT_BS1 = attr.TANGENT_BS1; + #else + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + temp_attributes.POSITION_BS2 = attr.POSITION_BS2; + temp_attributes.POSITION_BS3 = attr.POSITION_BS3; + + #ifdef RENDERER_BLENDSHAPE_HAS_NORMAL + temp_attributes.NORMAL_BS0 = attr.NORMAL_BS0; + temp_attributes.NORMAL_BS1 = attr.NORMAL_BS1; + temp_attributes.NORMAL_BS2 = attr.NORMAL_BS2; + temp_attributes.NORMAL_BS3 = attr.NORMAL_BS3; + #endif + + #ifdef RENDERER_BLENDSHAPE_HAS_TANGENT + temp_attributes.TANGENT_BS0 = attr.TANGENT_BS0; + temp_attributes.TANGENT_BS1 = attr.TANGENT_BS1; + temp_attributes.TANGENT_BS2 = attr.TANGENT_BS2; + temp_attributes.TANGENT_BS3 = attr.TANGENT_BS3; + #endif + + #else + temp_attributes.POSITION_BS2 = attr.POSITION_BS2; + temp_attributes.POSITION_BS3 = attr.POSITION_BS3; + temp_attributes.POSITION_BS4 = attr.POSITION_BS4; + temp_attributes.POSITION_BS5 = attr.POSITION_BS5; + temp_attributes.POSITION_BS6 = attr.POSITION_BS6; + temp_attributes.POSITION_BS7 = attr.POSITION_BS7; + #endif + #endif + #endif + #endif + + + #ifdef RENDERER_HAS_UV + temp_attributes.TEXCOORD_0 = attr.TEXCOORD_0; + #endif + + #ifdef RENDERER_HAS_UV1 + temp_attributes.TEXCOORD_1 = attr.TEXCOORD_1; + #endif + + #ifdef RENDERER_HAS_SKIN + temp_attributes.JOINTS_0 = attr.JOINTS_0; + temp_attributes.WEIGHTS_0 = attr.WEIGHTS_0; + #endif + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + temp_attributes.COLOR_0 = attr.COLOR_0; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + temp_attributes.NORMAL = attr.NORMAL; + #endif + + #ifdef RENDERER_HAS_TANGENT + temp_attributes.TANGENT = attr.TANGENT; + #endif + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/temp/transformVarying.glsl b/packages/shaderlab/src/shaders/temp/transformVarying.glsl new file mode 100644 index 00000000..efe64137 --- /dev/null +++ b/packages/shaderlab/src/shaders/temp/transformVarying.glsl @@ -0,0 +1,32 @@ + +#ifdef RENDERER_ENABLE_VERTEXCOLOR + temp_varyings.v_color = v.v_color; +#endif + +#if SCENE_FOG_MODE != 0 + temp_varyings.v_positionVS = v.v_positionVS; +#endif + +#ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + temp_varyings.v_normal = v.v_normal; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + temp_varyings.v_TBN = v.v_TBN; + #endif + #endif +#endif + +#ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT==1 + temp_varyings.v_shadowCoord = v.v_shadowCoord; + #endif +#endif + +temp_varyings.v_uv = v.v_uv; + +#ifdef RENDERER_HAS_UV1 + temp_varyings.v_uv1 = v.v_uv1; +#endif +#ifdef MATERIAL_NEED_WORLD_POS + temp_varyings.v_pos = v.v_pos; +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/transform_declare.glsl b/packages/shaderlab/src/shaders/transform_declare.glsl index 5cdee609..2d5aee0f 100644 --- a/packages/shaderlab/src/shaders/transform_declare.glsl +++ b/packages/shaderlab/src/shaders/transform_declare.glsl @@ -4,4 +4,7 @@ mat4 camera_ViewMat; mat4 camera_ProjMat; mat4 renderer_MVMat; mat4 renderer_MVPMat; -mat4 renderer_NormalMat; \ No newline at end of file +mat4 renderer_NormalMat; + +vec3 camera_Position; +vec3 camera_Forward; \ No newline at end of file From 4f52bb1b2321ae3ed9599269a5c9473be03b1caa Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 25 Apr 2024 18:40:19 +0800 Subject: [PATCH 05/89] refactor: delete shadow file --- .../shaderlab/src/shaders/ShadowCoord.glsl | 66 ------------- .../shaders/ShadowFragmentDeclaration.glsl | 98 ------------------- packages/shaderlab/src/shaders/index.ts | 10 +- packages/shaderlab/src/shaders/pbr.gs | 2 +- ...adowVertexDeclaration.glsl => shadow.glsl} | 0 5 files changed, 3 insertions(+), 173 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/ShadowCoord.glsl delete mode 100644 packages/shaderlab/src/shaders/ShadowFragmentDeclaration.glsl rename packages/shaderlab/src/shaders/{ShadowVertexDeclaration.glsl => shadow.glsl} (100%) diff --git a/packages/shaderlab/src/shaders/ShadowCoord.glsl b/packages/shaderlab/src/shaders/ShadowCoord.glsl deleted file mode 100644 index 13e3fbb6..00000000 --- a/packages/shaderlab/src/shaders/ShadowCoord.glsl +++ /dev/null @@ -1,66 +0,0 @@ - -mat4 scene_ShadowMatrices[SCENE_SHADOW_CASCADED_COUNT + 1]; -vec4 scene_ShadowSplitSpheres[4]; - -mediump int computeCascadeIndex(vec3 positionWS) { - vec3 fromCenter0 = positionWS - scene_ShadowSplitSpheres[0].xyz; - vec3 fromCenter1 = positionWS - scene_ShadowSplitSpheres[1].xyz; - vec3 fromCenter2 = positionWS - scene_ShadowSplitSpheres[2].xyz; - vec3 fromCenter3 = positionWS - scene_ShadowSplitSpheres[3].xyz; - - mediump vec4 comparison = vec4( - (dot(fromCenter0, fromCenter0) < scene_ShadowSplitSpheres[0].w), - (dot(fromCenter1, fromCenter1) < scene_ShadowSplitSpheres[1].w), - (dot(fromCenter2, fromCenter2) < scene_ShadowSplitSpheres[2].w), - (dot(fromCenter3, fromCenter3) < scene_ShadowSplitSpheres[3].w)); - comparison.yzw = clamp(comparison.yzw - comparison.xyz,0.0,1.0);//keep the nearest - mediump vec4 indexCoefficient = vec4(4.0,3.0,2.0,1.0); - mediump int index = 4 - int(dot(comparison, indexCoefficient)); - return index; -} - -vec3 getShadowCoord() { - #if SCENE_SHADOW_CASCADED_COUNT == 1 - mediump int cascadeIndex = 0; - #else - mediump int cascadeIndex = computeCascadeIndex(v.v_pos); - #endif - - #ifdef GRAPHICS_API_WEBGL2 - mat4 shadowMatrix = scene_ShadowMatrices[cascadeIndex]; - #else - mat4 shadowMatrix; - #if SCENE_SHADOW_CASCADED_COUNT == 4 - if (cascadeIndex == 0) { - shadowMatrix = scene_ShadowMatrices[0]; - } else if (cascadeIndex == 1) { - shadowMatrix = scene_ShadowMatrices[1]; - } else if (cascadeIndex == 2) { - shadowMatrix = scene_ShadowMatrices[2]; - } else if (cascadeIndex == 3) { - shadowMatrix = scene_ShadowMatrices[3]; - } else { - shadowMatrix = scene_ShadowMatrices[4]; - } - #endif - #if SCENE_SHADOW_CASCADED_COUNT == 2 - if (cascadeIndex == 0) { - shadowMatrix = scene_ShadowMatrices[0]; - } else if (cascadeIndex == 1) { - shadowMatrix = scene_ShadowMatrices[1]; - } else { - shadowMatrix = scene_ShadowMatrices[2]; - } - #endif - #if SCENE_SHADOW_CASCADED_COUNT == 1 - if (cascadeIndex == 0) { - shadowMatrix = scene_ShadowMatrices[0]; - } else { - shadowMatrix = scene_ShadowMatrices[1]; - } - #endif - #endif - - vec4 shadowCoord = shadowMatrix * vec4(v.v_pos, 1.0); - return shadowCoord.xyz; -} diff --git a/packages/shaderlab/src/shaders/ShadowFragmentDeclaration.glsl b/packages/shaderlab/src/shaders/ShadowFragmentDeclaration.glsl deleted file mode 100644 index 380dcbbe..00000000 --- a/packages/shaderlab/src/shaders/ShadowFragmentDeclaration.glsl +++ /dev/null @@ -1,98 +0,0 @@ -#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) - #define SCENE_IS_CALCULATE_SHADOWS -#endif - -#ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT != 1 - #include "ShadowCoord.glsl" - #endif - - // intensity, resolution, sunIndex - vec3 scene_ShadowInfo; - vec4 scene_ShadowMapSize; - - #ifdef GRAPHICS_API_WEBGL2 - mediump sampler2DShadow scene_ShadowMap; - #define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) textureLod(textureName, coord3 , 0.0) - #define TEXTURE2D_SHADOW_PARAM(shadowMap) mediump sampler2DShadow shadowMap - #else - sampler2D scene_ShadowMap; - #ifdef ENGINE_NO_DEPTH_TEXTURE - const vec4 bitShift = vec4(1.0, 1.0/256.0, 1.0/(256.0*256.0), 1.0/(256.0*256.0*256.0)); - /** - * Unpack depth value. - */ - float unpack(in vec4 rgbaDepth) { - return dot(rgbaDepth, bitShift); - } - #define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) (unpack(texture2D(textureName, coord3.xy)) < coord3.z ? 0.0 : 1.0) - #else - #define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) ((texture2D(textureName, coord3.xy)).r < coord3.z ? 0.0 : 1.0) - #endif - #define TEXTURE2D_SHADOW_PARAM(shadowMap) mediump sampler2D shadowMap - #endif - - #if SCENE_SHADOW_TYPE == 2 - float sampleShadowMapFiltered4(TEXTURE2D_SHADOW_PARAM(shadowMap), vec3 shadowCoord, vec4 shadowMapSize) { - float attenuation; - vec4 attenuation4; - vec2 offset=shadowMapSize.xy/2.0; - vec3 shadowCoord0=shadowCoord + vec3(-offset,0.0); - vec3 shadowCoord1=shadowCoord + vec3(offset.x,-offset.y,0.0); - vec3 shadowCoord2=shadowCoord + vec3(-offset.x,offset.y,0.0); - vec3 shadowCoord3=shadowCoord + vec3(offset,0.0); - attenuation4.x = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord0); - attenuation4.y = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord1); - attenuation4.z = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord2); - attenuation4.w = SAMPLE_TEXTURE2D_SHADOW(shadowMap, shadowCoord3); - attenuation = dot(attenuation4, vec4(0.25)); - return attenuation; - } - #endif - - #if SCENE_SHADOW_TYPE == 3 - #include "shadow_sample_tent.glsl" - - float sampleShadowMapFiltered9(TEXTURE2D_SHADOW_PARAM(shadowMap), vec3 shadowCoord, vec4 shadowmapSize) { - float attenuation; - float fetchesWeights[9]; - vec2 fetchesUV[9]; - sampleShadowComputeSamplesTent5x5(shadowmapSize, shadowCoord.xy, fetchesWeights, fetchesUV); - attenuation = fetchesWeights[0] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[0].xy, shadowCoord.z)); - attenuation += fetchesWeights[1] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[1].xy, shadowCoord.z)); - attenuation += fetchesWeights[2] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[2].xy, shadowCoord.z)); - attenuation += fetchesWeights[3] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[3].xy, shadowCoord.z)); - attenuation += fetchesWeights[4] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[4].xy, shadowCoord.z)); - attenuation += fetchesWeights[5] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[5].xy, shadowCoord.z)); - attenuation += fetchesWeights[6] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[6].xy, shadowCoord.z)); - attenuation += fetchesWeights[7] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[7].xy, shadowCoord.z)); - attenuation += fetchesWeights[8] * SAMPLE_TEXTURE2D_SHADOW(shadowMap, vec3(fetchesUV[8].xy, shadowCoord.z)); - return attenuation; - } - #endif - - float sampleShadowMap() { - #if SCENE_SHADOW_CASCADED_COUNT == 1 - vec3 shadowCoord = v.v_shadowCoord; - #else - vec3 shadowCoord = getShadowCoord(); - #endif - - float attenuation = 1.0; - if(shadowCoord.z > 0.0 && shadowCoord.z < 1.0) { - #if SCENE_SHADOW_TYPE == 1 - attenuation = SAMPLE_TEXTURE2D_SHADOW(scene_ShadowMap, shadowCoord); - #endif - - #if SCENE_SHADOW_TYPE == 2 - attenuation = sampleShadowMapFiltered4(scene_ShadowMap, shadowCoord, scene_ShadowMapSize); - #endif - - #if SCENE_SHADOW_TYPE == 3 - attenuation = sampleShadowMapFiltered9(scene_ShadowMap, shadowCoord, scene_ShadowMapSize); - #endif - attenuation = mix(1.0, attenuation, scene_ShadowInfo.x); - } - return attenuation; - } -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 82fa04a7..3ead61f6 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -4,7 +4,6 @@ import begin_position_vert from "./begin_position_vert.glsl"; import blendShape_input from "./blendShape_input.glsl"; import blendShape_vert from "./blendShape_vert.glsl"; import brdf from "./brdf.glsl"; -// import camera_declare from "./camera_declare.glsl"; import color_vert from "./color_vert.glsl"; import common_vert from "./common_vert.glsl"; import common from "./common.glsl"; @@ -20,10 +19,8 @@ import pbr_frag from "./pbr_frag.glsl"; import pbr_helper from "./pbr_helper.glsl"; import position_vert from "./position_vert.glsl"; import shadow_sample_tent from "./shadow_sample_tent.glsl"; -// import ShadowCoord from "./ShadowCoord.glsl"; -// import ShadowFragmentDeclaration from "./ShadowFragmentDeclaration.glsl"; import ShadowVertex from "./ShadowVertex.glsl"; -import ShadowVertexDeclaration from "./ShadowVertexDeclaration.glsl"; +import shadow from "./shadow.glsl"; import skinning_vert from "./skinning_vert.glsl"; import transform_declare from "./transform_declare.glsl"; import uv_vert from "./uv_vert.glsl"; @@ -46,7 +43,6 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: blendShape_input, includeKey: "blendShape_input.glsl" }, { source: blendShape_vert, includeKey: "blendShape_vert.glsl" }, { source: brdf, includeKey: "brdf.glsl" }, - // { source: camera_declare, includeKey: "camera_declare.glsl" }, { source: color_vert, includeKey: "color_vert.glsl" }, { source: common_vert, includeKey: "common_vert.glsl" }, { source: common, includeKey: "common.glsl" }, @@ -62,10 +58,8 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: pbr_helper, includeKey: "pbr_helper.glsl" }, { source: position_vert, includeKey: "position_vert.glsl" }, { source: shadow_sample_tent, includeKey: "shadow_sample_tent.glsl" }, - // { source: ShadowCoord, includeKey: "ShadowCoord.glsl" }, - // { source: ShadowFragmentDeclaration, includeKey: "ShadowFragmentDeclaration.glsl" }, { source: ShadowVertex, includeKey: "ShadowVertex.glsl" }, - { source: ShadowVertexDeclaration, includeKey: "ShadowVertexDeclaration.glsl" }, + { source: shadow, includeKey: "shadow.glsl" }, { source: skinning_vert, includeKey: "skinning_vert.glsl" }, { source: transform_declare, includeKey: "transform_declare.glsl" }, { source: uv_vert, includeKey: "uv_vert.glsl" }, diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 3ce4eee6..58c5d354 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -76,7 +76,7 @@ Shader "pbr.gs" { #include "common_vert.glsl" #include "blendShape_input.glsl" - #include "ShadowVertexDeclaration.glsl" + #include "shadow.glsl" // fragment uniforms #include "fog.glsl" diff --git a/packages/shaderlab/src/shaders/ShadowVertexDeclaration.glsl b/packages/shaderlab/src/shaders/shadow.glsl similarity index 100% rename from packages/shaderlab/src/shaders/ShadowVertexDeclaration.glsl rename to packages/shaderlab/src/shaders/shadow.glsl From 427718899553d220fb0c8358d696a169275875f0 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 25 Apr 2024 19:22:04 +0800 Subject: [PATCH 06/89] refactor: delete shader chunks --- packages/shaderlab/src/shaders/FogVertex.glsl | 4 - .../shaderlab/src/shaders/ShadowVertex.glsl | 5 - .../src/shaders/begin_normal_vert.glsl | 9 - .../src/shaders/begin_position_vert.glsl | 1 - .../shaderlab/src/shaders/color_vert.glsl | 5 - packages/shaderlab/src/shaders/index.ts | 34 +--- .../shaderlab/src/shaders/normal_vert.glsl | 12 -- packages/shaderlab/src/shaders/pbr.gs | 12 +- packages/shaderlab/src/shaders/pbr_vert1.glsl | 2 - packages/shaderlab/src/shaders/pbr_vert2.glsl | 5 - .../shaderlab/src/shaders/position_vert.glsl | 1 - .../{pbr_helper.glsl => shading_pbr.glsl} | 4 +- .../shaderlab/src/shaders/skinning_vert.glsl | 29 --- packages/shaderlab/src/shaders/uv_vert.glsl | 14 -- packages/shaderlab/src/shaders/vert_pbr.glsl | 182 ++++++++++++++++++ .../shaderlab/src/shaders/worldpos_vert.glsl | 4 - 16 files changed, 192 insertions(+), 131 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/FogVertex.glsl delete mode 100644 packages/shaderlab/src/shaders/ShadowVertex.glsl delete mode 100644 packages/shaderlab/src/shaders/begin_normal_vert.glsl delete mode 100644 packages/shaderlab/src/shaders/begin_position_vert.glsl delete mode 100644 packages/shaderlab/src/shaders/color_vert.glsl delete mode 100644 packages/shaderlab/src/shaders/normal_vert.glsl delete mode 100644 packages/shaderlab/src/shaders/pbr_vert1.glsl delete mode 100644 packages/shaderlab/src/shaders/pbr_vert2.glsl delete mode 100644 packages/shaderlab/src/shaders/position_vert.glsl rename packages/shaderlab/src/shaders/{pbr_helper.glsl => shading_pbr.glsl} (99%) delete mode 100644 packages/shaderlab/src/shaders/skinning_vert.glsl delete mode 100644 packages/shaderlab/src/shaders/uv_vert.glsl create mode 100644 packages/shaderlab/src/shaders/vert_pbr.glsl delete mode 100644 packages/shaderlab/src/shaders/worldpos_vert.glsl diff --git a/packages/shaderlab/src/shaders/FogVertex.glsl b/packages/shaderlab/src/shaders/FogVertex.glsl deleted file mode 100644 index d646c8e8..00000000 --- a/packages/shaderlab/src/shaders/FogVertex.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#if SCENE_FOG_MODE != 0 - vec4 positionVS = renderer_MVMat * position; - v.v_positionVS = positionVS.xyz / positionVS.w; -#endif diff --git a/packages/shaderlab/src/shaders/ShadowVertex.glsl b/packages/shaderlab/src/shaders/ShadowVertex.glsl deleted file mode 100644 index bc180176..00000000 --- a/packages/shaderlab/src/shaders/ShadowVertex.glsl +++ /dev/null @@ -1,5 +0,0 @@ -#ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT == 1 - v.v_shadowCoord = getShadowCoord(); - #endif -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/begin_normal_vert.glsl b/packages/shaderlab/src/shaders/begin_normal_vert.glsl deleted file mode 100644 index 88233d29..00000000 --- a/packages/shaderlab/src/shaders/begin_normal_vert.glsl +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 normal = vec3( attr.NORMAL ); - #endif - - #ifdef RENDERER_HAS_TANGENT - vec4 tangent = vec4( attr.TANGENT ); - #endif -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/begin_position_vert.glsl b/packages/shaderlab/src/shaders/begin_position_vert.glsl deleted file mode 100644 index 7d58ad9b..00000000 --- a/packages/shaderlab/src/shaders/begin_position_vert.glsl +++ /dev/null @@ -1 +0,0 @@ - vec4 position = vec4( attr.POSITION , 1.0 ); diff --git a/packages/shaderlab/src/shaders/color_vert.glsl b/packages/shaderlab/src/shaders/color_vert.glsl deleted file mode 100644 index 6c7ddfcb..00000000 --- a/packages/shaderlab/src/shaders/color_vert.glsl +++ /dev/null @@ -1,5 +0,0 @@ - #ifdef RENDERER_ENABLE_VERTEXCOLOR - - v.v_color = COLOR_0; - - #endif diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 3ead61f6..ebe83a60 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -1,35 +1,23 @@ import attrib from "./attrib.glsl"; -import begin_normal_vert from "./begin_normal_vert.glsl"; -import begin_position_vert from "./begin_position_vert.glsl"; import blendShape_input from "./blendShape_input.glsl"; -import blendShape_vert from "./blendShape_vert.glsl"; import brdf from "./brdf.glsl"; -import color_vert from "./color_vert.glsl"; import common_vert from "./common_vert.glsl"; import common from "./common.glsl"; import direct_irradiance_frag_define from "./direct_irradiance_frag_define.glsl"; import fog from "./fog.glsl"; -import FogVertex from "./FogVertex.glsl"; import ibl_frag_define from "./ibl_frag_define.glsl"; import light_frag_define from "./light_frag_define.glsl"; import normal_get from "./normal_get.glsl"; -import normal_vert from "./normal_vert.glsl"; import pbr_frag_define from "./pbr_frag_define.glsl"; import pbr_frag from "./pbr_frag.glsl"; -import pbr_helper from "./pbr_helper.glsl"; -import position_vert from "./position_vert.glsl"; import shadow_sample_tent from "./shadow_sample_tent.glsl"; -import ShadowVertex from "./ShadowVertex.glsl"; import shadow from "./shadow.glsl"; -import skinning_vert from "./skinning_vert.glsl"; import transform_declare from "./transform_declare.glsl"; -import uv_vert from "./uv_vert.glsl"; import varying from "./varying.glsl"; -import worldpos_vert from "./worldpos_vert.glsl"; import pbr from "./pbr.gs"; -import pbr_vert1 from "./pbr_vert1.glsl"; -import pbr_vert2 from "./pbr_vert2.glsl"; +import vert_pbr from "./vert_pbr.glsl"; import input from "./input.glsl"; +import shading_pbr from "./shading_pbr.glsl"; interface IShaderFragment { includeKey: string; @@ -38,35 +26,23 @@ interface IShaderFragment { const pbr_include_fragment_list: IShaderFragment[] = [ { source: attrib, includeKey: "attrib.glsl" }, - { source: begin_normal_vert, includeKey: "begin_normal_vert.glsl" }, - { source: begin_position_vert, includeKey: "begin_position_vert.glsl" }, { source: blendShape_input, includeKey: "blendShape_input.glsl" }, - { source: blendShape_vert, includeKey: "blendShape_vert.glsl" }, { source: brdf, includeKey: "brdf.glsl" }, - { source: color_vert, includeKey: "color_vert.glsl" }, { source: common_vert, includeKey: "common_vert.glsl" }, { source: common, includeKey: "common.glsl" }, { source: direct_irradiance_frag_define, includeKey: "direct_irradiance_frag_define.glsl" }, { source: fog, includeKey: "fog.glsl" }, - { source: FogVertex, includeKey: "FogVertex.glsl" }, { source: ibl_frag_define, includeKey: "ibl_frag_define.glsl" }, { source: light_frag_define, includeKey: "light_frag_define.glsl" }, { source: normal_get, includeKey: "normal_get.glsl" }, - { source: normal_vert, includeKey: "normal_vert.glsl" }, { source: pbr_frag_define, includeKey: "pbr_frag_define.glsl" }, { source: pbr_frag, includeKey: "pbr_frag.glsl" }, - { source: pbr_helper, includeKey: "pbr_helper.glsl" }, - { source: position_vert, includeKey: "position_vert.glsl" }, { source: shadow_sample_tent, includeKey: "shadow_sample_tent.glsl" }, - { source: ShadowVertex, includeKey: "ShadowVertex.glsl" }, { source: shadow, includeKey: "shadow.glsl" }, - { source: skinning_vert, includeKey: "skinning_vert.glsl" }, { source: transform_declare, includeKey: "transform_declare.glsl" }, - { source: uv_vert, includeKey: "uv_vert.glsl" }, { source: varying, includeKey: "varying.glsl" }, - { source: worldpos_vert, includeKey: "worldpos_vert.glsl" }, - { source: pbr_vert1, includeKey: "pbr_vert1.glsl" }, - { source: pbr_vert2, includeKey: "pbr_vert2.glsl" }, - { source: input, includeKey: "input.glsl" } + { source: vert_pbr, includeKey: "vert_pbr.glsl" }, + { source: input, includeKey: "input.glsl" }, + { source: shading_pbr, includeKey: "shading_pbr.glsl" } ]; export { pbr as pbrSource, pbr_include_fragment_list }; diff --git a/packages/shaderlab/src/shaders/normal_vert.glsl b/packages/shaderlab/src/shaders/normal_vert.glsl deleted file mode 100644 index 7e8fdb23..00000000 --- a/packages/shaderlab/src/shaders/normal_vert.glsl +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); - - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); - vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; - - v.v_TBN = mat3( tangentW, bitangentW, v.v_normal ); - #endif - #endif -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 58c5d354..f04ca0d1 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -82,8 +82,8 @@ Shader "pbr.gs" { #include "fog.glsl" #include "light_frag_define.glsl" #include "pbr_frag_define.glsl" - #include "pbr_helper.glsl" + #include "shading_pbr.glsl" // new #include "input.glsl" @@ -94,15 +94,7 @@ Shader "pbr.gs" { _galacean_v2f pbrVert(_galacean_a2v attr) { _galacean_v2f v; - #include "pbr_vert1.glsl" - - #include "blendShape_vert.glsl" - #include "skinning_vert.glsl" - - #include "pbr_vert2.glsl" - - #include "ShadowVertex.glsl" - #include "FogVertex.glsl" + #include "vert_pbr.glsl" return v; } diff --git a/packages/shaderlab/src/shaders/pbr_vert1.glsl b/packages/shaderlab/src/shaders/pbr_vert1.glsl deleted file mode 100644 index c8c72ada..00000000 --- a/packages/shaderlab/src/shaders/pbr_vert1.glsl +++ /dev/null @@ -1,2 +0,0 @@ -#include "begin_position_vert.glsl" -#include "begin_normal_vert.glsl" \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/pbr_vert2.glsl b/packages/shaderlab/src/shaders/pbr_vert2.glsl deleted file mode 100644 index 5a9a40ad..00000000 --- a/packages/shaderlab/src/shaders/pbr_vert2.glsl +++ /dev/null @@ -1,5 +0,0 @@ -#include "uv_vert.glsl" -#include "color_vert.glsl" -#include "normal_vert.glsl" -#include "worldpos_vert.glsl" -#include "position_vert.glsl" \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/position_vert.glsl b/packages/shaderlab/src/shaders/position_vert.glsl deleted file mode 100644 index 75bc9c5b..00000000 --- a/packages/shaderlab/src/shaders/position_vert.glsl +++ /dev/null @@ -1 +0,0 @@ - gl_Position = renderer_MVPMat * position; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/pbr_helper.glsl b/packages/shaderlab/src/shaders/shading_pbr.glsl similarity index 99% rename from packages/shaderlab/src/shaders/pbr_helper.glsl rename to packages/shaderlab/src/shaders/shading_pbr.glsl index a646528d..f301a908 100644 --- a/packages/shaderlab/src/shaders/pbr_helper.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr.glsl @@ -158,7 +158,9 @@ void initMaterial(out Material material, inout Geometry geometry){ } + + // direct + indirect #include "brdf.glsl" #include "direct_irradiance_frag_define.glsl" -#include "ibl_frag_define.glsl" +#include "ibl_frag_define.glsl" \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/skinning_vert.glsl b/packages/shaderlab/src/shaders/skinning_vert.glsl deleted file mode 100644 index 858e5d6f..00000000 --- a/packages/shaderlab/src/shaders/skinning_vert.glsl +++ /dev/null @@ -1,29 +0,0 @@ -#ifdef RENDERER_HAS_SKIN - - #ifdef RENDERER_USE_JOINT_TEXTURE - mat4 skinMatrix = - attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + - attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + - attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + - attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); - - #else - mat4 skinMatrix = - attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + - attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + - attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + - attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; - #endif - - position = skinMatrix * position; - - #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) - mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); - normal = normal * skinNormalMatrix; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - tangent.xyz = tangent.xyz * skinNormalMatrix; - #endif - - #endif - -#endif diff --git a/packages/shaderlab/src/shaders/uv_vert.glsl b/packages/shaderlab/src/shaders/uv_vert.glsl deleted file mode 100644 index 923f7bf1..00000000 --- a/packages/shaderlab/src/shaders/uv_vert.glsl +++ /dev/null @@ -1,14 +0,0 @@ -#ifdef RENDERER_HAS_UV - v.v_uv = attr.TEXCOORD_0; -#else - // may need this calculate normal - v.v_uv = vec2( 0.0, 0.0 ); -#endif - -#ifdef RENDERER_HAS_UV1 - v.v_uv1 = attr.TEXCOORD_1; -#endif - -#ifdef MATERIAL_NEED_TILING_OFFSET - v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/vert_pbr.glsl b/packages/shaderlab/src/shaders/vert_pbr.glsl new file mode 100644 index 00000000..ad3c0736 --- /dev/null +++ b/packages/shaderlab/src/shaders/vert_pbr.glsl @@ -0,0 +1,182 @@ +vec4 position = vec4( attr.POSITION , 1.0 ); + +#ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 normal = vec3( attr.NORMAL ); + #endif + + #ifdef RENDERER_HAS_TANGENT + vec4 tangent = vec4( attr.TANGENT ); + #endif +#endif + + +// blendShape +#ifdef RENDERER_HAS_BLENDSHAPE + #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE + int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; + for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){ + int vertexElementOffset = vertexOffset; + float weight = renderer_BlendShapeWeights[i]; + // Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13 + if(weight != 0.0){ + position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + + #ifndef MATERIAL_OMIT_NORMAL + #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) + vertexElementOffset += 1; + normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif + + #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + vertexElementOffset += 1; + tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif + #endif + } + + } + #else + position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; + position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; + + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + #endif + #if defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + #endif + #endif + #else + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + #ifndef MATERIAL_OMIT_NORMAL + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; + normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; + #endif + + #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; + tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; + #endif + #endif + #else + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; + position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; + position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; + position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; + #endif + #endif + #endif +#endif + + + +// skin +#ifdef RENDERER_HAS_SKIN + + #ifdef RENDERER_USE_JOINT_TEXTURE + mat4 skinMatrix = + attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + + attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + + attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + + attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); + + #else + mat4 skinMatrix = + attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + + attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + + attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + + attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; + #endif + + position = skinMatrix * position; + + #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) + mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); + normal = normal * skinNormalMatrix; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + tangent.xyz = tangent.xyz * skinNormalMatrix; + #endif + + #endif + +#endif + + +// shadow +#ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT == 1 + v.v_shadowCoord = getShadowCoord(); + #endif +#endif + + +// fog +#if SCENE_FOG_MODE != 0 + vec4 positionVS = renderer_MVMat * position; + v.v_positionVS = positionVS.xyz / positionVS.w; +#endif + + + + +// uv_vert +#ifdef RENDERER_HAS_UV + v.v_uv = attr.TEXCOORD_0; +#else + // may need this calculate normal + v.v_uv = vec2( 0.0, 0.0 ); +#endif + +#ifdef RENDERER_HAS_UV1 + v.v_uv1 = attr.TEXCOORD_1; +#endif + +#ifdef MATERIAL_NEED_TILING_OFFSET + v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; +#endif + + +// color_vert +#ifdef RENDERER_ENABLE_VERTEXCOLOR + v.v_color = COLOR_0; +#endif + + +// normal_vert +#ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); + + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); + vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; + + v.v_TBN = mat3( tangentW, bitangentW, v.v_normal ); + #endif + #endif +#endif + + +// worldpos_vert +#ifdef MATERIAL_NEED_WORLD_POS + vec4 temp_pos = renderer_ModelMat * position; + v.v_pos = temp_pos.xyz / temp_pos.w; +#endif + +// position_vert +gl_Position = renderer_MVPMat * position; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/worldpos_vert.glsl b/packages/shaderlab/src/shaders/worldpos_vert.glsl deleted file mode 100644 index acec975f..00000000 --- a/packages/shaderlab/src/shaders/worldpos_vert.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#ifdef MATERIAL_NEED_WORLD_POS - vec4 temp_pos = renderer_ModelMat * position; - v.v_pos = temp_pos.xyz / temp_pos.w; -#endif From 6f1bb858ec8e47c2a68ae05b1fb3b844f45b4afc Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 25 Apr 2024 20:01:27 +0800 Subject: [PATCH 07/89] refactor: init shading_pbr --- packages/shaderlab/src/shaders/index.ts | 15 +-- packages/shaderlab/src/shaders/pbr.gs | 11 ++- packages/shaderlab/src/shaders/pbr_frag.glsl | 73 -------------- .../src/shaders/{ => shading_pbr}/brdf.glsl | 0 .../src/shaders/shading_pbr/index.ts | 13 +++ .../light_direct_pbr.glsl} | 0 .../light_indirect_pbr.glsl} | 0 .../material_pbr.glsl} | 0 .../{ => shading_pbr}/shading_pbr.glsl | 95 ++++++++++++++++++- 9 files changed, 115 insertions(+), 92 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/pbr_frag.glsl rename packages/shaderlab/src/shaders/{ => shading_pbr}/brdf.glsl (100%) create mode 100644 packages/shaderlab/src/shaders/shading_pbr/index.ts rename packages/shaderlab/src/shaders/{direct_irradiance_frag_define.glsl => shading_pbr/light_direct_pbr.glsl} (100%) rename packages/shaderlab/src/shaders/{ibl_frag_define.glsl => shading_pbr/light_indirect_pbr.glsl} (100%) rename packages/shaderlab/src/shaders/{pbr_frag_define.glsl => shading_pbr/material_pbr.glsl} (100%) rename packages/shaderlab/src/shaders/{ => shading_pbr}/shading_pbr.glsl (67%) diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index ebe83a60..4a9e3369 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -1,15 +1,10 @@ import attrib from "./attrib.glsl"; import blendShape_input from "./blendShape_input.glsl"; -import brdf from "./brdf.glsl"; import common_vert from "./common_vert.glsl"; import common from "./common.glsl"; -import direct_irradiance_frag_define from "./direct_irradiance_frag_define.glsl"; import fog from "./fog.glsl"; -import ibl_frag_define from "./ibl_frag_define.glsl"; import light_frag_define from "./light_frag_define.glsl"; import normal_get from "./normal_get.glsl"; -import pbr_frag_define from "./pbr_frag_define.glsl"; -import pbr_frag from "./pbr_frag.glsl"; import shadow_sample_tent from "./shadow_sample_tent.glsl"; import shadow from "./shadow.glsl"; import transform_declare from "./transform_declare.glsl"; @@ -17,7 +12,7 @@ import varying from "./varying.glsl"; import pbr from "./pbr.gs"; import vert_pbr from "./vert_pbr.glsl"; import input from "./input.glsl"; -import shading_pbr from "./shading_pbr.glsl"; +import shading_pbr from "./shading_pbr"; interface IShaderFragment { includeKey: string; @@ -27,22 +22,18 @@ interface IShaderFragment { const pbr_include_fragment_list: IShaderFragment[] = [ { source: attrib, includeKey: "attrib.glsl" }, { source: blendShape_input, includeKey: "blendShape_input.glsl" }, - { source: brdf, includeKey: "brdf.glsl" }, { source: common_vert, includeKey: "common_vert.glsl" }, { source: common, includeKey: "common.glsl" }, - { source: direct_irradiance_frag_define, includeKey: "direct_irradiance_frag_define.glsl" }, { source: fog, includeKey: "fog.glsl" }, - { source: ibl_frag_define, includeKey: "ibl_frag_define.glsl" }, { source: light_frag_define, includeKey: "light_frag_define.glsl" }, { source: normal_get, includeKey: "normal_get.glsl" }, - { source: pbr_frag_define, includeKey: "pbr_frag_define.glsl" }, - { source: pbr_frag, includeKey: "pbr_frag.glsl" }, { source: shadow_sample_tent, includeKey: "shadow_sample_tent.glsl" }, { source: shadow, includeKey: "shadow.glsl" }, { source: transform_declare, includeKey: "transform_declare.glsl" }, { source: varying, includeKey: "varying.glsl" }, { source: vert_pbr, includeKey: "vert_pbr.glsl" }, { source: input, includeKey: "input.glsl" }, - { source: shading_pbr, includeKey: "shading_pbr.glsl" } + + ...shading_pbr ]; export { pbr as pbrSource, pbr_include_fragment_list }; diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index f04ca0d1..b77bd288 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -81,7 +81,6 @@ Shader "pbr.gs" { // fragment uniforms #include "fog.glsl" #include "light_frag_define.glsl" - #include "pbr_frag_define.glsl" #include "shading_pbr.glsl" @@ -100,7 +99,15 @@ Shader "pbr.gs" { } void pbrFrag(_galacean_v2f v) { - #include "pbr_frag.glsl" + Geometry geometry; + Material material; + + initSurfaceData(geometry, material); + + vec4 color = evaluateSurface(geometry, material); + + gl_FragColor = color; + #if SCENE_FOG_MODE != 0 gl_FragColor = fog(gl_FragColor, v.v_positionVS); diff --git a/packages/shaderlab/src/shaders/pbr_frag.glsl b/packages/shaderlab/src/shaders/pbr_frag.glsl deleted file mode 100644 index 542be214..00000000 --- a/packages/shaderlab/src/shaders/pbr_frag.glsl +++ /dev/null @@ -1,73 +0,0 @@ -Geometry geometry; -Material material; -ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); - -initGeometry(geometry, gl_FrontFacing); -initMaterial(material, geometry); - -// Direct Light -addTotalDirectRadiance(geometry, material, reflectedLight); - -// IBL diffuse -#ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, geometry.normal); - #ifdef ENGINE_IS_COLORSPACE_GAMMA - irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; - #endif - irradiance *= scene_EnvMapLight.diffuseIntensity; -#else - vec3 irradiance = scene_EnvMapLight.diffuse * scene_EnvMapLight.diffuseIntensity; - irradiance *= PI; -#endif - -reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); - -// IBL specular -vec3 radiance = getLightProbeRadiance(geometry, geometry.normal, material.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); -float radianceAttenuation = 1.0; - -#ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( geometry, geometry.clearCoatNormal, material.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); - - reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * envBRDFApprox(vec3( 0.04 ), material.clearCoatRoughness, geometry.clearCoatDotNV); - radianceAttenuation -= material.clearCoat * F_Schlick(material.f0, geometry.clearCoatDotNV); -#endif - -reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(material.specularColor, material.roughness, geometry.dotNV ); - - -// Occlusion -#ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - reflectedLight.indirectDiffuse *= ambientOcclusion; - #ifdef SCENE_USE_SPECULAR_ENV - reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, material.roughness, geometry.dotNV); - #endif -#endif - - -// Emissive -vec3 emissiveRadiance = material_EmissiveColor; -#ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - emissiveColor = gammaToLinear(emissiveColor); - #endif - emissiveRadiance *= emissiveColor.rgb; -#endif - -// Total -vec3 totalRadiance = reflectedLight.directDiffuse + - reflectedLight.indirectDiffuse + - reflectedLight.directSpecular + - reflectedLight.indirectSpecular + - emissiveRadiance; - -vec4 targetColor =vec4(totalRadiance, material.opacity); -gl_FragColor = targetColor; diff --git a/packages/shaderlab/src/shaders/brdf.glsl b/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl similarity index 100% rename from packages/shaderlab/src/shaders/brdf.glsl rename to packages/shaderlab/src/shaders/shading_pbr/brdf.glsl diff --git a/packages/shaderlab/src/shaders/shading_pbr/index.ts b/packages/shaderlab/src/shaders/shading_pbr/index.ts new file mode 100644 index 00000000..1254d5da --- /dev/null +++ b/packages/shaderlab/src/shaders/shading_pbr/index.ts @@ -0,0 +1,13 @@ +import brdf from "./brdf.glsl"; +import light_direct_pbr from "./light_direct_pbr.glsl"; +import light_indirect_pbr from "./light_indirect_pbr.glsl"; +import material_pbr from "./material_pbr.glsl"; +import shading_pbr from "./shading_pbr.glsl"; + +export default [ + { source: brdf, includeKey: "brdf.glsl" }, + { source: light_direct_pbr, includeKey: "light_direct_pbr.glsl" }, + { source: light_indirect_pbr, includeKey: "light_indirect_pbr.glsl" }, + { source: material_pbr, includeKey: "material_pbr.glsl" }, + { source: shading_pbr, includeKey: "shading_pbr.glsl" } +]; diff --git a/packages/shaderlab/src/shaders/direct_irradiance_frag_define.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl similarity index 100% rename from packages/shaderlab/src/shaders/direct_irradiance_frag_define.glsl rename to packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl diff --git a/packages/shaderlab/src/shaders/ibl_frag_define.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl similarity index 100% rename from packages/shaderlab/src/shaders/ibl_frag_define.glsl rename to packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl diff --git a/packages/shaderlab/src/shaders/pbr_frag_define.glsl b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl similarity index 100% rename from packages/shaderlab/src/shaders/pbr_frag_define.glsl rename to packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl diff --git a/packages/shaderlab/src/shaders/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl similarity index 67% rename from packages/shaderlab/src/shaders/shading_pbr.glsl rename to packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index f301a908..c54bdb6d 100644 --- a/packages/shaderlab/src/shaders/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -1,5 +1,9 @@ +#include "material_pbr.glsl" #include "normal_get.glsl" - +// direct + indirect +#include "brdf.glsl" +#include "light_direct_pbr.glsl" +#include "light_indirect_pbr.glsl" float computeSpecularOcclusion(float ambientOcclusion, float roughness, float dotNV ) { return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); @@ -158,9 +162,90 @@ void initMaterial(out Material material, inout Geometry geometry){ } +void initSurfaceData(out Geometry geometry, out Material material){ + + +initGeometry(geometry, gl_FrontFacing); +initMaterial(material, geometry); + + +} + + +vec4 evaluateSurface(in Geometry geometry, in Material material){ + + ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); + + // Direct Light + addTotalDirectRadiance(geometry, material, reflectedLight); + + // IBL diffuse + #ifdef SCENE_USE_SH + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, geometry.normal); + #ifdef ENGINE_IS_COLORSPACE_GAMMA + irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; + #endif + irradiance *= scene_EnvMapLight.diffuseIntensity; + #else + vec3 irradiance = scene_EnvMapLight.diffuse * scene_EnvMapLight.diffuseIntensity; + irradiance *= PI; + #endif + + reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); + + // IBL specular + vec3 radiance = getLightProbeRadiance(geometry, geometry.normal, material.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); + float radianceAttenuation = 1.0; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatRadiance = getLightProbeRadiance( geometry, geometry.clearCoatNormal, material.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); + + reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * envBRDFApprox(vec3( 0.04 ), material.clearCoatRoughness, geometry.clearCoatDotNV); + radianceAttenuation -= material.clearCoat * F_Schlick(material.f0, geometry.clearCoatDotNV); + #endif + + reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(material.specularColor, material.roughness, geometry.dotNV ); + + + // Occlusion + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + reflectedLight.indirectDiffuse *= ambientOcclusion; + #ifdef SCENE_USE_SPECULAR_ENV + reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, material.roughness, geometry.dotNV); + #endif + #endif + + + // Emissive + vec3 emissiveRadiance = material_EmissiveColor; + #ifdef MATERIAL_HAS_EMISSIVETEXTURE + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + emissiveColor = gammaToLinear(emissiveColor); + #endif + emissiveRadiance *= emissiveColor.rgb; + #endif + + // Total + vec3 totalRadiance = reflectedLight.directDiffuse + + reflectedLight.indirectDiffuse + + reflectedLight.directSpecular + + reflectedLight.indirectSpecular + + emissiveRadiance; + + vec4 targetColor =vec4(totalRadiance, material.opacity); + + + return targetColor; + + +} -// direct + indirect -#include "brdf.glsl" -#include "direct_irradiance_frag_define.glsl" -#include "ibl_frag_define.glsl" \ No newline at end of file From 9de2d4adfb050a22a26ee6fe223e0adac8c48cfd Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 25 Apr 2024 21:23:33 +0800 Subject: [PATCH 08/89] refactor: init ibl and radiance --- .../shaders/shading_pbr/light_direct_pbr.glsl | 2 +- .../shading_pbr/light_indirect_pbr.glsl | 50 ++++++++++++ .../src/shaders/shading_pbr/material_pbr.glsl | 1 + .../src/shaders/shading_pbr/shading_pbr.glsl | 78 ++++--------------- 4 files changed, 69 insertions(+), 62 deletions(-) diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl index 6bc18c17..defca9ad 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl @@ -70,7 +70,7 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Ma #endif -void addTotalDirectRadiance(Geometry geometry, Material material, inout ReflectedLight reflectedLight){ +void evaluateDirectRadiance(Geometry geometry, Material material, inout ReflectedLight reflectedLight){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl index 37b22b16..ebf6b569 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl @@ -84,4 +84,54 @@ vec3 getLightProbeRadiance(Geometry geometry, vec3 normal, float roughness, int #endif +} + +float computeSpecularOcclusion(float ambientOcclusion, float roughness, float dotNV ) { + return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); +} + +void evaluateIBL(Geometry geometry, Material material, inout ReflectedLight reflectedLight ){ + // IBL diffuse + #ifdef SCENE_USE_SH + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, geometry.normal); + #ifdef ENGINE_IS_COLORSPACE_GAMMA + irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; + #endif + irradiance *= scene_EnvMapLight.diffuseIntensity; + #else + vec3 irradiance = scene_EnvMapLight.diffuse * scene_EnvMapLight.diffuseIntensity; + irradiance *= PI; + #endif + + reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); + + // IBL specular + vec3 radiance = getLightProbeRadiance(geometry, geometry.normal, material.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); + float radianceAttenuation = 1.0; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatRadiance = getLightProbeRadiance( geometry, geometry.clearCoatNormal, material.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); + + reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * envBRDFApprox(vec3( 0.04 ), material.clearCoatRoughness, geometry.clearCoatDotNV); + radianceAttenuation -= material.clearCoat * F_Schlick(material.f0, geometry.clearCoatDotNV); + #endif + + reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(material.specularColor, material.roughness, geometry.dotNV ); + + + // Occlusion + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + reflectedLight.indirectDiffuse *= ambientOcclusion; + #ifdef SCENE_USE_SPECULAR_ENV + reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, material.roughness, geometry.dotNV); + #endif + #endif + } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl index 6ab2eced..b77f2582 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl @@ -100,5 +100,6 @@ struct Material { float clearCoat; float clearCoatRoughness; #endif + vec3 emissive; } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index c54bdb6d..ba07d4a5 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -5,9 +5,7 @@ #include "light_direct_pbr.glsl" #include "light_indirect_pbr.glsl" -float computeSpecularOcclusion(float ambientOcclusion, float roughness, float dotNV ) { - return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); -} + float getAARoughnessFactor(vec3 normal) { // Kaplanyan 2016, "Stable specular highlights" @@ -160,6 +158,16 @@ void initMaterial(out Material material, inout Geometry geometry){ geometry.anisotropicN = getAnisotropicBentNormal(geometry, geometry.normal, material.roughness); #endif + vec3 emissiveRadiance = material_EmissiveColor; + #ifdef MATERIAL_HAS_EMISSIVETEXTURE + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + emissiveColor = gammaToLinear(emissiveColor); + #endif + emissiveRadiance *= emissiveColor.rgb; + #endif + material.emissive = emissiveRadiance; + } void initSurfaceData(out Geometry geometry, out Material material){ @@ -177,73 +185,21 @@ vec4 evaluateSurface(in Geometry geometry, in Material material){ ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); // Direct Light - addTotalDirectRadiance(geometry, material, reflectedLight); - - // IBL diffuse - #ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, geometry.normal); - #ifdef ENGINE_IS_COLORSPACE_GAMMA - irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; - #endif - irradiance *= scene_EnvMapLight.diffuseIntensity; - #else - vec3 irradiance = scene_EnvMapLight.diffuse * scene_EnvMapLight.diffuseIntensity; - irradiance *= PI; - #endif - - reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); - - // IBL specular - vec3 radiance = getLightProbeRadiance(geometry, geometry.normal, material.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); - float radianceAttenuation = 1.0; - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( geometry, geometry.clearCoatNormal, material.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); - - reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * envBRDFApprox(vec3( 0.04 ), material.clearCoatRoughness, geometry.clearCoatDotNV); - radianceAttenuation -= material.clearCoat * F_Schlick(material.f0, geometry.clearCoatDotNV); - #endif - - reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(material.specularColor, material.roughness, geometry.dotNV ); - - - // Occlusion - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - reflectedLight.indirectDiffuse *= ambientOcclusion; - #ifdef SCENE_USE_SPECULAR_ENV - reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, material.roughness, geometry.dotNV); - #endif - #endif + evaluateDirectRadiance(geometry, material, reflectedLight); - // Emissive - vec3 emissiveRadiance = material_EmissiveColor; - #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - emissiveColor = gammaToLinear(emissiveColor); - #endif - emissiveRadiance *= emissiveColor.rgb; - #endif + // IBL + evaluateIBL(geometry, material, reflectedLight); // Total vec3 totalRadiance = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + - reflectedLight.indirectSpecular + - emissiveRadiance; - - vec4 targetColor =vec4(totalRadiance, material.opacity); + reflectedLight.indirectSpecular + + material.emissive; - return targetColor; + return vec4(totalRadiance, material.opacity); } From f9fa5418b3a4ef0fa6b6cffea1bcdb3cffcd0189 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 25 Apr 2024 21:34:29 +0800 Subject: [PATCH 09/89] refactor: init surfaceData.glsl --- packages/shaderlab/src/shaders/input.glsl | 34 --- .../src/shaders/shading_pbr/index.ts | 4 +- .../src/shaders/shading_pbr/shading_pbr.glsl | 175 +--------------- .../shaders/shading_pbr/surfaceData_pbr.glsl | 198 ++++++++++++++++++ 4 files changed, 202 insertions(+), 209 deletions(-) create mode 100644 packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/input.glsl index 29a70361..c5bb1ddc 100644 --- a/packages/shaderlab/src/shaders/input.glsl +++ b/packages/shaderlab/src/shaders/input.glsl @@ -4,35 +4,6 @@ // vec4 tangent; // } -struct SurfaceData{ - vec3 position; - vec3 normal; - vec3 viewDir; - float dotNV; - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatNormal; - float clearCoatDotNV; - #endif - - #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 anisotropicT; - vec3 anisotropicB; - vec3 anisotropicN; - float anisotropy; - #endif - - vec3 diffuseColor; - float roughness; - vec3 specularColor; - float opacity; - float f0; - #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoat; - float clearCoatRoughness; - #endif -} - struct Attributes{ vec3 POSITION; @@ -254,9 +225,4 @@ void intVertexData(in Temp_Attributes attributes, out Temp_Varyings varyings){ #endif -} - - -void initSurfaceData(in Temp_Varyings varyings, out SurfaceData surfaceData){ - } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/index.ts b/packages/shaderlab/src/shaders/shading_pbr/index.ts index 1254d5da..13c8961b 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/index.ts +++ b/packages/shaderlab/src/shaders/shading_pbr/index.ts @@ -3,11 +3,13 @@ import light_direct_pbr from "./light_direct_pbr.glsl"; import light_indirect_pbr from "./light_indirect_pbr.glsl"; import material_pbr from "./material_pbr.glsl"; import shading_pbr from "./shading_pbr.glsl"; +import surfaceData_pbr from "./surfaceData_pbr.glsl"; export default [ { source: brdf, includeKey: "brdf.glsl" }, { source: light_direct_pbr, includeKey: "light_direct_pbr.glsl" }, { source: light_indirect_pbr, includeKey: "light_indirect_pbr.glsl" }, { source: material_pbr, includeKey: "material_pbr.glsl" }, - { source: shading_pbr, includeKey: "shading_pbr.glsl" } + { source: shading_pbr, includeKey: "shading_pbr.glsl" }, + { source: surfaceData_pbr, includeKey: "surfaceData_pbr.glsl" } ]; diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index ba07d4a5..469c69b2 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -4,180 +4,7 @@ #include "brdf.glsl" #include "light_direct_pbr.glsl" #include "light_indirect_pbr.glsl" - - - -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); - #else - return MIN_PERCEPTUAL_ROUGHNESS; - #endif -} - -#ifdef MATERIAL_ENABLE_ANISOTROPY - // Aniso Bent Normals - // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(Geometry geometry, vec3 n, float roughness) { - vec3 anisotropyDirection = (geometry.anisotropy >= 0.0) ? geometry.anisotropicB : geometry.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, geometry.viewDir); - vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); - // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(n, anisotropicNormal, abs(geometry.anisotropy) * saturate( 5.0 * roughness)) ); - - return bentNormal; - } -#endif - -void initGeometry(out Geometry geometry, bool isFrontFacing){ - geometry.position = v.v_pos; - #ifdef CAMERA_ORTHOGRAPHIC - geometry.viewDir = -camera_Forward; - #else - geometry.viewDir = normalize(camera_Position - v.v_pos); - #endif - #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) - mat3 tbn = getTBN(isFrontFacing); - #endif - - #ifdef MATERIAL_HAS_NORMALTEXTURE - geometry.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - geometry.normal = getNormal(isFrontFacing); - #endif - - geometry.dotNV = saturate( dot(geometry.normal, geometry.viewDir) ); - - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - geometry.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - geometry.clearCoatNormal = getNormal(isFrontFacing); - #endif - geometry.clearCoatDotNV = saturate( dot(geometry.clearCoatNormal, geometry.viewDir) ); - #endif - - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy = material_AnisotropyInfo.z; - vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; - anisotropy *= anisotropyTextureInfo.b; - anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; - #endif - - geometry.anisotropy = anisotropy; - geometry.anisotropicT = normalize(tbn * anisotropicDirection); - geometry.anisotropicB = normalize(cross(geometry.normal, geometry.anisotropicT)); - #endif -} - -void initMaterial(out Material material, inout Geometry geometry){ - vec4 baseColor = material_BaseColor; - float metal = material_Metal; - float roughness = material_Roughness; - vec3 specularColor = material_PBRSpecularColor; - float glossiness = material_Glossiness; - float alphaCutoff = material_AlphaCutoff; - float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); - - material.f0 = f0; - - #ifdef MATERIAL_HAS_BASETEXTURE - vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - baseTextureColor = gammaToLinear(baseTextureColor); - #endif - baseColor *= baseTextureColor; - #endif - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - baseColor *= v.v_color; - #endif - - - #ifdef MATERIAL_IS_ALPHA_CUTOFF - if( baseColor.a < alphaCutoff ) { - discard; - } - #endif - - #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE - vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); - roughness *= metalRoughMapColor.g; - metal *= metalRoughMapColor.b; - #endif - - #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE - vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - specularGlossinessColor = gammaToLinear(specularGlossinessColor); - #endif - specularColor *= specularGlossinessColor.rgb; - glossiness *= specularGlossinessColor.a; - #endif - - - #ifdef IS_METALLIC_WORKFLOW - material.diffuseColor = baseColor.rgb * ( 1.0 - metal ); - material.specularColor = mix( vec3(f0), baseColor.rgb, metal ); - material.roughness = roughness; - #else - float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); - material.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); - material.specularColor = specularColor; - material.roughness = 1.0 - glossiness; - #endif - - material.roughness = max(material.roughness, getAARoughnessFactor(geometry.normal)); - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - material.clearCoat = material_ClearCoat; - material.clearCoatRoughness = material_ClearCoatRoughness; - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - material.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; - #endif - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - material.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; - #endif - material.clearCoat = saturate( material.clearCoat ); - material.clearCoatRoughness = max(material.clearCoatRoughness, getAARoughnessFactor(geometry.clearCoatNormal)); - #endif - - #ifdef MATERIAL_IS_TRANSPARENT - material.opacity = baseColor.a; - #else - material.opacity = 1.0; - #endif - #ifdef MATERIAL_ENABLE_ANISOTROPY - geometry.anisotropicN = getAnisotropicBentNormal(geometry, geometry.normal, material.roughness); - #endif - - vec3 emissiveRadiance = material_EmissiveColor; - #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - emissiveColor = gammaToLinear(emissiveColor); - #endif - emissiveRadiance *= emissiveColor.rgb; - #endif - material.emissive = emissiveRadiance; - -} - -void initSurfaceData(out Geometry geometry, out Material material){ - - -initGeometry(geometry, gl_FrontFacing); -initMaterial(material, geometry); - - -} +#include "surfaceData_pbr.glsl" vec4 evaluateSurface(in Geometry geometry, in Material material){ diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl new file mode 100644 index 00000000..5aef3df4 --- /dev/null +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -0,0 +1,198 @@ +struct SurfaceData{ + vec3 position; + vec3 normal; + vec3 viewDir; + float dotNV; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatNormal; + float clearCoatDotNV; + #endif + + #ifdef MATERIAL_ENABLE_ANISOTROPY + vec3 anisotropicT; + vec3 anisotropicB; + vec3 anisotropicN; + float anisotropy; + #endif + + vec3 diffuseColor; + float roughness; + vec3 specularColor; + float opacity; + float f0; + #ifdef MATERIAL_ENABLE_CLEAR_COAT + float clearCoat; + float clearCoatRoughness; + #endif + vec3 emissive; +} + + +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); + #else + return MIN_PERCEPTUAL_ROUGHNESS; + #endif +} + +#ifdef MATERIAL_ENABLE_ANISOTROPY + // Aniso Bent Normals + // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far + vec3 getAnisotropicBentNormal(Geometry geometry, vec3 n, float roughness) { + vec3 anisotropyDirection = (geometry.anisotropy >= 0.0) ? geometry.anisotropicB : geometry.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, geometry.viewDir); + vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); + // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 + vec3 bentNormal = normalize( mix(n, anisotropicNormal, abs(geometry.anisotropy) * saturate( 5.0 * roughness)) ); + + return bentNormal; + } +#endif + +void initGeometry(out Geometry geometry, bool isFrontFacing){ + geometry.position = v.v_pos; + #ifdef CAMERA_ORTHOGRAPHIC + geometry.viewDir = -camera_Forward; + #else + geometry.viewDir = normalize(camera_Position - v.v_pos); + #endif + #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) + mat3 tbn = getTBN(isFrontFacing); + #endif + + #ifdef MATERIAL_HAS_NORMALTEXTURE + geometry.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + geometry.normal = getNormal(isFrontFacing); + #endif + + geometry.dotNV = saturate( dot(geometry.normal, geometry.viewDir) ); + + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + geometry.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + geometry.clearCoatNormal = getNormal(isFrontFacing); + #endif + geometry.clearCoatDotNV = saturate( dot(geometry.clearCoatNormal, geometry.viewDir) ); + #endif + + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy = material_AnisotropyInfo.z; + vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; + anisotropy *= anisotropyTextureInfo.b; + anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; + #endif + + geometry.anisotropy = anisotropy; + geometry.anisotropicT = normalize(tbn * anisotropicDirection); + geometry.anisotropicB = normalize(cross(geometry.normal, geometry.anisotropicT)); + #endif +} + +void initMaterial(out Material material, inout Geometry geometry){ + vec4 baseColor = material_BaseColor; + float metal = material_Metal; + float roughness = material_Roughness; + vec3 specularColor = material_PBRSpecularColor; + float glossiness = material_Glossiness; + float alphaCutoff = material_AlphaCutoff; + float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); + + material.f0 = f0; + + #ifdef MATERIAL_HAS_BASETEXTURE + vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + baseTextureColor = gammaToLinear(baseTextureColor); + #endif + baseColor *= baseTextureColor; + #endif + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + baseColor *= v.v_color; + #endif + + + #ifdef MATERIAL_IS_ALPHA_CUTOFF + if( baseColor.a < alphaCutoff ) { + discard; + } + #endif + + #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE + vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); + roughness *= metalRoughMapColor.g; + metal *= metalRoughMapColor.b; + #endif + + #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE + vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + specularGlossinessColor = gammaToLinear(specularGlossinessColor); + #endif + specularColor *= specularGlossinessColor.rgb; + glossiness *= specularGlossinessColor.a; + #endif + + + #ifdef IS_METALLIC_WORKFLOW + material.diffuseColor = baseColor.rgb * ( 1.0 - metal ); + material.specularColor = mix( vec3(f0), baseColor.rgb, metal ); + material.roughness = roughness; + #else + float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); + material.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); + material.specularColor = specularColor; + material.roughness = 1.0 - glossiness; + #endif + + material.roughness = max(material.roughness, getAARoughnessFactor(geometry.normal)); + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + material.clearCoat = material_ClearCoat; + material.clearCoatRoughness = material_ClearCoatRoughness; + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + material.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; + #endif + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + material.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; + #endif + material.clearCoat = saturate( material.clearCoat ); + material.clearCoatRoughness = max(material.clearCoatRoughness, getAARoughnessFactor(geometry.clearCoatNormal)); + #endif + + #ifdef MATERIAL_IS_TRANSPARENT + material.opacity = baseColor.a; + #else + material.opacity = 1.0; + #endif + #ifdef MATERIAL_ENABLE_ANISOTROPY + geometry.anisotropicN = getAnisotropicBentNormal(geometry, geometry.normal, material.roughness); + #endif + + vec3 emissiveRadiance = material_EmissiveColor; + #ifdef MATERIAL_HAS_EMISSIVETEXTURE + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + emissiveColor = gammaToLinear(emissiveColor); + #endif + emissiveRadiance *= emissiveColor.rgb; + #endif + material.emissive = emissiveRadiance; + +} + +void initSurfaceData(out Geometry geometry, out Material material){ + initGeometry(geometry, gl_FrontFacing); + initMaterial(material, geometry); +} From 6528327d4f9453fbb99940cb51dd75a52d66cc34 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 26 Apr 2024 16:30:43 +0800 Subject: [PATCH 10/89] refactor: init temp --- packages/shaderlab/src/shaders/attrib.glsl | 68 ------ .../shaderlab/src/shaders/common_vert.glsl | 25 --- packages/shaderlab/src/shaders/index.ts | 22 +- .../{light_frag_define.glsl => light.glsl} | 0 packages/shaderlab/src/shaders/pbr.gs | 37 ++- .../src/shaders/shading_pbr/shading_pbr.glsl | 2 +- .../shaders/shading_pbr/surfaceData_pbr.glsl | 10 +- ...ormVarying.glsl => transformVaryings.glsl} | 0 ...{transform_declare.glsl => transform.glsl} | 0 packages/shaderlab/src/shaders/varying.glsl | 29 --- packages/shaderlab/src/shaders/vert.glsl | 212 ++++++++++++++++++ packages/shaderlab/src/shaders/vert_pbr.glsl | 182 --------------- 12 files changed, 246 insertions(+), 341 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/attrib.glsl delete mode 100644 packages/shaderlab/src/shaders/common_vert.glsl rename packages/shaderlab/src/shaders/{light_frag_define.glsl => light.glsl} (100%) rename packages/shaderlab/src/shaders/temp/{transformVarying.glsl => transformVaryings.glsl} (100%) rename packages/shaderlab/src/shaders/{transform_declare.glsl => transform.glsl} (100%) delete mode 100644 packages/shaderlab/src/shaders/varying.glsl create mode 100644 packages/shaderlab/src/shaders/vert.glsl delete mode 100644 packages/shaderlab/src/shaders/vert_pbr.glsl diff --git a/packages/shaderlab/src/shaders/attrib.glsl b/packages/shaderlab/src/shaders/attrib.glsl deleted file mode 100644 index b85b1bde..00000000 --- a/packages/shaderlab/src/shaders/attrib.glsl +++ /dev/null @@ -1,68 +0,0 @@ -#ifdef RENDERER_HAS_BLENDSHAPE - #ifndef RENDERER_BLENDSHAPE_USE_TEXTURE - vec3 POSITION_BS0; - vec3 POSITION_BS1; - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - vec3 NORMAL_BS0; - vec3 NORMAL_BS1; - vec3 TANGENT_BS0; - vec3 TANGENT_BS1; - #else - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - vec3 POSITION_BS2; - vec3 POSITION_BS3; - #ifdef RENDERER_BLENDSHAPE_HAS_NORMAL - vec3 NORMAL_BS0; - vec3 NORMAL_BS1; - vec3 NORMAL_BS2; - vec3 NORMAL_BS3; - #endif - - #ifdef RENDERER_BLENDSHAPE_HAS_TANGENT - vec3 TANGENT_BS0; - vec3 TANGENT_BS1; - vec3 TANGENT_BS2; - vec3 TANGENT_BS3; - #endif - - #else - vec3 POSITION_BS2; - vec3 POSITION_BS3; - vec3 POSITION_BS4; - vec3 POSITION_BS5; - vec3 POSITION_BS6; - vec3 POSITION_BS7; - #endif - #endif - #endif - - #endif - - vec3 POSITION; - - #ifdef RENDERER_HAS_UV - vec2 TEXCOORD_0; - #endif - - #ifdef RENDERER_HAS_UV1 - vec2 TEXCOORD_1; - #endif - - #ifdef RENDERER_HAS_SKIN - vec4 JOINTS_0; - vec4 WEIGHTS_0; - #endif - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 COLOR_0; - #endif - - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 NORMAL; - #endif - - #ifdef RENDERER_HAS_TANGENT - vec4 TANGENT; - #endif - #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/common_vert.glsl b/packages/shaderlab/src/shaders/common_vert.glsl deleted file mode 100644 index 1a89b7e0..00000000 --- a/packages/shaderlab/src/shaders/common_vert.glsl +++ /dev/null @@ -1,25 +0,0 @@ -#ifdef RENDERER_HAS_SKIN - #ifdef RENDERER_USE_JOINT_TEXTURE - sampler2D renderer_JointSampler; - float renderer_JointCount; - - mat4 getJointMatrix(sampler2D smp, float index) - { - float base = index / renderer_JointCount; - float hf = 0.5 / renderer_JointCount; - float v = base + hf; - - vec4 m0 = texture2D(smp, vec2(0.125, v )); - vec4 m1 = texture2D(smp, vec2(0.375, v )); - vec4 m2 = texture2D(smp, vec2(0.625, v )); - vec4 m3 = texture2D(smp, vec2(0.875, v )); - - return mat4(m0, m1, m2, m3); - } - - #else - mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; - #endif -#endif - -vec4 material_TilingOffset; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 4a9e3369..f7e8ff35 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -1,18 +1,17 @@ -import attrib from "./attrib.glsl"; import blendShape_input from "./blendShape_input.glsl"; -import common_vert from "./common_vert.glsl"; import common from "./common.glsl"; import fog from "./fog.glsl"; -import light_frag_define from "./light_frag_define.glsl"; +import light from "./light.glsl"; import normal_get from "./normal_get.glsl"; import shadow_sample_tent from "./shadow_sample_tent.glsl"; import shadow from "./shadow.glsl"; -import transform_declare from "./transform_declare.glsl"; -import varying from "./varying.glsl"; +import transform from "./transform.glsl"; import pbr from "./pbr.gs"; -import vert_pbr from "./vert_pbr.glsl"; +import vert from "./vert.glsl"; import input from "./input.glsl"; import shading_pbr from "./shading_pbr"; +import temp_transformAttributes from "./temp/transformAttributes.glsl"; +import temp_transformVaryings from "./temp/transformVaryings.glsl"; interface IShaderFragment { includeKey: string; @@ -20,19 +19,18 @@ interface IShaderFragment { } const pbr_include_fragment_list: IShaderFragment[] = [ - { source: attrib, includeKey: "attrib.glsl" }, { source: blendShape_input, includeKey: "blendShape_input.glsl" }, - { source: common_vert, includeKey: "common_vert.glsl" }, { source: common, includeKey: "common.glsl" }, { source: fog, includeKey: "fog.glsl" }, - { source: light_frag_define, includeKey: "light_frag_define.glsl" }, + { source: light, includeKey: "light.glsl" }, { source: normal_get, includeKey: "normal_get.glsl" }, { source: shadow_sample_tent, includeKey: "shadow_sample_tent.glsl" }, { source: shadow, includeKey: "shadow.glsl" }, - { source: transform_declare, includeKey: "transform_declare.glsl" }, - { source: varying, includeKey: "varying.glsl" }, - { source: vert_pbr, includeKey: "vert_pbr.glsl" }, + { source: transform, includeKey: "transform.glsl" }, + { source: vert, includeKey: "vert.glsl" }, { source: input, includeKey: "input.glsl" }, + { source: temp_transformAttributes, includeKey: "temp_transformAttributes.glsl" }, + { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, ...shading_pbr ]; diff --git a/packages/shaderlab/src/shaders/light_frag_define.glsl b/packages/shaderlab/src/shaders/light.glsl similarity index 100% rename from packages/shaderlab/src/shaders/light_frag_define.glsl rename to packages/shaderlab/src/shaders/light.glsl diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index b77bd288..301cb416 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -63,46 +63,45 @@ Shader "pbr.gs" { #define IS_METALLIC_WORKFLOW - struct _galacean_a2v { - #include "attrib.glsl" - } - - struct _galacean_v2f { - #include "varying.glsl" - } - + #include "input.glsl" #include "common.glsl" - #include "transform_declare.glsl" + #include "transform.glsl" - #include "common_vert.glsl" + #include "vert.glsl" #include "blendShape_input.glsl" #include "shadow.glsl" - // fragment uniforms #include "fog.glsl" - #include "light_frag_define.glsl" + #include "light.glsl" #include "shading_pbr.glsl" - // new - #include "input.glsl" VertexShader = pbrVert; FragmentShader = pbrFrag; - _galacean_v2f pbrVert(_galacean_a2v attr) { - _galacean_v2f v; + Varyings pbrVert(Attributes attr) { + Varyings v; - #include "vert_pbr.glsl" + // @todo delete + Temp_Attributes temp_attributes; + Temp_Varyings temp_varyings; + #include "temp_transformAttributes.glsl" + #include "temp_transformVaryings.glsl" + + initVert(); return v; } - void pbrFrag(_galacean_v2f v) { + void pbrFrag(Varyings v) { Geometry geometry; Material material; - initSurfaceData(geometry, material); + // @todo delete + Temp_Varyings temp_varyings; + + initSurfaceData(geometry, material, temp_varyings); vec4 color = evaluateSurface(geometry, material); diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index 469c69b2..5ae63907 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -1,10 +1,10 @@ #include "material_pbr.glsl" #include "normal_get.glsl" +#include "surfaceData_pbr.glsl" // direct + indirect #include "brdf.glsl" #include "light_direct_pbr.glsl" #include "light_indirect_pbr.glsl" -#include "surfaceData_pbr.glsl" vec4 evaluateSurface(in Geometry geometry, in Material material){ diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl index 5aef3df4..e59e66bd 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -55,7 +55,7 @@ float getAARoughnessFactor(vec3 normal) { } #endif -void initGeometry(out Geometry geometry, bool isFrontFacing){ +void initGeometry(out Geometry geometry, bool isFrontFacing, Temp_Varyings v){ geometry.position = v.v_pos; #ifdef CAMERA_ORTHOGRAPHIC geometry.viewDir = -camera_Forward; @@ -99,7 +99,7 @@ void initGeometry(out Geometry geometry, bool isFrontFacing){ #endif } -void initMaterial(out Material material, inout Geometry geometry){ +void initMaterial(out Material material, inout Geometry geometry, Temp_Varyings v){ vec4 baseColor = material_BaseColor; float metal = material_Metal; float roughness = material_Roughness; @@ -192,7 +192,7 @@ void initMaterial(out Material material, inout Geometry geometry){ } -void initSurfaceData(out Geometry geometry, out Material material){ - initGeometry(geometry, gl_FrontFacing); - initMaterial(material, geometry); +void initSurfaceData(out Geometry geometry, out Material material, Temp_Varyings v ){ + initGeometry(geometry, gl_FrontFacing, v); + initMaterial(material, geometry, v); } diff --git a/packages/shaderlab/src/shaders/temp/transformVarying.glsl b/packages/shaderlab/src/shaders/temp/transformVaryings.glsl similarity index 100% rename from packages/shaderlab/src/shaders/temp/transformVarying.glsl rename to packages/shaderlab/src/shaders/temp/transformVaryings.glsl diff --git a/packages/shaderlab/src/shaders/transform_declare.glsl b/packages/shaderlab/src/shaders/transform.glsl similarity index 100% rename from packages/shaderlab/src/shaders/transform_declare.glsl rename to packages/shaderlab/src/shaders/transform.glsl diff --git a/packages/shaderlab/src/shaders/varying.glsl b/packages/shaderlab/src/shaders/varying.glsl deleted file mode 100644 index ca4c585e..00000000 --- a/packages/shaderlab/src/shaders/varying.glsl +++ /dev/null @@ -1,29 +0,0 @@ -#ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 v_color; -#endif - -#if SCENE_FOG_MODE != 0 - vec3 v_positionVS; -#endif - -#ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 v_normal; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - mat3 v_TBN; - #endif - #endif -#endif -#ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 - vec3 v_shadowCoord; - #endif -#endif -vec2 v_uv; - -#ifdef RENDERER_HAS_UV1 - vec2 v_uv1; -#endif -#ifdef MATERIAL_NEED_WORLD_POS - vec3 v_pos; -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/vert.glsl b/packages/shaderlab/src/shaders/vert.glsl new file mode 100644 index 00000000..c8db8237 --- /dev/null +++ b/packages/shaderlab/src/shaders/vert.glsl @@ -0,0 +1,212 @@ +#ifdef RENDERER_HAS_SKIN + #ifdef RENDERER_USE_JOINT_TEXTURE + sampler2D renderer_JointSampler; + float renderer_JointCount; + + mat4 getJointMatrix(sampler2D smp, float index) + { + float base = index / renderer_JointCount; + float hf = 0.5 / renderer_JointCount; + float v = base + hf; + + vec4 m0 = texture2D(smp, vec2(0.125, v )); + vec4 m1 = texture2D(smp, vec2(0.375, v )); + vec4 m2 = texture2D(smp, vec2(0.625, v )); + vec4 m3 = texture2D(smp, vec2(0.875, v )); + + return mat4(m0, m1, m2, m3); + } + + #else + mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; + #endif +#endif + +vec4 material_TilingOffset; + + + +void initVert(){ + vec4 position = vec4( attr.POSITION , 1.0 ); + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 normal = vec3( attr.NORMAL ); + #endif + + #ifdef RENDERER_HAS_TANGENT + vec4 tangent = vec4( attr.TANGENT ); + #endif + #endif + + + // blendShape + #ifdef RENDERER_HAS_BLENDSHAPE + #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE + int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; + for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){ + int vertexElementOffset = vertexOffset; + float weight = renderer_BlendShapeWeights[i]; + // Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13 + if(weight != 0.0){ + position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + + #ifndef MATERIAL_OMIT_NORMAL + #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) + vertexElementOffset += 1; + normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif + + #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + vertexElementOffset += 1; + tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif + #endif + } + + } + #else + position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; + position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; + + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + #endif + #if defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + #endif + #endif + #else + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + #ifndef MATERIAL_OMIT_NORMAL + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; + normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; + #endif + + #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; + tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; + #endif + #endif + #else + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; + position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; + position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; + position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; + #endif + #endif + #endif + #endif + + + + // skin + #ifdef RENDERER_HAS_SKIN + + #ifdef RENDERER_USE_JOINT_TEXTURE + mat4 skinMatrix = + attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + + attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + + attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + + attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); + + #else + mat4 skinMatrix = + attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + + attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + + attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + + attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; + #endif + + position = skinMatrix * position; + + #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) + mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); + normal = normal * skinNormalMatrix; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + tangent.xyz = tangent.xyz * skinNormalMatrix; + #endif + + #endif + + #endif + + + // shadow + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT == 1 + v.v_shadowCoord = getShadowCoord(); + #endif + #endif + + + // fog + #if SCENE_FOG_MODE != 0 + vec4 positionVS = renderer_MVMat * position; + v.v_positionVS = positionVS.xyz / positionVS.w; + #endif + + + + + // uv_vert + #ifdef RENDERER_HAS_UV + v.v_uv = attr.TEXCOORD_0; + #else + // may need this calculate normal + v.v_uv = vec2( 0.0, 0.0 ); + #endif + + #ifdef RENDERER_HAS_UV1 + v.v_uv1 = attr.TEXCOORD_1; + #endif + + #ifdef MATERIAL_NEED_TILING_OFFSET + v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; + #endif + + + // color_vert + #ifdef RENDERER_ENABLE_VERTEXCOLOR + v.v_color = COLOR_0; + #endif + + + // normal_vert + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); + + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); + vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; + + v.v_TBN = mat3( tangentW, bitangentW, v.v_normal ); + #endif + #endif + #endif + + + // worldpos_vert + #ifdef MATERIAL_NEED_WORLD_POS + vec4 temp_pos = renderer_ModelMat * position; + v.v_pos = temp_pos.xyz / temp_pos.w; + #endif + + // position_vert + gl_Position = renderer_MVPMat * position; +} \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/vert_pbr.glsl b/packages/shaderlab/src/shaders/vert_pbr.glsl deleted file mode 100644 index ad3c0736..00000000 --- a/packages/shaderlab/src/shaders/vert_pbr.glsl +++ /dev/null @@ -1,182 +0,0 @@ -vec4 position = vec4( attr.POSITION , 1.0 ); - -#ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 normal = vec3( attr.NORMAL ); - #endif - - #ifdef RENDERER_HAS_TANGENT - vec4 tangent = vec4( attr.TANGENT ); - #endif -#endif - - -// blendShape -#ifdef RENDERER_HAS_BLENDSHAPE - #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE - int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; - for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){ - int vertexElementOffset = vertexOffset; - float weight = renderer_BlendShapeWeights[i]; - // Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13 - if(weight != 0.0){ - position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - - #ifndef MATERIAL_OMIT_NORMAL - #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) - vertexElementOffset += 1; - normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif - - #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - vertexElementOffset += 1; - tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif - #endif - } - - } - #else - position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; - position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; - - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - #endif - #if defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - #endif - #endif - #else - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; - normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; - #endif - - #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; - tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; - #endif - #endif - #else - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; - position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; - position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; - position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; - #endif - #endif - #endif -#endif - - - -// skin -#ifdef RENDERER_HAS_SKIN - - #ifdef RENDERER_USE_JOINT_TEXTURE - mat4 skinMatrix = - attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + - attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + - attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + - attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); - - #else - mat4 skinMatrix = - attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + - attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + - attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + - attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; - #endif - - position = skinMatrix * position; - - #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) - mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); - normal = normal * skinNormalMatrix; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - tangent.xyz = tangent.xyz * skinNormalMatrix; - #endif - - #endif - -#endif - - -// shadow -#ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT == 1 - v.v_shadowCoord = getShadowCoord(); - #endif -#endif - - -// fog -#if SCENE_FOG_MODE != 0 - vec4 positionVS = renderer_MVMat * position; - v.v_positionVS = positionVS.xyz / positionVS.w; -#endif - - - - -// uv_vert -#ifdef RENDERER_HAS_UV - v.v_uv = attr.TEXCOORD_0; -#else - // may need this calculate normal - v.v_uv = vec2( 0.0, 0.0 ); -#endif - -#ifdef RENDERER_HAS_UV1 - v.v_uv1 = attr.TEXCOORD_1; -#endif - -#ifdef MATERIAL_NEED_TILING_OFFSET - v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; -#endif - - -// color_vert -#ifdef RENDERER_ENABLE_VERTEXCOLOR - v.v_color = COLOR_0; -#endif - - -// normal_vert -#ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); - - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); - vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; - - v.v_TBN = mat3( tangentW, bitangentW, v.v_normal ); - #endif - #endif -#endif - - -// worldpos_vert -#ifdef MATERIAL_NEED_WORLD_POS - vec4 temp_pos = renderer_ModelMat * position; - v.v_pos = temp_pos.xyz / temp_pos.w; -#endif - -// position_vert -gl_Position = renderer_MVPMat * position; \ No newline at end of file From ac62e5ab1b69b775317890e7acd63a38dc48c122 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 26 Apr 2024 17:09:22 +0800 Subject: [PATCH 11/89] refactor: init surfaceData --- packages/shaderlab/src/shaders/input.glsl | 12 --- packages/shaderlab/src/shaders/pbr.gs | 11 ++- .../src/shaders/shading_pbr/brdf.glsl | 20 ++--- .../shaders/shading_pbr/light_direct_pbr.glsl | 38 ++++----- .../shading_pbr/light_indirect_pbr.glsl | 28 +++---- .../src/shaders/shading_pbr/material_pbr.glsl | 62 +++++++------- .../src/shaders/shading_pbr/shading_pbr.glsl | 10 +-- .../shaders/shading_pbr/surfaceData_pbr.glsl | 80 ++++++++++--------- packages/shaderlab/src/shaders/vert.glsl | 2 +- 9 files changed, 126 insertions(+), 137 deletions(-) diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/input.glsl index c5bb1ddc..5468e7be 100644 --- a/packages/shaderlab/src/shaders/input.glsl +++ b/packages/shaderlab/src/shaders/input.glsl @@ -214,15 +214,3 @@ struct Temp_Varyings{ vec3 v_pos; #endif } - -void intVertexData(in Temp_Attributes attributes, out Temp_Varyings varyings){ - vec4 position = vec4( attributes.POSITION , 1.0 ); - - - #if SCENE_FOG_MODE != 0 - vec4 positionVS = renderer_MVMat * position; - varyings.v_positionVS = positionVS.xyz / positionVS.w; - #endif - - -} \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 301cb416..e34c9796 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -89,21 +89,20 @@ Shader "pbr.gs" { #include "temp_transformAttributes.glsl" #include "temp_transformVaryings.glsl" - initVert(); + initVertex(); return v; } void pbrFrag(Varyings v) { - Geometry geometry; - Material material; - + SurfaceData surfaceData; + // @todo delete Temp_Varyings temp_varyings; - initSurfaceData(geometry, material, temp_varyings); + initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - vec4 color = evaluateSurface(geometry, material); + vec4 color = evaluateSurface(surfaceData); gl_FragColor = color; diff --git a/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl b/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl index d2846b5e..864b081f 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl @@ -74,10 +74,10 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl } #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 anisotropicLobe(vec3 h, vec3 l, Geometry geometry, vec3 specularColor, float alpha, float dotNV, float dotNL, float dotNH, float dotLH) { - vec3 t = geometry.anisotropicT; - vec3 b = geometry.anisotropicB; - vec3 v = geometry.viewDir; + vec3 anisotropicLobe(vec3 h, vec3 l, SurfaceData surfaceData, vec3 specularColor, float alpha, float dotNV, float dotNL, float dotNH, float dotLH) { + vec3 t = surfaceData.anisotropicT; + vec3 b = surfaceData.anisotropicB; + vec3 v = surfaceData.viewDir; float dotTV = dot(t, v); float dotBV = dot(b, v); @@ -88,8 +88,8 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl // Aniso parameter remapping // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24 - float at = max(alpha * (1.0 + geometry.anisotropy), MIN_ROUGHNESS); - float ab = max(alpha * (1.0 - geometry.anisotropy), MIN_ROUGHNESS); + float at = max(alpha * (1.0 + surfaceData.anisotropy), MIN_ROUGHNESS); + float ab = max(alpha * (1.0 - surfaceData.anisotropy), MIN_ROUGHNESS); // specular anisotropic BRDF vec3 F = F_Schlick( specularColor, dotLH ); @@ -101,19 +101,19 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl #endif // GGX Distribution, Schlick Fresnel, GGX-Smith Visibility -vec3 BRDF_Specular_GGX(vec3 incidentDirection, Geometry geometry, vec3 normal, vec3 specularColor, float roughness ) { +vec3 BRDF_Specular_GGX(vec3 incidentDirection, SurfaceData surfaceData, vec3 normal, vec3 specularColor, float roughness ) { float alpha = pow2( roughness ); // UE4's roughness - vec3 halfDir = normalize( incidentDirection + geometry.viewDir ); + vec3 halfDir = normalize( incidentDirection + surfaceData.viewDir ); float dotNL = saturate( dot( normal, incidentDirection ) ); - float dotNV = saturate( dot( normal, geometry.viewDir ) ); + float dotNV = saturate( dot( normal, surfaceData.viewDir ) ); float dotNH = saturate( dot( normal, halfDir ) ); float dotLH = saturate( dot( incidentDirection, halfDir ) ); #ifdef MATERIAL_ENABLE_ANISOTROPY - return anisotropicLobe(halfDir, incidentDirection, geometry, specularColor, alpha, dotNV, dotNL, dotNH, dotLH); + return anisotropicLobe(halfDir, incidentDirection, surfaceData, specularColor, alpha, dotNV, dotNL, dotNH, dotLH); #else return isotropicLobe(specularColor, alpha, dotNV, dotNL, dotNH, dotLH); #endif diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl index defca9ad..53923e52 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl @@ -1,29 +1,29 @@ -void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Material material, inout ReflectedLight reflectedLight) { +void addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { float attenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoatDotNL = saturate( dot( geometry.clearCoatNormal, incidentDirection ) ); + float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; - reflectedLight.directSpecular += material.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, geometry, geometry.clearCoatNormal, vec3( 0.04 ), material.clearCoatRoughness ); - attenuation -= material.clearCoat * F_Schlick(material.f0, geometry.clearCoatDotNV); + reflectedLight.directSpecular += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); + attenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); #endif - float dotNL = saturate( dot( geometry.normal, incidentDirection ) ); + float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); vec3 irradiance = dotNL * color * PI; - reflectedLight.directSpecular += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, geometry, geometry.normal, material.specularColor, material.roughness); - reflectedLight.directDiffuse += attenuation * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); + reflectedLight.directSpecular += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); + reflectedLight.directDiffuse += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); } #ifdef SCENE_DIRECT_LIGHT_COUNT - void addDirectionalDirectLightRadiance(DirectLight directionalLight, Geometry geometry, Material material, inout ReflectedLight reflectedLight) { + void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { vec3 color = directionalLight.color; vec3 direction = -directionalLight.direction; - addDirectRadiance( direction, color, geometry, material, reflectedLight ); + addDirectRadiance( direction, color, surfaceData, reflectedLight ); } @@ -31,9 +31,9 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Ma #ifdef SCENE_POINT_LIGHT_COUNT - void addPointDirectLightRadiance(PointLight pointLight, Geometry geometry, Material material, inout ReflectedLight reflectedLight) { + void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { - vec3 lVector = pointLight.position - geometry.position; + vec3 lVector = pointLight.position - surfaceData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); @@ -41,7 +41,7 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Ma vec3 color = pointLight.color; color *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - addDirectRadiance( direction, color, geometry, material, reflectedLight ); + addDirectRadiance( direction, color, surfaceData, reflectedLight ); } @@ -49,9 +49,9 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Ma #ifdef SCENE_SPOT_LIGHT_COUNT - void addSpotDirectLightRadiance(SpotLight spotLight, Geometry geometry, Material material, inout ReflectedLight reflectedLight) { + void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { - vec3 lVector = spotLight.position - geometry.position; + vec3 lVector = spotLight.position - surfaceData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); @@ -63,14 +63,14 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, Geometry geometry, Ma vec3 color = spotLight.color; color *= spotEffect * decayEffect; - addDirectRadiance( direction, color, geometry, material, reflectedLight ); + addDirectRadiance( direction, color, surfaceData, reflectedLight ); } #endif -void evaluateDirectRadiance(Geometry geometry, Material material, inout ReflectedLight reflectedLight){ +void evaluateDirectRadiance(SurfaceData surfaceData, inout ReflectedLight reflectedLight){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT @@ -91,7 +91,7 @@ void evaluateDirectRadiance(Geometry geometry, Material material, inout Reflecte } #endif directionalLight.direction = scene_DirectLightDirection[i]; - addDirectionalDirectLightRadiance( directionalLight, geometry, material, reflectedLight ); + addDirectionalDirectLightRadiance( directionalLight, surfaceData, reflectedLight ); } } @@ -107,7 +107,7 @@ void evaluateDirectRadiance(Geometry geometry, Material material, inout Reflecte pointLight.position = scene_PointLightPosition[i]; pointLight.distance = scene_PointLightDistance[i]; - addPointDirectLightRadiance( pointLight, geometry, material, reflectedLight ); + addPointDirectLightRadiance( pointLight, surfaceData, reflectedLight ); } } @@ -126,7 +126,7 @@ void evaluateDirectRadiance(Geometry geometry, Material material, inout Reflecte spotLight.angleCos = scene_SpotLightAngleCos[i]; spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; - addSpotDirectLightRadiance( spotLight, geometry, material, reflectedLight ); + addSpotDirectLightRadiance( spotLight, surfaceData, reflectedLight ); } } diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl index ebf6b569..2811539c 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl @@ -43,22 +43,22 @@ float getSpecularMIPLevel(float roughness, int maxMIPLevel ) { return roughness * float(maxMIPLevel); } -vec3 getReflectedVector(Geometry geometry, vec3 n) { +vec3 getReflectedVector(SurfaceData surfaceData, vec3 n) { #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 r = reflect(-geometry.viewDir, geometry.anisotropicN); + vec3 r = reflect(-surfaceData.viewDir, surfaceData.anisotropicN); #else - vec3 r = reflect(-geometry.viewDir, n); + vec3 r = reflect(-surfaceData.viewDir, n); #endif return r; } -vec3 getLightProbeRadiance(Geometry geometry, vec3 normal, float roughness, int maxMIPLevel, float specularIntensity) { +vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness, int maxMIPLevel, float specularIntensity) { #ifndef SCENE_USE_SPECULAR_ENV return vec3(0); #else - vec3 reflectVec = getReflectedVector(geometry, normal); + vec3 reflectVec = getReflectedVector(surfaceData, normal); reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse float specularMIPLevel = getSpecularMIPLevel(roughness, maxMIPLevel ); @@ -90,10 +90,10 @@ float computeSpecularOcclusion(float ambientOcclusion, float roughness, float do return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); } -void evaluateIBL(Geometry geometry, Material material, inout ReflectedLight reflectedLight ){ +void evaluateIBL(SurfaceData surfaceData, inout ReflectedLight reflectedLight ){ // IBL diffuse #ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, geometry.normal); + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; #endif @@ -103,20 +103,20 @@ void evaluateIBL(Geometry geometry, Material material, inout ReflectedLight refl irradiance *= PI; #endif - reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor ); + reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); // IBL specular - vec3 radiance = getLightProbeRadiance(geometry, geometry.normal, material.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); + vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); float radianceAttenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( geometry, geometry.clearCoatNormal, material.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); + vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); - reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * envBRDFApprox(vec3( 0.04 ), material.clearCoatRoughness, geometry.clearCoatDotNV); - radianceAttenuation -= material.clearCoat * F_Schlick(material.f0, geometry.clearCoatDotNV); + reflectedLight.indirectSpecular += clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); + radianceAttenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); #endif - reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(material.specularColor, material.roughness, geometry.dotNV ); + reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); // Occlusion @@ -130,7 +130,7 @@ void evaluateIBL(Geometry geometry, Material material, inout ReflectedLight refl float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; reflectedLight.indirectDiffuse *= ambientOcclusion; #ifdef SCENE_USE_SPECULAR_ENV - reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, material.roughness, geometry.dotNV); + reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, surfaceData.roughness, surfaceData.dotNV); #endif #endif diff --git a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl index b77f2582..6b663d36 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl @@ -71,35 +71,35 @@ struct ReflectedLight { vec3 indirectSpecular; } -struct Geometry { - vec3 position; - vec3 normal; - vec3 viewDir; - float dotNV; +// struct Geometry { +// vec3 position; +// vec3 normal; +// vec3 viewDir; +// float dotNV; - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatNormal; - float clearCoatDotNV; - #endif - - #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 anisotropicT; - vec3 anisotropicB; - vec3 anisotropicN; - float anisotropy; - #endif -} - -struct Material { - vec3 diffuseColor; - float roughness; - vec3 specularColor; - float opacity; - float f0; - #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoat; - float clearCoatRoughness; - #endif - vec3 emissive; - -} \ No newline at end of file +// #ifdef MATERIAL_ENABLE_CLEAR_COAT +// vec3 clearCoatNormal; +// float clearCoatDotNV; +// #endif + +// #ifdef MATERIAL_ENABLE_ANISOTROPY +// vec3 anisotropicT; +// vec3 anisotropicB; +// vec3 anisotropicN; +// float anisotropy; +// #endif +// } + +// struct Material { +// vec3 diffuseColor; +// float roughness; +// vec3 specularColor; +// float opacity; +// float f0; +// #ifdef MATERIAL_ENABLE_CLEAR_COAT +// float clearCoat; +// float clearCoatRoughness; +// #endif +// vec3 emissive; + +// } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index 5ae63907..74f3b105 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -7,26 +7,26 @@ #include "light_indirect_pbr.glsl" -vec4 evaluateSurface(in Geometry geometry, in Material material){ +vec4 evaluateSurface(SurfaceData surfaceData){ ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); // Direct Light - evaluateDirectRadiance(geometry, material, reflectedLight); + evaluateDirectRadiance(surfaceData, reflectedLight); // IBL - evaluateIBL(geometry, material, reflectedLight); + evaluateIBL(surfaceData, reflectedLight); // Total vec3 totalRadiance = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + - material.emissive; + surfaceData.emissive; - return vec4(totalRadiance, material.opacity); + return vec4(totalRadiance, surfaceData.opacity); } diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl index e59e66bd..3d5c77f9 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -44,44 +44,46 @@ float getAARoughnessFactor(vec3 normal) { #ifdef MATERIAL_ENABLE_ANISOTROPY // Aniso Bent Normals // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(Geometry geometry, vec3 n, float roughness) { - vec3 anisotropyDirection = (geometry.anisotropy >= 0.0) ? geometry.anisotropicB : geometry.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, geometry.viewDir); + vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { + vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(n, anisotropicNormal, abs(geometry.anisotropy) * saturate( 5.0 * roughness)) ); + vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); return bentNormal; } #endif -void initGeometry(out Geometry geometry, bool isFrontFacing, Temp_Varyings v){ - geometry.position = v.v_pos; + + +void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ + surfaceData.position = v.v_pos; #ifdef CAMERA_ORTHOGRAPHIC - geometry.viewDir = -camera_Forward; + surfaceData.viewDir = -camera_Forward; #else - geometry.viewDir = normalize(camera_Position - v.v_pos); + surfaceData.viewDir = normalize(camera_Position - v.v_pos); #endif #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) mat3 tbn = getTBN(isFrontFacing); #endif #ifdef MATERIAL_HAS_NORMALTEXTURE - geometry.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else - geometry.normal = getNormal(isFrontFacing); + surfaceData.normal = getNormal(isFrontFacing); #endif - geometry.dotNV = saturate( dot(geometry.normal, geometry.viewDir) ); + surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); #ifdef MATERIAL_ENABLE_CLEAR_COAT #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - geometry.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + surfaceData.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else - geometry.clearCoatNormal = getNormal(isFrontFacing); + surfaceData.clearCoatNormal = getNormal(isFrontFacing); #endif - geometry.clearCoatDotNV = saturate( dot(geometry.clearCoatNormal, geometry.viewDir) ); + surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); #endif #ifdef MATERIAL_ENABLE_ANISOTROPY @@ -93,13 +95,13 @@ void initGeometry(out Geometry geometry, bool isFrontFacing, Temp_Varyings v){ anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; #endif - geometry.anisotropy = anisotropy; - geometry.anisotropicT = normalize(tbn * anisotropicDirection); - geometry.anisotropicB = normalize(cross(geometry.normal, geometry.anisotropicT)); + surfaceData.anisotropy = anisotropy; + surfaceData.anisotropicT = normalize(tbn * anisotropicDirection); + surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); #endif } -void initMaterial(out Material material, inout Geometry geometry, Temp_Varyings v){ +void initMaterial(Temp_Varyings v, inout SurfaceData surfaceData){ vec4 baseColor = material_BaseColor; float metal = material_Metal; float roughness = material_Roughness; @@ -108,7 +110,7 @@ void initMaterial(out Material material, inout Geometry geometry, Temp_Varyings float alphaCutoff = material_AlphaCutoff; float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); - material.f0 = f0; + surfaceData.f0 = f0; #ifdef MATERIAL_HAS_BASETEXTURE vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); @@ -146,38 +148,38 @@ void initMaterial(out Material material, inout Geometry geometry, Temp_Varyings #ifdef IS_METALLIC_WORKFLOW - material.diffuseColor = baseColor.rgb * ( 1.0 - metal ); - material.specularColor = mix( vec3(f0), baseColor.rgb, metal ); - material.roughness = roughness; + surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - metal ); + surfaceData.specularColor = mix( vec3(f0), baseColor.rgb, metal ); + surfaceData.roughness = roughness; #else float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); - material.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); - material.specularColor = specularColor; - material.roughness = 1.0 - glossiness; + surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); + surfaceData.specularColor = specularColor; + surfaceData.roughness = 1.0 - glossiness; #endif - material.roughness = max(material.roughness, getAARoughnessFactor(geometry.normal)); + surfaceData.roughness = max(surfaceData.roughness, getAARoughnessFactor(surfaceData.normal)); #ifdef MATERIAL_ENABLE_CLEAR_COAT - material.clearCoat = material_ClearCoat; - material.clearCoatRoughness = material_ClearCoatRoughness; + surfaceData.clearCoat = material_ClearCoat; + surfaceData.clearCoatRoughness = material_ClearCoatRoughness; #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - material.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; + surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; #endif #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - material.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; + surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; #endif - material.clearCoat = saturate( material.clearCoat ); - material.clearCoatRoughness = max(material.clearCoatRoughness, getAARoughnessFactor(geometry.clearCoatNormal)); + surfaceData.clearCoat = saturate( surfaceData.clearCoat ); + surfaceData.clearCoatRoughness = max(surfaceData.clearCoatRoughness, getAARoughnessFactor(surfaceData.clearCoatNormal)); #endif #ifdef MATERIAL_IS_TRANSPARENT - material.opacity = baseColor.a; + surfaceData.opacity = baseColor.a; #else - material.opacity = 1.0; + surfaceData.opacity = 1.0; #endif #ifdef MATERIAL_ENABLE_ANISOTROPY - geometry.anisotropicN = getAnisotropicBentNormal(geometry, geometry.normal, material.roughness); + surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); #endif vec3 emissiveRadiance = material_EmissiveColor; @@ -188,11 +190,11 @@ void initMaterial(out Material material, inout Geometry geometry, Temp_Varyings #endif emissiveRadiance *= emissiveColor.rgb; #endif - material.emissive = emissiveRadiance; + surfaceData.emissive = emissiveRadiance; } -void initSurfaceData(out Geometry geometry, out Material material, Temp_Varyings v ){ - initGeometry(geometry, gl_FrontFacing, v); - initMaterial(material, geometry, v); +void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ + initGeometry(v, surfaceData, isFrontFacing); + initMaterial(v, surfaceData); } diff --git a/packages/shaderlab/src/shaders/vert.glsl b/packages/shaderlab/src/shaders/vert.glsl index c8db8237..519348f7 100644 --- a/packages/shaderlab/src/shaders/vert.glsl +++ b/packages/shaderlab/src/shaders/vert.glsl @@ -26,7 +26,7 @@ vec4 material_TilingOffset; -void initVert(){ +void initVertex(){ vec4 position = vec4( attr.POSITION , 1.0 ); #ifndef MATERIAL_OMIT_NORMAL From 271ab34f98d89393edb5f2316b4548465078652a Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 26 Apr 2024 17:34:23 +0800 Subject: [PATCH 12/89] refactor: delete reflected light --- packages/shaderlab/src/shaders/pbr.gs | 2 - .../shaders/shading_pbr/light_direct_pbr.glsl | 43 ++++++++------- .../shading_pbr/light_indirect_pbr.glsl | 19 +++---- .../src/shaders/shading_pbr/material_pbr.glsl | 52 +++++-------------- .../src/shaders/shading_pbr/shading_pbr.glsl | 23 +++----- .../shaders/shading_pbr/surfaceData_pbr.glsl | 12 ----- 6 files changed, 54 insertions(+), 97 deletions(-) diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index e34c9796..2abbda98 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -103,10 +103,8 @@ Shader "pbr.gs" { initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); vec4 color = evaluateSurface(surfaceData); - gl_FragColor = color; - #if SCENE_FOG_MODE != 0 gl_FragColor = fog(gl_FragColor, v.v_positionVS); #endif diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl index 53923e52..0ad9394b 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl @@ -1,29 +1,34 @@ -void addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { +vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceData) { float attenuation = 1.0; + vec3 Fd = vec3(0); + vec3 Fs = vec3(0); + #ifdef MATERIAL_ENABLE_CLEAR_COAT float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; - reflectedLight.directSpecular += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); + Fs += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); attenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); #endif float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); vec3 irradiance = dotNL * color * PI; - reflectedLight.directSpecular += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); - reflectedLight.directDiffuse += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + Fs += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); + Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + + return Fd + Fs; } #ifdef SCENE_DIRECT_LIGHT_COUNT - void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { - vec3 color = directionalLight.color; + void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout vec3 color) { + vec3 color2 = directionalLight.color; vec3 direction = -directionalLight.direction; - addDirectRadiance( direction, color, surfaceData, reflectedLight ); + color += addDirectRadiance( direction, color2, surfaceData ); } @@ -31,17 +36,17 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa #ifdef SCENE_POINT_LIGHT_COUNT - void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { + void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, inout vec3 color) { vec3 lVector = pointLight.position - surfaceData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); - vec3 color = pointLight.color; - color *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); + vec3 color2 = pointLight.color; + color2 *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - addDirectRadiance( direction, color, surfaceData, reflectedLight ); + color += addDirectRadiance( direction, color2, surfaceData ); } @@ -49,7 +54,7 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa #ifdef SCENE_SPOT_LIGHT_COUNT - void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, inout ReflectedLight reflectedLight) { + void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, inout vec3 color) { vec3 lVector = spotLight.position - surfaceData.position; vec3 direction = normalize( lVector ); @@ -60,17 +65,17 @@ void addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa float spotEffect = smoothstep( spotLight.penumbraCos, spotLight.angleCos, angleCos ); float decayEffect = clamp(1.0 - pow(lightDistance/spotLight.distance, 4.0), 0.0, 1.0); - vec3 color = spotLight.color; - color *= spotEffect * decayEffect; + vec3 color2 = spotLight.color; + color2 *= spotEffect * decayEffect; - addDirectRadiance( direction, color, surfaceData, reflectedLight ); + color += addDirectRadiance( direction, color2, surfaceData ); } #endif -void evaluateDirectRadiance(SurfaceData surfaceData, inout ReflectedLight reflectedLight){ +void evaluateDirectRadiance(SurfaceData surfaceData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT @@ -91,7 +96,7 @@ void evaluateDirectRadiance(SurfaceData surfaceData, inout ReflectedLight reflec } #endif directionalLight.direction = scene_DirectLightDirection[i]; - addDirectionalDirectLightRadiance( directionalLight, surfaceData, reflectedLight ); + addDirectionalDirectLightRadiance( directionalLight, surfaceData, color ); } } @@ -107,7 +112,7 @@ void evaluateDirectRadiance(SurfaceData surfaceData, inout ReflectedLight reflec pointLight.position = scene_PointLightPosition[i]; pointLight.distance = scene_PointLightDistance[i]; - addPointDirectLightRadiance( pointLight, surfaceData, reflectedLight ); + addPointDirectLightRadiance( pointLight, surfaceData, color ); } } @@ -126,7 +131,7 @@ void evaluateDirectRadiance(SurfaceData surfaceData, inout ReflectedLight reflec spotLight.angleCos = scene_SpotLightAngleCos[i]; spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; - addSpotDirectLightRadiance( spotLight, surfaceData, reflectedLight ); + addSpotDirectLightRadiance( spotLight, surfaceData, color ); } } diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl index 2811539c..36b95ef2 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl @@ -86,11 +86,11 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness } -float computeSpecularOcclusion(float ambientOcclusion, float roughness, float dotNV ) { - return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); -} -void evaluateIBL(SurfaceData surfaceData, inout ReflectedLight reflectedLight ){ +void evaluateIBL(SurfaceData surfaceData, inout vec3 color){ + vec3 Fd = vec3(0); + vec3 Fs = vec3(0); + // IBL diffuse #ifdef SCENE_USE_SH vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); @@ -103,7 +103,7 @@ void evaluateIBL(SurfaceData surfaceData, inout ReflectedLight reflectedLight ){ irradiance *= PI; #endif - reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + Fd += irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); // IBL specular vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); @@ -112,11 +112,11 @@ void evaluateIBL(SurfaceData surfaceData, inout ReflectedLight reflectedLight ){ #ifdef MATERIAL_ENABLE_CLEAR_COAT vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); - reflectedLight.indirectSpecular += clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); + Fs += clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); radianceAttenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); #endif - reflectedLight.indirectSpecular += radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); + Fs += radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); // Occlusion @@ -128,10 +128,11 @@ void evaluateIBL(SurfaceData surfaceData, inout ReflectedLight reflectedLight ){ } #endif float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - reflectedLight.indirectDiffuse *= ambientOcclusion; + Fd *= ambientOcclusion; #ifdef SCENE_USE_SPECULAR_ENV - reflectedLight.indirectSpecular *= computeSpecularOcclusion(ambientOcclusion, surfaceData.roughness, surfaceData.dotNV); + Fs *= computeSpecularOcclusion(ambientOcclusion, surfaceData.roughness, surfaceData.dotNV); #endif #endif + color += Fd + Fs; } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl index 6b663d36..771bb64d 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl @@ -63,43 +63,19 @@ float material_OcclusionTextureCoord; sampler2D material_OcclusionTexture; #endif -// Runtime -struct ReflectedLight { - vec3 directDiffuse; - vec3 directSpecular; - vec3 indirectDiffuse; - vec3 indirectSpecular; +float computeSpecularOcclusion(float ambientOcclusion, float roughness, float dotNV ) { + return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); } -// struct Geometry { -// vec3 position; -// vec3 normal; -// vec3 viewDir; -// float dotNV; - -// #ifdef MATERIAL_ENABLE_CLEAR_COAT -// vec3 clearCoatNormal; -// float clearCoatDotNV; -// #endif - -// #ifdef MATERIAL_ENABLE_ANISOTROPY -// vec3 anisotropicT; -// vec3 anisotropicB; -// vec3 anisotropicN; -// float anisotropy; -// #endif -// } - -// struct Material { -// vec3 diffuseColor; -// float roughness; -// vec3 specularColor; -// float opacity; -// float f0; -// #ifdef MATERIAL_ENABLE_CLEAR_COAT -// float clearCoat; -// float clearCoatRoughness; -// #endif -// vec3 emissive; - -// } \ No newline at end of file + +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); + #else + return MIN_PERCEPTUAL_ROUGHNESS; + #endif +} diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index 74f3b105..8b3fc71e 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -8,27 +8,16 @@ vec4 evaluateSurface(SurfaceData surfaceData){ - - ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); + vec3 color = vec3(0); // Direct Light - evaluateDirectRadiance(surfaceData, reflectedLight); - - + evaluateDirectRadiance(surfaceData, color); // IBL - evaluateIBL(surfaceData, reflectedLight); - - // Total - vec3 totalRadiance = reflectedLight.directDiffuse + - reflectedLight.indirectDiffuse + - reflectedLight.directSpecular + - reflectedLight.indirectSpecular + - surfaceData.emissive; - - - return vec4(totalRadiance, surfaceData.opacity); - + evaluateIBL(surfaceData, color); + // Emissive + color += surfaceData.emissive; + return vec4(color, surfaceData.opacity); } diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl index 3d5c77f9..2b8abd0e 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -29,18 +29,6 @@ struct SurfaceData{ } -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); - #else - return MIN_PERCEPTUAL_ROUGHNESS; - #endif -} - #ifdef MATERIAL_ENABLE_ANISOTROPY // Aniso Bent Normals // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far From 46b29f02b82ac7b20ba003998d6b0df3ac53b128 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 26 Apr 2024 18:31:55 +0800 Subject: [PATCH 13/89] refactor: delete inner varyings --- .../shaderlab/src/shaders/normal_get.glsl | 6 +- packages/shaderlab/src/shaders/pbr.gs | 5 +- .../shaders/shading_pbr/light_direct_pbr.glsl | 4 +- .../shading_pbr/light_indirect_pbr.glsl | 2 +- .../src/shaders/shading_pbr/shading_pbr.glsl | 6 +- .../shaders/shading_pbr/surfaceData_pbr.glsl | 6 +- packages/shaderlab/src/shaders/shadow.glsl | 88 +++++++++---------- packages/shaderlab/src/shaders/vert.glsl | 15 ++-- 8 files changed, 66 insertions(+), 66 deletions(-) diff --git a/packages/shaderlab/src/shaders/normal_get.glsl b/packages/shaderlab/src/shaders/normal_get.glsl index d9a033d2..edfca420 100644 --- a/packages/shaderlab/src/shaders/normal_get.glsl +++ b/packages/shaderlab/src/shaders/normal_get.glsl @@ -1,7 +1,7 @@ // gl_FrontFacing has random value on Adreno GPUs // the Adreno bug is only when gl_FrontFacing is inside a function // https://bugs.chromium.org/p/chromium/issues/detail?id=1154842 -vec3 getNormal(bool isFrontFacing){ +vec3 getNormal(Temp_Varyings v, bool isFrontFacing){ #ifdef RENDERER_HAS_NORMAL vec3 normal = normalize(v.v_normal); #elif defined(HAS_DERIVATIVES) @@ -24,11 +24,11 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt return normal; } -mat3 getTBN(bool isFrontFacing){ +mat3 getTBN(Temp_Varyings v, bool isFrontFacing){ #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) mat3 tbn = v.v_TBN; #else - vec3 normal = getNormal(isFrontFacing); + vec3 normal = getNormal(v, isFrontFacing); vec3 position = v.v_pos; vec2 uv = isFrontFacing? v.v_uv: -v.v_uv; diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 2abbda98..80911b14 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -67,8 +67,8 @@ Shader "pbr.gs" { #include "common.glsl" #include "transform.glsl" - #include "vert.glsl" #include "blendShape_input.glsl" + #include "vert.glsl" #include "shadow.glsl" #include "fog.glsl" @@ -99,10 +99,11 @@ Shader "pbr.gs" { // @todo delete Temp_Varyings temp_varyings; + #include "temp_transformVaryings.glsl" initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - vec4 color = evaluateSurface(surfaceData); + vec4 color = evaluateSurface(temp_varyings, surfaceData); gl_FragColor = color; #if SCENE_FOG_MODE != 0 diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl index 0ad9394b..32d8b9b1 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl @@ -75,13 +75,13 @@ vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa #endif -void evaluateDirectRadiance(SurfaceData surfaceData, inout vec3 color){ +void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT shadowAttenuation = 1.0; #ifdef SCENE_IS_CALCULATE_SHADOWS - shadowAttenuation *= sampleShadowMap(); + shadowAttenuation *= sampleShadowMap(v); // int sunIndex = int(scene_ShadowInfo.z); #endif diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl index 36b95ef2..c1eedc94 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl @@ -87,7 +87,7 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness } -void evaluateIBL(SurfaceData surfaceData, inout vec3 color){ +void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ vec3 Fd = vec3(0); vec3 Fs = vec3(0); diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index 8b3fc71e..4b176760 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -7,13 +7,13 @@ #include "light_indirect_pbr.glsl" -vec4 evaluateSurface(SurfaceData surfaceData){ +vec4 evaluateSurface(Temp_Varyings v, SurfaceData surfaceData){ vec3 color = vec3(0); // Direct Light - evaluateDirectRadiance(surfaceData, color); + evaluateDirectRadiance(v, surfaceData, color); // IBL - evaluateIBL(surfaceData, color); + evaluateIBL(v, surfaceData, color); // Emissive color += surfaceData.emissive; diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl index 2b8abd0e..dee3863f 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -53,13 +53,13 @@ void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFa surfaceData.viewDir = normalize(camera_Position - v.v_pos); #endif #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) - mat3 tbn = getTBN(isFrontFacing); + mat3 tbn = getTBN(v, isFrontFacing); #endif #ifdef MATERIAL_HAS_NORMALTEXTURE surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else - surfaceData.normal = getNormal(isFrontFacing); + surfaceData.normal = getNormal(v, isFrontFacing); #endif surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); @@ -69,7 +69,7 @@ void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFa #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE surfaceData.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else - surfaceData.clearCoatNormal = getNormal(isFrontFacing); + surfaceData.clearCoatNormal = getNormal(v, isFrontFacing); #endif surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); #endif diff --git a/packages/shaderlab/src/shaders/shadow.glsl b/packages/shaderlab/src/shaders/shadow.glsl index 1a5d0f91..44c41906 100644 --- a/packages/shaderlab/src/shaders/shadow.glsl +++ b/packages/shaderlab/src/shaders/shadow.glsl @@ -25,50 +25,50 @@ return index; } - vec3 getShadowCoord() { - #if SCENE_SHADOW_CASCADED_COUNT == 1 - mediump int cascadeIndex = 0; - #else - mediump int cascadeIndex = computeCascadeIndex(v.v_pos); - #endif - - #ifdef GRAPHICS_API_WEBGL2 - mat4 shadowMatrix = scene_ShadowMatrices[cascadeIndex]; - #else - mat4 shadowMatrix; - #if SCENE_SHADOW_CASCADED_COUNT == 4 - if (cascadeIndex == 0) { - shadowMatrix = scene_ShadowMatrices[0]; - } else if (cascadeIndex == 1) { - shadowMatrix = scene_ShadowMatrices[1]; - } else if (cascadeIndex == 2) { - shadowMatrix = scene_ShadowMatrices[2]; - } else if (cascadeIndex == 3) { - shadowMatrix = scene_ShadowMatrices[3]; - } else { - shadowMatrix = scene_ShadowMatrices[4]; - } - #endif - #if SCENE_SHADOW_CASCADED_COUNT == 2 - if (cascadeIndex == 0) { - shadowMatrix = scene_ShadowMatrices[0]; - } else if (cascadeIndex == 1) { - shadowMatrix = scene_ShadowMatrices[1]; - } else { - shadowMatrix = scene_ShadowMatrices[2]; - } - #endif + vec3 getShadowCoord(vec3 positionWS) { #if SCENE_SHADOW_CASCADED_COUNT == 1 - if (cascadeIndex == 0) { - shadowMatrix = scene_ShadowMatrices[0]; - } else { - shadowMatrix = scene_ShadowMatrices[1]; - } + mediump int cascadeIndex = 0; + #else + mediump int cascadeIndex = computeCascadeIndex(positionWS); #endif - #endif - - vec4 shadowCoord = shadowMatrix * vec4(v.v_pos, 1.0); - return shadowCoord.xyz; + + #ifdef GRAPHICS_API_WEBGL2 + mat4 shadowMatrix = scene_ShadowMatrices[cascadeIndex]; + #else + mat4 shadowMatrix; + #if SCENE_SHADOW_CASCADED_COUNT == 4 + if (cascadeIndex == 0) { + shadowMatrix = scene_ShadowMatrices[0]; + } else if (cascadeIndex == 1) { + shadowMatrix = scene_ShadowMatrices[1]; + } else if (cascadeIndex == 2) { + shadowMatrix = scene_ShadowMatrices[2]; + } else if (cascadeIndex == 3) { + shadowMatrix = scene_ShadowMatrices[3]; + } else { + shadowMatrix = scene_ShadowMatrices[4]; + } + #endif + #if SCENE_SHADOW_CASCADED_COUNT == 2 + if (cascadeIndex == 0) { + shadowMatrix = scene_ShadowMatrices[0]; + } else if (cascadeIndex == 1) { + shadowMatrix = scene_ShadowMatrices[1]; + } else { + shadowMatrix = scene_ShadowMatrices[2]; + } + #endif + #if SCENE_SHADOW_CASCADED_COUNT == 1 + if (cascadeIndex == 0) { + shadowMatrix = scene_ShadowMatrices[0]; + } else { + shadowMatrix = scene_ShadowMatrices[1]; + } + #endif + #endif + + vec4 shadowCoord = shadowMatrix * vec4(positionWS, 1.0); + return shadowCoord.xyz; } #endif @@ -148,11 +148,11 @@ } - float sampleShadowMap() { + float sampleShadowMap(Temp_Varyings v) { #if SCENE_SHADOW_CASCADED_COUNT == 1 vec3 shadowCoord = v.v_shadowCoord; #else - vec3 shadowCoord = getShadowCoord(); + vec3 shadowCoord = getShadowCoord(v.v_pos); #endif float attenuation = 1.0; diff --git a/packages/shaderlab/src/shaders/vert.glsl b/packages/shaderlab/src/shaders/vert.glsl index 519348f7..e6f5c584 100644 --- a/packages/shaderlab/src/shaders/vert.glsl +++ b/packages/shaderlab/src/shaders/vert.glsl @@ -146,12 +146,6 @@ void initVertex(){ #endif - // shadow - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT == 1 - v.v_shadowCoord = getShadowCoord(); - #endif - #endif // fog @@ -161,8 +155,6 @@ void initVertex(){ #endif - - // uv_vert #ifdef RENDERER_HAS_UV v.v_uv = attr.TEXCOORD_0; @@ -207,6 +199,13 @@ void initVertex(){ v.v_pos = temp_pos.xyz / temp_pos.w; #endif + // shadow + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT == 1 + v.v_shadowCoord = getShadowCoord(v.v_pos); + #endif + #endif + // position_vert gl_Position = renderer_MVPMat * position; } \ No newline at end of file From 2aeb827c12fb8194f799c8db5a18fff41fb76e62 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 29 Apr 2024 17:13:30 +0800 Subject: [PATCH 14/89] refactor: enhance ibl function --- .../src/shaders/blendShape_vert.glsl | 72 ------------------- packages/shaderlab/src/shaders/pbr.gs | 6 +- .../shading_pbr/light_indirect_pbr.glsl | 62 ++++++++-------- .../src/shaders/shading_pbr/material_pbr.glsl | 27 ++++++- 4 files changed, 60 insertions(+), 107 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/blendShape_vert.glsl diff --git a/packages/shaderlab/src/shaders/blendShape_vert.glsl b/packages/shaderlab/src/shaders/blendShape_vert.glsl deleted file mode 100644 index 92eed11d..00000000 --- a/packages/shaderlab/src/shaders/blendShape_vert.glsl +++ /dev/null @@ -1,72 +0,0 @@ -#ifdef RENDERER_HAS_BLENDSHAPE - #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE - int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; - for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){ - int vertexElementOffset = vertexOffset; - float weight = renderer_BlendShapeWeights[i]; - // Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13 - if(weight != 0.0){ - position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - - #ifndef MATERIAL_OMIT_NORMAL - #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) - vertexElementOffset += 1; - normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif - - #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - vertexElementOffset += 1; - tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif - #endif - } - - } - #else - position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; - position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; - - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - #endif - #if defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - #endif - #endif - #else - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; - normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; - #endif - - #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; - tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; - #endif - #endif - #else - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; - position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; - position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; - position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; - #endif - #endif - #endif -#endif - - diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 80911b14..2672aea6 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -83,12 +83,13 @@ Shader "pbr.gs" { Varyings pbrVert(Attributes attr) { Varyings v; - // @todo delete + // @todo: delete Temp_Attributes temp_attributes; Temp_Varyings temp_varyings; #include "temp_transformAttributes.glsl" #include "temp_transformVaryings.glsl" + // @todo: use initVertex(attr, v); initVertex(); return v; @@ -97,7 +98,7 @@ Shader "pbr.gs" { void pbrFrag(Varyings v) { SurfaceData surfaceData; - // @todo delete + // @todo: delete Temp_Varyings temp_varyings; #include "temp_transformVaryings.glsl" @@ -115,5 +116,6 @@ Shader "pbr.gs" { #endif } } + } } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl index c1eedc94..94f91617 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl @@ -22,7 +22,7 @@ vec3 getLightProbeIrradiance(vec3 sh[9], vec3 normal){ // ------------------------Specular------------------------ // ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile -vec3 envBRDFApprox(vec3 specularColor,float roughness, float dotNV ) { +vec3 envBRDFApprox(vec3 specularColor, float roughness, float dotNV ) { const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 ); @@ -53,7 +53,7 @@ vec3 getReflectedVector(SurfaceData surfaceData, vec3 n) { return r; } -vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness, int maxMIPLevel, float specularIntensity) { +vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness) { #ifndef SCENE_USE_SPECULAR_ENV return vec3(0); @@ -61,7 +61,7 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness vec3 reflectVec = getReflectedVector(surfaceData, normal); reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse - float specularMIPLevel = getSpecularMIPLevel(roughness, maxMIPLevel ); + float specularMIPLevel = getSpecularMIPLevel(roughness, int(scene_EnvMapLight.mipMapLevel) ); #ifdef HAS_TEX_LOD vec4 envMapColor = textureCubeLodEXT( scene_EnvSpecularSampler, reflectVec, specularMIPLevel ); @@ -80,18 +80,14 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness #endif #endif - return envMapColor.rgb * specularIntensity; + return envMapColor.rgb * scene_EnvMapLight.specularIntensity; #endif } -void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ - vec3 Fd = vec3(0); - vec3 Fs = vec3(0); - - // IBL diffuse +void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd){ #ifdef SCENE_USE_SH vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA @@ -103,36 +99,40 @@ void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ irradiance *= PI; #endif - Fd += irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); +} - // IBL specular - vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity); +void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ float radianceAttenuation = 1.0; - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness, int(scene_EnvMapLight.mipMapLevel), scene_EnvMapLight.specularIntensity ); - - Fs += clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); radianceAttenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); #endif - Fs += radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); - + vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness); + Fs += specularAO * radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); +} - // Occlusion - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - float ambientOcclusion = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - Fd *= ambientOcclusion; - #ifdef SCENE_USE_SPECULAR_ENV - Fs *= computeSpecularOcclusion(ambientOcclusion, surfaceData.roughness, surfaceData.dotNV); - #endif +void evaluateClearCoatIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness); + Fs += specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); #endif +} + +void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ + vec3 Fd = vec3(0); + vec3 Fs = vec3(0); + float diffuseAO = evaluateDiffuseAO(v); + float specularAO = evaluateSpecularAO(diffuseAO, surfaceData.roughness, surfaceData.dotNV); + + // IBL diffuse + evaluateDiffuseIBL(surfaceData, diffuseAO, Fd); + + // IBL specular + evaluateSpecularIBL(surfaceData, specularAO, Fs); + + // IBL ClearCoat + evaluateClearCoatIBL(surfaceData, specularAO, Fs); color += Fd + Fs; } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl index 771bb64d..0d3a5c34 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl @@ -63,8 +63,31 @@ float material_OcclusionTextureCoord; sampler2D material_OcclusionTexture; #endif -float computeSpecularOcclusion(float ambientOcclusion, float roughness, float dotNV ) { - return saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion ); + +float evaluateDiffuseAO(Temp_Varyings v){ + float diffuseAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + return diffuseAO; +} + +float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ + float specularAO = 1.0; + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + return specularAO; } From b77eedcf956e07676f2478de21087dc4e6244963 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 29 Apr 2024 17:17:16 +0800 Subject: [PATCH 15/89] perf: enhance anisotropy --- .../shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl index dee3863f..e5967fc5 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -86,6 +86,8 @@ void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFa surfaceData.anisotropy = anisotropy; surfaceData.anisotropicT = normalize(tbn * anisotropicDirection); surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); + surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); + #endif } @@ -166,9 +168,6 @@ void initMaterial(Temp_Varyings v, inout SurfaceData surfaceData){ #else surfaceData.opacity = 1.0; #endif - #ifdef MATERIAL_ENABLE_ANISOTROPY - surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); - #endif vec3 emissiveRadiance = material_EmissiveColor; #ifdef MATERIAL_HAS_EMISSIVETEXTURE From a0b981254e9fbf16d41f98529e7064fdf14b45cf Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 30 Apr 2024 10:31:21 +0800 Subject: [PATCH 16/89] refactor: lobe --- .../src/shaders/shading_pbr/brdf.glsl | 2 +- .../shaders/shading_pbr/light_direct_pbr.glsl | 42 +++++++++++++------ .../shading_pbr/light_indirect_pbr.glsl | 26 ++++++------ .../src/shaders/shading_pbr/shading_pbr.glsl | 4 -- .../shaders/shading_pbr/surfaceData_pbr.glsl | 3 ++ 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl b/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl index 864b081f..62cfe45e 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl @@ -120,6 +120,6 @@ vec3 BRDF_Specular_GGX(vec3 incidentDirection, SurfaceData surfaceData, vec3 nor } -vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor ) { +vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor) { return RECIPROCAL_PI * diffuseColor; } diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl index 32d8b9b1..f2333d47 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl @@ -1,22 +1,42 @@ -vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceData) { - float attenuation = 1.0; - vec3 Fd = vec3(0); - vec3 Fs = vec3(0); +#include "brdf.glsl" + +void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ + Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); +} + +void specularLobe(SurfaceData surfaceData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ + Fs += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); +} + +float clearCoatLobe(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, inout vec3 Fs){ + float attenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; Fs += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); - attenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); + attenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); #endif + return attenuation; +} + + +vec3 getDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceData) { + + vec3 Fd = vec3(0); + vec3 Fs = vec3(0); float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); vec3 irradiance = dotNL * color * PI; - Fs += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); - Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + // ClearCoat Lobe + float attenuation = clearCoatLobe(incidentDirection, color, surfaceData, Fs); + // Diffuse Lobe + diffuseLobe(surfaceData, irradiance, attenuation, Fd); + // Specular Lobe + specularLobe(surfaceData, incidentDirection, irradiance, attenuation, Fs); return Fd + Fs; @@ -28,7 +48,7 @@ vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa vec3 color2 = directionalLight.color; vec3 direction = -directionalLight.direction; - color += addDirectRadiance( direction, color2, surfaceData ); + color += getDirectRadiance( direction, color2, surfaceData ); } @@ -40,13 +60,12 @@ vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa vec3 lVector = pointLight.position - surfaceData.position; vec3 direction = normalize( lVector ); - float lightDistance = length( lVector ); vec3 color2 = pointLight.color; color2 *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - color += addDirectRadiance( direction, color2, surfaceData ); + color += getDirectRadiance( direction, color2, surfaceData ); } @@ -58,7 +77,6 @@ vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa vec3 lVector = spotLight.position - surfaceData.position; vec3 direction = normalize( lVector ); - float lightDistance = length( lVector ); float angleCos = dot( direction, -spotLight.direction ); @@ -68,7 +86,7 @@ vec3 addDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa vec3 color2 = spotLight.color; color2 *= spotEffect * decayEffect; - color += addDirectRadiance( direction, color2, surfaceData ); + color += getDirectRadiance( direction, color2, surfaceData ); } diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl index 94f91617..64c6d17f 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl @@ -1,3 +1,4 @@ + // ------------------------Diffuse------------------------ // sh need be pre-scaled in CPU. @@ -102,22 +103,23 @@ void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd) Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); } -void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ +float evaluateClearCoatIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ float radianceAttenuation = 1.0; + #ifdef MATERIAL_ENABLE_CLEAR_COAT - radianceAttenuation -= surfaceData.clearCoat * F_Schlick(surfaceData.f0, surfaceData.clearCoatDotNV); + vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness); + Fs += specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); + radianceAttenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); #endif + return radianceAttenuation; +} + +void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, float radianceAttenuation, inout vec3 Fs){ vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness); Fs += specularAO * radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); } -void evaluateClearCoatIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness); - Fs += specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); - #endif -} void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ vec3 Fd = vec3(0); @@ -128,11 +130,11 @@ void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ // IBL diffuse evaluateDiffuseIBL(surfaceData, diffuseAO, Fd); - // IBL specular - evaluateSpecularIBL(surfaceData, specularAO, Fs); - // IBL ClearCoat - evaluateClearCoatIBL(surfaceData, specularAO, Fs); + float radianceAttenuation = evaluateClearCoatIBL(surfaceData, specularAO, Fs); + + // IBL specular + evaluateSpecularIBL(surfaceData, specularAO, radianceAttenuation, Fs); color += Fd + Fs; } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl index 4b176760..c9007fd8 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl @@ -1,8 +1,4 @@ -#include "material_pbr.glsl" -#include "normal_get.glsl" #include "surfaceData_pbr.glsl" -// direct + indirect -#include "brdf.glsl" #include "light_direct_pbr.glsl" #include "light_indirect_pbr.glsl" diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl index e5967fc5..9439f3c4 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl @@ -1,3 +1,6 @@ +#include "material_pbr.glsl" +#include "normal_get.glsl" + struct SurfaceData{ vec3 position; vec3 normal; From 9f1b5343aa7ea96da433954854a08e5099e7f252 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 30 Apr 2024 14:50:05 +0800 Subject: [PATCH 17/89] refactor: rename --- ...{blendShape_input.glsl => blendShape.glsl} | 0 .../shaderlab/src/shaders/camera_declare.glsl | 2 -- packages/shaderlab/src/shaders/index.ts | 26 ++++++++++--------- .../{normal_get.glsl => normalGet.glsl} | 0 packages/shaderlab/src/shaders/pbr.gs | 4 +-- .../{shading_pbr => shadingPBR}/brdf.glsl | 0 .../shaderlab/src/shaders/shadingPBR/index.ts | 15 +++++++++++ .../lightDirectPBR.glsl} | 0 .../lightIndirectPBR.glsl} | 0 .../materialInputPBR.glsl} | 0 .../shadingPBR.glsl} | 6 ++--- .../surfaceDataPBR.glsl} | 4 +-- .../src/shaders/shading_pbr/index.ts | 15 ----------- packages/shaderlab/src/shaders/shadow.glsl | 2 +- ...sample_tent.glsl => shadowSampleTent.glsl} | 0 packages/shaderlab/src/shaders/skin.glsl | 23 ++++++++++++++++ packages/shaderlab/src/shaders/vert.glsl | 24 +---------------- 17 files changed, 61 insertions(+), 60 deletions(-) rename packages/shaderlab/src/shaders/{blendShape_input.glsl => blendShape.glsl} (100%) delete mode 100644 packages/shaderlab/src/shaders/camera_declare.glsl rename packages/shaderlab/src/shaders/{normal_get.glsl => normalGet.glsl} (100%) rename packages/shaderlab/src/shaders/{shading_pbr => shadingPBR}/brdf.glsl (100%) create mode 100644 packages/shaderlab/src/shaders/shadingPBR/index.ts rename packages/shaderlab/src/shaders/{shading_pbr/light_direct_pbr.glsl => shadingPBR/lightDirectPBR.glsl} (100%) rename packages/shaderlab/src/shaders/{shading_pbr/light_indirect_pbr.glsl => shadingPBR/lightIndirectPBR.glsl} (100%) rename packages/shaderlab/src/shaders/{shading_pbr/material_pbr.glsl => shadingPBR/materialInputPBR.glsl} (100%) rename packages/shaderlab/src/shaders/{shading_pbr/shading_pbr.glsl => shadingPBR/shadingPBR.glsl} (75%) rename packages/shaderlab/src/shaders/{shading_pbr/surfaceData_pbr.glsl => shadingPBR/surfaceDataPBR.glsl} (99%) delete mode 100644 packages/shaderlab/src/shaders/shading_pbr/index.ts rename packages/shaderlab/src/shaders/{shadow_sample_tent.glsl => shadowSampleTent.glsl} (100%) create mode 100644 packages/shaderlab/src/shaders/skin.glsl diff --git a/packages/shaderlab/src/shaders/blendShape_input.glsl b/packages/shaderlab/src/shaders/blendShape.glsl similarity index 100% rename from packages/shaderlab/src/shaders/blendShape_input.glsl rename to packages/shaderlab/src/shaders/blendShape.glsl diff --git a/packages/shaderlab/src/shaders/camera_declare.glsl b/packages/shaderlab/src/shaders/camera_declare.glsl deleted file mode 100644 index 65a27e47..00000000 --- a/packages/shaderlab/src/shaders/camera_declare.glsl +++ /dev/null @@ -1,2 +0,0 @@ -vec3 camera_Position; -vec3 camera_Forward; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index f7e8ff35..cda661bb 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -1,17 +1,18 @@ -import blendShape_input from "./blendShape_input.glsl"; +import blendShape from "./blendShape.glsl"; import common from "./common.glsl"; import fog from "./fog.glsl"; +import input from "./input.glsl"; import light from "./light.glsl"; -import normal_get from "./normal_get.glsl"; -import shadow_sample_tent from "./shadow_sample_tent.glsl"; -import shadow from "./shadow.glsl"; -import transform from "./transform.glsl"; +import normalGet from "./normalGet.glsl"; import pbr from "./pbr.gs"; -import vert from "./vert.glsl"; -import input from "./input.glsl"; -import shading_pbr from "./shading_pbr"; +import shadingPBR from "./shadingPBR"; +import shadow from "./shadow.glsl"; +import shadowSampleTent from "./shadowSampleTent.glsl"; +import skin from "./skin.glsl"; import temp_transformAttributes from "./temp/transformAttributes.glsl"; import temp_transformVaryings from "./temp/transformVaryings.glsl"; +import transform from "./transform.glsl"; +import vert from "./vert.glsl"; interface IShaderFragment { includeKey: string; @@ -19,19 +20,20 @@ interface IShaderFragment { } const pbr_include_fragment_list: IShaderFragment[] = [ - { source: blendShape_input, includeKey: "blendShape_input.glsl" }, + { source: blendShape, includeKey: "blendShape.glsl" }, { source: common, includeKey: "common.glsl" }, { source: fog, includeKey: "fog.glsl" }, { source: light, includeKey: "light.glsl" }, - { source: normal_get, includeKey: "normal_get.glsl" }, - { source: shadow_sample_tent, includeKey: "shadow_sample_tent.glsl" }, + { source: normalGet, includeKey: "normalGet.glsl" }, + { source: shadowSampleTent, includeKey: "shadowSampleTent.glsl" }, { source: shadow, includeKey: "shadow.glsl" }, { source: transform, includeKey: "transform.glsl" }, { source: vert, includeKey: "vert.glsl" }, { source: input, includeKey: "input.glsl" }, { source: temp_transformAttributes, includeKey: "temp_transformAttributes.glsl" }, { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, + { source: skin, includeKey: "skin.glsl" }, - ...shading_pbr + ...shadingPBR ]; export { pbr as pbrSource, pbr_include_fragment_list }; diff --git a/packages/shaderlab/src/shaders/normal_get.glsl b/packages/shaderlab/src/shaders/normalGet.glsl similarity index 100% rename from packages/shaderlab/src/shaders/normal_get.glsl rename to packages/shaderlab/src/shaders/normalGet.glsl diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 2672aea6..687206a2 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -67,14 +67,14 @@ Shader "pbr.gs" { #include "common.glsl" #include "transform.glsl" - #include "blendShape_input.glsl" + #include "blendShape.glsl" #include "vert.glsl" #include "shadow.glsl" #include "fog.glsl" #include "light.glsl" - #include "shading_pbr.glsl" + #include "shadingPBR.glsl" VertexShader = pbrVert; diff --git a/packages/shaderlab/src/shaders/shading_pbr/brdf.glsl b/packages/shaderlab/src/shaders/shadingPBR/brdf.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shading_pbr/brdf.glsl rename to packages/shaderlab/src/shaders/shadingPBR/brdf.glsl diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts new file mode 100644 index 00000000..ecfd89bf --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -0,0 +1,15 @@ +import brdf from "./brdf.glsl"; +import lightDirectPBR from "./lightDirectPBR.glsl"; +import lightIndirectPBR from "./lightIndirectPBR.glsl"; +import materialInputPBR from "./materialInputPBR.glsl"; +import shadingPBR from "./shadingPBR.glsl"; +import surfaceDataPBR from "./surfaceDataPBR.glsl"; + +export default [ + { source: brdf, includeKey: "brdf.glsl" }, + { source: lightDirectPBR, includeKey: "lightDirectPBR.glsl" }, + { source: lightIndirectPBR, includeKey: "lightIndirectPBR.glsl" }, + { source: materialInputPBR, includeKey: "materialInputPBR.glsl" }, + { source: shadingPBR, includeKey: "shadingPBR.glsl" }, + { source: surfaceDataPBR, includeKey: "surfaceDataPBR.glsl" } +]; diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shading_pbr/light_direct_pbr.glsl rename to packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl diff --git a/packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shading_pbr/light_indirect_pbr.glsl rename to packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl diff --git a/packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shading_pbr/material_pbr.glsl rename to packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl diff --git a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl b/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl similarity index 75% rename from packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl rename to packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl index c9007fd8..281c22d4 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/shading_pbr.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl @@ -1,6 +1,6 @@ -#include "surfaceData_pbr.glsl" -#include "light_direct_pbr.glsl" -#include "light_indirect_pbr.glsl" +#include "surfaceDataPBR.glsl" +#include "lightDirectPBR.glsl" +#include "lightIndirectPBR.glsl" vec4 evaluateSurface(Temp_Varyings v, SurfaceData surfaceData){ diff --git a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl similarity index 99% rename from packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl rename to packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl index 9439f3c4..5de34509 100644 --- a/packages/shaderlab/src/shaders/shading_pbr/surfaceData_pbr.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl @@ -1,5 +1,5 @@ -#include "material_pbr.glsl" -#include "normal_get.glsl" +#include "materialInputPBR.glsl" +#include "normalGet.glsl" struct SurfaceData{ vec3 position; diff --git a/packages/shaderlab/src/shaders/shading_pbr/index.ts b/packages/shaderlab/src/shaders/shading_pbr/index.ts deleted file mode 100644 index 13c8961b..00000000 --- a/packages/shaderlab/src/shaders/shading_pbr/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -import brdf from "./brdf.glsl"; -import light_direct_pbr from "./light_direct_pbr.glsl"; -import light_indirect_pbr from "./light_indirect_pbr.glsl"; -import material_pbr from "./material_pbr.glsl"; -import shading_pbr from "./shading_pbr.glsl"; -import surfaceData_pbr from "./surfaceData_pbr.glsl"; - -export default [ - { source: brdf, includeKey: "brdf.glsl" }, - { source: light_direct_pbr, includeKey: "light_direct_pbr.glsl" }, - { source: light_indirect_pbr, includeKey: "light_indirect_pbr.glsl" }, - { source: material_pbr, includeKey: "material_pbr.glsl" }, - { source: shading_pbr, includeKey: "shading_pbr.glsl" }, - { source: surfaceData_pbr, includeKey: "surfaceData_pbr.glsl" } -]; diff --git a/packages/shaderlab/src/shaders/shadow.glsl b/packages/shaderlab/src/shaders/shadow.glsl index 44c41906..ebb42d37 100644 --- a/packages/shaderlab/src/shaders/shadow.glsl +++ b/packages/shaderlab/src/shaders/shadow.glsl @@ -120,7 +120,7 @@ #endif #if SCENE_SHADOW_TYPE == 3 - #include "shadow_sample_tent.glsl" + #include "shadowSampleTent.glsl" float sampleShadowMapFiltered9(TEXTURE2D_SHADOW_PARAM(shadowMap), vec3 shadowCoord, vec4 shadowmapSize) { float attenuation; diff --git a/packages/shaderlab/src/shaders/shadow_sample_tent.glsl b/packages/shaderlab/src/shaders/shadowSampleTent.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shadow_sample_tent.glsl rename to packages/shaderlab/src/shaders/shadowSampleTent.glsl diff --git a/packages/shaderlab/src/shaders/skin.glsl b/packages/shaderlab/src/shaders/skin.glsl new file mode 100644 index 00000000..dbcd405a --- /dev/null +++ b/packages/shaderlab/src/shaders/skin.glsl @@ -0,0 +1,23 @@ +#ifdef RENDERER_HAS_SKIN + #ifdef RENDERER_USE_JOINT_TEXTURE + sampler2D renderer_JointSampler; + float renderer_JointCount; + + mat4 getJointMatrix(sampler2D smp, float index) + { + float base = index / renderer_JointCount; + float hf = 0.5 / renderer_JointCount; + float v = base + hf; + + vec4 m0 = texture2D(smp, vec2(0.125, v )); + vec4 m1 = texture2D(smp, vec2(0.375, v )); + vec4 m2 = texture2D(smp, vec2(0.625, v )); + vec4 m3 = texture2D(smp, vec2(0.875, v )); + + return mat4(m0, m1, m2, m3); + } + + #else + mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; + #endif +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/vert.glsl b/packages/shaderlab/src/shaders/vert.glsl index e6f5c584..5932b60e 100644 --- a/packages/shaderlab/src/shaders/vert.glsl +++ b/packages/shaderlab/src/shaders/vert.glsl @@ -1,26 +1,4 @@ -#ifdef RENDERER_HAS_SKIN - #ifdef RENDERER_USE_JOINT_TEXTURE - sampler2D renderer_JointSampler; - float renderer_JointCount; - - mat4 getJointMatrix(sampler2D smp, float index) - { - float base = index / renderer_JointCount; - float hf = 0.5 / renderer_JointCount; - float v = base + hf; - - vec4 m0 = texture2D(smp, vec2(0.125, v )); - vec4 m1 = texture2D(smp, vec2(0.375, v )); - vec4 m2 = texture2D(smp, vec2(0.625, v )); - vec4 m3 = texture2D(smp, vec2(0.875, v )); - - return mat4(m0, m1, m2, m3); - } - - #else - mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; - #endif -#endif +#include "skin.glsl" vec4 material_TilingOffset; From 91e1b696aaeb95d1d1b1e135d5240a471b231e6a Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 30 Apr 2024 16:58:09 +0800 Subject: [PATCH 18/89] fix: test #define bug --- packages/shaderlab/src/shaders/input.glsl | 6 ------ packages/shaderlab/src/shaders/shadingPBR/brdf.glsl | 6 ++++++ .../shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl | 8 +++++++- .../src/shaders/shadingPBR/lightIndirectPBR.glsl | 8 +++++++- packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl | 5 +++++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/input.glsl index 5468e7be..a6193ea6 100644 --- a/packages/shaderlab/src/shaders/input.glsl +++ b/packages/shaderlab/src/shaders/input.glsl @@ -1,9 +1,3 @@ -// struct VertexData{ -// vec4 position; -// vec3 normal; -// vec4 tangent; -// } - struct Attributes{ vec3 POSITION; diff --git a/packages/shaderlab/src/shaders/shadingPBR/brdf.glsl b/packages/shaderlab/src/shaders/shadingPBR/brdf.glsl index 62cfe45e..5a467e55 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/brdf.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/brdf.glsl @@ -1,4 +1,7 @@ +#ifndef BRDF_INCLUDED +#define BRDF_INCLUDED 1 + float F_Schlick(float f0, float dotLH) { return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); } @@ -123,3 +126,6 @@ vec3 BRDF_Specular_GGX(vec3 incidentDirection, SurfaceData surfaceData, vec3 nor vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor) { return RECIPROCAL_PI * diffuseColor; } + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl index f2333d47..b98bf8d0 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl @@ -1,4 +1,7 @@ +#ifndef LIGHT_DIRECT_PBR_INCLUDED +// #define LIGHT_DIRECT_PBR_INCLUDED 1 + #include "brdf.glsl" void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ @@ -154,4 +157,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 } #endif -} \ No newline at end of file +} + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl index 64c6d17f..4e2f1bed 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl @@ -1,4 +1,7 @@ +// #ifndef LIGHT_INDIRECT_PBR_INCLUDED +// #define LIGHT_INDIRECT_PBR_INCLUDED 1 + // ------------------------Diffuse------------------------ // sh need be pre-scaled in CPU. @@ -137,4 +140,7 @@ void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ evaluateSpecularIBL(surfaceData, specularAO, radianceAttenuation, Fs); color += Fd + Fs; -} \ No newline at end of file +} + + +// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl index 281c22d4..2fee04c9 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl @@ -1,3 +1,6 @@ +// #ifndef SHADING_PBR_INCLUDED +// #define SHADING_PBR_INCLUDED 1 + #include "surfaceDataPBR.glsl" #include "lightDirectPBR.glsl" #include "lightIndirectPBR.glsl" @@ -17,3 +20,5 @@ vec4 evaluateSurface(Temp_Varyings v, SurfaceData surfaceData){ } +// #endif + From cff36bcbe7fbeed9de22c194a1b3fde5d9679a97 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 30 Apr 2024 17:47:25 +0800 Subject: [PATCH 19/89] refactor: avoid repeat include --- packages/shaderlab/src/shaders/blendShape.glsl | 6 ++++++ packages/shaderlab/src/shaders/common.glsl | 6 ++++++ packages/shaderlab/src/shaders/fog.glsl | 6 ++++++ packages/shaderlab/src/shaders/index.ts | 4 ++-- packages/shaderlab/src/shaders/input.glsl | 6 ++++++ packages/shaderlab/src/shaders/light.glsl | 6 ++++++ packages/shaderlab/src/shaders/normalGet.glsl | 8 +++++++- packages/shaderlab/src/shaders/pbr.gs | 8 +------- .../src/shaders/shadingPBR/lightDirectPBR.glsl | 7 +++++-- .../src/shaders/shadingPBR/lightIndirectPBR.glsl | 3 +++ .../src/shaders/shadingPBR/materialInputPBR.glsl | 6 ++++++ .../src/shaders/shadingPBR/surfaceDataPBR.glsl | 6 ++++++ packages/shaderlab/src/shaders/shadow.glsl | 6 ++++++ .../shaderlab/src/shaders/shadowSampleTent.glsl | 8 +++++++- packages/shaderlab/src/shaders/skin.glsl | 7 +++++++ packages/shaderlab/src/shaders/transform.glsl | 7 ++++++- .../src/shaders/{vert.glsl => vertex.glsl} | 13 ++++++++++--- 17 files changed, 96 insertions(+), 17 deletions(-) rename packages/shaderlab/src/shaders/{vert.glsl => vertex.glsl} (98%) diff --git a/packages/shaderlab/src/shaders/blendShape.glsl b/packages/shaderlab/src/shaders/blendShape.glsl index 2318d20a..022d08a5 100644 --- a/packages/shaderlab/src/shaders/blendShape.glsl +++ b/packages/shaderlab/src/shaders/blendShape.glsl @@ -1,3 +1,6 @@ +#ifndef BLENDSHAPE_INCLUDED +#define BLENDSHAPE_INCLUDED 1 + #ifdef RENDERER_HAS_BLENDSHAPE #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE mediump sampler2DArray renderer_BlendShapeTexture; @@ -25,3 +28,6 @@ } #endif #endif + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/common.glsl b/packages/shaderlab/src/shaders/common.glsl index c74fcb31..0889d0ce 100644 --- a/packages/shaderlab/src/shaders/common.glsl +++ b/packages/shaderlab/src/shaders/common.glsl @@ -1,3 +1,6 @@ +#ifndef COMMON_INCLUDED +#define COMMON_INCLUDED 1 + #define PI 3.14159265359 #define RECIPROCAL_PI 0.31830988618 #define EPSILON 1e-6 @@ -92,3 +95,6 @@ float remapDepthBufferLinear01(float z){ #define INVERSE_MAT(mat) inverseMat(mat) #endif + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/fog.glsl b/packages/shaderlab/src/shaders/fog.glsl index 0a69ad64..444e4648 100644 --- a/packages/shaderlab/src/shaders/fog.glsl +++ b/packages/shaderlab/src/shaders/fog.glsl @@ -1,3 +1,6 @@ +#ifndef FOG_INCLUDED +#define FOG_INCLUDED 1 + #if SCENE_FOG_MODE != 0 vec4 scene_FogColor; vec4 scene_FogParams; // (-1/(end-start), end/(end-start), density/ln(2),density/sprt(ln(2))); @@ -22,3 +25,6 @@ return color; } #endif + + +#endif diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index cda661bb..b714af69 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -12,7 +12,7 @@ import skin from "./skin.glsl"; import temp_transformAttributes from "./temp/transformAttributes.glsl"; import temp_transformVaryings from "./temp/transformVaryings.glsl"; import transform from "./transform.glsl"; -import vert from "./vert.glsl"; +import vertex from "./vertex.glsl"; interface IShaderFragment { includeKey: string; @@ -28,7 +28,7 @@ const pbr_include_fragment_list: IShaderFragment[] = [ { source: shadowSampleTent, includeKey: "shadowSampleTent.glsl" }, { source: shadow, includeKey: "shadow.glsl" }, { source: transform, includeKey: "transform.glsl" }, - { source: vert, includeKey: "vert.glsl" }, + { source: vertex, includeKey: "vertex.glsl" }, { source: input, includeKey: "input.glsl" }, { source: temp_transformAttributes, includeKey: "temp_transformAttributes.glsl" }, { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/input.glsl index a6193ea6..1f7dc6fa 100644 --- a/packages/shaderlab/src/shaders/input.glsl +++ b/packages/shaderlab/src/shaders/input.glsl @@ -1,3 +1,6 @@ +// #ifndef INPUT_INCLUDED +// #define INPUT_INCLUDED 1 + struct Attributes{ vec3 POSITION; @@ -208,3 +211,6 @@ struct Temp_Varyings{ vec3 v_pos; #endif } + + +// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/light.glsl b/packages/shaderlab/src/shaders/light.glsl index be55f81c..6f49e64c 100644 --- a/packages/shaderlab/src/shaders/light.glsl +++ b/packages/shaderlab/src/shaders/light.glsl @@ -1,3 +1,6 @@ +#ifndef LIGHT_INCLUDED +#define LIGHT_INCLUDED 1 + // Directional light #ifdef SCENE_DIRECT_LIGHT_COUNT @@ -92,3 +95,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) return true; #endif } + + +#endif diff --git a/packages/shaderlab/src/shaders/normalGet.glsl b/packages/shaderlab/src/shaders/normalGet.glsl index edfca420..c2b2ea66 100644 --- a/packages/shaderlab/src/shaders/normalGet.glsl +++ b/packages/shaderlab/src/shaders/normalGet.glsl @@ -1,3 +1,6 @@ +#ifndef NORMAL_GET_INCLUDED +#define NORMAL_GET_INCLUDED 1 + // gl_FrontFacing has random value on Adreno GPUs // the Adreno bug is only when gl_FrontFacing is inside a function // https://bugs.chromium.org/p/chromium/issues/detail?id=1154842 @@ -55,4 +58,7 @@ mat3 getTBN(Temp_Varyings v, bool isFrontFacing){ #endif return tbn; -} \ No newline at end of file +} + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 687206a2..4dd6d517 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -65,14 +65,8 @@ Shader "pbr.gs" { #include "input.glsl" #include "common.glsl" - #include "transform.glsl" - - #include "blendShape.glsl" - #include "vert.glsl" - #include "shadow.glsl" - + #include "vertex.glsl" #include "fog.glsl" - #include "light.glsl" #include "shadingPBR.glsl" diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl index b98bf8d0..7ccc8568 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl @@ -1,8 +1,11 @@ -#ifndef LIGHT_DIRECT_PBR_INCLUDED +// #ifndef LIGHT_DIRECT_PBR_INCLUDED // #define LIGHT_DIRECT_PBR_INCLUDED 1 #include "brdf.glsl" +#include "light.glsl" +#include "shadow.glsl" + void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); @@ -160,4 +163,4 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 } -#endif \ No newline at end of file +// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl index 4e2f1bed..85b162fd 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl @@ -2,6 +2,9 @@ // #ifndef LIGHT_INDIRECT_PBR_INCLUDED // #define LIGHT_INDIRECT_PBR_INCLUDED 1 +#include "brdf.glsl" +#include "light.glsl" + // ------------------------Diffuse------------------------ // sh need be pre-scaled in CPU. diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl index 0d3a5c34..78a6fd58 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl @@ -1,3 +1,6 @@ +#ifndef MATERIAL_INPUT_PBR_INCLUDED +#define MATERIAL_INPUT_PBR_INCLUDED 1 + #define MIN_PERCEPTUAL_ROUGHNESS 0.045 #define MIN_ROUGHNESS 0.002025 @@ -102,3 +105,6 @@ float getAARoughnessFactor(vec3 normal) { return MIN_PERCEPTUAL_ROUGHNESS; #endif } + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl index 5de34509..2bf63423 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl @@ -1,3 +1,6 @@ +#ifndef SURFACEDATA_PBR_INCLUDED +#define SURFACEDATA_PBR_INCLUDED 1 + #include "materialInputPBR.glsl" #include "normalGet.glsl" @@ -188,3 +191,6 @@ void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontF initGeometry(v, surfaceData, isFrontFacing); initMaterial(v, surfaceData); } + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadow.glsl b/packages/shaderlab/src/shaders/shadow.glsl index ebb42d37..2b4a76e6 100644 --- a/packages/shaderlab/src/shaders/shadow.glsl +++ b/packages/shaderlab/src/shaders/shadow.glsl @@ -1,3 +1,6 @@ +#ifndef SHADOW_INCLUDED +#define SHADOW_INCLUDED 1 + #if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) #define SCENE_IS_CALCULATE_SHADOWS #endif @@ -176,4 +179,7 @@ return attenuation; } +#endif + + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadowSampleTent.glsl b/packages/shaderlab/src/shaders/shadowSampleTent.glsl index db9418f5..05272b78 100644 --- a/packages/shaderlab/src/shaders/shadowSampleTent.glsl +++ b/packages/shaderlab/src/shaders/shadowSampleTent.glsl @@ -1,3 +1,6 @@ +#ifndef SHADOW_SAMPLE_TENT_INCLUDED +#define SHADOW_SAMPLE_TENT_INCLUDED 1 + // ------------------------------------------------------------------ // PCF Filtering Tent Functions // ------------------------------------------------------------------ @@ -111,4 +114,7 @@ void sampleShadowComputeSamplesTent5x5(vec4 shadowMapTextureTexelSize, vec2 coor fetchesWeights[6] = fetchesWeightsU.x * fetchesWeightsV.z; fetchesWeights[7] = fetchesWeightsU.y * fetchesWeightsV.z; fetchesWeights[8] = fetchesWeightsU.z * fetchesWeightsV.z; -} \ No newline at end of file +} + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/skin.glsl b/packages/shaderlab/src/shaders/skin.glsl index dbcd405a..defdcae6 100644 --- a/packages/shaderlab/src/shaders/skin.glsl +++ b/packages/shaderlab/src/shaders/skin.glsl @@ -1,3 +1,7 @@ +#ifndef SKIN_INCLUDED +#define SKIN_INCLUDED 1 + + #ifdef RENDERER_HAS_SKIN #ifdef RENDERER_USE_JOINT_TEXTURE sampler2D renderer_JointSampler; @@ -20,4 +24,7 @@ #else mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; #endif +#endif + + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/transform.glsl b/packages/shaderlab/src/shaders/transform.glsl index 2d5aee0f..e26ed34c 100644 --- a/packages/shaderlab/src/shaders/transform.glsl +++ b/packages/shaderlab/src/shaders/transform.glsl @@ -1,3 +1,6 @@ +#ifndef TRANSFORM_INCLUDED +#define TRANSFORM_INCLUDED 1 + mat4 renderer_LocalMat; mat4 renderer_ModelMat; mat4 camera_ViewMat; @@ -7,4 +10,6 @@ mat4 renderer_MVPMat; mat4 renderer_NormalMat; vec3 camera_Position; -vec3 camera_Forward; \ No newline at end of file +vec3 camera_Forward; + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/vert.glsl b/packages/shaderlab/src/shaders/vertex.glsl similarity index 98% rename from packages/shaderlab/src/shaders/vert.glsl rename to packages/shaderlab/src/shaders/vertex.glsl index 5932b60e..b90fa6a1 100644 --- a/packages/shaderlab/src/shaders/vert.glsl +++ b/packages/shaderlab/src/shaders/vertex.glsl @@ -1,8 +1,13 @@ -#include "skin.glsl" +// #ifndef VERTEX_INCLUDE +// #define VERTEX_INCLUDE 1 -vec4 material_TilingOffset; +#include "skin.glsl" +#include "blendShape.glsl" +#include "shadow.glsl" +#include "transform.glsl" +vec4 material_TilingOffset; void initVertex(){ vec4 position = vec4( attr.POSITION , 1.0 ); @@ -186,4 +191,6 @@ void initVertex(){ // position_vert gl_Position = renderer_MVPMat * position; -} \ No newline at end of file +} + +// #endif \ No newline at end of file From c1a8901530c39d77c2a75fc25273b1f35dcce172 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 6 May 2024 18:51:57 +0800 Subject: [PATCH 20/89] refactor: perf code --- packages/shaderlab/src/GSLPBRMaterial.ts | 12 ++-- .../shaderlab/src/shaders/shadingPBR/index.ts | 4 +- .../shaders/shadingPBR/lightDirectPBR.glsl | 24 ++++---- .../shaders/shadingPBR/lightIndirectPBR.glsl | 1 + .../shadingPBR/materialFunctionPBR.glsl | 59 +++++++++++++++++++ .../shaders/shadingPBR/materialInputPBR.glsl | 39 ------------ .../shaders/shadingPBR/surfaceDataPBR.glsl | 21 +------ 7 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl diff --git a/packages/shaderlab/src/GSLPBRMaterial.ts b/packages/shaderlab/src/GSLPBRMaterial.ts index e8ed8b44..444686ae 100644 --- a/packages/shaderlab/src/GSLPBRMaterial.ts +++ b/packages/shaderlab/src/GSLPBRMaterial.ts @@ -1,7 +1,7 @@ -import { Engine, PBRBaseMaterial, Shader, ShaderFactory, Vector3 } from "@galacean/engine"; +import { Engine, PBRMaterial, Shader, ShaderFactory } from "@galacean/engine"; import { pbrSource, pbr_include_fragment_list } from "./shaders"; -export class GSLPBRMaterial extends PBRBaseMaterial { +export class GSLPBRMaterial extends PBRMaterial { private static _registered = false; static registerIncludes() { @@ -16,12 +16,8 @@ export class GSLPBRMaterial extends PBRBaseMaterial { constructor(engine: Engine) { GSLPBRMaterial.registerIncludes(); const shader = Shader.find("pbr.gs") || Shader.create(pbrSource); - super(engine, shader); + super(engine); - const shaderData = this.shaderData; - shaderData.setFloat("material_Metal", 1); - shaderData.setFloat("material_Roughness", 1); - shaderData.setFloat("material_IOR", 1.5); - shaderData.setVector3("material_AnisotropyInfo", new Vector3(1, 0, 0)); + this.shader = shader; } } diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index ecfd89bf..eb5d4cc4 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -1,6 +1,7 @@ import brdf from "./brdf.glsl"; import lightDirectPBR from "./lightDirectPBR.glsl"; import lightIndirectPBR from "./lightIndirectPBR.glsl"; +import materialFunctionPBR from "./materialFunctionPBR.glsl"; import materialInputPBR from "./materialInputPBR.glsl"; import shadingPBR from "./shadingPBR.glsl"; import surfaceDataPBR from "./surfaceDataPBR.glsl"; @@ -11,5 +12,6 @@ export default [ { source: lightIndirectPBR, includeKey: "lightIndirectPBR.glsl" }, { source: materialInputPBR, includeKey: "materialInputPBR.glsl" }, { source: shadingPBR, includeKey: "shadingPBR.glsl" }, - { source: surfaceDataPBR, includeKey: "surfaceDataPBR.glsl" } + { source: surfaceDataPBR, includeKey: "surfaceDataPBR.glsl" }, + { source: materialFunctionPBR, includeKey: "materialFunctionPBR.glsl" } ]; diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl index 7ccc8568..6ebb414e 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl @@ -30,31 +30,31 @@ float clearCoatLobe(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, } -vec3 getDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceData) { +void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceData, inout vec3 color) { vec3 Fd = vec3(0); vec3 Fs = vec3(0); float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); - vec3 irradiance = dotNL * color * PI; + vec3 irradiance = dotNL * lightColor * PI; // ClearCoat Lobe - float attenuation = clearCoatLobe(incidentDirection, color, surfaceData, Fs); + float attenuation = clearCoatLobe(incidentDirection, lightColor, surfaceData, Fs); // Diffuse Lobe diffuseLobe(surfaceData, irradiance, attenuation, Fd); // Specular Lobe specularLobe(surfaceData, incidentDirection, irradiance, attenuation, Fs); - return Fd + Fs; + color += Fd + Fs; } #ifdef SCENE_DIRECT_LIGHT_COUNT void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout vec3 color) { - vec3 color2 = directionalLight.color; + vec3 lightColor = directionalLight.color; vec3 direction = -directionalLight.direction; - color += getDirectRadiance( direction, color2, surfaceData ); + addRadiance( direction, lightColor, surfaceData, color ); } @@ -68,10 +68,10 @@ vec3 getDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); - vec3 color2 = pointLight.color; - color2 *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); + vec3 lightColor = pointLight.color; + lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - color += getDirectRadiance( direction, color2, surfaceData ); + addRadiance( direction, lightColor, surfaceData, color ); } @@ -89,10 +89,10 @@ vec3 getDirectRadiance(vec3 incidentDirection, vec3 color, SurfaceData surfaceDa float spotEffect = smoothstep( spotLight.penumbraCos, spotLight.angleCos, angleCos ); float decayEffect = clamp(1.0 - pow(lightDistance/spotLight.distance, 4.0), 0.0, 1.0); - vec3 color2 = spotLight.color; - color2 *= spotEffect * decayEffect; + vec3 lightColor = spotLight.color; + lightColor *= spotEffect * decayEffect; - color += getDirectRadiance( direction, color2, surfaceData ); + addRadiance( direction, lightColor, surfaceData, color ); } diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl index 85b162fd..732fc4b3 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl @@ -4,6 +4,7 @@ #include "brdf.glsl" #include "light.glsl" +#include "materialFunctionPBR.glsl" // ------------------------Diffuse------------------------ diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl new file mode 100644 index 00000000..2847fad1 --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl @@ -0,0 +1,59 @@ +#ifndef MATERIAL_FUNCTION_PBR_INCLUDED +#define MATERIAL_FUNCTION_PBR_INCLUDED 1 + +#ifdef MATERIAL_ENABLE_ANISOTROPY + // Aniso Bent Normals + // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far + vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { + vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); + vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); + // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 + vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); + + return bentNormal; + } +#endif + + +float evaluateDiffuseAO(Temp_Varyings v){ + float diffuseAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + return diffuseAO; +} + +float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ + float specularAO = 1.0; + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + return specularAO; +} + + +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); + #else + return MIN_PERCEPTUAL_ROUGHNESS; + #endif +} + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl index 78a6fd58..9c153f1a 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl @@ -67,44 +67,5 @@ float material_OcclusionTextureCoord; #endif -float evaluateDiffuseAO(Temp_Varyings v){ - float diffuseAO = 1.0; - - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - #endif - - return diffuseAO; -} - -float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ - float specularAO = 1.0; - - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) - specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); - #endif - - return specularAO; -} - - -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); - #else - return MIN_PERCEPTUAL_ROUGHNESS; - #endif -} - #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl index 2bf63423..e448322c 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl @@ -1,9 +1,6 @@ #ifndef SURFACEDATA_PBR_INCLUDED #define SURFACEDATA_PBR_INCLUDED 1 -#include "materialInputPBR.glsl" -#include "normalGet.glsl" - struct SurfaceData{ vec3 position; vec3 normal; @@ -34,21 +31,9 @@ struct SurfaceData{ vec3 emissive; } - -#ifdef MATERIAL_ENABLE_ANISOTROPY - // Aniso Bent Normals - // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { - vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); - vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); - // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); - - return bentNormal; - } -#endif - +#include "materialInputPBR.glsl" +#include "normalGet.glsl" +#include "materialFunctionPBR.glsl" void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ From f2b8a0081ffa3a4beea0428dab66954930893903 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 8 May 2024 17:57:59 +0800 Subject: [PATCH 21/89] refactor: move register function --- packages/shaderlab/src/GSLPBRMaterial.ts | 16 ++-------------- packages/shaderlab/src/index.ts | 13 +++++++++++++ packages/shaderlab/src/shaders/index.ts | 7 ++++--- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/shaderlab/src/GSLPBRMaterial.ts b/packages/shaderlab/src/GSLPBRMaterial.ts index 444686ae..0b9d731e 100644 --- a/packages/shaderlab/src/GSLPBRMaterial.ts +++ b/packages/shaderlab/src/GSLPBRMaterial.ts @@ -1,20 +1,8 @@ -import { Engine, PBRMaterial, Shader, ShaderFactory } from "@galacean/engine"; -import { pbrSource, pbr_include_fragment_list } from "./shaders"; +import { Engine, PBRMaterial, Shader } from "@galacean/engine"; +import { pbrSource } from "./shaders"; export class GSLPBRMaterial extends PBRMaterial { - private static _registered = false; - - static registerIncludes() { - if (this._registered) return; - - for (const sourceFragment of pbr_include_fragment_list) { - ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source); - } - this._registered = true; - } - constructor(engine: Engine) { - GSLPBRMaterial.registerIncludes(); const shader = Shader.find("pbr.gs") || Shader.create(pbrSource); super(engine); diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index 5437db58..8bf91fcd 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1 +1,14 @@ export { GSLPBRMaterial } from "./GSLPBRMaterial"; +import { ShaderFactory } from "@galacean/engine"; +import { fragmentList } from "./shaders"; + +let registered = false; + +export function registerIncludes() { + if (registered) return; + + for (const sourceFragment of fragmentList) { + ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source); + } + registered = true; +} diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index b714af69..1bb471e8 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -4,13 +4,14 @@ import fog from "./fog.glsl"; import input from "./input.glsl"; import light from "./light.glsl"; import normalGet from "./normalGet.glsl"; -import pbr from "./pbr.gs"; +import pbrSource from "./pbr.gs"; import shadingPBR from "./shadingPBR"; import shadow from "./shadow.glsl"; import shadowSampleTent from "./shadowSampleTent.glsl"; import skin from "./skin.glsl"; import temp_transformAttributes from "./temp/transformAttributes.glsl"; import temp_transformVaryings from "./temp/transformVaryings.glsl"; +import thinSource from "./thin.gs"; import transform from "./transform.glsl"; import vertex from "./vertex.glsl"; @@ -19,7 +20,7 @@ interface IShaderFragment { source: string; } -const pbr_include_fragment_list: IShaderFragment[] = [ +const fragmentList: IShaderFragment[] = [ { source: blendShape, includeKey: "blendShape.glsl" }, { source: common, includeKey: "common.glsl" }, { source: fog, includeKey: "fog.glsl" }, @@ -36,4 +37,4 @@ const pbr_include_fragment_list: IShaderFragment[] = [ ...shadingPBR ]; -export { pbr as pbrSource, pbr_include_fragment_list }; +export { fragmentList, pbrSource, thinSource }; From f51f8741a212ecb3c6a782ea70f88cf11be842d0 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 8 May 2024 18:25:38 +0800 Subject: [PATCH 22/89] refactor: create shader --- packages/shaderlab/src/GSLPBRMaterial.ts | 3 +-- packages/shaderlab/src/index.ts | 7 +++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/shaderlab/src/GSLPBRMaterial.ts b/packages/shaderlab/src/GSLPBRMaterial.ts index 0b9d731e..5f00767e 100644 --- a/packages/shaderlab/src/GSLPBRMaterial.ts +++ b/packages/shaderlab/src/GSLPBRMaterial.ts @@ -1,9 +1,8 @@ import { Engine, PBRMaterial, Shader } from "@galacean/engine"; -import { pbrSource } from "./shaders"; export class GSLPBRMaterial extends PBRMaterial { constructor(engine: Engine) { - const shader = Shader.find("pbr.gs") || Shader.create(pbrSource); + const shader = Shader.find("pbr.gs"); super(engine); this.shader = shader; diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index 8bf91fcd..fa3aea6d 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1,6 +1,6 @@ export { GSLPBRMaterial } from "./GSLPBRMaterial"; -import { ShaderFactory } from "@galacean/engine"; -import { fragmentList } from "./shaders"; +import { Shader, ShaderFactory } from "@galacean/engine"; +import { fragmentList, pbrSource } from "./shaders"; let registered = false; @@ -10,5 +10,8 @@ export function registerIncludes() { for (const sourceFragment of fragmentList) { ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source); } + + Shader.create(pbrSource); + registered = true; } From 008bb2dcb73225c529ed084501afff11e7567c29 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 8 May 2024 19:28:59 +0800 Subject: [PATCH 23/89] chore: support thin material demo --- packages/shaderlab/src/ThinMaterial.ts | 10 + packages/shaderlab/src/index.ts | 4 +- packages/shaderlab/src/shaders/index.ts | 4 +- .../src/shaders/shadingThin/brdfThin.glsl | 129 +++++++++++++ .../src/shaders/shadingThin/index.ts | 11 ++ .../shaders/shadingThin/lightDirectThin.glsl | 177 ++++++++++++++++++ .../shadingThin/lightIndirectThin.glsl | 159 ++++++++++++++++ .../src/shaders/shadingThin/shadingThin.glsl | 29 +++ packages/shaderlab/src/shaders/thin.gs | 122 ++++++++++++ 9 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 packages/shaderlab/src/ThinMaterial.ts create mode 100644 packages/shaderlab/src/shaders/shadingThin/brdfThin.glsl create mode 100644 packages/shaderlab/src/shaders/shadingThin/index.ts create mode 100644 packages/shaderlab/src/shaders/shadingThin/lightDirectThin.glsl create mode 100644 packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl create mode 100644 packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl create mode 100644 packages/shaderlab/src/shaders/thin.gs diff --git a/packages/shaderlab/src/ThinMaterial.ts b/packages/shaderlab/src/ThinMaterial.ts new file mode 100644 index 00000000..8551ff67 --- /dev/null +++ b/packages/shaderlab/src/ThinMaterial.ts @@ -0,0 +1,10 @@ +import { Engine, PBRMaterial, Shader } from "@galacean/engine"; + +export class ThinMaterial extends PBRMaterial { + constructor(engine: Engine) { + const shader = Shader.find("thin.gs"); + super(engine); + + this.shader = shader; + } +} diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index fa3aea6d..dbedaea9 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1,6 +1,7 @@ export { GSLPBRMaterial } from "./GSLPBRMaterial"; +export { ThinMaterial } from "./ThinMaterial"; import { Shader, ShaderFactory } from "@galacean/engine"; -import { fragmentList, pbrSource } from "./shaders"; +import { fragmentList, pbrSource, thinSource } from "./shaders"; let registered = false; @@ -12,6 +13,7 @@ export function registerIncludes() { } Shader.create(pbrSource); + Shader.create(thinSource); registered = true; } diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 1bb471e8..bf467546 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -6,6 +6,7 @@ import light from "./light.glsl"; import normalGet from "./normalGet.glsl"; import pbrSource from "./pbr.gs"; import shadingPBR from "./shadingPBR"; +import shadingThin from "./shadingThin"; import shadow from "./shadow.glsl"; import shadowSampleTent from "./shadowSampleTent.glsl"; import skin from "./skin.glsl"; @@ -35,6 +36,7 @@ const fragmentList: IShaderFragment[] = [ { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, { source: skin, includeKey: "skin.glsl" }, - ...shadingPBR + ...shadingPBR, + ...shadingThin ]; export { fragmentList, pbrSource, thinSource }; diff --git a/packages/shaderlab/src/shaders/shadingThin/brdfThin.glsl b/packages/shaderlab/src/shaders/shadingThin/brdfThin.glsl new file mode 100644 index 00000000..09dbcb01 --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingThin/brdfThin.glsl @@ -0,0 +1,129 @@ +#ifndef BRDF_THIN_INCLUDED +#define BRDF_THIN_INCLUDED 1 + +float sqr(float x) { return x * x; } +vec2 sqr(vec2 x) {return x * x;} +float IorToFresnel0 ( float transmittedIor, float incidentIor) +{ + return pow((transmittedIor - incidentIor) / (transmittedIor + incidentIor), 2.0); + } +vec3 IorToFresnel0(vec3 transmittedIor, float incidentIor) +{ + return pow((transmittedIor - incidentIor) / (transmittedIor + incidentIor),vec3(2.0,2.0,2.0)); +} + +float Depol (vec3 polV) {return 0.5 * (polV.x + polV.y); } +vec3 DepolColor (vec3 colS, vec3 colP){ return 0.5 * (colS + colP); } + +vec3 Fresnel0ToIor(vec3 F0) +{ + vec3 sqrtF0 = sqrt(F0); + return (vec3(1.0,1.0,1.0) + sqrtF0) / (vec3(1.0,1.0,1.0) - sqrtF0); +} + +//float smithG1_GGX(float NdotV, float a) +///{ + //float a2 = sqr(a); +// return 2.0 / (1.0 + sqrt(1.0 + a2 * (1.0-sqr(NdotV)) / sqr(NdotV) )); +//} + +//float smithG_GGX(float a, float NdotL, float NdotV) +//{ +//return smithG1_GGX(a, NdotL) * smithG1_GGX(a, NdotV); +/// } + + vec3 EvalSensitivity(float opd, vec3 shift) +{ + // Use Gaussian fits, given by 3 parameters: val, pos and var + float phase = 2.0 * PI * opd * 1.0e-6; + vec3 val = vec3(5.4856e-13, 4.4201e-13, 5.2481e-13); + vec3 pos = vec3(1.6810e+6, 1.7953e+6, 2.2084e+6); + vec3 var = vec3(4.3278e+9, 9.3046e+9, 6.6121e+9); + vec3 xyz = val * sqrt(2.0*PI * var) * cos(pos * phase + shift) * exp(-var * sqr(phase)); + xyz.x += 9.7470e-14 * sqrt(2.0*PI * 4.5282e+9) * cos(2.2399e+6 * phase + shift[0]) * exp(-4.5282e+9 * sqr(phase)); + xyz /= 1.0685e-7; + + mat3 XYZ_TO_RGB = mat3( 3.2404542, -0.9692660, 0.0556434, + -1.5371385, 1.8760108, -0.2040259, + -0.4985314, 0.0415560, 1.0572252); + vec3 rgb = XYZ_TO_RGB * xyz; + + return rgb; +} + +vec3 ThinFilmIridescence(float cosTheta1, float eta2, vec3 baseF0,float iridescenceThickness) +{ + float eta1 = 1.0; + float dinc = 2.0 * iridescenceThickness; + // float eta_2 = mix(2.0, 1.0, iridescenceThickness); + float sinTheta2 = pow(eta1 / eta2, 2.0) * (1.0 - pow(cosTheta1 , 2.0)); + float cosTheta2Sq = (1.0 - sinTheta2); + //if (cosTheta2Sq < 0.0) {return vec3(1.0,1.0,1.0);} + float cosTheta2 = sqrt(cosTheta2Sq); + + // First interface + float R0 = IorToFresnel0(eta2, eta1); + float R12 = F_Schlick(R0, cosTheta1); + float R21 = R12; + float T121 =1.0 - R12; + + float phi12 = 0.0; + // if (eta2 < eta1) { phi12 = PI;} + float phi21 = PI - phi12; + + vec3 baseIor = Fresnel0ToIor(baseF0 + 0.0001); + vec3 R1 = IorToFresnel0(baseIor, eta2); + vec3 R23 = F_Schlick(R1, cosTheta2); + + vec3 phi23 =vec3( 0.0,0.0,0.0); + if (baseIor[0] < eta2) {phi23[0] = PI;} + if (baseIor[1] < eta2) {phi23[1] = PI;} + if (baseIor[2] < eta2) {phi23[2] = PI;} + // Phase shift + float OPD = dinc * cosTheta1 * eta2;//dinc * cosTheta2; + vec3 phi = vec3(phi21,phi21,phi21) + phi23; + + vec3 R123 = clamp(R12 * R23, 1e-5, 0.9999); + vec3 r123 = sqrt(R123); + vec3 Rs = sqr(T121) * R23 / (vec3(1.0,1.0,1.0)-R123); + + vec3 C0 = R12 + Rs; + // vec3 S0 = EvalSensitivity(0.0, 0.0 ); + vec3 I = vec3(0.0, 0.0, 0.0); + I = C0;// Depol(C0)*S0; + + vec3 Cm = Rs - T121; + for (int m = 1; m <= 2; ++m) + { + Cm *= r123; + vec3 Sm = 2.0 * EvalSensitivity(float(m) * OPD, float(m) * phi); + // vec3 SmP = 2.0 * (EvalSensitivity(m * OPD, m * phi2.y)); + I += Cm * Sm;//DepolColor(Cm.x * SmS, Cm.y * SmP); + } + I = max(I, vec3(0.0, 0.0, 0.0)); + + return I ; +} + +vec3 DirectBDRFIridescence(vec3 incidentDirection, vec3 viewDir, vec3 normal,vec3 specularColor, float roughness ) +{ + // Compute dot products + float NdotL = saturate( dot( normal, incidentDirection ) ); + float NdotV = saturate( dot( normal, viewDir ) ); + vec3 halfDir = normalize( incidentDirection + viewDir ); + float NdotH = saturate( dot( normal, halfDir ) ); + + float cosTheta1 = dot(halfDir, incidentDirection); + + vec3 I = ThinFilmIridescence(cosTheta1,material_Eta2,specularColor,material_IridescenceThickness); + // Microfacet BRDF formula + float D = D_GGX(roughness, NdotH); + float G = G_GGX_SmithCorrelated(roughness, NdotL, NdotV); + + vec3 color = I ; // (4.0 * NdotL * NdotV); + + return color; +} + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/index.ts b/packages/shaderlab/src/shaders/shadingThin/index.ts new file mode 100644 index 00000000..5e44049e --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingThin/index.ts @@ -0,0 +1,11 @@ +import brdf from "./brdfThin.glsl"; +import lightDirectThin from "./lightDirectThin.glsl"; +import lightIndirectThin from "./lightIndirectThin.glsl"; +import shadingThin from "./shadingThin.glsl"; + +export default [ + { source: brdf, includeKey: "brdfThin.glsl" }, + { source: lightDirectThin, includeKey: "lightDirectThin.glsl" }, + { source: lightIndirectThin, includeKey: "lightIndirectThin.glsl" }, + { source: shadingThin, includeKey: "shadingThin.glsl" } +]; diff --git a/packages/shaderlab/src/shaders/shadingThin/lightDirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/lightDirectThin.glsl new file mode 100644 index 00000000..a1a1006e --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingThin/lightDirectThin.glsl @@ -0,0 +1,177 @@ + +// #ifndef LIGHT_DIRECT_PBR_INCLUDED +// #define LIGHT_DIRECT_PBR_INCLUDED 1 + +#include "brdf.glsl" +#include "brdfThin.glsl" +#include "light.glsl" +#include "shadow.glsl" + +void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ + Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); +} + +void specularLobe(SurfaceData surfaceData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ + vec3 V = surfaceData.viewDir; + vec3 H = normalize(incidentDirection+V); + float VoH = dot(H, V); + vec3 F = F_Schlick( surfaceData.specularColor, VoH ); + + vec3 thin = DirectBDRFIridescence(incidentDirection , surfaceData.viewDir , surfaceData.normal ,surfaceData.specularColor , surfaceData.roughness); + vec3 F_iridescence = thin * material_Iridescence;//mix(vec3(0.0,0.0,0.0),thin,material_Iridescence); + vec3 BRDF_Specular = BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); + vec3 factor = mix(BRDF_Specular, thin, material_Iridescence); + + + Fs += attenuation * irradiance * factor; +} + +float clearCoatLobe(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, inout vec3 Fs){ + float attenuation = 1.0; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); + vec3 clearCoatIrradiance = clearCoatDotNL * color; + + Fs += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); + attenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); + #endif + + return attenuation; +} + + +void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceData, inout vec3 color) { + + vec3 Fd = vec3(0); + vec3 Fs = vec3(0); + float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); + vec3 irradiance = dotNL * lightColor * PI; + + // ClearCoat Lobe + float attenuation = clearCoatLobe(incidentDirection, lightColor, surfaceData, Fs); + // Diffuse Lobe + diffuseLobe(surfaceData, irradiance, attenuation, Fd); + // //Iridescence Specular Lobe + specularLobe(surfaceData, incidentDirection, irradiance, attenuation, Fs); + + color += Fd + Fs; + +} + +#ifdef SCENE_DIRECT_LIGHT_COUNT + + void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout vec3 color) { + vec3 lightColor = directionalLight.color; + vec3 direction = -directionalLight.direction; + + addRadiance( direction, lightColor, surfaceData, color ); + + } + +#endif + +#ifdef SCENE_POINT_LIGHT_COUNT + + void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, inout vec3 color) { + + vec3 lVector = pointLight.position - surfaceData.position; + vec3 direction = normalize( lVector ); + float lightDistance = length( lVector ); + + vec3 lightColor = pointLight.color; + lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); + + addRadiance( direction, lightColor, surfaceData, color ); + + } + +#endif + +#ifdef SCENE_SPOT_LIGHT_COUNT + + void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, inout vec3 color) { + + vec3 lVector = spotLight.position - surfaceData.position; + vec3 direction = normalize( lVector ); + float lightDistance = length( lVector ); + float angleCos = dot( direction, -spotLight.direction ); + + float spotEffect = smoothstep( spotLight.penumbraCos, spotLight.angleCos, angleCos ); + float decayEffect = clamp(1.0 - pow(lightDistance/spotLight.distance, 4.0), 0.0, 1.0); + + vec3 lightColor = spotLight.color; + lightColor *= spotEffect * decayEffect; + + addRadiance( direction, lightColor, surfaceData, color ); + + } + + +#endif + +void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ + float shadowAttenuation = 1.0; + + #ifdef SCENE_DIRECT_LIGHT_COUNT + shadowAttenuation = 1.0; + #ifdef SCENE_IS_CALCULATE_SHADOWS + shadowAttenuation *= sampleShadowMap(v); + // int sunIndex = int(scene_ShadowInfo.z); + #endif + + DirectLight directionalLight; + for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { + // warning: use `continue` syntax may trigger flickering bug in safri 16.1. + if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){ + directionalLight.color = scene_DirectLightColor[i]; + #ifdef SCENE_IS_CALCULATE_SHADOWS + if (i == 0) { // Sun light index is always 0 + directionalLight.color *= shadowAttenuation; + } + #endif + directionalLight.direction = scene_DirectLightDirection[i]; + addDirectionalDirectLightRadiance( directionalLight, surfaceData, color ); + } + } + + #endif + + #ifdef SCENE_POINT_LIGHT_COUNT + + PointLight pointLight; + + for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) { + if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){ + pointLight.color = scene_PointLightColor[i]; + pointLight.position = scene_PointLightPosition[i]; + pointLight.distance = scene_PointLightDistance[i]; + + addPointDirectLightRadiance( pointLight, surfaceData, color ); + } + } + + #endif + + #ifdef SCENE_SPOT_LIGHT_COUNT + + SpotLight spotLight; + + for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) { + if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){ + spotLight.color = scene_SpotLightColor[i]; + spotLight.position = scene_SpotLightPosition[i]; + spotLight.direction = scene_SpotLightDirection[i]; + spotLight.distance = scene_SpotLightDistance[i]; + spotLight.angleCos = scene_SpotLightAngleCos[i]; + spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; + + addSpotDirectLightRadiance( spotLight, surfaceData, color ); + } + } + + #endif +} + + +// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl new file mode 100644 index 00000000..804bab70 --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl @@ -0,0 +1,159 @@ + +// #ifndef LIGHT_INDIRECT_PBR_INCLUDED +// #define LIGHT_INDIRECT_PBR_INCLUDED 1 + +#include "brdf.glsl" +#include "brdfThin.glsl" +#include "light.glsl" +#include "materialFunctionPBR.glsl" + +// ------------------------Diffuse------------------------ + +// sh need be pre-scaled in CPU. +vec3 getLightProbeIrradiance(vec3 sh[9], vec3 normal){ + normal.x = -normal.x; + vec3 result = sh[0] + + + sh[1] * (normal.y) + + sh[2] * (normal.z) + + sh[3] * (normal.x) + + + sh[4] * (normal.y * normal.x) + + sh[5] * (normal.y * normal.z) + + sh[6] * (3.0 * normal.z * normal.z - 1.0) + + sh[7] * (normal.z * normal.x) + + sh[8] * (normal.x * normal.x - normal.y * normal.y); + + return max(result, vec3(0.0)); + +} + +// ------------------------Specular------------------------ + +// ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile +vec3 envBRDFApprox(vec3 specularColor, float roughness, float dotNV ) { + + const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 ); + + const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 ); + + vec4 r = roughness * c0 + c1; + + float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y; + + vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; + + return specularColor * AB.x + AB.y; + +} + + +float getSpecularMIPLevel(float roughness, int maxMIPLevel ) { + return roughness * float(maxMIPLevel); +} + +vec3 getReflectedVector(SurfaceData surfaceData, vec3 n) { + #ifdef MATERIAL_ENABLE_ANISOTROPY + vec3 r = reflect(-surfaceData.viewDir, surfaceData.anisotropicN); + #else + vec3 r = reflect(-surfaceData.viewDir, n); + #endif + + return r; +} + +vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness) { + + #ifndef SCENE_USE_SPECULAR_ENV + return vec3(0); + #else + vec3 reflectVec = getReflectedVector(surfaceData, normal); + reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse + + float specularMIPLevel = getSpecularMIPLevel(roughness, int(scene_EnvMapLight.mipMapLevel) ); + + #ifdef HAS_TEX_LOD + vec4 envMapColor = textureCubeLodEXT( scene_EnvSpecularSampler, reflectVec, specularMIPLevel ); + #else + vec4 envMapColor = textureCube( scene_EnvSpecularSampler, reflectVec, specularMIPLevel ); + #endif + + #ifdef SCENE_IS_DECODE_ENV_RGBM + envMapColor.rgb = (RGBMToLinear(envMapColor, 5.0)).rgb; + #ifdef ENGINE_IS_COLORSPACE_GAMMA + envMapColor = linearToGamma(envMapColor); + #endif + #else + #ifndef ENGINE_IS_COLORSPACE_GAMMA + envMapColor = gammaToLinear(envMapColor); + #endif + #endif + + return envMapColor.rgb * scene_EnvMapLight.specularIntensity; + + #endif + +} + + +void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd){ + #ifdef SCENE_USE_SH + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); + #ifdef ENGINE_IS_COLORSPACE_GAMMA + irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; + #endif + irradiance *= scene_EnvMapLight.diffuseIntensity; + #else + vec3 irradiance = scene_EnvMapLight.diffuse * scene_EnvMapLight.diffuseIntensity; + irradiance *= PI; + #endif + + Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); +} + +float evaluateClearCoatIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ + float radianceAttenuation = 1.0; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness); + Fs += specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); + radianceAttenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); + #endif + + return radianceAttenuation; +} + +void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, float radianceAttenuation, inout vec3 Fs){ + vec3 reflectdir = reflect(surfaceData.normal, -surfaceData.viewDir); + vec3 halfdir = reflectdir + surfaceData.viewDir; + float cosTheta1 = dot(halfdir, reflectdir); + vec3 fresnelIridescent = ThinFilmIridescence(cosTheta1, material_Eta2 , surfaceData.specularColor ,material_IridescenceThickness); + //vec3 F_iridescence = fresnelIridescent * material_Iridescence;//mix(vec3(0.0,0.0,0.0),fresnelIridescent,material_Iridescence); + vec3 envBRDF = envBRDFApprox( surfaceData.specularColor , surfaceData.roughness, surfaceData.dotNV ); + vec3 fator = mix(envBRDF, fresnelIridescent, material_Iridescence); + + vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness); + Fs += specularAO * radianceAttenuation * radiance * fator; +} + + +void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ + vec3 Fd = vec3(0); + vec3 Fs = vec3(0); + float diffuseAO = evaluateDiffuseAO(v); + float specularAO = evaluateSpecularAO(diffuseAO, surfaceData.roughness, surfaceData.dotNV); + + // IBL diffuse + evaluateDiffuseIBL(surfaceData, diffuseAO, Fd); + + // IBL ClearCoat + float radianceAttenuation = evaluateClearCoatIBL(surfaceData, specularAO, Fs); + + // IBL Iridescence Specular + evaluateSpecularIBL(surfaceData, specularAO, radianceAttenuation, Fs); + + color += Fd + Fs; +} + + +// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl b/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl new file mode 100644 index 00000000..ab1ddc4f --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl @@ -0,0 +1,29 @@ +// #ifndef SHADING_PBR_INCLUDED +// #define SHADING_PBR_INCLUDED 1 + +float material_Iridescence; +float material_IridescenceThickness; +float material_Eta2; + + +#include "surfaceDataPBR.glsl" +#include "lightDirectThin.glsl" +#include "lightIndirectThin.glsl" + + +vec4 evaluateSurface(Temp_Varyings v, SurfaceData surfaceData){ + vec3 color = vec3(0); + + // Direct Light + evaluateDirectRadiance(v, surfaceData, color); + // IBL + evaluateIBL(v, surfaceData, color); + // Emissive + color += surfaceData.emissive; + + return vec4(color, surfaceData.opacity); +} + + +// #endif + diff --git a/packages/shaderlab/src/shaders/thin.gs b/packages/shaderlab/src/shaders/thin.gs new file mode 100644 index 00000000..8221c940 --- /dev/null +++ b/packages/shaderlab/src/shaders/thin.gs @@ -0,0 +1,122 @@ +Shader "thin.gs" { + EditorProperties { + material_Metal( "Metal", Range(0,1,0.01) ) = 1; + material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; + material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; + material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); + material_AnisotropyTexture("AnisotropyTexture", Texture2D); + + Header("Base PBR") { + material_BaseColor("BaseColor", Color) = (1,1,1,1); + material_EmissiveColor( "EmissiveColor", Color ) = (0,0,0,1); + material_TilingOffset("TilingOffset", Vector4) = (1,1,0,0); + material_NormalIntensity("NormalIntensity", Float) = 1; + material_OcclusionIntensity("OcclusionIntensity", Float) = 1; + material_OcclusionTextureCoord("OcclusionTextureCoord", Int) = 0; + material_ClearCoat("ClearCoat", Float) = 0; + material_ClearCoatRoughness("ClearCoatRoughness", Float) = 0; + } + + Header("Thin Film") { + material_Eta2( "iridescentIor", Range( 1, 10, 0.1 ) ) = 1; + // material_Eta3("iridescentIor", Range(0,5, 0.1)) = 1.0; + //material_Kappa3("k3", Range(0, 10, 0.1)) = 1.0; + material_Iridescence("Iridescence", Range(0.0 , 1 , 0.01)) = 0.0; + material_IridescenceThickness("iridescenceThickness", Range(0.01 , 5 , 0.01)) = 0.07; + } + + Collapsible("Base Material") { + material_AlphaCutoff( "AlphaCutoff", Float ) = 0; + material_PBRSpecularColor("PBRSpecularColor", Color) = (1,1,1,1); + material_Glossiness("Glossiness", Float) = 1.0; + material_BaseTexture("BaseTexture", Texture2D); + material_NormalTexture("NormalTexture", Texture2D); + material_EmissiveTexture("EmissiveTexture", Texture2D); + material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); + material_SpecularGlossinessTexture("SpecularGlossinessTexture", Texture2D); + material_OcclusionTexture("OcclusionTexture", Texture2D); + material_ClearCoatTexture("ClearCoatTexture", Texture2D); + material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); + material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); + } + } + + EditorMacros { + Header("Conditional Macors") { + MATERIAL_OMIT_NORMAL("OMIT_NORMAL"); + MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); + MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); + MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); + MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); + MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); + MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); + MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); + MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); + } + + Collapsible("Enabled Macros") { + [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); + [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); + } + } + + SubShader "Default" { + UsePass "pbr/Default/ShadowCaster" + + Pass "Forward Pass" { + Tags { pipelineStage = "Forward"} + + #define IS_METALLIC_WORKFLOW + + #include "input.glsl" + #include "common.glsl" + #include "vertex.glsl" + #include "fog.glsl" + + #include "shadingThin.glsl" + + VertexShader = pbrVert; + FragmentShader = pbrFrag; + + Varyings pbrVert(Attributes attr) { + Varyings v; + + // @todo: delete + Temp_Attributes temp_attributes; + Temp_Varyings temp_varyings; + #include "temp_transformAttributes.glsl" + #include "temp_transformVaryings.glsl" + + // @todo: use initVertex(attr, v); + initVertex(); + + return v; + } + + void pbrFrag(Varyings v) { + SurfaceData surfaceData; + + // @todo: delete + Temp_Varyings temp_varyings; + #include "temp_transformVaryings.glsl" + + initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); + + vec4 color = evaluateSurface(temp_varyings, surfaceData); + gl_FragColor = color; + + #if SCENE_FOG_MODE != 0 + gl_FragColor = fog(gl_FragColor, v.v_positionVS); + #endif + + #ifndef ENGINE_IS_COLORSPACE_GAMMA + gl_FragColor = linearToGamma(gl_FragColor); + #endif + } + } + + } + } \ No newline at end of file From d249f3760e5c39a428329824601064331b46e979 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 8 May 2024 19:29:53 +0800 Subject: [PATCH 24/89] chore: merge error --- packages/shaderlab/src/shaders/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 1bb471e8..8d4ec816 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -11,7 +11,6 @@ import shadowSampleTent from "./shadowSampleTent.glsl"; import skin from "./skin.glsl"; import temp_transformAttributes from "./temp/transformAttributes.glsl"; import temp_transformVaryings from "./temp/transformVaryings.glsl"; -import thinSource from "./thin.gs"; import transform from "./transform.glsl"; import vertex from "./vertex.glsl"; @@ -37,4 +36,4 @@ const fragmentList: IShaderFragment[] = [ ...shadingPBR ]; -export { fragmentList, pbrSource, thinSource }; +export { fragmentList, pbrSource }; From 0f7e615a3ecb5554a09ac93faf94495cfeba4f7c Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 10 May 2024 14:14:38 +0800 Subject: [PATCH 25/89] "v0.0.0-experimental-shaderlab.0" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index cafcbd1e..9e8991f4 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index c6535037..8c6d62d8 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 67789b3c..ada2e8c0 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 2d5270ed..1e5bb946 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 2f7e35a2..7149edea 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index d0a73d2d..f1cbaec3 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index eb9066a3..38cc1495 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index ae066dea..3c652b96 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 5f2f0ecb..715aa851 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index ff6372e5..ae51edf6 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index fabfefe0..e98f6dab 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index d6851c56..fcc19993 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 2f34a4bd..92e256d9 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 203678a0..9343855e 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 717a8947..eaaeb3f6 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 5f619aee..ca386757 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 47a14c76..5c5ba1b1 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 6da3e7a4..5b63804d 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 75259b86..7cf8fcde 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 1825da468167013c2c1cebb40ea5ec5f916687de Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 10 May 2024 14:17:31 +0800 Subject: [PATCH 26/89] "v0.0.0-experimental-shaderlab-thin.0" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index cafcbd1e..050caae3 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index c6535037..4cbdda4b 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 67789b3c..d503f1dc 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 2d5270ed..61c28b9a 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 2f7e35a2..7b34b2d2 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index d0a73d2d..9bb200ef 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index eb9066a3..901182ea 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index ae066dea..66bc076a 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 5f2f0ecb..449a973b 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index ff6372e5..134cd7e5 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index fabfefe0..eb1ec3a5 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index d6851c56..53ca9945 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 2f34a4bd..479f4a60 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 203678a0..0f4ecc09 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 717a8947..15f6d5a5 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 5f619aee..22428ea8 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 47a14c76..965c1b54 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 6da3e7a4..0039a585 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 75259b86..c35e7e6a 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "1.2.0-beta.3", + "version": "0.0.0-experimental-shaderlab-thin.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 073f951c414765265545962bc2adafc301d13bab Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 10 May 2024 17:25:55 +0800 Subject: [PATCH 27/89] Revert ""v0.0.0-experimental-shaderlab.0"" This reverts commit 0f7e615a3ecb5554a09ac93faf94495cfeba4f7c. --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 9e8991f4..cafcbd1e 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 8c6d62d8..c6535037 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index ada2e8c0..67789b3c 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 1e5bb946..2d5270ed 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 7149edea..2f7e35a2 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index f1cbaec3..d0a73d2d 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 38cc1495..eb9066a3 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 3c652b96..ae066dea 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 715aa851..5f2f0ecb 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index ae51edf6..ff6372e5 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index e98f6dab..fabfefe0 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index fcc19993..d6851c56 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 92e256d9..2f34a4bd 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 9343855e..203678a0 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index eaaeb3f6..717a8947 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index ca386757..5f619aee 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 5c5ba1b1..47a14c76 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 5b63804d..6da3e7a4 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 7cf8fcde..75259b86 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab.0", + "version": "1.2.0-beta.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From db9703fccdaef30394c2372aa6ce820231641c01 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Sat, 11 May 2024 13:11:50 +0800 Subject: [PATCH 28/89] refactor: prettier editor props --- packages/shaderlab/src/shaders/pbr.gs | 71 ++++++++++++++++----------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 4dd6d517..442cc7a8 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -1,55 +1,70 @@ Shader "pbr.gs" { EditorProperties { - material_Metal( "Metal", Range(0,1,0.01) ) = 1; - material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; - material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; - material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); - material_AnisotropyTexture("AnisotropyTexture", Texture2D); - - Header("Base PBR") { - material_BaseColor("BaseColor", Color) = (1,1,1,1); - material_EmissiveColor( "EmissiveColor", Color ) = (0,0,0,1); - material_TilingOffset("TilingOffset", Vector4) = (1,1,0,0); - material_NormalIntensity("NormalIntensity", Float) = 1; - material_OcclusionIntensity("OcclusionIntensity", Float) = 1; - material_OcclusionTextureCoord("OcclusionTextureCoord", Int) = 0; - material_ClearCoat("ClearCoat", Float) = 0; - material_ClearCoatRoughness("ClearCoatRoughness", Float) = 0; - } - - Collapsible("Base Material") { - material_AlphaCutoff( "AlphaCutoff", Float ) = 0; - material_PBRSpecularColor("PBRSpecularColor", Color) = (1,1,1,1); - material_Glossiness("Glossiness", Float) = 1.0; + Header("Base"){ + material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; + material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); material_BaseTexture("BaseTexture", Texture2D); + } + + Header("Metal Roughness") { + material_Metal( "Metal", Range(0,1,0.01) ) = 1; + material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; + material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); + } + + Header("Anisotropy") { + material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); + material_AnisotropyTexture("AnisotropyTexture", Texture2D); + } + + Header("Normal") { material_NormalTexture("NormalTexture", Texture2D); + material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; + } + + Header("Emissive") { + material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); material_EmissiveTexture("EmissiveTexture", Texture2D); - material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); - material_SpecularGlossinessTexture("SpecularGlossinessTexture", Texture2D); + } + + Header("Occlusion") { material_OcclusionTexture("OcclusionTexture", Texture2D); + material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; + material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; + } + + Header("Clear Coat") { + material_ClearCoat("ClearCoat", Range(0, 1, 0.01)) = 0; material_ClearCoatTexture("ClearCoatTexture", Texture2D); + material_ClearCoatRoughness("ClearCoatRoughness", Range(0, 1, 0.01)) = 0; material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); } + + Header("Common") { + material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; + material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); + } } EditorMacros { Header("Conditional Macors") { - MATERIAL_OMIT_NORMAL("OMIT_NORMAL"); - MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); + MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); + MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY"); + MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE") MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); - MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); + MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); - MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); + MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); } - Collapsible("Enabled Macros") { + Header("Enabled Macros") { [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); } From 480b43dfeaec44a889e0f7ec8080747d12b61884 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Sat, 11 May 2024 13:24:23 +0800 Subject: [PATCH 29/89] refactor: change default value --- packages/shaderlab/src/shaders/thin.gs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shaderlab/src/shaders/thin.gs b/packages/shaderlab/src/shaders/thin.gs index bf7a4935..f259835c 100644 --- a/packages/shaderlab/src/shaders/thin.gs +++ b/packages/shaderlab/src/shaders/thin.gs @@ -1,9 +1,9 @@ Shader "thin.gs" { EditorProperties { Header("Thin Film") { - material_Eta2("iridescentIor", Range(1, 10, 0.1)) = 1.5; - material_Iridescence("Iridescence", Range(0, 1, 0.01)) = 0; + material_Iridescence("Iridescence", Range(0, 1, 0.01)) = 1; material_IridescenceThickness("iridescenceThickness", Range(0.01, 5, 0.01)) = 0.1; + material_Eta2("iridescentIor", Range(1, 5, 0.1)) = 1.5; } Header("Base"){ From 1d8a2bb14f23f7e855bc97850d248608d84ddb63 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Sat, 11 May 2024 13:25:50 +0800 Subject: [PATCH 30/89] "v0.0.0-experimental-shaderlab-thin.1" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 050caae3..5d3dbc5a 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 4cbdda4b..a8bf903f 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index d503f1dc..015e1518 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 61c28b9a..541606ba 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 7b34b2d2..2a82942d 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 9bb200ef..00dfdb01 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 901182ea..b0210131 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 66bc076a..49e3788d 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 449a973b..51ca6cdb 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 134cd7e5..4a230611 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index eb1ec3a5..6384591a 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 53ca9945..6f2080a4 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 479f4a60..9a1e0a30 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 0f4ecc09..f3cc3e82 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 15f6d5a5..f05cf936 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 22428ea8..afaaa457 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 965c1b54..0192736c 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 0039a585..b814ede5 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index c35e7e6a..95eba2a5 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.0", + "version": "0.0.0-experimental-shaderlab-thin.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 4005483778e35f845c3f25b897ca6e04c86948a6 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 14 May 2024 19:27:05 +0800 Subject: [PATCH 31/89] refactor: code enhance --- packages/shaderlab/src/shaders/index.ts | 6 +- .../shaders/{normalGet.glsl => normal.glsl} | 0 packages/shaderlab/src/shaders/pbr.gs | 47 +--- .../attributesPBR.glsl} | 73 ------- .../shaders/shadingPBR/forwardPassPBR.glsl | 48 +++++ .../shaderlab/src/shaders/shadingPBR/index.ts | 12 +- .../shaders/shadingPBR/lightIndirectPBR.glsl | 27 ++- .../shadingPBR/materialFunctionPBR.glsl | 59 ----- .../shaders/shadingPBR/materialInputPBR.glsl | 201 ++++++++++++++++++ .../src/shaders/shadingPBR/shadingPBR.glsl | 1 - .../shaders/shadingPBR/surfaceDataPBR.glsl | 181 ---------------- .../src/shaders/shadingPBR/varyingsPBR.glsl | 68 ++++++ 12 files changed, 353 insertions(+), 370 deletions(-) rename packages/shaderlab/src/shaders/{normalGet.glsl => normal.glsl} (100%) rename packages/shaderlab/src/shaders/{input.glsl => shadingPBR/attributesPBR.glsl} (68%) create mode 100644 packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl delete mode 100644 packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl delete mode 100644 packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl create mode 100644 packages/shaderlab/src/shaders/shadingPBR/varyingsPBR.glsl diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 8d4ec816..7b223a2c 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -1,9 +1,8 @@ import blendShape from "./blendShape.glsl"; import common from "./common.glsl"; import fog from "./fog.glsl"; -import input from "./input.glsl"; import light from "./light.glsl"; -import normalGet from "./normalGet.glsl"; +import normal from "./normal.glsl"; import pbrSource from "./pbr.gs"; import shadingPBR from "./shadingPBR"; import shadow from "./shadow.glsl"; @@ -24,12 +23,11 @@ const fragmentList: IShaderFragment[] = [ { source: common, includeKey: "common.glsl" }, { source: fog, includeKey: "fog.glsl" }, { source: light, includeKey: "light.glsl" }, - { source: normalGet, includeKey: "normalGet.glsl" }, + { source: normal, includeKey: "normal.glsl" }, { source: shadowSampleTent, includeKey: "shadowSampleTent.glsl" }, { source: shadow, includeKey: "shadow.glsl" }, { source: transform, includeKey: "transform.glsl" }, { source: vertex, includeKey: "vertex.glsl" }, - { source: input, includeKey: "input.glsl" }, { source: temp_transformAttributes, includeKey: "temp_transformAttributes.glsl" }, { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, { source: skin, includeKey: "skin.glsl" }, diff --git a/packages/shaderlab/src/shaders/normalGet.glsl b/packages/shaderlab/src/shaders/normal.glsl similarity index 100% rename from packages/shaderlab/src/shaders/normalGet.glsl rename to packages/shaderlab/src/shaders/normal.glsl diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index 442cc7a8..e6be46e6 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -77,53 +77,8 @@ Shader "pbr.gs" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW + #include "forwardPassPBR.glsl" - #include "input.glsl" - #include "common.glsl" - #include "vertex.glsl" - #include "fog.glsl" - - #include "shadingPBR.glsl" - - - VertexShader = pbrVert; - FragmentShader = pbrFrag; - - Varyings pbrVert(Attributes attr) { - Varyings v; - - // @todo: delete - Temp_Attributes temp_attributes; - Temp_Varyings temp_varyings; - #include "temp_transformAttributes.glsl" - #include "temp_transformVaryings.glsl" - - // @todo: use initVertex(attr, v); - initVertex(); - - return v; - } - - void pbrFrag(Varyings v) { - SurfaceData surfaceData; - - // @todo: delete - Temp_Varyings temp_varyings; - #include "temp_transformVaryings.glsl" - - initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - - vec4 color = evaluateSurface(temp_varyings, surfaceData); - gl_FragColor = color; - - #if SCENE_FOG_MODE != 0 - gl_FragColor = fog(gl_FragColor, v.v_positionVS); - #endif - - #ifndef ENGINE_IS_COLORSPACE_GAMMA - gl_FragColor = linearToGamma(gl_FragColor); - #endif - } } } diff --git a/packages/shaderlab/src/shaders/input.glsl b/packages/shaderlab/src/shaders/shadingPBR/attributesPBR.glsl similarity index 68% rename from packages/shaderlab/src/shaders/input.glsl rename to packages/shaderlab/src/shaders/shadingPBR/attributesPBR.glsl index 1f7dc6fa..a1dd59e0 100644 --- a/packages/shaderlab/src/shaders/input.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/attributesPBR.glsl @@ -1,6 +1,3 @@ -// #ifndef INPUT_INCLUDED -// #define INPUT_INCLUDED 1 - struct Attributes{ vec3 POSITION; @@ -73,39 +70,6 @@ struct Attributes{ #endif } -struct Varyings{ - vec2 v_uv; - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 v_color; - #endif - - #if SCENE_FOG_MODE != 0 - vec3 v_positionVS; - #endif - - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 v_normal; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - mat3 v_TBN; - #endif - #endif - #endif - - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 - vec3 v_shadowCoord; - #endif - #endif - - #ifdef RENDERER_HAS_UV1 - vec2 v_uv1; - #endif - #ifdef MATERIAL_NEED_WORLD_POS - vec3 v_pos; - #endif -} struct Temp_Attributes{ #ifdef RENDERER_HAS_BLENDSHAPE @@ -177,40 +141,3 @@ struct Temp_Attributes{ #endif #endif } - -struct Temp_Varyings{ - #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 v_color; - #endif - - #if SCENE_FOG_MODE != 0 - vec3 v_positionVS; - #endif - - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 v_normal; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - mat3 v_TBN; - #endif - #endif - #endif - - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 - vec3 v_shadowCoord; - #endif - #endif - - vec2 v_uv; - - #ifdef RENDERER_HAS_UV1 - vec2 v_uv1; - #endif - #ifdef MATERIAL_NEED_WORLD_POS - vec3 v_pos; - #endif -} - - -// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl new file mode 100644 index 00000000..ac6d2ffc --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl @@ -0,0 +1,48 @@ +#include "attributesPBR.glsl" +#include "varyingsPBR.glsl" +#include "common.glsl" +#include "vertex.glsl" +#include "fog.glsl" + +#include "materialInputPBR.glsl" +#include "shadingPBR.glsl" + + +VertexShader = pbrVert; +FragmentShader = pbrFrag; + +Varyings pbrVert(Attributes attr) { + Varyings v; + + // @todo: delete + Temp_Attributes temp_attributes; + Temp_Varyings temp_varyings; + #include "temp_transformAttributes.glsl" + #include "temp_transformVaryings.glsl" + + // @todo: use initVertex(attr, v); + initVertex(); + + return v; +} + +void pbrFrag(Varyings v) { + SurfaceData surfaceData; + + // @todo: delete + Temp_Varyings temp_varyings; + #include "temp_transformVaryings.glsl" + + initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); + + vec4 color = evaluateSurface(temp_varyings, surfaceData); + gl_FragColor = color; + + #if SCENE_FOG_MODE != 0 + gl_FragColor = fog(gl_FragColor, v.v_positionVS); + #endif + + #ifndef ENGINE_IS_COLORSPACE_GAMMA + gl_FragColor = linearToGamma(gl_FragColor); + #endif +} \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index eb5d4cc4..85b7e2f3 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -1,17 +1,19 @@ +import attributesPBR from "./attributesPBR.glsl"; import brdf from "./brdf.glsl"; +import forwardPassPBR from "./forwardPassPBR.glsl"; import lightDirectPBR from "./lightDirectPBR.glsl"; import lightIndirectPBR from "./lightIndirectPBR.glsl"; -import materialFunctionPBR from "./materialFunctionPBR.glsl"; import materialInputPBR from "./materialInputPBR.glsl"; import shadingPBR from "./shadingPBR.glsl"; -import surfaceDataPBR from "./surfaceDataPBR.glsl"; +import varyingsPBR from "./varyingsPBR.glsl"; export default [ { source: brdf, includeKey: "brdf.glsl" }, { source: lightDirectPBR, includeKey: "lightDirectPBR.glsl" }, { source: lightIndirectPBR, includeKey: "lightIndirectPBR.glsl" }, - { source: materialInputPBR, includeKey: "materialInputPBR.glsl" }, { source: shadingPBR, includeKey: "shadingPBR.glsl" }, - { source: surfaceDataPBR, includeKey: "surfaceDataPBR.glsl" }, - { source: materialFunctionPBR, includeKey: "materialFunctionPBR.glsl" } + { source: materialInputPBR, includeKey: "materialInputPBR.glsl" }, + { source: attributesPBR, includeKey: "attributesPBR.glsl" }, + { source: varyingsPBR, includeKey: "varyingsPBR.glsl" }, + { source: forwardPassPBR, includeKey: "forwardPassPBR.glsl" } ]; diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl index 732fc4b3..08a959b8 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl @@ -4,7 +4,6 @@ #include "brdf.glsl" #include "light.glsl" -#include "materialFunctionPBR.glsl" // ------------------------Diffuse------------------------ @@ -128,6 +127,32 @@ void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, float radian } +float evaluateDiffuseAO(Temp_Varyings v){ + float diffuseAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + return diffuseAO; +} + +float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ + float specularAO = 1.0; + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + return specularAO; +} + void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ vec3 Fd = vec3(0); vec3 Fs = vec3(0); diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl deleted file mode 100644 index 2847fad1..00000000 --- a/packages/shaderlab/src/shaders/shadingPBR/materialFunctionPBR.glsl +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef MATERIAL_FUNCTION_PBR_INCLUDED -#define MATERIAL_FUNCTION_PBR_INCLUDED 1 - -#ifdef MATERIAL_ENABLE_ANISOTROPY - // Aniso Bent Normals - // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { - vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); - vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); - // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); - - return bentNormal; - } -#endif - - -float evaluateDiffuseAO(Temp_Varyings v){ - float diffuseAO = 1.0; - - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - #endif - - return diffuseAO; -} - -float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ - float specularAO = 1.0; - - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) - specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); - #endif - - return specularAO; -} - - -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); - #else - return MIN_PERCEPTUAL_ROUGHNESS; - #endif -} - - -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl index 9c153f1a..043ca144 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl @@ -1,6 +1,38 @@ #ifndef MATERIAL_INPUT_PBR_INCLUDED #define MATERIAL_INPUT_PBR_INCLUDED 1 +#include "normal.glsl" + +struct SurfaceData{ + vec3 position; + vec3 normal; + vec3 viewDir; + float dotNV; + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + vec3 clearCoatNormal; + float clearCoatDotNV; + #endif + + #ifdef MATERIAL_ENABLE_ANISOTROPY + vec3 anisotropicT; + vec3 anisotropicB; + vec3 anisotropicN; + float anisotropy; + #endif + + vec3 diffuseColor; + float roughness; + vec3 specularColor; + float opacity; + float f0; + #ifdef MATERIAL_ENABLE_CLEAR_COAT + float clearCoat; + float clearCoatRoughness; + #endif + vec3 emissive; +} + #define MIN_PERCEPTUAL_ROUGHNESS 0.045 #define MIN_ROUGHNESS 0.002025 @@ -68,4 +100,173 @@ float material_OcclusionTextureCoord; +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); + #else + return MIN_PERCEPTUAL_ROUGHNESS; + #endif +} + +#ifdef MATERIAL_ENABLE_ANISOTROPY + // Aniso Bent Normals + // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far + vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { + vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); + vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); + // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 + vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); + + return bentNormal; + } +#endif + + +void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ + surfaceData.position = v.v_pos; + #ifdef CAMERA_ORTHOGRAPHIC + surfaceData.viewDir = -camera_Forward; + #else + surfaceData.viewDir = normalize(camera_Position - v.v_pos); + #endif + #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) + mat3 tbn = getTBN(v, isFrontFacing); + #endif + + #ifdef MATERIAL_HAS_NORMALTEXTURE + surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + surfaceData.normal = getNormal(v, isFrontFacing); + #endif + + surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); + + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + surfaceData.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + surfaceData.clearCoatNormal = getNormal(v, isFrontFacing); + #endif + surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); + #endif + + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy = material_AnisotropyInfo.z; + vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; + anisotropy *= anisotropyTextureInfo.b; + anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; + #endif + + surfaceData.anisotropy = anisotropy; + surfaceData.anisotropicT = normalize(tbn * anisotropicDirection); + surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); + surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); + + #endif +} + +void initMaterial(Temp_Varyings v, inout SurfaceData surfaceData){ + vec4 baseColor = material_BaseColor; + float metal = material_Metal; + float roughness = material_Roughness; + vec3 specularColor = material_PBRSpecularColor; + float glossiness = material_Glossiness; + float alphaCutoff = material_AlphaCutoff; + float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); + + surfaceData.f0 = f0; + + #ifdef MATERIAL_HAS_BASETEXTURE + vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + baseTextureColor = gammaToLinear(baseTextureColor); + #endif + baseColor *= baseTextureColor; + #endif + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + baseColor *= v.v_color; + #endif + + + #ifdef MATERIAL_IS_ALPHA_CUTOFF + if( baseColor.a < alphaCutoff ) { + discard; + } + #endif + + #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE + vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); + roughness *= metalRoughMapColor.g; + metal *= metalRoughMapColor.b; + #endif + + #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE + vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + specularGlossinessColor = gammaToLinear(specularGlossinessColor); + #endif + specularColor *= specularGlossinessColor.rgb; + glossiness *= specularGlossinessColor.a; + #endif + + + #ifdef IS_METALLIC_WORKFLOW + surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - metal ); + surfaceData.specularColor = mix( vec3(f0), baseColor.rgb, metal ); + surfaceData.roughness = roughness; + #else + float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); + surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); + surfaceData.specularColor = specularColor; + surfaceData.roughness = 1.0 - glossiness; + #endif + + surfaceData.roughness = max(surfaceData.roughness, getAARoughnessFactor(surfaceData.normal)); + + #ifdef MATERIAL_ENABLE_CLEAR_COAT + surfaceData.clearCoat = material_ClearCoat; + surfaceData.clearCoatRoughness = material_ClearCoatRoughness; + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; + #endif + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; + #endif + surfaceData.clearCoat = saturate( surfaceData.clearCoat ); + surfaceData.clearCoatRoughness = max(surfaceData.clearCoatRoughness, getAARoughnessFactor(surfaceData.clearCoatNormal)); + #endif + + #ifdef MATERIAL_IS_TRANSPARENT + surfaceData.opacity = baseColor.a; + #else + surfaceData.opacity = 1.0; + #endif + + vec3 emissiveRadiance = material_EmissiveColor; + #ifdef MATERIAL_HAS_EMISSIVETEXTURE + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + emissiveColor = gammaToLinear(emissiveColor); + #endif + emissiveRadiance *= emissiveColor.rgb; + #endif + surfaceData.emissive = emissiveRadiance; + +} + +void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ + initGeometry(v, surfaceData, isFrontFacing); + initMaterial(v, surfaceData); +} + + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl index 2fee04c9..a0eb6557 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl @@ -1,7 +1,6 @@ // #ifndef SHADING_PBR_INCLUDED // #define SHADING_PBR_INCLUDED 1 -#include "surfaceDataPBR.glsl" #include "lightDirectPBR.glsl" #include "lightIndirectPBR.glsl" diff --git a/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl deleted file mode 100644 index e448322c..00000000 --- a/packages/shaderlab/src/shaders/shadingPBR/surfaceDataPBR.glsl +++ /dev/null @@ -1,181 +0,0 @@ -#ifndef SURFACEDATA_PBR_INCLUDED -#define SURFACEDATA_PBR_INCLUDED 1 - -struct SurfaceData{ - vec3 position; - vec3 normal; - vec3 viewDir; - float dotNV; - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatNormal; - float clearCoatDotNV; - #endif - - #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 anisotropicT; - vec3 anisotropicB; - vec3 anisotropicN; - float anisotropy; - #endif - - vec3 diffuseColor; - float roughness; - vec3 specularColor; - float opacity; - float f0; - #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoat; - float clearCoatRoughness; - #endif - vec3 emissive; -} - -#include "materialInputPBR.glsl" -#include "normalGet.glsl" -#include "materialFunctionPBR.glsl" - - -void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ - surfaceData.position = v.v_pos; - #ifdef CAMERA_ORTHOGRAPHIC - surfaceData.viewDir = -camera_Forward; - #else - surfaceData.viewDir = normalize(camera_Position - v.v_pos); - #endif - #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) - mat3 tbn = getTBN(v, isFrontFacing); - #endif - - #ifdef MATERIAL_HAS_NORMALTEXTURE - surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - surfaceData.normal = getNormal(v, isFrontFacing); - #endif - - surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); - - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - surfaceData.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - surfaceData.clearCoatNormal = getNormal(v, isFrontFacing); - #endif - surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); - #endif - - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy = material_AnisotropyInfo.z; - vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; - anisotropy *= anisotropyTextureInfo.b; - anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; - #endif - - surfaceData.anisotropy = anisotropy; - surfaceData.anisotropicT = normalize(tbn * anisotropicDirection); - surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); - surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); - - #endif -} - -void initMaterial(Temp_Varyings v, inout SurfaceData surfaceData){ - vec4 baseColor = material_BaseColor; - float metal = material_Metal; - float roughness = material_Roughness; - vec3 specularColor = material_PBRSpecularColor; - float glossiness = material_Glossiness; - float alphaCutoff = material_AlphaCutoff; - float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); - - surfaceData.f0 = f0; - - #ifdef MATERIAL_HAS_BASETEXTURE - vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - baseTextureColor = gammaToLinear(baseTextureColor); - #endif - baseColor *= baseTextureColor; - #endif - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - baseColor *= v.v_color; - #endif - - - #ifdef MATERIAL_IS_ALPHA_CUTOFF - if( baseColor.a < alphaCutoff ) { - discard; - } - #endif - - #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE - vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); - roughness *= metalRoughMapColor.g; - metal *= metalRoughMapColor.b; - #endif - - #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE - vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - specularGlossinessColor = gammaToLinear(specularGlossinessColor); - #endif - specularColor *= specularGlossinessColor.rgb; - glossiness *= specularGlossinessColor.a; - #endif - - - #ifdef IS_METALLIC_WORKFLOW - surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - metal ); - surfaceData.specularColor = mix( vec3(f0), baseColor.rgb, metal ); - surfaceData.roughness = roughness; - #else - float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); - surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); - surfaceData.specularColor = specularColor; - surfaceData.roughness = 1.0 - glossiness; - #endif - - surfaceData.roughness = max(surfaceData.roughness, getAARoughnessFactor(surfaceData.normal)); - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - surfaceData.clearCoat = material_ClearCoat; - surfaceData.clearCoatRoughness = material_ClearCoatRoughness; - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; - #endif - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; - #endif - surfaceData.clearCoat = saturate( surfaceData.clearCoat ); - surfaceData.clearCoatRoughness = max(surfaceData.clearCoatRoughness, getAARoughnessFactor(surfaceData.clearCoatNormal)); - #endif - - #ifdef MATERIAL_IS_TRANSPARENT - surfaceData.opacity = baseColor.a; - #else - surfaceData.opacity = 1.0; - #endif - - vec3 emissiveRadiance = material_EmissiveColor; - #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - emissiveColor = gammaToLinear(emissiveColor); - #endif - emissiveRadiance *= emissiveColor.rgb; - #endif - surfaceData.emissive = emissiveRadiance; - -} - -void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ - initGeometry(v, surfaceData, isFrontFacing); - initMaterial(v, surfaceData); -} - - -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/varyingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/varyingsPBR.glsl new file mode 100644 index 00000000..702f49f8 --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingPBR/varyingsPBR.glsl @@ -0,0 +1,68 @@ +struct Varyings{ + vec2 v_uv; + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 v_color; + #endif + + #if SCENE_FOG_MODE != 0 + vec3 v_positionVS; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 v_normal; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + mat3 v_TBN; + #endif + #endif + #endif + + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT==1 + vec3 v_shadowCoord; + #endif + #endif + + #ifdef RENDERER_HAS_UV1 + vec2 v_uv1; + #endif + #ifdef MATERIAL_NEED_WORLD_POS + vec3 v_pos; + #endif +} + + +struct Temp_Varyings{ + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 v_color; + #endif + + #if SCENE_FOG_MODE != 0 + vec3 v_positionVS; + #endif + + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + vec3 v_normal; + #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + mat3 v_TBN; + #endif + #endif + #endif + + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT==1 + vec3 v_shadowCoord; + #endif + #endif + + vec2 v_uv; + + #ifdef RENDERER_HAS_UV1 + vec2 v_uv1; + #endif + #ifdef MATERIAL_NEED_WORLD_POS + vec3 v_pos; + #endif +} From 702a57427707121829dbf283ba6fb8317bbde125 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 14 May 2024 20:30:08 +0800 Subject: [PATCH 32/89] fix: shader error --- packages/shaderlab/src/shaders/vertex.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shaderlab/src/shaders/vertex.glsl b/packages/shaderlab/src/shaders/vertex.glsl index b90fa6a1..9eee68e2 100644 --- a/packages/shaderlab/src/shaders/vertex.glsl +++ b/packages/shaderlab/src/shaders/vertex.glsl @@ -157,7 +157,7 @@ void initVertex(){ // color_vert #ifdef RENDERER_ENABLE_VERTEXCOLOR - v.v_color = COLOR_0; + v.v_color = attr.COLOR_0; #endif From d850b7074c59ab793fe19ba5e54beb6a563c1df3 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 14 May 2024 20:38:37 +0800 Subject: [PATCH 33/89] enhance: split surface init --- .../shaders/shadingPBR/materialInputPBR.glsl | 199 +++++++++--------- 1 file changed, 105 insertions(+), 94 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl index 043ca144..88385980 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl @@ -6,6 +6,7 @@ struct SurfaceData{ vec3 position; vec3 normal; + mat3 tbn; vec3 viewDir; float dotNV; @@ -127,15 +128,19 @@ float getAARoughnessFactor(vec3 normal) { #endif -void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ + +void initCommonSurfaceData(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ surfaceData.position = v.v_pos; + #ifdef CAMERA_ORTHOGRAPHIC - surfaceData.viewDir = -camera_Forward; + surfaceData.viewDir = -camera_Forward; #else - surfaceData.viewDir = normalize(camera_Position - v.v_pos); + surfaceData.viewDir = normalize(camera_Position - v.v_pos); #endif + #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) mat3 tbn = getTBN(v, isFrontFacing); + surfaceData.tbn = tbn; #endif #ifdef MATERIAL_HAS_NORMALTEXTURE @@ -146,126 +151,132 @@ void initGeometry(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFa surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); + vec4 baseColor = material_BaseColor; + float metal = material_Metal; + float roughness = material_Roughness; + vec3 specularColor = material_PBRSpecularColor; + float glossiness = material_Glossiness; + float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); - #ifdef MATERIAL_ENABLE_CLEAR_COAT - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - surfaceData.clearCoatNormal = getNormalByNormalTexture(tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - surfaceData.clearCoatNormal = getNormal(v, isFrontFacing); + surfaceData.f0 = f0; + + #ifdef MATERIAL_HAS_BASETEXTURE + vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + baseTextureColor = gammaToLinear(baseTextureColor); #endif - surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); + baseColor *= baseTextureColor; #endif - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy = material_AnisotropyInfo.z; - vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; - anisotropy *= anisotropyTextureInfo.b; - anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; - #endif + #ifdef RENDERER_ENABLE_VERTEXCOLOR + baseColor *= v.v_color; + #endif - surfaceData.anisotropy = anisotropy; - surfaceData.anisotropicT = normalize(tbn * anisotropicDirection); - surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); - surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); + #ifdef MATERIAL_IS_ALPHA_CUTOFF + if( baseColor.a < material_AlphaCutoff ) { + discard; + } #endif -} -void initMaterial(Temp_Varyings v, inout SurfaceData surfaceData){ - vec4 baseColor = material_BaseColor; - float metal = material_Metal; - float roughness = material_Roughness; - vec3 specularColor = material_PBRSpecularColor; - float glossiness = material_Glossiness; - float alphaCutoff = material_AlphaCutoff; - float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); - - surfaceData.f0 = f0; - - #ifdef MATERIAL_HAS_BASETEXTURE - vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - baseTextureColor = gammaToLinear(baseTextureColor); - #endif - baseColor *= baseTextureColor; - #endif + #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE + vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); + roughness *= metalRoughMapColor.g; + metal *= metalRoughMapColor.b; + #endif - #ifdef RENDERER_ENABLE_VERTEXCOLOR - baseColor *= v.v_color; + #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE + vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + specularGlossinessColor = gammaToLinear(specularGlossinessColor); #endif + specularColor *= specularGlossinessColor.rgb; + glossiness *= specularGlossinessColor.a; + #endif - #ifdef MATERIAL_IS_ALPHA_CUTOFF - if( baseColor.a < alphaCutoff ) { - discard; - } - #endif + #ifdef IS_METALLIC_WORKFLOW + surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - metal ); + surfaceData.specularColor = mix( vec3(f0), baseColor.rgb, metal ); + surfaceData.roughness = roughness; + #else + float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); + surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); + surfaceData.specularColor = specularColor; + surfaceData.roughness = 1.0 - glossiness; + #endif - #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE - vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); - roughness *= metalRoughMapColor.g; - metal *= metalRoughMapColor.b; - #endif + surfaceData.roughness = max(surfaceData.roughness, getAARoughnessFactor(surfaceData.normal)); + + #ifdef MATERIAL_IS_TRANSPARENT + surfaceData.opacity = baseColor.a; + #else + surfaceData.opacity = 1.0; + #endif - #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE - vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - specularGlossinessColor = gammaToLinear(specularGlossinessColor); - #endif - specularColor *= specularGlossinessColor.rgb; - glossiness *= specularGlossinessColor.a; + vec3 emissiveRadiance = material_EmissiveColor; + #ifdef MATERIAL_HAS_EMISSIVETEXTURE + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + emissiveColor = gammaToLinear(emissiveColor); #endif + emissiveRadiance *= emissiveColor.rgb; + #endif + surfaceData.emissive = emissiveRadiance; +} - #ifdef IS_METALLIC_WORKFLOW - surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - metal ); - surfaceData.specularColor = mix( vec3(f0), baseColor.rgb, metal ); - surfaceData.roughness = roughness; +void initClearCoatSurfaceData(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ + #ifdef MATERIAL_ENABLE_CLEAR_COAT + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + surfaceData.clearCoatNormal = getNormalByNormalTexture(surfaceData.tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else - float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); - surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); - surfaceData.specularColor = specularColor; - surfaceData.roughness = 1.0 - glossiness; + surfaceData.clearCoatNormal = getNormal(v, isFrontFacing); #endif + surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); - surfaceData.roughness = max(surfaceData.roughness, getAARoughnessFactor(surfaceData.normal)); - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - surfaceData.clearCoat = material_ClearCoat; - surfaceData.clearCoatRoughness = material_ClearCoatRoughness; - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; - #endif - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; - #endif - surfaceData.clearCoat = saturate( surfaceData.clearCoat ); - surfaceData.clearCoatRoughness = max(surfaceData.clearCoatRoughness, getAARoughnessFactor(surfaceData.clearCoatNormal)); + surfaceData.clearCoat = material_ClearCoat; + surfaceData.clearCoatRoughness = material_ClearCoatRoughness; + + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; #endif - #ifdef MATERIAL_IS_TRANSPARENT - surfaceData.opacity = baseColor.a; - #else - surfaceData.opacity = 1.0; + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; #endif - vec3 emissiveRadiance = material_EmissiveColor; - #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - emissiveColor = gammaToLinear(emissiveColor); - #endif - emissiveRadiance *= emissiveColor.rgb; + surfaceData.clearCoat = saturate( surfaceData.clearCoat ); + surfaceData.clearCoatRoughness = max(surfaceData.clearCoatRoughness, getAARoughnessFactor(surfaceData.clearCoatNormal)); + + #endif + +} + +void initAnisotropySurfaceData(Temp_Varyings v, inout SurfaceData surfaceData){ + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy = material_AnisotropyInfo.z; + vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; + anisotropy *= anisotropyTextureInfo.b; + anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; #endif - surfaceData.emissive = emissiveRadiance; + + surfaceData.anisotropy = anisotropy; + surfaceData.anisotropicT = normalize(surfaceData.tbn * anisotropicDirection); + surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); + surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); + + #endif } + void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ - initGeometry(v, surfaceData, isFrontFacing); - initMaterial(v, surfaceData); + initCommonSurfaceData(v, surfaceData, isFrontFacing); + initClearCoatSurfaceData(v, surfaceData, isFrontFacing); + initAnisotropySurfaceData(v, surfaceData); } From 0c50d1afa1c95028ca75f1e4e21fbf569ec82e87 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 14 May 2024 20:49:00 +0800 Subject: [PATCH 34/89] refactor: new api --- .../shaders/shadingThin/forwardPassThin.glsl | 48 +++++++++++++++++++ .../src/shaders/shadingThin/index.ts | 4 +- .../shadingThin/lightIndirectThin.glsl | 27 ++++++++++- .../src/shaders/shadingThin/shadingThin.glsl | 1 - packages/shaderlab/src/shaders/thin.gs | 46 +----------------- 5 files changed, 78 insertions(+), 48 deletions(-) create mode 100644 packages/shaderlab/src/shaders/shadingThin/forwardPassThin.glsl diff --git a/packages/shaderlab/src/shaders/shadingThin/forwardPassThin.glsl b/packages/shaderlab/src/shaders/shadingThin/forwardPassThin.glsl new file mode 100644 index 00000000..1aced2f7 --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingThin/forwardPassThin.glsl @@ -0,0 +1,48 @@ +#include "attributesPBR.glsl" +#include "varyingsPBR.glsl" +#include "common.glsl" +#include "vertex.glsl" +#include "fog.glsl" + +#include "materialInputPBR.glsl" +#include "shadingThin.glsl" + + +VertexShader = pbrVert; +FragmentShader = pbrFrag; + +Varyings pbrVert(Attributes attr) { + Varyings v; + + // @todo: delete + Temp_Attributes temp_attributes; + Temp_Varyings temp_varyings; + #include "temp_transformAttributes.glsl" + #include "temp_transformVaryings.glsl" + + // @todo: use initVertex(attr, v); + initVertex(); + + return v; +} + +void pbrFrag(Varyings v) { + SurfaceData surfaceData; + + // @todo: delete + Temp_Varyings temp_varyings; + #include "temp_transformVaryings.glsl" + + initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); + + vec4 color = evaluateSurface(temp_varyings, surfaceData); + gl_FragColor = color; + + #if SCENE_FOG_MODE != 0 + gl_FragColor = fog(gl_FragColor, v.v_positionVS); + #endif + + #ifndef ENGINE_IS_COLORSPACE_GAMMA + gl_FragColor = linearToGamma(gl_FragColor); + #endif +} \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/index.ts b/packages/shaderlab/src/shaders/shadingThin/index.ts index 5e44049e..74f40e01 100644 --- a/packages/shaderlab/src/shaders/shadingThin/index.ts +++ b/packages/shaderlab/src/shaders/shadingThin/index.ts @@ -1,4 +1,5 @@ import brdf from "./brdfThin.glsl"; +import forwardPassThin from "./forwardPassThin.glsl"; import lightDirectThin from "./lightDirectThin.glsl"; import lightIndirectThin from "./lightIndirectThin.glsl"; import shadingThin from "./shadingThin.glsl"; @@ -7,5 +8,6 @@ export default [ { source: brdf, includeKey: "brdfThin.glsl" }, { source: lightDirectThin, includeKey: "lightDirectThin.glsl" }, { source: lightIndirectThin, includeKey: "lightIndirectThin.glsl" }, - { source: shadingThin, includeKey: "shadingThin.glsl" } + { source: shadingThin, includeKey: "shadingThin.glsl" }, + { source: forwardPassThin, includeKey: "forwardPassThin.glsl" } ]; diff --git a/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl index 804bab70..7f14a41e 100644 --- a/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl +++ b/packages/shaderlab/src/shaders/shadingThin/lightIndirectThin.glsl @@ -5,7 +5,6 @@ #include "brdf.glsl" #include "brdfThin.glsl" #include "light.glsl" -#include "materialFunctionPBR.glsl" // ------------------------Diffuse------------------------ @@ -136,6 +135,32 @@ void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, float radian Fs += specularAO * radianceAttenuation * radiance * fator; } +float evaluateDiffuseAO(Temp_Varyings v){ + float diffuseAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + return diffuseAO; +} + +float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ + float specularAO = 1.0; + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + return specularAO; +} + void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ vec3 Fd = vec3(0); diff --git a/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl b/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl index ab1ddc4f..e47797bd 100644 --- a/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl +++ b/packages/shaderlab/src/shaders/shadingThin/shadingThin.glsl @@ -6,7 +6,6 @@ float material_IridescenceThickness; float material_Eta2; -#include "surfaceDataPBR.glsl" #include "lightDirectThin.glsl" #include "lightIndirectThin.glsl" diff --git a/packages/shaderlab/src/shaders/thin.gs b/packages/shaderlab/src/shaders/thin.gs index f259835c..20f9b5ca 100644 --- a/packages/shaderlab/src/shaders/thin.gs +++ b/packages/shaderlab/src/shaders/thin.gs @@ -83,52 +83,8 @@ Shader "thin.gs" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW + #include "forwardPassThin.glsl" - #include "input.glsl" - #include "common.glsl" - #include "vertex.glsl" - #include "fog.glsl" - - #include "shadingThin.glsl" - - VertexShader = pbrVert; - FragmentShader = pbrFrag; - - Varyings pbrVert(Attributes attr) { - Varyings v; - - // @todo: delete - Temp_Attributes temp_attributes; - Temp_Varyings temp_varyings; - #include "temp_transformAttributes.glsl" - #include "temp_transformVaryings.glsl" - - // @todo: use initVertex(attr, v); - initVertex(); - - return v; - } - - void pbrFrag(Varyings v) { - SurfaceData surfaceData; - - // @todo: delete - Temp_Varyings temp_varyings; - #include "temp_transformVaryings.glsl" - - initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - - vec4 color = evaluateSurface(temp_varyings, surfaceData); - gl_FragColor = color; - - #if SCENE_FOG_MODE != 0 - gl_FragColor = fog(gl_FragColor, v.v_positionVS); - #endif - - #ifndef ENGINE_IS_COLORSPACE_GAMMA - gl_FragColor = linearToGamma(gl_FragColor); - #endif - } } } From 0d455e835ab07b7934ad5ed2220f9ebbfdd45ca4 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 17 May 2024 09:58:05 +0800 Subject: [PATCH 35/89] chore: move pass --- packages/shaderlab/src/shaders/pbr.gs | 6 ++++-- .../shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl | 3 --- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/pbr.gs index e6be46e6..4f513d64 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/pbr.gs @@ -77,9 +77,11 @@ Shader "pbr.gs" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW - #include "forwardPassPBR.glsl" + VertexShader = pbrVert; + FragmentShader = pbrFrag; + + #include "forwardPassPBR.glsl" } - } } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl index ac6d2ffc..1166a877 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl @@ -8,9 +8,6 @@ #include "shadingPBR.glsl" -VertexShader = pbrVert; -FragmentShader = pbrFrag; - Varyings pbrVert(Attributes attr) { Varyings v; From 5189541f203362f8a058f10a984dc8be505065a7 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 17 May 2024 14:41:18 +0800 Subject: [PATCH 36/89] chore: lowercase to uppercase --- packages/shaderlab/src/GSLPBRMaterial.ts | 2 +- packages/shaderlab/src/index.ts | 6 ++- .../{blendShape.glsl => BlendShape.glsl} | 0 .../src/shaders/{common.glsl => Common.glsl} | 0 .../src/shaders/{fog.glsl => Fog.glsl} | 0 .../src/shaders/{light.glsl => Light.glsl} | 0 .../src/shaders/{normal.glsl => Normal.glsl} | 0 .../shaderlab/src/shaders/{pbr.gs => PBR.gs} | 10 +++-- .../src/shaders/{shadow.glsl => Shadow.glsl} | 2 +- ...wSampleTent.glsl => ShadowSampleTent.glsl} | 0 .../src/shaders/{skin.glsl => Skin.glsl} | 0 .../{transform.glsl => Transform.glsl} | 0 .../src/shaders/{vertex.glsl => Vertex.glsl} | 8 ++-- packages/shaderlab/src/shaders/index.ts | 44 +++++++++---------- ...{attributesPBR.glsl => AttributesPBR.glsl} | 0 .../shadingPBR/{brdf.glsl => BRDF.glsl} | 0 ...orwardPassPBR.glsl => ForwardPassPBR.glsl} | 18 ++++---- ...ightDirectPBR.glsl => LightDirectPBR.glsl} | 6 +-- ...IndirectPBR.glsl => LightIndirectPBR.glsl} | 4 +- ...ialInputPBR.glsl => MaterialInputPBR.glsl} | 2 +- .../{shadingPBR.glsl => ShadingPBR.glsl} | 4 +- .../{varyingsPBR.glsl => VaryingsPBR.glsl} | 0 .../shaderlab/src/shaders/shadingPBR/index.ts | 32 +++++++------- 23 files changed, 71 insertions(+), 67 deletions(-) rename packages/shaderlab/src/shaders/{blendShape.glsl => BlendShape.glsl} (100%) rename packages/shaderlab/src/shaders/{common.glsl => Common.glsl} (100%) rename packages/shaderlab/src/shaders/{fog.glsl => Fog.glsl} (100%) rename packages/shaderlab/src/shaders/{light.glsl => Light.glsl} (100%) rename packages/shaderlab/src/shaders/{normal.glsl => Normal.glsl} (100%) rename packages/shaderlab/src/shaders/{pbr.gs => PBR.gs} (95%) rename packages/shaderlab/src/shaders/{shadow.glsl => Shadow.glsl} (99%) rename packages/shaderlab/src/shaders/{shadowSampleTent.glsl => ShadowSampleTent.glsl} (100%) rename packages/shaderlab/src/shaders/{skin.glsl => Skin.glsl} (100%) rename packages/shaderlab/src/shaders/{transform.glsl => Transform.glsl} (100%) rename packages/shaderlab/src/shaders/{vertex.glsl => Vertex.glsl} (98%) rename packages/shaderlab/src/shaders/shadingPBR/{attributesPBR.glsl => AttributesPBR.glsl} (100%) rename packages/shaderlab/src/shaders/shadingPBR/{brdf.glsl => BRDF.glsl} (100%) rename packages/shaderlab/src/shaders/shadingPBR/{forwardPassPBR.glsl => ForwardPassPBR.glsl} (73%) rename packages/shaderlab/src/shaders/shadingPBR/{lightDirectPBR.glsl => LightDirectPBR.glsl} (98%) rename packages/shaderlab/src/shaders/shadingPBR/{lightIndirectPBR.glsl => LightIndirectPBR.glsl} (99%) rename packages/shaderlab/src/shaders/shadingPBR/{materialInputPBR.glsl => MaterialInputPBR.glsl} (99%) rename packages/shaderlab/src/shaders/shadingPBR/{shadingPBR.glsl => ShadingPBR.glsl} (86%) rename packages/shaderlab/src/shaders/shadingPBR/{varyingsPBR.glsl => VaryingsPBR.glsl} (100%) diff --git a/packages/shaderlab/src/GSLPBRMaterial.ts b/packages/shaderlab/src/GSLPBRMaterial.ts index 5f00767e..e4df9039 100644 --- a/packages/shaderlab/src/GSLPBRMaterial.ts +++ b/packages/shaderlab/src/GSLPBRMaterial.ts @@ -2,7 +2,7 @@ import { Engine, PBRMaterial, Shader } from "@galacean/engine"; export class GSLPBRMaterial extends PBRMaterial { constructor(engine: Engine) { - const shader = Shader.find("pbr.gs"); + const shader = Shader.find("PBR.gs"); super(engine); this.shader = shader; diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index fa3aea6d..e49dbd73 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1,6 +1,6 @@ export { GSLPBRMaterial } from "./GSLPBRMaterial"; import { Shader, ShaderFactory } from "@galacean/engine"; -import { fragmentList, pbrSource } from "./shaders"; +import { fragmentList, PBRSource } from "./shaders"; let registered = false; @@ -11,7 +11,9 @@ export function registerIncludes() { ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source); } - Shader.create(pbrSource); + Shader.create(PBRSource); registered = true; } + +export { PBRSource }; diff --git a/packages/shaderlab/src/shaders/blendShape.glsl b/packages/shaderlab/src/shaders/BlendShape.glsl similarity index 100% rename from packages/shaderlab/src/shaders/blendShape.glsl rename to packages/shaderlab/src/shaders/BlendShape.glsl diff --git a/packages/shaderlab/src/shaders/common.glsl b/packages/shaderlab/src/shaders/Common.glsl similarity index 100% rename from packages/shaderlab/src/shaders/common.glsl rename to packages/shaderlab/src/shaders/Common.glsl diff --git a/packages/shaderlab/src/shaders/fog.glsl b/packages/shaderlab/src/shaders/Fog.glsl similarity index 100% rename from packages/shaderlab/src/shaders/fog.glsl rename to packages/shaderlab/src/shaders/Fog.glsl diff --git a/packages/shaderlab/src/shaders/light.glsl b/packages/shaderlab/src/shaders/Light.glsl similarity index 100% rename from packages/shaderlab/src/shaders/light.glsl rename to packages/shaderlab/src/shaders/Light.glsl diff --git a/packages/shaderlab/src/shaders/normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl similarity index 100% rename from packages/shaderlab/src/shaders/normal.glsl rename to packages/shaderlab/src/shaders/Normal.glsl diff --git a/packages/shaderlab/src/shaders/pbr.gs b/packages/shaderlab/src/shaders/PBR.gs similarity index 95% rename from packages/shaderlab/src/shaders/pbr.gs rename to packages/shaderlab/src/shaders/PBR.gs index 4f513d64..ca5988f7 100644 --- a/packages/shaderlab/src/shaders/pbr.gs +++ b/packages/shaderlab/src/shaders/PBR.gs @@ -1,4 +1,4 @@ -Shader "pbr.gs" { +Shader "PBR.gs" { EditorProperties { Header("Base"){ material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; @@ -71,6 +71,7 @@ Shader "pbr.gs" { } SubShader "Default" { + UsePass "pbr/Default/ShadowCaster" Pass "Forward Pass" { @@ -78,10 +79,11 @@ Shader "pbr.gs" { #define IS_METALLIC_WORKFLOW - VertexShader = pbrVert; - FragmentShader = pbrFrag; + VertexShader = PBRVertex; + FragmentShader = PBRFragment; - #include "forwardPassPBR.glsl" + #include "ForwardPassPBR.glsl" + } } } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl similarity index 99% rename from packages/shaderlab/src/shaders/shadow.glsl rename to packages/shaderlab/src/shaders/Shadow.glsl index 2b4a76e6..c49d29d5 100644 --- a/packages/shaderlab/src/shaders/shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -123,7 +123,7 @@ #endif #if SCENE_SHADOW_TYPE == 3 - #include "shadowSampleTent.glsl" + #include "ShadowSampleTent.glsl" float sampleShadowMapFiltered9(TEXTURE2D_SHADOW_PARAM(shadowMap), vec3 shadowCoord, vec4 shadowmapSize) { float attenuation; diff --git a/packages/shaderlab/src/shaders/shadowSampleTent.glsl b/packages/shaderlab/src/shaders/ShadowSampleTent.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shadowSampleTent.glsl rename to packages/shaderlab/src/shaders/ShadowSampleTent.glsl diff --git a/packages/shaderlab/src/shaders/skin.glsl b/packages/shaderlab/src/shaders/Skin.glsl similarity index 100% rename from packages/shaderlab/src/shaders/skin.glsl rename to packages/shaderlab/src/shaders/Skin.glsl diff --git a/packages/shaderlab/src/shaders/transform.glsl b/packages/shaderlab/src/shaders/Transform.glsl similarity index 100% rename from packages/shaderlab/src/shaders/transform.glsl rename to packages/shaderlab/src/shaders/Transform.glsl diff --git a/packages/shaderlab/src/shaders/vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl similarity index 98% rename from packages/shaderlab/src/shaders/vertex.glsl rename to packages/shaderlab/src/shaders/Vertex.glsl index 9eee68e2..50c133e2 100644 --- a/packages/shaderlab/src/shaders/vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -1,10 +1,10 @@ // #ifndef VERTEX_INCLUDE // #define VERTEX_INCLUDE 1 -#include "skin.glsl" -#include "blendShape.glsl" -#include "shadow.glsl" -#include "transform.glsl" +#include "Skin.glsl" +#include "BlendShape.glsl" +#include "Shadow.glsl" +#include "Transform.glsl" vec4 material_TilingOffset; diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 7b223a2c..d506385b 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -1,17 +1,17 @@ -import blendShape from "./blendShape.glsl"; -import common from "./common.glsl"; -import fog from "./fog.glsl"; -import light from "./light.glsl"; -import normal from "./normal.glsl"; -import pbrSource from "./pbr.gs"; +import BlendShape from "./BlendShape.glsl"; +import Common from "./Common.glsl"; +import Fog from "./Fog.glsl"; +import Light from "./Light.glsl"; +import Normal from "./Normal.glsl"; +import PBRSource from "./PBR.gs"; import shadingPBR from "./shadingPBR"; -import shadow from "./shadow.glsl"; -import shadowSampleTent from "./shadowSampleTent.glsl"; -import skin from "./skin.glsl"; +import Shadow from "./Shadow.glsl"; +import ShadowSampleTent from "./ShadowSampleTent.glsl"; +import Skin from "./Skin.glsl"; import temp_transformAttributes from "./temp/transformAttributes.glsl"; import temp_transformVaryings from "./temp/transformVaryings.glsl"; -import transform from "./transform.glsl"; -import vertex from "./vertex.glsl"; +import Transform from "./Transform.glsl"; +import Vertex from "./Vertex.glsl"; interface IShaderFragment { includeKey: string; @@ -19,19 +19,19 @@ interface IShaderFragment { } const fragmentList: IShaderFragment[] = [ - { source: blendShape, includeKey: "blendShape.glsl" }, - { source: common, includeKey: "common.glsl" }, - { source: fog, includeKey: "fog.glsl" }, - { source: light, includeKey: "light.glsl" }, - { source: normal, includeKey: "normal.glsl" }, - { source: shadowSampleTent, includeKey: "shadowSampleTent.glsl" }, - { source: shadow, includeKey: "shadow.glsl" }, - { source: transform, includeKey: "transform.glsl" }, - { source: vertex, includeKey: "vertex.glsl" }, + { source: BlendShape, includeKey: "BlendShape.glsl" }, + { source: Common, includeKey: "Common.glsl" }, + { source: Fog, includeKey: "Fog.glsl" }, + { source: Light, includeKey: "Light.glsl" }, + { source: Normal, includeKey: "Normal.glsl" }, + { source: ShadowSampleTent, includeKey: "ShadowSampleTent.glsl" }, + { source: Shadow, includeKey: "Shadow.glsl" }, + { source: Transform, includeKey: "Transform.glsl" }, + { source: Vertex, includeKey: "Vertex.glsl" }, { source: temp_transformAttributes, includeKey: "temp_transformAttributes.glsl" }, { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, - { source: skin, includeKey: "skin.glsl" }, + { source: Skin, includeKey: "Skin.glsl" }, ...shadingPBR ]; -export { fragmentList, pbrSource }; +export { fragmentList, PBRSource }; diff --git a/packages/shaderlab/src/shaders/shadingPBR/attributesPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shadingPBR/attributesPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl diff --git a/packages/shaderlab/src/shaders/shadingPBR/brdf.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shadingPBR/brdf.glsl rename to packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl diff --git a/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl similarity index 73% rename from packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 1166a877..7ca21498 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/forwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -1,14 +1,14 @@ -#include "attributesPBR.glsl" -#include "varyingsPBR.glsl" -#include "common.glsl" -#include "vertex.glsl" -#include "fog.glsl" +#include "AttributesPBR.glsl" +#include "VaryingsPBR.glsl" +#include "Common.glsl" +#include "Vertex.glsl" +#include "Fog.glsl" -#include "materialInputPBR.glsl" -#include "shadingPBR.glsl" +#include "MaterialInputPBR.glsl" +#include "ShadingPBR.glsl" -Varyings pbrVert(Attributes attr) { +Varyings PBRVertex(Attributes attr) { Varyings v; // @todo: delete @@ -23,7 +23,7 @@ Varyings pbrVert(Attributes attr) { return v; } -void pbrFrag(Varyings v) { +void PBRFragment(Varyings v) { SurfaceData surfaceData; // @todo: delete diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl similarity index 98% rename from packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 6ebb414e..013fec8f 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -2,9 +2,9 @@ // #ifndef LIGHT_DIRECT_PBR_INCLUDED // #define LIGHT_DIRECT_PBR_INCLUDED 1 -#include "brdf.glsl" -#include "light.glsl" -#include "shadow.glsl" +#include "BRDF.glsl" +#include "Light.glsl" +#include "Shadow.glsl" void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ diff --git a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl similarity index 99% rename from packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index 08a959b8..fb0a305c 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/lightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -2,8 +2,8 @@ // #ifndef LIGHT_INDIRECT_PBR_INCLUDED // #define LIGHT_INDIRECT_PBR_INCLUDED 1 -#include "brdf.glsl" -#include "light.glsl" +#include "BRDF.glsl" +#include "Light.glsl" // ------------------------Diffuse------------------------ diff --git a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl similarity index 99% rename from packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index 88385980..18d26192 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/materialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -1,7 +1,7 @@ #ifndef MATERIAL_INPUT_PBR_INCLUDED #define MATERIAL_INPUT_PBR_INCLUDED 1 -#include "normal.glsl" +#include "Normal.glsl" struct SurfaceData{ vec3 position; diff --git a/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl similarity index 86% rename from packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl index a0eb6557..910f33df 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/shadingPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl @@ -1,8 +1,8 @@ // #ifndef SHADING_PBR_INCLUDED // #define SHADING_PBR_INCLUDED 1 -#include "lightDirectPBR.glsl" -#include "lightIndirectPBR.glsl" +#include "LightDirectPBR.glsl" +#include "LightIndirectPBR.glsl" vec4 evaluateSurface(Temp_Varyings v, SurfaceData surfaceData){ diff --git a/packages/shaderlab/src/shaders/shadingPBR/varyingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl similarity index 100% rename from packages/shaderlab/src/shaders/shadingPBR/varyingsPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index 85b7e2f3..b7676d7d 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -1,19 +1,19 @@ -import attributesPBR from "./attributesPBR.glsl"; -import brdf from "./brdf.glsl"; -import forwardPassPBR from "./forwardPassPBR.glsl"; -import lightDirectPBR from "./lightDirectPBR.glsl"; -import lightIndirectPBR from "./lightIndirectPBR.glsl"; -import materialInputPBR from "./materialInputPBR.glsl"; -import shadingPBR from "./shadingPBR.glsl"; -import varyingsPBR from "./varyingsPBR.glsl"; +import AttributesPBR from "./AttributesPBR.glsl"; +import BRDF from "./BRDF.glsl"; +import ForwardPassPBR from "./ForwardPassPBR.glsl"; +import LightDirectPBR from "./LightDirectPBR.glsl"; +import LightIndirectPBR from "./LightIndirectPBR.glsl"; +import MaterialInputPBR from "./MaterialInputPBR.glsl"; +import ShadingPBR from "./ShadingPBR.glsl"; +import VaryingsPBR from "./VaryingsPBR.glsl"; export default [ - { source: brdf, includeKey: "brdf.glsl" }, - { source: lightDirectPBR, includeKey: "lightDirectPBR.glsl" }, - { source: lightIndirectPBR, includeKey: "lightIndirectPBR.glsl" }, - { source: shadingPBR, includeKey: "shadingPBR.glsl" }, - { source: materialInputPBR, includeKey: "materialInputPBR.glsl" }, - { source: attributesPBR, includeKey: "attributesPBR.glsl" }, - { source: varyingsPBR, includeKey: "varyingsPBR.glsl" }, - { source: forwardPassPBR, includeKey: "forwardPassPBR.glsl" } + { source: BRDF, includeKey: "BRDF.glsl" }, + { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" }, + { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" }, + { source: ShadingPBR, includeKey: "ShadingPBR.glsl" }, + { source: MaterialInputPBR, includeKey: "MaterialInputPBR.glsl" }, + { source: AttributesPBR, includeKey: "AttributesPBR.glsl" }, + { source: VaryingsPBR, includeKey: "VaryingsPBR.glsl" }, + { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" } ]; From 022a9f2d87a4858600d0dc4d216096e0852a9b46 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 17 May 2024 14:53:56 +0800 Subject: [PATCH 37/89] chore: remove shadingPBR.glsl --- .../shaders/shadingPBR/ForwardPassPBR.glsl | 20 +++++++++++----- .../src/shaders/shadingPBR/ShadingPBR.glsl | 23 ------------------- .../shaderlab/src/shaders/shadingPBR/index.ts | 12 ++++------ 3 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 7ca21498..df8158f7 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -5,8 +5,8 @@ #include "Fog.glsl" #include "MaterialInputPBR.glsl" -#include "ShadingPBR.glsl" - +#include "LightDirectPBR.glsl" +#include "LightIndirectPBR.glsl" Varyings PBRVertex(Attributes attr) { Varyings v; @@ -32,14 +32,22 @@ void PBRFragment(Varyings v) { initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - vec4 color = evaluateSurface(temp_varyings, surfaceData); - gl_FragColor = color; + vec4 color = vec4(0, 0, 0, surfaceData.opacity); + + // Direct Light + evaluateDirectRadiance(temp_varyings, surfaceData, color.rgb); + // IBL + evaluateIBL(temp_varyings, surfaceData, color.rgb); + // Emissive + color.rgb += surfaceData.emissive; #if SCENE_FOG_MODE != 0 - gl_FragColor = fog(gl_FragColor, v.v_positionVS); + color = fog(color, v.v_positionVS); #endif #ifndef ENGINE_IS_COLORSPACE_GAMMA - gl_FragColor = linearToGamma(gl_FragColor); + color = linearToGamma(color); #endif + + gl_FragColor = color; } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl deleted file mode 100644 index 910f33df..00000000 --- a/packages/shaderlab/src/shaders/shadingPBR/ShadingPBR.glsl +++ /dev/null @@ -1,23 +0,0 @@ -// #ifndef SHADING_PBR_INCLUDED -// #define SHADING_PBR_INCLUDED 1 - -#include "LightDirectPBR.glsl" -#include "LightIndirectPBR.glsl" - - -vec4 evaluateSurface(Temp_Varyings v, SurfaceData surfaceData){ - vec3 color = vec3(0); - - // Direct Light - evaluateDirectRadiance(v, surfaceData, color); - // IBL - evaluateIBL(v, surfaceData, color); - // Emissive - color += surfaceData.emissive; - - return vec4(color, surfaceData.opacity); -} - - -// #endif - diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index b7676d7d..3abeef71 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -4,16 +4,14 @@ import ForwardPassPBR from "./ForwardPassPBR.glsl"; import LightDirectPBR from "./LightDirectPBR.glsl"; import LightIndirectPBR from "./LightIndirectPBR.glsl"; import MaterialInputPBR from "./MaterialInputPBR.glsl"; -import ShadingPBR from "./ShadingPBR.glsl"; import VaryingsPBR from "./VaryingsPBR.glsl"; export default [ - { source: BRDF, includeKey: "BRDF.glsl" }, - { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" }, - { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" }, - { source: ShadingPBR, includeKey: "ShadingPBR.glsl" }, - { source: MaterialInputPBR, includeKey: "MaterialInputPBR.glsl" }, + { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" }, { source: AttributesPBR, includeKey: "AttributesPBR.glsl" }, { source: VaryingsPBR, includeKey: "VaryingsPBR.glsl" }, - { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" } + { source: MaterialInputPBR, includeKey: "MaterialInputPBR.glsl" }, + { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" }, + { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" }, + { source: BRDF, includeKey: "BRDF.glsl" } ]; From 8c8bdfbd590a2d4d861dad4fde606e9a3af849a8 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 17 May 2024 15:17:11 +0800 Subject: [PATCH 38/89] "v0.0.0-experimental-shaderlab-thin.2" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 5d3dbc5a..2b9fb0c2 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index a8bf903f..65eff065 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 015e1518..a096ebd6 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 541606ba..7b535998 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 2a82942d..1f91aef6 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 00dfdb01..84aa3343 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index b0210131..46f5905d 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 49e3788d..6b130f5e 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 51ca6cdb..6b8b7f59 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 4a230611..8790d3cd 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 6384591a..4fda64e2 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 6f2080a4..e05768c6 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 9a1e0a30..e144b84a 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index f3cc3e82..bb4813e9 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index f05cf936..fbae0347 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index afaaa457..e1abcb9f 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 0192736c..c47a8dee 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index b814ede5..b57c2283 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 95eba2a5..01dad110 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.1", + "version": "0.0.0-experimental-shaderlab-thin.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 24b96a6dd413461db54e4e8fb9d675314cc024c1 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 12 Jun 2024 22:34:29 +0800 Subject: [PATCH 39/89] refactor: add brdfData to enable modify surfaceData --- .../src/shaders/shadingPBR/BRDF.glsl | 20 +- .../shaders/shadingPBR/ForwardPassPBR.glsl | 12 +- .../shaders/shadingPBR/LightDirectPBR.glsl | 50 ++--- .../shaders/shadingPBR/LightIndirectPBR.glsl | 40 ++-- .../shaders/shadingPBR/MaterialInputPBR.glsl | 186 ++++++++++-------- .../shaders/shadingThin/ForwardPassThin.glsl | 11 +- .../shaders/shadingThin/LightDirectThin.glsl | 56 +++--- .../shadingThin/LightIndirectThin.glsl | 46 ++--- 8 files changed, 224 insertions(+), 197 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index 5a467e55..d5131a7e 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -77,10 +77,10 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl } #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 anisotropicLobe(vec3 h, vec3 l, SurfaceData surfaceData, vec3 specularColor, float alpha, float dotNV, float dotNL, float dotNH, float dotLH) { - vec3 t = surfaceData.anisotropicT; - vec3 b = surfaceData.anisotropicB; - vec3 v = surfaceData.viewDir; + vec3 anisotropicLobe(vec3 h, vec3 l, BRDFData brdfData, vec3 specularColor, float alpha, float dotNV, float dotNL, float dotNH, float dotLH) { + vec3 t = brdfData.anisotropicT; + vec3 b = brdfData.anisotropicB; + vec3 v = brdfData.viewDir; float dotTV = dot(t, v); float dotBV = dot(b, v); @@ -91,8 +91,8 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl // Aniso parameter remapping // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24 - float at = max(alpha * (1.0 + surfaceData.anisotropy), MIN_ROUGHNESS); - float ab = max(alpha * (1.0 - surfaceData.anisotropy), MIN_ROUGHNESS); + float at = max(alpha * (1.0 + brdfData.anisotropy), MIN_ROUGHNESS); + float ab = max(alpha * (1.0 - brdfData.anisotropy), MIN_ROUGHNESS); // specular anisotropic BRDF vec3 F = F_Schlick( specularColor, dotLH ); @@ -104,19 +104,19 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl #endif // GGX Distribution, Schlick Fresnel, GGX-Smith Visibility -vec3 BRDF_Specular_GGX(vec3 incidentDirection, SurfaceData surfaceData, vec3 normal, vec3 specularColor, float roughness ) { +vec3 BRDF_Specular_GGX(vec3 incidentDirection, BRDFData brdfData, vec3 normal, vec3 specularColor, float roughness ) { float alpha = pow2( roughness ); // UE4's roughness - vec3 halfDir = normalize( incidentDirection + surfaceData.viewDir ); + vec3 halfDir = normalize( incidentDirection + brdfData.viewDir ); float dotNL = saturate( dot( normal, incidentDirection ) ); - float dotNV = saturate( dot( normal, surfaceData.viewDir ) ); + float dotNV = saturate( dot( normal, brdfData.viewDir ) ); float dotNH = saturate( dot( normal, halfDir ) ); float dotLH = saturate( dot( incidentDirection, halfDir ) ); #ifdef MATERIAL_ENABLE_ANISOTROPY - return anisotropicLobe(halfDir, incidentDirection, surfaceData, specularColor, alpha, dotNV, dotNL, dotNH, dotLH); + return anisotropicLobe(halfDir, incidentDirection, brdfData, specularColor, alpha, dotNV, dotNL, dotNH, dotLH); #else return isotropicLobe(specularColor, alpha, dotNV, dotNL, dotNH, dotLH); #endif diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index df8158f7..d2f017ae 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -25,21 +25,25 @@ Varyings PBRVertex(Attributes attr) { void PBRFragment(Varyings v) { SurfaceData surfaceData; + BRDFData brdfData; // @todo: delete Temp_Varyings temp_varyings; #include "temp_transformVaryings.glsl" - initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); + initSurfaceData(temp_varyings, surfaceData); + // Can modify surfaceData here. + initBRDFData(temp_varyings, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); // Direct Light - evaluateDirectRadiance(temp_varyings, surfaceData, color.rgb); + evaluateDirectRadiance(temp_varyings, brdfData, color.rgb); // IBL - evaluateIBL(temp_varyings, surfaceData, color.rgb); + evaluateIBL(temp_varyings, brdfData, color.rgb); // Emissive - color.rgb += surfaceData.emissive; + color.rgb += surfaceData.emissiveColor; + #if SCENE_FOG_MODE != 0 color = fog(color, v.v_positionVS); diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 013fec8f..37782c5c 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -7,42 +7,42 @@ #include "Shadow.glsl" -void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ - Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); +void diffuseLobe(BRDFData brdfData, vec3 irradiance, float attenuation, inout vec3 Fd){ + Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -void specularLobe(SurfaceData surfaceData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ - Fs += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); +void specularLobe(BRDFData brdfData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ + Fs += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.normal, brdfData.specularColor, brdfData.roughness); } -float clearCoatLobe(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, inout vec3 Fs){ +float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout vec3 Fs){ float attenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); + float clearCoatDotNL = saturate( dot( brdfData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; - Fs += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); - attenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); + Fs += brdfData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.clearCoatNormal, vec3( 0.04 ), brdfData.clearCoatRoughness ); + attenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif return attenuation; } -void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceData, inout vec3 color) { +void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { vec3 Fd = vec3(0); vec3 Fs = vec3(0); - float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); + float dotNL = saturate( dot( brdfData.normal, incidentDirection ) ); vec3 irradiance = dotNL * lightColor * PI; // ClearCoat Lobe - float attenuation = clearCoatLobe(incidentDirection, lightColor, surfaceData, Fs); + float attenuation = clearCoatLobe(incidentDirection, lightColor, brdfData, Fs); // Diffuse Lobe - diffuseLobe(surfaceData, irradiance, attenuation, Fd); + diffuseLobe(brdfData, irradiance, attenuation, Fd); // Specular Lobe - specularLobe(surfaceData, incidentDirection, irradiance, attenuation, Fs); + specularLobe(brdfData, incidentDirection, irradiance, attenuation, Fs); color += Fd + Fs; @@ -50,11 +50,11 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat #ifdef SCENE_DIRECT_LIGHT_COUNT - void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout vec3 color) { + void addDirectionalDirectLightRadiance(DirectLight directionalLight, BRDFData brdfData, inout vec3 color) { vec3 lightColor = directionalLight.color; vec3 direction = -directionalLight.direction; - addRadiance( direction, lightColor, surfaceData, color ); + addRadiance( direction, lightColor, brdfData, color ); } @@ -62,16 +62,16 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat #ifdef SCENE_POINT_LIGHT_COUNT - void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, inout vec3 color) { + void addPointDirectLightRadiance(PointLight pointLight, BRDFData brdfData, inout vec3 color) { - vec3 lVector = pointLight.position - surfaceData.position; + vec3 lVector = pointLight.position - brdfData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); vec3 lightColor = pointLight.color; lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - addRadiance( direction, lightColor, surfaceData, color ); + addRadiance( direction, lightColor, brdfData, color ); } @@ -79,9 +79,9 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat #ifdef SCENE_SPOT_LIGHT_COUNT - void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, inout vec3 color) { + void addSpotDirectLightRadiance(SpotLight spotLight, BRDFData brdfData, inout vec3 color) { - vec3 lVector = spotLight.position - surfaceData.position; + vec3 lVector = spotLight.position - brdfData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); float angleCos = dot( direction, -spotLight.direction ); @@ -92,14 +92,14 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat vec3 lightColor = spotLight.color; lightColor *= spotEffect * decayEffect; - addRadiance( direction, lightColor, surfaceData, color ); + addRadiance( direction, lightColor, brdfData, color ); } #endif -void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ +void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT @@ -120,7 +120,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 } #endif directionalLight.direction = scene_DirectLightDirection[i]; - addDirectionalDirectLightRadiance( directionalLight, surfaceData, color ); + addDirectionalDirectLightRadiance( directionalLight, brdfData, color ); } } @@ -136,7 +136,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 pointLight.position = scene_PointLightPosition[i]; pointLight.distance = scene_PointLightDistance[i]; - addPointDirectLightRadiance( pointLight, surfaceData, color ); + addPointDirectLightRadiance( pointLight, brdfData, color ); } } @@ -155,7 +155,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 spotLight.angleCos = scene_SpotLightAngleCos[i]; spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; - addSpotDirectLightRadiance( spotLight, surfaceData, color ); + addSpotDirectLightRadiance( spotLight, brdfData, color ); } } diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index fb0a305c..11b3de6d 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -50,22 +50,22 @@ float getSpecularMIPLevel(float roughness, int maxMIPLevel ) { return roughness * float(maxMIPLevel); } -vec3 getReflectedVector(SurfaceData surfaceData, vec3 n) { +vec3 getReflectedVector(BRDFData brdfData, vec3 n) { #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 r = reflect(-surfaceData.viewDir, surfaceData.anisotropicN); + vec3 r = reflect(-brdfData.viewDir, brdfData.anisotropicN); #else - vec3 r = reflect(-surfaceData.viewDir, n); + vec3 r = reflect(-brdfData.viewDir, n); #endif return r; } -vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness) { +vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { #ifndef SCENE_USE_SPECULAR_ENV return vec3(0); #else - vec3 reflectVec = getReflectedVector(surfaceData, normal); + vec3 reflectVec = getReflectedVector(brdfData, normal); reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse float specularMIPLevel = getSpecularMIPLevel(roughness, int(scene_EnvMapLight.mipMapLevel) ); @@ -94,9 +94,9 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness } -void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd){ +void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 Fd){ #ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, brdfData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; #endif @@ -106,24 +106,24 @@ void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd) irradiance *= PI; #endif - Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -float evaluateClearCoatIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ +float evaluateClearCoatIBL(BRDFData brdfData, float specularAO, inout vec3 Fs){ float radianceAttenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness); - Fs += specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); - radianceAttenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); + vec3 clearCoatRadiance = getLightProbeRadiance( brdfData, brdfData.clearCoatNormal, brdfData.clearCoatRoughness); + Fs += specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); + radianceAttenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif return radianceAttenuation; } -void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, float radianceAttenuation, inout vec3 Fs){ - vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness); - Fs += specularAO * radianceAttenuation * radiance * envBRDFApprox(surfaceData.specularColor, surfaceData.roughness, surfaceData.dotNV ); +void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAttenuation, inout vec3 Fs){ + vec3 radiance = getLightProbeRadiance(brdfData, brdfData.normal, brdfData.roughness); + Fs += specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, brdfData.dotNV ); } @@ -153,20 +153,20 @@ float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ return specularAO; } -void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ +void evaluateIBL(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ vec3 Fd = vec3(0); vec3 Fs = vec3(0); float diffuseAO = evaluateDiffuseAO(v); - float specularAO = evaluateSpecularAO(diffuseAO, surfaceData.roughness, surfaceData.dotNV); + float specularAO = evaluateSpecularAO(diffuseAO, brdfData.roughness, brdfData.dotNV); // IBL diffuse - evaluateDiffuseIBL(surfaceData, diffuseAO, Fd); + evaluateDiffuseIBL(brdfData, diffuseAO, Fd); // IBL ClearCoat - float radianceAttenuation = evaluateClearCoatIBL(surfaceData, specularAO, Fs); + float radianceAttenuation = evaluateClearCoatIBL(brdfData, specularAO, Fs); // IBL specular - evaluateSpecularIBL(surfaceData, specularAO, radianceAttenuation, Fs); + evaluateSpecularIBL(brdfData, specularAO, radianceAttenuation, Fs); color += Fd + Fs; } diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index 18d26192..f6e33871 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -4,34 +4,43 @@ #include "Normal.glsl" struct SurfaceData{ - vec3 position; + vec3 albedoColor; + vec3 specularColor; + vec3 emissiveColor; + float metallic; + float roughness; + float f0; + float opacity; +} + +struct BRDFData{ + // common + vec3 diffuseColor; + vec3 specularColor; + float roughness; + + // geometry + vec3 position; vec3 normal; mat3 tbn; vec3 viewDir; float dotNV; - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatNormal; - float clearCoatDotNV; - #endif + // Anisotropy #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy; vec3 anisotropicT; vec3 anisotropicB; vec3 anisotropicN; - float anisotropy; #endif - - vec3 diffuseColor; - float roughness; - vec3 specularColor; - float opacity; - float f0; + + // Clear coat #ifdef MATERIAL_ENABLE_CLEAR_COAT float clearCoat; float clearCoatRoughness; + vec3 clearCoatNormal; + float clearCoatDotNV; #endif - vec3 emissive; } #define MIN_PERCEPTUAL_ROUGHNESS 0.045 @@ -116,49 +125,26 @@ float getAARoughnessFactor(vec3 normal) { #ifdef MATERIAL_ENABLE_ANISOTROPY // Aniso Bent Normals // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { - vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); + vec3 getAnisotropicBentNormal(BRDFData brdfData) { + vec3 anisotropyDirection = (brdfData.anisotropy >= 0.0) ? brdfData.anisotropicB : brdfData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, brdfData.viewDir); vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); + vec3 bentNormal = normalize( mix(brdfData.normal, anisotropicNormal, abs(brdfData.anisotropy) * saturate( 5.0 * brdfData.roughness)) ); return bentNormal; } #endif - -void initCommonSurfaceData(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ - surfaceData.position = v.v_pos; - - #ifdef CAMERA_ORTHOGRAPHIC - surfaceData.viewDir = -camera_Forward; - #else - surfaceData.viewDir = normalize(camera_Position - v.v_pos); - #endif - - #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) - mat3 tbn = getTBN(v, isFrontFacing); - surfaceData.tbn = tbn; - #endif - - #ifdef MATERIAL_HAS_NORMALTEXTURE - surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - surfaceData.normal = getNormal(v, isFrontFacing); - #endif - - surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); - +void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData){ vec4 baseColor = material_BaseColor; - float metal = material_Metal; + float metallic = material_Metal; float roughness = material_Roughness; vec3 specularColor = material_PBRSpecularColor; float glossiness = material_Glossiness; float f0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) ); - - surfaceData.f0 = f0; + vec3 emissiveRadiance = material_EmissiveColor; #ifdef MATERIAL_HAS_BASETEXTURE vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); @@ -182,7 +168,7 @@ void initCommonSurfaceData(Temp_Varyings v, inout SurfaceData surfaceData, bool #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); roughness *= metalRoughMapColor.g; - metal *= metalRoughMapColor.b; + metallic *= metalRoughMapColor.b; #endif #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE @@ -192,68 +178,101 @@ void initCommonSurfaceData(Temp_Varyings v, inout SurfaceData surfaceData, bool #endif specularColor *= specularGlossinessColor.rgb; glossiness *= specularGlossinessColor.a; + roughness = 1.0 - glossiness; + #endif + + #ifdef MATERIAL_HAS_EMISSIVETEXTURE + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + #ifndef ENGINE_IS_COLORSPACE_GAMMA + emissiveColor = gammaToLinear(emissiveColor); + #endif + emissiveRadiance *= emissiveColor.rgb; #endif + surfaceData.albedoColor = baseColor.rgb; + surfaceData.specularColor = specularColor; + surfaceData.emissiveColor = emissiveRadiance; + surfaceData.metallic = metallic; + surfaceData.roughness = roughness; + surfaceData.f0 = f0; + + #ifdef MATERIAL_IS_TRANSPARENT + surfaceData.opacity = baseColor.a; + #else + surfaceData.opacity = 1.0; + #endif +} + +void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ + vec3 albedoColor = surfaceData.albedoColor; + vec3 specularColor = surfaceData.specularColor; + float metallic = surfaceData.metallic; + float roughness = surfaceData.roughness; + float f0 = surfaceData.f0; #ifdef IS_METALLIC_WORKFLOW - surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - metal ); - surfaceData.specularColor = mix( vec3(f0), baseColor.rgb, metal ); - surfaceData.roughness = roughness; + brdfData.diffuseColor = albedoColor * ( 1.0 - metallic ); + brdfData.specularColor = mix( vec3(f0), albedoColor, metallic ); #else float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); - surfaceData.diffuseColor = baseColor.rgb * ( 1.0 - specularStrength ); - surfaceData.specularColor = specularColor; - surfaceData.roughness = 1.0 - glossiness; + brdfData.diffuseColor = albedoColor * ( 1.0 - specularStrength ); + brdfData.specularColor = specularColor; #endif - surfaceData.roughness = max(surfaceData.roughness, getAARoughnessFactor(surfaceData.normal)); + brdfData.roughness = max(roughness, getAARoughnessFactor(brdfData.normal)); +} + +void initGeometryData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFacing){ + brdfData.position = v.v_pos; - #ifdef MATERIAL_IS_TRANSPARENT - surfaceData.opacity = baseColor.a; + #ifdef CAMERA_ORTHOGRAPHIC + brdfData.viewDir = -camera_Forward; #else - surfaceData.opacity = 1.0; + brdfData.viewDir = normalize(camera_Position - v.v_pos); #endif - vec3 emissiveRadiance = material_EmissiveColor; - #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); - #ifndef ENGINE_IS_COLORSPACE_GAMMA - emissiveColor = gammaToLinear(emissiveColor); - #endif - emissiveRadiance *= emissiveColor.rgb; + #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) + mat3 tbn = getTBN(v, isFrontFacing); + brdfData.tbn = tbn; #endif - surfaceData.emissive = emissiveRadiance; + #ifdef MATERIAL_HAS_NORMALTEXTURE + brdfData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + brdfData.normal = getNormal(v, isFrontFacing); + #endif + + brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); } -void initClearCoatSurfaceData(Temp_Varyings v, inout SurfaceData surfaceData, bool isFrontFacing){ +void initClearCoatBRDFData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFacing){ #ifdef MATERIAL_ENABLE_CLEAR_COAT #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - surfaceData.clearCoatNormal = getNormalByNormalTexture(surfaceData.tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + brdfData.clearCoatNormal = getNormalByNormalTexture(brdfData.tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else - surfaceData.clearCoatNormal = getNormal(v, isFrontFacing); + brdfData.clearCoatNormal = getNormal(v, isFrontFacing); #endif - surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); + brdfData.clearCoatDotNV = saturate( dot(brdfData.clearCoatNormal, brdfData.viewDir) ); - surfaceData.clearCoat = material_ClearCoat; - surfaceData.clearCoatRoughness = material_ClearCoatRoughness; + brdfData.clearCoat = material_ClearCoat; + brdfData.clearCoatRoughness = material_ClearCoatRoughness; #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; + brdfData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; #endif #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; + brdfData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; #endif - surfaceData.clearCoat = saturate( surfaceData.clearCoat ); - surfaceData.clearCoatRoughness = max(surfaceData.clearCoatRoughness, getAARoughnessFactor(surfaceData.clearCoatNormal)); + brdfData.clearCoat = saturate( brdfData.clearCoat ); + brdfData.clearCoatRoughness = max(brdfData.clearCoatRoughness, getAARoughnessFactor(brdfData.clearCoatNormal)); #endif } -void initAnisotropySurfaceData(Temp_Varyings v, inout SurfaceData surfaceData){ +void initAnisotropyBRDFData(Temp_Varyings v, inout BRDFData brdfData){ #ifdef MATERIAL_ENABLE_ANISOTROPY float anisotropy = material_AnisotropyInfo.z; vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); @@ -263,21 +282,22 @@ void initAnisotropySurfaceData(Temp_Varyings v, inout SurfaceData surfaceData){ anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; #endif - surfaceData.anisotropy = anisotropy; - surfaceData.anisotropicT = normalize(surfaceData.tbn * anisotropicDirection); - surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); - surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); + brdfData.anisotropy = anisotropy; + brdfData.anisotropicT = normalize(brdfData.tbn * anisotropicDirection); + brdfData.anisotropicB = normalize(cross(brdfData.normal, brdfData.anisotropicT)); + brdfData.anisotropicN = getAnisotropicBentNormal(brdfData); #endif } - -void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ - initCommonSurfaceData(v, surfaceData, isFrontFacing); - initClearCoatSurfaceData(v, surfaceData, isFrontFacing); - initAnisotropySurfaceData(v, surfaceData); +void initBRDFData(Temp_Varyings v, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ + initGeometryData(v, brdfData, isFrontFacing); + initCommonBRDFData(surfaceData, brdfData); + initClearCoatBRDFData(v, brdfData, isFrontFacing); + initAnisotropyBRDFData(v, brdfData); } + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl b/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl index 44e05a10..ef50185d 100644 --- a/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl +++ b/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl @@ -32,21 +32,24 @@ Varyings PBRVertex(Attributes attr) { void PBRFragment(Varyings v) { SurfaceData surfaceData; + BRDFData brdfData; // @todo: delete Temp_Varyings temp_varyings; #include "temp_transformVaryings.glsl" - initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); + initSurfaceData(temp_varyings, surfaceData); + // Can modify surfaceData here. + initBRDFData(temp_varyings, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); // Direct Light - evaluateDirectRadiance(temp_varyings, surfaceData, color.rgb); + evaluateDirectRadiance(temp_varyings, brdfData, color.rgb); // IBL - evaluateIBL(temp_varyings, surfaceData, color.rgb); + evaluateIBL(temp_varyings, brdfData, color.rgb); // Emissive - color.rgb += surfaceData.emissive; + color.rgb += surfaceData.emissiveColor; #if SCENE_FOG_MODE != 0 color = fog(color, v.v_positionVS); diff --git a/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl index 327b5529..38c027a2 100644 --- a/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl +++ b/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl @@ -8,53 +8,53 @@ #include "Shadow.glsl" -void diffuseLobe(SurfaceData surfaceData, vec3 irradiance, float attenuation, inout vec3 Fd){ - Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); +void diffuseLobe(BRDFData brdfData, vec3 irradiance, float attenuation, inout vec3 Fd){ + Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -void specularLobe(SurfaceData surfaceData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ - vec3 V = surfaceData.viewDir; +void specularLobe(BRDFData brdfData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ + vec3 V = brdfData.viewDir; vec3 H = normalize(incidentDirection+V); float VoH = dot(H, V); - vec3 F = F_Schlick( surfaceData.specularColor, VoH ); + vec3 F = F_Schlick( brdfData.specularColor, VoH ); - vec3 thin = DirectBDRFIridescence(incidentDirection , surfaceData.viewDir , surfaceData.normal ,surfaceData.specularColor , surfaceData.roughness); + vec3 thin = DirectBDRFIridescence(incidentDirection , brdfData.viewDir , brdfData.normal ,brdfData.specularColor , brdfData.roughness); vec3 F_iridescence = thin * material_Iridescence;//mix(vec3(0.0,0.0,0.0),thin,material_Iridescence); - vec3 BRDF_Specular = BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, surfaceData.specularColor, surfaceData.roughness); + vec3 BRDF_Specular = BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.normal, brdfData.specularColor, brdfData.roughness); vec3 factor = mix(BRDF_Specular, thin, material_Iridescence); Fs += attenuation * irradiance * factor; } -float clearCoatLobe(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, inout vec3 Fs){ +float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout vec3 Fs){ float attenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); + float clearCoatDotNL = saturate( dot( brdfData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; - Fs += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, vec3( 0.04 ), surfaceData.clearCoatRoughness ); - attenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); + Fs += brdfData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.clearCoatNormal, vec3( 0.04 ), brdfData.clearCoatRoughness ); + attenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif return attenuation; } -void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceData, inout vec3 color) { +void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { vec3 Fd = vec3(0); vec3 Fs = vec3(0); - float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); + float dotNL = saturate( dot( brdfData.normal, incidentDirection ) ); vec3 irradiance = dotNL * lightColor * PI; // ClearCoat Lobe - float attenuation = clearCoatLobe(incidentDirection, lightColor, surfaceData, Fs); + float attenuation = clearCoatLobe(incidentDirection, lightColor, brdfData, Fs); // Diffuse Lobe - diffuseLobe(surfaceData, irradiance, attenuation, Fd); + diffuseLobe(brdfData, irradiance, attenuation, Fd); // //Iridescence Specular Lobe - specularLobe(surfaceData, incidentDirection, irradiance, attenuation, Fs); + specularLobe(brdfData, incidentDirection, irradiance, attenuation, Fs); color += Fd + Fs; @@ -62,11 +62,11 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat #ifdef SCENE_DIRECT_LIGHT_COUNT - void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, inout vec3 color) { + void addDirectionalDirectLightRadiance(DirectLight directionalLight, BRDFData brdfData, inout vec3 color) { vec3 lightColor = directionalLight.color; vec3 direction = -directionalLight.direction; - addRadiance( direction, lightColor, surfaceData, color ); + addRadiance( direction, lightColor, brdfData, color ); } @@ -74,16 +74,16 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat #ifdef SCENE_POINT_LIGHT_COUNT - void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, inout vec3 color) { + void addPointDirectLightRadiance(PointLight pointLight, BRDFData brdfData, inout vec3 color) { - vec3 lVector = pointLight.position - surfaceData.position; + vec3 lVector = pointLight.position - brdfData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); vec3 lightColor = pointLight.color; lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - addRadiance( direction, lightColor, surfaceData, color ); + addRadiance( direction, lightColor, brdfData, color ); } @@ -91,9 +91,9 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat #ifdef SCENE_SPOT_LIGHT_COUNT - void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, inout vec3 color) { + void addSpotDirectLightRadiance(SpotLight spotLight, BRDFData brdfData, inout vec3 color) { - vec3 lVector = spotLight.position - surfaceData.position; + vec3 lVector = spotLight.position - brdfData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); float angleCos = dot( direction, -spotLight.direction ); @@ -104,14 +104,14 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceDat vec3 lightColor = spotLight.color; lightColor *= spotEffect * decayEffect; - addRadiance( direction, lightColor, surfaceData, color ); + addRadiance( direction, lightColor, brdfData, color ); } #endif -void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ +void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT @@ -132,7 +132,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 } #endif directionalLight.direction = scene_DirectLightDirection[i]; - addDirectionalDirectLightRadiance( directionalLight, surfaceData, color ); + addDirectionalDirectLightRadiance( directionalLight, brdfData, color ); } } @@ -148,7 +148,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 pointLight.position = scene_PointLightPosition[i]; pointLight.distance = scene_PointLightDistance[i]; - addPointDirectLightRadiance( pointLight, surfaceData, color ); + addPointDirectLightRadiance( pointLight, brdfData, color ); } } @@ -167,7 +167,7 @@ void evaluateDirectRadiance(Temp_Varyings v, SurfaceData surfaceData, inout vec3 spotLight.angleCos = scene_SpotLightAngleCos[i]; spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; - addSpotDirectLightRadiance( spotLight, surfaceData, color ); + addSpotDirectLightRadiance( spotLight, brdfData, color ); } } diff --git a/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl index 7754d9be..c0530efa 100644 --- a/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl +++ b/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl @@ -51,22 +51,22 @@ float getSpecularMIPLevel(float roughness, int maxMIPLevel ) { return roughness * float(maxMIPLevel); } -vec3 getReflectedVector(SurfaceData surfaceData, vec3 n) { +vec3 getReflectedVector(BRDFData brdfData, vec3 n) { #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 r = reflect(-surfaceData.viewDir, surfaceData.anisotropicN); + vec3 r = reflect(-brdfData.viewDir, brdfData.anisotropicN); #else - vec3 r = reflect(-surfaceData.viewDir, n); + vec3 r = reflect(-brdfData.viewDir, n); #endif return r; } -vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness) { +vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { #ifndef SCENE_USE_SPECULAR_ENV return vec3(0); #else - vec3 reflectVec = getReflectedVector(surfaceData, normal); + vec3 reflectVec = getReflectedVector(brdfData, normal); reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse float specularMIPLevel = getSpecularMIPLevel(roughness, int(scene_EnvMapLight.mipMapLevel) ); @@ -95,9 +95,9 @@ vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness } -void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd){ +void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 Fd){ #ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, brdfData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; #endif @@ -107,31 +107,31 @@ void evaluateDiffuseIBL(SurfaceData surfaceData, float diffuseAO, inout vec3 Fd) irradiance *= PI; #endif - Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( surfaceData.diffuseColor ); + Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -float evaluateClearCoatIBL(SurfaceData surfaceData, float specularAO, inout vec3 Fs){ +float evaluateClearCoatIBL(BRDFData brdfData, float specularAO, inout vec3 Fs){ float radianceAttenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( surfaceData, surfaceData.clearCoatNormal, surfaceData.clearCoatRoughness); - Fs += specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(vec3( 0.04 ), surfaceData.clearCoatRoughness, surfaceData.clearCoatDotNV); - radianceAttenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); + vec3 clearCoatRadiance = getLightProbeRadiance( brdfData, brdfData.clearCoatNormal, brdfData.clearCoatRoughness); + Fs += specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); + radianceAttenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif return radianceAttenuation; } -void evaluateSpecularIBL(SurfaceData surfaceData, float specularAO, float radianceAttenuation, inout vec3 Fs){ - vec3 reflectdir = reflect(surfaceData.normal, -surfaceData.viewDir); - vec3 halfdir = reflectdir + surfaceData.viewDir; +void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAttenuation, inout vec3 Fs){ + vec3 reflectdir = reflect(brdfData.normal, -brdfData.viewDir); + vec3 halfdir = reflectdir + brdfData.viewDir; float cosTheta1 = dot(halfdir, reflectdir); - vec3 fresnelIridescent = ThinFilmIridescence(cosTheta1, material_Eta2 , surfaceData.specularColor ,material_IridescenceThickness); + vec3 fresnelIridescent = ThinFilmIridescence(cosTheta1, material_Eta2 , brdfData.specularColor ,material_IridescenceThickness); //vec3 F_iridescence = fresnelIridescent * material_Iridescence;//mix(vec3(0.0,0.0,0.0),fresnelIridescent,material_Iridescence); - vec3 envBRDF = envBRDFApprox( surfaceData.specularColor , surfaceData.roughness, surfaceData.dotNV ); + vec3 envBRDF = envBRDFApprox( brdfData.specularColor , brdfData.roughness, brdfData.dotNV ); vec3 fator = mix(envBRDF, fresnelIridescent, material_Iridescence); - vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, surfaceData.roughness); + vec3 radiance = getLightProbeRadiance(brdfData, brdfData.normal, brdfData.roughness); Fs += specularAO * radianceAttenuation * radiance * fator; } @@ -162,20 +162,20 @@ float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ } -void evaluateIBL(Temp_Varyings v, SurfaceData surfaceData, inout vec3 color){ +void evaluateIBL(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ vec3 Fd = vec3(0); vec3 Fs = vec3(0); float diffuseAO = evaluateDiffuseAO(v); - float specularAO = evaluateSpecularAO(diffuseAO, surfaceData.roughness, surfaceData.dotNV); + float specularAO = evaluateSpecularAO(diffuseAO, brdfData.roughness, brdfData.dotNV); // IBL diffuse - evaluateDiffuseIBL(surfaceData, diffuseAO, Fd); + evaluateDiffuseIBL(brdfData, diffuseAO, Fd); // IBL ClearCoat - float radianceAttenuation = evaluateClearCoatIBL(surfaceData, specularAO, Fs); + float radianceAttenuation = evaluateClearCoatIBL(brdfData, specularAO, Fs); // IBL Iridescence Specular - evaluateSpecularIBL(surfaceData, specularAO, radianceAttenuation, Fs); + evaluateSpecularIBL(brdfData, specularAO, radianceAttenuation, Fs); color += Fd + Fs; } From c3bd539173253ce6064de951d917142e125e0c03 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 12 Jun 2024 22:39:50 +0800 Subject: [PATCH 40/89] "v0.0.0-experimental-shaderlab-thin.3" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 2b9fb0c2..c0ffb5d1 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 65eff065..30d4b335 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index a096ebd6..a7fce230 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 7b535998..84f5fdad 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 1f91aef6..a2754f12 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 84aa3343..33ecb25e 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 46f5905d..9940f151 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 6b130f5e..b63d043e 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 6b8b7f59..74977d01 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 8790d3cd..9574677a 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 4fda64e2..7d8b8f66 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index e05768c6..99a81c0f 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index e144b84a..f91f9997 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index bb4813e9..e8e515d1 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index fbae0347..93868e86 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index e1abcb9f..cbe5a8c7 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index c47a8dee..a215f703 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index b57c2283..6e86187b 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 01dad110..17701a23 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.2", + "version": "0.0.0-experimental-shaderlab-thin.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 93161ae252aa1b06077731a84c1984192b1a2faa Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 13 Jun 2024 11:23:27 +0800 Subject: [PATCH 41/89] refactor: delete brdfData.tbn --- packages/shaderlab/src/shaders/Normal.glsl | 2 +- packages/shaderlab/src/shaders/Vertex.glsl | 3 ++- .../shaders/shadingPBR/MaterialInputPBR.glsl | 19 +++++++++++-------- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 6 ++++-- .../src/shaders/temp/transformVaryings.glsl | 3 ++- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index c2b2ea66..bc94c200 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -29,7 +29,7 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt mat3 getTBN(Temp_Varyings v, bool isFrontFacing){ #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - mat3 tbn = v.v_TBN; + mat3 tbn = mat3(v.v_tangent, v.v_bitangent, v.v_normal); #else vec3 normal = getNormal(v, isFrontFacing); vec3 position = v.v_pos; diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 50c133e2..5bc8c862 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -170,7 +170,8 @@ void initVertex(){ vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; - v.v_TBN = mat3( tangentW, bitangentW, v.v_normal ); + v.v_tangent = tangentW; + v.v_bitangent = bitangentW; #endif #endif #endif diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index f6e33871..de6065ea 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -22,7 +22,8 @@ struct BRDFData{ // geometry vec3 position; vec3 normal; - mat3 tbn; + vec3 tangent; + vec3 bitangent; vec3 viewDir; float dotNV; @@ -233,11 +234,13 @@ void initGeometryData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFaci #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) mat3 tbn = getTBN(v, isFrontFacing); - brdfData.tbn = tbn; - #endif - - #ifdef MATERIAL_HAS_NORMALTEXTURE - brdfData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + brdfData.tangent = tbn[0]; + brdfData.bitangent = tbn[1]; + #ifdef MATERIAL_HAS_NORMALTEXTURE + brdfData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + brdfData.normal = tbn[2]; + #endif #else brdfData.normal = getNormal(v, isFrontFacing); #endif @@ -248,7 +251,7 @@ void initGeometryData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFaci void initClearCoatBRDFData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFacing){ #ifdef MATERIAL_ENABLE_CLEAR_COAT #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - brdfData.clearCoatNormal = getNormalByNormalTexture(brdfData.tbn, material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); #else brdfData.clearCoatNormal = getNormal(v, isFrontFacing); #endif @@ -283,7 +286,7 @@ void initAnisotropyBRDFData(Temp_Varyings v, inout BRDFData brdfData){ #endif brdfData.anisotropy = anisotropy; - brdfData.anisotropicT = normalize(brdfData.tbn * anisotropicDirection); + brdfData.anisotropicT = normalize(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal) * anisotropicDirection); brdfData.anisotropicB = normalize(cross(brdfData.normal, brdfData.anisotropicT)); brdfData.anisotropicN = getAnisotropicBentNormal(brdfData); diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index 702f49f8..a7240fae 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -13,7 +13,8 @@ struct Varyings{ #ifdef RENDERER_HAS_NORMAL vec3 v_normal; #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - mat3 v_TBN; + vec3 v_tangent; + vec3 v_bitangent; #endif #endif #endif @@ -46,7 +47,8 @@ struct Temp_Varyings{ #ifdef RENDERER_HAS_NORMAL vec3 v_normal; #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - mat3 v_TBN; + vec3 v_tangent; + vec3 v_bitangent; #endif #endif #endif diff --git a/packages/shaderlab/src/shaders/temp/transformVaryings.glsl b/packages/shaderlab/src/shaders/temp/transformVaryings.glsl index efe64137..c219382c 100644 --- a/packages/shaderlab/src/shaders/temp/transformVaryings.glsl +++ b/packages/shaderlab/src/shaders/temp/transformVaryings.glsl @@ -11,7 +11,8 @@ #ifdef RENDERER_HAS_NORMAL temp_varyings.v_normal = v.v_normal; #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) - temp_varyings.v_TBN = v.v_TBN; + temp_varyings.v_tangent = v.v_tangent; + temp_varyings.v_bitangent = v.v_bitangent; #endif #endif #endif From 9285572dc233c19aa53df88e330e4c7cab1ffd9c Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 13 Jun 2024 11:24:21 +0800 Subject: [PATCH 42/89] "v0.0.0-experimental-shaderlab-thin.4" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index c0ffb5d1..3076b36f 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 30d4b335..c65bb36e 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index a7fce230..c8a3ebd8 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 84f5fdad..11e58fd4 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index a2754f12..fcaa6b0a 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 33ecb25e..c3509cc9 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 9940f151..a36d8888 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index b63d043e..d0207c18 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 74977d01..442b1cbe 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 9574677a..c43e132c 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 7d8b8f66..420d91c7 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 99a81c0f..bc699e33 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index f91f9997..6998147b 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index e8e515d1..f119b489 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 93868e86..985a415c 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index cbe5a8c7..3c7746db 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index a215f703..e547a0f3 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 6e86187b..8ac34aaf 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 17701a23..be6423fd 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.3", + "version": "0.0.0-experimental-shaderlab-thin.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From bb48d43cdbe644f13d773b774549d7cc64242436 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 14 Jun 2024 17:01:04 +0800 Subject: [PATCH 43/89] refactor: init geometry data to surfaceData --- .../shaders/shadingPBR/ForwardPassPBR.glsl | 2 +- .../shaders/shadingPBR/MaterialInputPBR.glsl | 71 ++++++++++++------- .../shaders/shadingThin/ForwardPassThin.glsl | 2 +- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index d2f017ae..cac55e47 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -31,7 +31,7 @@ void PBRFragment(Varyings v) { Temp_Varyings temp_varyings; #include "temp_transformVaryings.glsl" - initSurfaceData(temp_varyings, surfaceData); + initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); // Can modify surfaceData here. initBRDFData(temp_varyings, surfaceData, brdfData, gl_FrontFacing); diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index de6065ea..c2b54084 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -4,6 +4,7 @@ #include "Normal.glsl" struct SurfaceData{ + // common vec3 albedoColor; vec3 specularColor; vec3 emissiveColor; @@ -11,6 +12,13 @@ struct SurfaceData{ float roughness; float f0; float opacity; + + // geometry + vec3 position; + vec3 normal; + vec3 tangent; + vec3 bitangent; + vec3 viewDir; } struct BRDFData{ @@ -138,7 +146,8 @@ float getAARoughnessFactor(vec3 normal) { #endif -void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData){ +void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ + // common vec4 baseColor = material_BaseColor; float metallic = material_Metal; float roughness = material_Roughness; @@ -202,6 +211,39 @@ void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData){ #else surfaceData.opacity = 1.0; #endif + + + // geometry + surfaceData.position = v.v_pos; + + #ifdef CAMERA_ORTHOGRAPHIC + surfaceData.viewDir = -camera_Forward; + #else + surfaceData.viewDir = normalize(camera_Position - v.v_pos); + #endif + + #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) + mat3 tbn = getTBN(v, isFrontFacing); + surfaceData.tangent = tbn[0]; + surfaceData.bitangent = tbn[1]; + #ifdef MATERIAL_HAS_NORMALTEXTURE + surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + #else + surfaceData.normal = tbn[2]; + #endif + #else + surfaceData.normal = getNormal(v, isFrontFacing); + #endif +} + +void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ + brdfData.position = surfaceData.position; + brdfData.normal = surfaceData.normal; + brdfData.tangent = surfaceData.tangent; + brdfData.bitangent = surfaceData.bitangent; + brdfData.viewDir = surfaceData.viewDir; + + brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); } void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ @@ -223,31 +265,6 @@ void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.roughness = max(roughness, getAARoughnessFactor(brdfData.normal)); } -void initGeometryData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFacing){ - brdfData.position = v.v_pos; - - #ifdef CAMERA_ORTHOGRAPHIC - brdfData.viewDir = -camera_Forward; - #else - brdfData.viewDir = normalize(camera_Position - v.v_pos); - #endif - - #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) - mat3 tbn = getTBN(v, isFrontFacing); - brdfData.tangent = tbn[0]; - brdfData.bitangent = tbn[1]; - #ifdef MATERIAL_HAS_NORMALTEXTURE - brdfData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); - #else - brdfData.normal = tbn[2]; - #endif - #else - brdfData.normal = getNormal(v, isFrontFacing); - #endif - - brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); -} - void initClearCoatBRDFData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFacing){ #ifdef MATERIAL_ENABLE_CLEAR_COAT #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE @@ -295,7 +312,7 @@ void initAnisotropyBRDFData(Temp_Varyings v, inout BRDFData brdfData){ } void initBRDFData(Temp_Varyings v, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ - initGeometryData(v, brdfData, isFrontFacing); + initGeometryData(surfaceData, brdfData); initCommonBRDFData(surfaceData, brdfData); initClearCoatBRDFData(v, brdfData, isFrontFacing); initAnisotropyBRDFData(v, brdfData); diff --git a/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl b/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl index ef50185d..46247962 100644 --- a/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl +++ b/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl @@ -38,7 +38,7 @@ void PBRFragment(Varyings v) { Temp_Varyings temp_varyings; #include "temp_transformVaryings.glsl" - initSurfaceData(temp_varyings, surfaceData); + initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); // Can modify surfaceData here. initBRDFData(temp_varyings, surfaceData, brdfData, gl_FrontFacing); From dbf5d2d54870f0de8e86c2d92adde59962380266 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Fri, 14 Jun 2024 17:01:53 +0800 Subject: [PATCH 44/89] "v0.0.0-experimental-shaderlab-thin.5" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 3076b36f..c5289966 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index c65bb36e..827f4ad7 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index c8a3ebd8..9c92365a 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 11e58fd4..54dd2992 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index fcaa6b0a..5eb81682 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index c3509cc9..fc9ba198 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index a36d8888..58069deb 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index d0207c18..d5d8f2ba 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 442b1cbe..88e32c4f 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index c43e132c..d54c7f7f 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 420d91c7..9368f586 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index bc699e33..92e1f1fc 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 6998147b..52f6fcf5 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index f119b489..9b86c81d 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 985a415c..7b23438c 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 3c7746db..55098f1a 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index e547a0f3..94760334 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 8ac34aaf..e3519c45 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index be6423fd..9b07151b 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.4", + "version": "0.0.0-experimental-shaderlab-thin.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 26a48cfd39825a34b72bb9f30699b55261d4017c Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 18 Jun 2024 16:14:48 +0800 Subject: [PATCH 45/89] refactor: change tangent logic --- packages/shaderlab/src/shaders/Normal.glsl | 2 +- packages/shaderlab/src/shaders/Vertex.glsl | 11 ++++--- .../shaders/shadingPBR/MaterialInputPBR.glsl | 32 ++++++++++++------- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 4 +-- .../src/shaders/temp/transformVaryings.glsl | 2 +- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index bc94c200..fe7d4383 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -28,7 +28,7 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt } mat3 getTBN(Temp_Varyings v, bool isFrontFacing){ - #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) mat3 tbn = mat3(v.v_tangent, v.v_bitangent, v.v_normal); #else vec3 normal = getNormal(v, isFrontFacing); diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 5bc8c862..84cbf769 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -40,7 +40,7 @@ void initVertex(){ normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; #endif - #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) vertexElementOffset += 1; tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; #endif @@ -58,7 +58,8 @@ void initVertex(){ normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; #endif - #if defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + + #ifdef RENDERER_HAS_TANGENT tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; #endif @@ -76,7 +77,7 @@ void initVertex(){ normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; #endif - #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; @@ -120,7 +121,7 @@ void initVertex(){ #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); normal = normal * skinNormalMatrix; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) ) + #ifdef RENDERER_HAS_TANGENT tangent.xyz = tangent.xyz * skinNormalMatrix; #endif @@ -166,7 +167,7 @@ void initVertex(){ #ifdef RENDERER_HAS_NORMAL v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + #ifdef RENDERER_HAS_TANGENT vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index c2b54084..b69ee8b1 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -14,10 +14,14 @@ struct SurfaceData{ float opacity; // geometry - vec3 position; - vec3 normal; - vec3 tangent; - vec3 bitangent; + vec3 position; + vec3 normal; + + #ifdef RENDERER_HAS_TANGENT + vec3 tangent; + vec3 bitangent; + #endif + vec3 viewDir; } @@ -28,10 +32,14 @@ struct BRDFData{ float roughness; // geometry - vec3 position; - vec3 normal; - vec3 tangent; - vec3 bitangent; + vec3 position; + vec3 normal; + + #ifdef RENDERER_HAS_TANGENT + vec3 tangent; + vec3 bitangent; + #endif + vec3 viewDir; float dotNV; @@ -222,7 +230,7 @@ void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontF surfaceData.viewDir = normalize(camera_Position - v.v_pos); #endif - #if defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) + #ifdef RENDERER_HAS_TANGENT mat3 tbn = getTBN(v, isFrontFacing); surfaceData.tangent = tbn[0]; surfaceData.bitangent = tbn[1]; @@ -239,8 +247,10 @@ void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontF void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.position = surfaceData.position; brdfData.normal = surfaceData.normal; - brdfData.tangent = surfaceData.tangent; - brdfData.bitangent = surfaceData.bitangent; + #ifdef RENDERER_HAS_TANGENT + brdfData.tangent = surfaceData.tangent; + brdfData.bitangent = surfaceData.bitangent; + #endif brdfData.viewDir = surfaceData.viewDir; brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index a7240fae..d064f0db 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -12,7 +12,7 @@ struct Varyings{ #ifndef MATERIAL_OMIT_NORMAL #ifdef RENDERER_HAS_NORMAL vec3 v_normal; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + #ifdef RENDERER_HAS_TANGENT vec3 v_tangent; vec3 v_bitangent; #endif @@ -46,7 +46,7 @@ struct Temp_Varyings{ #ifndef MATERIAL_OMIT_NORMAL #ifdef RENDERER_HAS_NORMAL vec3 v_normal; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + #ifdef RENDERER_HAS_TANGENT vec3 v_tangent; vec3 v_bitangent; #endif diff --git a/packages/shaderlab/src/shaders/temp/transformVaryings.glsl b/packages/shaderlab/src/shaders/temp/transformVaryings.glsl index c219382c..b0389d51 100644 --- a/packages/shaderlab/src/shaders/temp/transformVaryings.glsl +++ b/packages/shaderlab/src/shaders/temp/transformVaryings.glsl @@ -10,7 +10,7 @@ #ifndef MATERIAL_OMIT_NORMAL #ifdef RENDERER_HAS_NORMAL temp_varyings.v_normal = v.v_normal; - #if defined(RENDERER_HAS_TANGENT) && ( defined(MATERIAL_HAS_NORMALTEXTURE) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_ENABLE_ANISOTROPY) ) + #ifdef RENDERER_HAS_TANGENT temp_varyings.v_tangent = v.v_tangent; temp_varyings.v_bitangent = v.v_bitangent; #endif From 190609dc7f86f370d045c96b9a4b6c4cbbcb43d3 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 18 Jun 2024 16:23:31 +0800 Subject: [PATCH 46/89] "v0.0.0-experimental-shaderlab-thin.6" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index c5289966..12bbc631 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 827f4ad7..07d33f07 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 9c92365a..b9b918d0 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 54dd2992..4e403d0b 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 5eb81682..1844d8ca 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index fc9ba198..6c0fd14a 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 58069deb..5ec8b239 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index d5d8f2ba..a8d963f4 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 88e32c4f..32962ba0 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index d54c7f7f..215c286f 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 9368f586..3dcd7a21 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 92e1f1fc..61885f6c 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 52f6fcf5..a8abdd75 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 9b86c81d..f863a4e4 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 7b23438c..b0fc9093 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 55098f1a..a2b2806a 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 94760334..03752695 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index e3519c45..80cba8fd 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 9b07151b..d7834e9f 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.5", + "version": "0.0.0-experimental-shaderlab-thin.6", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 412549e9ebbd523dd0221bb9c229131281048273 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 15:10:25 +0800 Subject: [PATCH 47/89] refactor: delete thin shader --- packages/shaderlab/src/ThinMaterial.ts | 10 - packages/shaderlab/src/index.ts | 6 +- packages/shaderlab/src/shaders/index.ts | 7 +- .../src/shaders/shadingThin/BRDFThin.glsl | 129 ------------ .../shaders/shadingThin/ForwardPassThin.glsl | 63 ------ .../shaders/shadingThin/LightDirectThin.glsl | 178 ----------------- .../shadingThin/LightIndirectThin.glsl | 184 ------------------ .../src/shaders/shadingThin/index.ts | 11 -- packages/shaderlab/src/shaders/thin.gs | 94 --------- 9 files changed, 4 insertions(+), 678 deletions(-) delete mode 100644 packages/shaderlab/src/ThinMaterial.ts delete mode 100644 packages/shaderlab/src/shaders/shadingThin/BRDFThin.glsl delete mode 100644 packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl delete mode 100644 packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl delete mode 100644 packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl delete mode 100644 packages/shaderlab/src/shaders/shadingThin/index.ts delete mode 100644 packages/shaderlab/src/shaders/thin.gs diff --git a/packages/shaderlab/src/ThinMaterial.ts b/packages/shaderlab/src/ThinMaterial.ts deleted file mode 100644 index e95d107d..00000000 --- a/packages/shaderlab/src/ThinMaterial.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Engine, PBRMaterial, Shader } from "@galacean/engine"; - -export class ThinMaterial extends PBRMaterial { - constructor(engine: Engine) { - const shader = Shader.find("Thin.gs"); - super(engine); - - this.shader = shader; - } -} diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index 366a151e..717ac7ef 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1,7 +1,6 @@ export { GSLPBRMaterial } from "./GSLPBRMaterial"; -export { ThinMaterial } from "./ThinMaterial"; import { Shader, ShaderFactory } from "@galacean/engine"; -import { PBRSource, ThinSource, fragmentList } from "./shaders"; +import { PBRSource, fragmentList } from "./shaders"; let registered = false; @@ -13,9 +12,8 @@ export function registerIncludes() { } Shader.create(PBRSource); - Shader.create(ThinSource); registered = true; } -export { PBRSource, ThinSource }; +export { PBRSource }; diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 77a0c0b2..8dc3f96e 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -7,11 +7,9 @@ import PBRSource from "./PBR.gs"; import Shadow from "./Shadow.glsl"; import ShadowSampleTent from "./ShadowSampleTent.glsl"; import Skin from "./Skin.glsl"; -import ThinSource from "./Thin.gs"; import Transform from "./Transform.glsl"; import Vertex from "./Vertex.glsl"; import shadingPBR from "./shadingPBR"; -import shadingThin from "./shadingThin"; import temp_transformAttributes from "./temp/transformAttributes.glsl"; import temp_transformVaryings from "./temp/transformVaryings.glsl"; @@ -34,7 +32,6 @@ const fragmentList: IShaderFragment[] = [ { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, { source: Skin, includeKey: "Skin.glsl" }, - ...shadingPBR, - ...shadingThin + ...shadingPBR ]; -export { PBRSource, ThinSource, fragmentList }; +export { PBRSource, fragmentList }; diff --git a/packages/shaderlab/src/shaders/shadingThin/BRDFThin.glsl b/packages/shaderlab/src/shaders/shadingThin/BRDFThin.glsl deleted file mode 100644 index 09dbcb01..00000000 --- a/packages/shaderlab/src/shaders/shadingThin/BRDFThin.glsl +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef BRDF_THIN_INCLUDED -#define BRDF_THIN_INCLUDED 1 - -float sqr(float x) { return x * x; } -vec2 sqr(vec2 x) {return x * x;} -float IorToFresnel0 ( float transmittedIor, float incidentIor) -{ - return pow((transmittedIor - incidentIor) / (transmittedIor + incidentIor), 2.0); - } -vec3 IorToFresnel0(vec3 transmittedIor, float incidentIor) -{ - return pow((transmittedIor - incidentIor) / (transmittedIor + incidentIor),vec3(2.0,2.0,2.0)); -} - -float Depol (vec3 polV) {return 0.5 * (polV.x + polV.y); } -vec3 DepolColor (vec3 colS, vec3 colP){ return 0.5 * (colS + colP); } - -vec3 Fresnel0ToIor(vec3 F0) -{ - vec3 sqrtF0 = sqrt(F0); - return (vec3(1.0,1.0,1.0) + sqrtF0) / (vec3(1.0,1.0,1.0) - sqrtF0); -} - -//float smithG1_GGX(float NdotV, float a) -///{ - //float a2 = sqr(a); -// return 2.0 / (1.0 + sqrt(1.0 + a2 * (1.0-sqr(NdotV)) / sqr(NdotV) )); -//} - -//float smithG_GGX(float a, float NdotL, float NdotV) -//{ -//return smithG1_GGX(a, NdotL) * smithG1_GGX(a, NdotV); -/// } - - vec3 EvalSensitivity(float opd, vec3 shift) -{ - // Use Gaussian fits, given by 3 parameters: val, pos and var - float phase = 2.0 * PI * opd * 1.0e-6; - vec3 val = vec3(5.4856e-13, 4.4201e-13, 5.2481e-13); - vec3 pos = vec3(1.6810e+6, 1.7953e+6, 2.2084e+6); - vec3 var = vec3(4.3278e+9, 9.3046e+9, 6.6121e+9); - vec3 xyz = val * sqrt(2.0*PI * var) * cos(pos * phase + shift) * exp(-var * sqr(phase)); - xyz.x += 9.7470e-14 * sqrt(2.0*PI * 4.5282e+9) * cos(2.2399e+6 * phase + shift[0]) * exp(-4.5282e+9 * sqr(phase)); - xyz /= 1.0685e-7; - - mat3 XYZ_TO_RGB = mat3( 3.2404542, -0.9692660, 0.0556434, - -1.5371385, 1.8760108, -0.2040259, - -0.4985314, 0.0415560, 1.0572252); - vec3 rgb = XYZ_TO_RGB * xyz; - - return rgb; -} - -vec3 ThinFilmIridescence(float cosTheta1, float eta2, vec3 baseF0,float iridescenceThickness) -{ - float eta1 = 1.0; - float dinc = 2.0 * iridescenceThickness; - // float eta_2 = mix(2.0, 1.0, iridescenceThickness); - float sinTheta2 = pow(eta1 / eta2, 2.0) * (1.0 - pow(cosTheta1 , 2.0)); - float cosTheta2Sq = (1.0 - sinTheta2); - //if (cosTheta2Sq < 0.0) {return vec3(1.0,1.0,1.0);} - float cosTheta2 = sqrt(cosTheta2Sq); - - // First interface - float R0 = IorToFresnel0(eta2, eta1); - float R12 = F_Schlick(R0, cosTheta1); - float R21 = R12; - float T121 =1.0 - R12; - - float phi12 = 0.0; - // if (eta2 < eta1) { phi12 = PI;} - float phi21 = PI - phi12; - - vec3 baseIor = Fresnel0ToIor(baseF0 + 0.0001); - vec3 R1 = IorToFresnel0(baseIor, eta2); - vec3 R23 = F_Schlick(R1, cosTheta2); - - vec3 phi23 =vec3( 0.0,0.0,0.0); - if (baseIor[0] < eta2) {phi23[0] = PI;} - if (baseIor[1] < eta2) {phi23[1] = PI;} - if (baseIor[2] < eta2) {phi23[2] = PI;} - // Phase shift - float OPD = dinc * cosTheta1 * eta2;//dinc * cosTheta2; - vec3 phi = vec3(phi21,phi21,phi21) + phi23; - - vec3 R123 = clamp(R12 * R23, 1e-5, 0.9999); - vec3 r123 = sqrt(R123); - vec3 Rs = sqr(T121) * R23 / (vec3(1.0,1.0,1.0)-R123); - - vec3 C0 = R12 + Rs; - // vec3 S0 = EvalSensitivity(0.0, 0.0 ); - vec3 I = vec3(0.0, 0.0, 0.0); - I = C0;// Depol(C0)*S0; - - vec3 Cm = Rs - T121; - for (int m = 1; m <= 2; ++m) - { - Cm *= r123; - vec3 Sm = 2.0 * EvalSensitivity(float(m) * OPD, float(m) * phi); - // vec3 SmP = 2.0 * (EvalSensitivity(m * OPD, m * phi2.y)); - I += Cm * Sm;//DepolColor(Cm.x * SmS, Cm.y * SmP); - } - I = max(I, vec3(0.0, 0.0, 0.0)); - - return I ; -} - -vec3 DirectBDRFIridescence(vec3 incidentDirection, vec3 viewDir, vec3 normal,vec3 specularColor, float roughness ) -{ - // Compute dot products - float NdotL = saturate( dot( normal, incidentDirection ) ); - float NdotV = saturate( dot( normal, viewDir ) ); - vec3 halfDir = normalize( incidentDirection + viewDir ); - float NdotH = saturate( dot( normal, halfDir ) ); - - float cosTheta1 = dot(halfDir, incidentDirection); - - vec3 I = ThinFilmIridescence(cosTheta1,material_Eta2,specularColor,material_IridescenceThickness); - // Microfacet BRDF formula - float D = D_GGX(roughness, NdotH); - float G = G_GGX_SmithCorrelated(roughness, NdotL, NdotV); - - vec3 color = I ; // (4.0 * NdotL * NdotV); - - return color; -} - - -#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl b/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl deleted file mode 100644 index 46247962..00000000 --- a/packages/shaderlab/src/shaders/shadingThin/ForwardPassThin.glsl +++ /dev/null @@ -1,63 +0,0 @@ -#include "AttributesPBR.glsl" -#include "VaryingsPBR.glsl" -#include "Common.glsl" -#include "Vertex.glsl" -#include "Fog.glsl" - -#include "MaterialInputPBR.glsl" -float material_Iridescence; -float material_IridescenceThickness; -float material_Eta2; - -#include "LightDirectThin.glsl" -#include "LightIndirectThin.glsl" - -VertexShader = pbrVert; -FragmentShader = pbrFrag; - -Varyings PBRVertex(Attributes attr) { - Varyings v; - - // @todo: delete - Temp_Attributes temp_attributes; - Temp_Varyings temp_varyings; - #include "temp_transformAttributes.glsl" - #include "temp_transformVaryings.glsl" - - // @todo: use initVertex(attr, v); - initVertex(); - - return v; -} - -void PBRFragment(Varyings v) { - SurfaceData surfaceData; - BRDFData brdfData; - - // @todo: delete - Temp_Varyings temp_varyings; - #include "temp_transformVaryings.glsl" - - initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - // Can modify surfaceData here. - initBRDFData(temp_varyings, surfaceData, brdfData, gl_FrontFacing); - - vec4 color = vec4(0, 0, 0, surfaceData.opacity); - - // Direct Light - evaluateDirectRadiance(temp_varyings, brdfData, color.rgb); - // IBL - evaluateIBL(temp_varyings, brdfData, color.rgb); - // Emissive - color.rgb += surfaceData.emissiveColor; - - #if SCENE_FOG_MODE != 0 - color = fog(color, v.v_positionVS); - #endif - - #ifndef ENGINE_IS_COLORSPACE_GAMMA - color = linearToGamma(color); - #endif - - gl_FragColor = color; -} \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl deleted file mode 100644 index 38c027a2..00000000 --- a/packages/shaderlab/src/shaders/shadingThin/LightDirectThin.glsl +++ /dev/null @@ -1,178 +0,0 @@ - -// #ifndef LIGHT_DIRECT_THIN_INCLUDED -// #define LIGHT_DIRECT_THIN_INCLUDED 1 - -#include "BRDF.glsl" -#include "BRDFThin.glsl" -#include "Light.glsl" -#include "Shadow.glsl" - - -void diffuseLobe(BRDFData brdfData, vec3 irradiance, float attenuation, inout vec3 Fd){ - Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); -} - -void specularLobe(BRDFData brdfData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ - vec3 V = brdfData.viewDir; - vec3 H = normalize(incidentDirection+V); - float VoH = dot(H, V); - vec3 F = F_Schlick( brdfData.specularColor, VoH ); - - vec3 thin = DirectBDRFIridescence(incidentDirection , brdfData.viewDir , brdfData.normal ,brdfData.specularColor , brdfData.roughness); - vec3 F_iridescence = thin * material_Iridescence;//mix(vec3(0.0,0.0,0.0),thin,material_Iridescence); - vec3 BRDF_Specular = BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.normal, brdfData.specularColor, brdfData.roughness); - vec3 factor = mix(BRDF_Specular, thin, material_Iridescence); - - - Fs += attenuation * irradiance * factor; -} - -float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout vec3 Fs){ - float attenuation = 1.0; - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoatDotNL = saturate( dot( brdfData.clearCoatNormal, incidentDirection ) ); - vec3 clearCoatIrradiance = clearCoatDotNL * color; - - Fs += brdfData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.clearCoatNormal, vec3( 0.04 ), brdfData.clearCoatRoughness ); - attenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); - #endif - - return attenuation; -} - - -void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { - - vec3 Fd = vec3(0); - vec3 Fs = vec3(0); - float dotNL = saturate( dot( brdfData.normal, incidentDirection ) ); - vec3 irradiance = dotNL * lightColor * PI; - - // ClearCoat Lobe - float attenuation = clearCoatLobe(incidentDirection, lightColor, brdfData, Fs); - // Diffuse Lobe - diffuseLobe(brdfData, irradiance, attenuation, Fd); - // //Iridescence Specular Lobe - specularLobe(brdfData, incidentDirection, irradiance, attenuation, Fs); - - color += Fd + Fs; - -} - -#ifdef SCENE_DIRECT_LIGHT_COUNT - - void addDirectionalDirectLightRadiance(DirectLight directionalLight, BRDFData brdfData, inout vec3 color) { - vec3 lightColor = directionalLight.color; - vec3 direction = -directionalLight.direction; - - addRadiance( direction, lightColor, brdfData, color ); - - } - -#endif - -#ifdef SCENE_POINT_LIGHT_COUNT - - void addPointDirectLightRadiance(PointLight pointLight, BRDFData brdfData, inout vec3 color) { - - vec3 lVector = pointLight.position - brdfData.position; - vec3 direction = normalize( lVector ); - float lightDistance = length( lVector ); - - vec3 lightColor = pointLight.color; - lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - - addRadiance( direction, lightColor, brdfData, color ); - - } - -#endif - -#ifdef SCENE_SPOT_LIGHT_COUNT - - void addSpotDirectLightRadiance(SpotLight spotLight, BRDFData brdfData, inout vec3 color) { - - vec3 lVector = spotLight.position - brdfData.position; - vec3 direction = normalize( lVector ); - float lightDistance = length( lVector ); - float angleCos = dot( direction, -spotLight.direction ); - - float spotEffect = smoothstep( spotLight.penumbraCos, spotLight.angleCos, angleCos ); - float decayEffect = clamp(1.0 - pow(lightDistance/spotLight.distance, 4.0), 0.0, 1.0); - - vec3 lightColor = spotLight.color; - lightColor *= spotEffect * decayEffect; - - addRadiance( direction, lightColor, brdfData, color ); - - } - - -#endif - -void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ - float shadowAttenuation = 1.0; - - #ifdef SCENE_DIRECT_LIGHT_COUNT - shadowAttenuation = 1.0; - #ifdef SCENE_IS_CALCULATE_SHADOWS - shadowAttenuation *= sampleShadowMap(v); - // int sunIndex = int(scene_ShadowInfo.z); - #endif - - DirectLight directionalLight; - for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { - // warning: use `continue` syntax may trigger flickering bug in safri 16.1. - if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){ - directionalLight.color = scene_DirectLightColor[i]; - #ifdef SCENE_IS_CALCULATE_SHADOWS - if (i == 0) { // Sun light index is always 0 - directionalLight.color *= shadowAttenuation; - } - #endif - directionalLight.direction = scene_DirectLightDirection[i]; - addDirectionalDirectLightRadiance( directionalLight, brdfData, color ); - } - } - - #endif - - #ifdef SCENE_POINT_LIGHT_COUNT - - PointLight pointLight; - - for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) { - if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){ - pointLight.color = scene_PointLightColor[i]; - pointLight.position = scene_PointLightPosition[i]; - pointLight.distance = scene_PointLightDistance[i]; - - addPointDirectLightRadiance( pointLight, brdfData, color ); - } - } - - #endif - - #ifdef SCENE_SPOT_LIGHT_COUNT - - SpotLight spotLight; - - for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) { - if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){ - spotLight.color = scene_SpotLightColor[i]; - spotLight.position = scene_SpotLightPosition[i]; - spotLight.direction = scene_SpotLightDirection[i]; - spotLight.distance = scene_SpotLightDistance[i]; - spotLight.angleCos = scene_SpotLightAngleCos[i]; - spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; - - addSpotDirectLightRadiance( spotLight, brdfData, color ); - } - } - - #endif -} - - -// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl b/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl deleted file mode 100644 index c0530efa..00000000 --- a/packages/shaderlab/src/shaders/shadingThin/LightIndirectThin.glsl +++ /dev/null @@ -1,184 +0,0 @@ - -// #ifndef LIGHT_INDIRECT_THIN_INCLUDED -// #define LIGHT_INDIRECT_THIN_INCLUDED 1 - -#include "BRDF.glsl" -#include "BRDFThin.glsl" -#include "Light.glsl" - -// ------------------------Diffuse------------------------ - -// sh need be pre-scaled in CPU. -vec3 getLightProbeIrradiance(vec3 sh[9], vec3 normal){ - normal.x = -normal.x; - vec3 result = sh[0] + - - sh[1] * (normal.y) + - sh[2] * (normal.z) + - sh[3] * (normal.x) + - - sh[4] * (normal.y * normal.x) + - sh[5] * (normal.y * normal.z) + - sh[6] * (3.0 * normal.z * normal.z - 1.0) + - sh[7] * (normal.z * normal.x) + - sh[8] * (normal.x * normal.x - normal.y * normal.y); - - return max(result, vec3(0.0)); - -} - -// ------------------------Specular------------------------ - -// ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile -vec3 envBRDFApprox(vec3 specularColor, float roughness, float dotNV ) { - - const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 ); - - const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 ); - - vec4 r = roughness * c0 + c1; - - float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y; - - vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; - - return specularColor * AB.x + AB.y; - -} - - -float getSpecularMIPLevel(float roughness, int maxMIPLevel ) { - return roughness * float(maxMIPLevel); -} - -vec3 getReflectedVector(BRDFData brdfData, vec3 n) { - #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 r = reflect(-brdfData.viewDir, brdfData.anisotropicN); - #else - vec3 r = reflect(-brdfData.viewDir, n); - #endif - - return r; -} - -vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { - - #ifndef SCENE_USE_SPECULAR_ENV - return vec3(0); - #else - vec3 reflectVec = getReflectedVector(brdfData, normal); - reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse - - float specularMIPLevel = getSpecularMIPLevel(roughness, int(scene_EnvMapLight.mipMapLevel) ); - - #ifdef HAS_TEX_LOD - vec4 envMapColor = textureCubeLodEXT( scene_EnvSpecularSampler, reflectVec, specularMIPLevel ); - #else - vec4 envMapColor = textureCube( scene_EnvSpecularSampler, reflectVec, specularMIPLevel ); - #endif - - #ifdef SCENE_IS_DECODE_ENV_RGBM - envMapColor.rgb = (RGBMToLinear(envMapColor, 5.0)).rgb; - #ifdef ENGINE_IS_COLORSPACE_GAMMA - envMapColor = linearToGamma(envMapColor); - #endif - #else - #ifndef ENGINE_IS_COLORSPACE_GAMMA - envMapColor = gammaToLinear(envMapColor); - #endif - #endif - - return envMapColor.rgb * scene_EnvMapLight.specularIntensity; - - #endif - -} - - -void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 Fd){ - #ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, brdfData.normal); - #ifdef ENGINE_IS_COLORSPACE_GAMMA - irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; - #endif - irradiance *= scene_EnvMapLight.diffuseIntensity; - #else - vec3 irradiance = scene_EnvMapLight.diffuse * scene_EnvMapLight.diffuseIntensity; - irradiance *= PI; - #endif - - Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); -} - -float evaluateClearCoatIBL(BRDFData brdfData, float specularAO, inout vec3 Fs){ - float radianceAttenuation = 1.0; - - #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( brdfData, brdfData.clearCoatNormal, brdfData.clearCoatRoughness); - Fs += specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); - radianceAttenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); - #endif - - return radianceAttenuation; -} - -void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAttenuation, inout vec3 Fs){ - vec3 reflectdir = reflect(brdfData.normal, -brdfData.viewDir); - vec3 halfdir = reflectdir + brdfData.viewDir; - float cosTheta1 = dot(halfdir, reflectdir); - vec3 fresnelIridescent = ThinFilmIridescence(cosTheta1, material_Eta2 , brdfData.specularColor ,material_IridescenceThickness); - //vec3 F_iridescence = fresnelIridescent * material_Iridescence;//mix(vec3(0.0,0.0,0.0),fresnelIridescent,material_Iridescence); - vec3 envBRDF = envBRDFApprox( brdfData.specularColor , brdfData.roughness, brdfData.dotNV ); - vec3 fator = mix(envBRDF, fresnelIridescent, material_Iridescence); - - vec3 radiance = getLightProbeRadiance(brdfData, brdfData.normal, brdfData.roughness); - Fs += specularAO * radianceAttenuation * radiance * fator; -} - -float evaluateDiffuseAO(Temp_Varyings v){ - float diffuseAO = 1.0; - - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - #endif - - return diffuseAO; -} - -float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ - float specularAO = 1.0; - - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) - specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); - #endif - - return specularAO; -} - - -void evaluateIBL(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ - vec3 Fd = vec3(0); - vec3 Fs = vec3(0); - float diffuseAO = evaluateDiffuseAO(v); - float specularAO = evaluateSpecularAO(diffuseAO, brdfData.roughness, brdfData.dotNV); - - // IBL diffuse - evaluateDiffuseIBL(brdfData, diffuseAO, Fd); - - // IBL ClearCoat - float radianceAttenuation = evaluateClearCoatIBL(brdfData, specularAO, Fs); - - // IBL Iridescence Specular - evaluateSpecularIBL(brdfData, specularAO, radianceAttenuation, Fs); - - color += Fd + Fs; -} - - -// #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingThin/index.ts b/packages/shaderlab/src/shaders/shadingThin/index.ts deleted file mode 100644 index 4f71020d..00000000 --- a/packages/shaderlab/src/shaders/shadingThin/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import BRDF from "./BRDFThin.glsl"; -import ForwardPassThin from "./ForwardPassThin.glsl"; -import LightDirectThin from "./LightDirectThin.glsl"; -import LightIndirectThin from "./LightIndirectThin.glsl"; - -export default [ - { source: ForwardPassThin, includeKey: "ForwardPassThin.glsl" }, - { source: BRDF, includeKey: "BRDFThin.glsl" }, - { source: LightDirectThin, includeKey: "LightDirectThin.glsl" }, - { source: LightIndirectThin, includeKey: "LightIndirectThin.glsl" } -]; diff --git a/packages/shaderlab/src/shaders/thin.gs b/packages/shaderlab/src/shaders/thin.gs deleted file mode 100644 index 8b8847c1..00000000 --- a/packages/shaderlab/src/shaders/thin.gs +++ /dev/null @@ -1,94 +0,0 @@ -Shader "Thin.gs" { - EditorProperties { - Header("Thin Film") { - material_Iridescence("Iridescence", Range(0, 1, 0.01)) = 1; - material_IridescenceThickness("iridescenceThickness", Range(0.01, 5, 0.01)) = 0.1; - material_Eta2("iridescentIor", Range(1, 5, 0.1)) = 1.5; - } - - Header("Base"){ - material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; - material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); - material_BaseTexture("BaseTexture", Texture2D); - } - - Header("Metal Roughness") { - material_Metal( "Metal", Range(0,1,0.01) ) = 1; - material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; - material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); - } - - Header("Anisotropy") { - material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); - material_AnisotropyTexture("AnisotropyTexture", Texture2D); - } - - Header("Normal") { - material_NormalTexture("NormalTexture", Texture2D); - material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; - } - - Header("Emissive") { - material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); - material_EmissiveTexture("EmissiveTexture", Texture2D); - } - - Header("Occlusion") { - material_OcclusionTexture("OcclusionTexture", Texture2D); - material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; - material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; - } - - Header("Clear Coat") { - material_ClearCoat("ClearCoat", Range(0, 1, 0.01)) = 0; - material_ClearCoatTexture("ClearCoatTexture", Texture2D); - material_ClearCoatRoughness("ClearCoatRoughness", Range(0, 1, 0.01)) = 0; - material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); - material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); - } - - Header("Common") { - material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; - material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); - } - } - - EditorMacros { - Header("Conditional Macors") { - MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); - MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); - MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY"); - MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE") - MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); - MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); - MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); - MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); - MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); - MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); - MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); - MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); - MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); - } - - Header("Enabled Macros") { - [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); - [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); - } - } - - SubShader "Default" { - UsePass "pbr/Default/ShadowCaster" - - Pass "Forward Pass" { - Tags { pipelineStage = "Forward"} - - #define IS_METALLIC_WORKFLOW - - VertexShader = PBRVertex; - FragmentShader = PBRFragment; - - #include "ForwardPassThin.glsl" - } - - } - } \ No newline at end of file From 4f2f3736768d8d190eeb033d837340fcc53b1450 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 15:41:32 +0800 Subject: [PATCH 48/89] perf: enhance code --- packages/shaderlab/src/shaders/Light.glsl | 69 +++++++++++++------ .../shaders/shadingPBR/LightDirectPBR.glsl | 51 ++++++-------- .../shaders/shadingPBR/LightIndirectPBR.glsl | 24 +++---- 3 files changed, 81 insertions(+), 63 deletions(-) diff --git a/packages/shaderlab/src/shaders/Light.glsl b/packages/shaderlab/src/shaders/Light.glsl index 6f49e64c..9db02d55 100644 --- a/packages/shaderlab/src/shaders/Light.glsl +++ b/packages/shaderlab/src/shaders/Light.glsl @@ -1,18 +1,47 @@ #ifndef LIGHT_INCLUDED #define LIGHT_INCLUDED 1 + +ivec4 renderer_Layer; +#ifndef GRAPHICS_API_WEBGL2 + bool isBitSet(float value, float mask, float bitIndex){ + return mod(floor(value / pow(2.0, bitIndex)), 2.0) == 1.0 && mod(floor(mask / pow(2.0, bitIndex)), 2.0) == 1.0; + } +#endif + +bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) +{ + #ifdef GRAPHICS_API_WEBGL2 + return !((rendererLayer.x & lightCullingMask.x) != 0 || (rendererLayer.y & lightCullingMask.y) != 0); + #else + for (int i = 0; i < 16; i++) { + if (isBitSet( float(rendererLayer.x), float(lightCullingMask.x), float(i)) || isBitSet( float(rendererLayer.y), float(lightCullingMask.y), float(i))) { + return false; + } + } + return true; + #endif +} + // Directional light #ifdef SCENE_DIRECT_LIGHT_COUNT struct DirectLight { vec3 color; vec3 direction; + bool isCulled; } ivec2 scene_DirectLightCullingMask[SCENE_DIRECT_LIGHT_COUNT]; vec3 scene_DirectLightColor[SCENE_DIRECT_LIGHT_COUNT]; vec3 scene_DirectLightDirection[SCENE_DIRECT_LIGHT_COUNT]; + void getLightInfo(int index, out DirectLight light){ + light.color = scene_DirectLightColor[index]; + light.direction = scene_DirectLightDirection[index]; + light.isCulled = isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[index]); + } + #endif @@ -23,6 +52,7 @@ vec3 color; vec3 position; float distance; + bool isCulled; } ivec2 scene_PointLightCullingMask[ SCENE_POINT_LIGHT_COUNT ]; @@ -30,6 +60,13 @@ vec3 scene_PointLightPosition[ SCENE_POINT_LIGHT_COUNT ]; float scene_PointLightDistance[ SCENE_POINT_LIGHT_COUNT ]; + void getLightInfo(int index, out PointLight light){ + light.color = scene_PointLightColor[index]; + light.position = scene_PointLightPosition[index]; + light.distance = scene_PointLightDistance[index]; + light.isCulled = isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[index]); + } + #endif @@ -43,6 +80,7 @@ float distance; float angleCos; float penumbraCos; + bool isCulled; } ivec2 scene_SpotLightCullingMask[ SCENE_SPOT_LIGHT_COUNT ]; @@ -53,6 +91,17 @@ float scene_SpotLightAngleCos[ SCENE_SPOT_LIGHT_COUNT ]; float scene_SpotLightPenumbraCos[ SCENE_SPOT_LIGHT_COUNT ]; + void getLightInfo(int index, out SpotLight light){ + light.color = scene_SpotLightColor[index]; + light.position = scene_SpotLightPosition[index]; + light.direction = scene_SpotLightDirection[index]; + light.distance = scene_SpotLightDistance[index]; + light.angleCos = scene_SpotLightAngleCos[index]; + light.penumbraCos = scene_SpotLightPenumbraCos[index]; + light.isCulled = isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[index]); + } + + #endif // Ambient light @@ -65,7 +114,6 @@ struct EnvMapLight { EnvMapLight scene_EnvMapLight; -ivec4 renderer_Layer; #ifdef SCENE_USE_SH vec3 scene_EnvSH[9]; @@ -75,26 +123,7 @@ ivec4 renderer_Layer; samplerCube scene_EnvSpecularSampler; #endif -#ifndef GRAPHICS_API_WEBGL2 -bool isBitSet(float value, float mask, float bitIndex) -{ - return mod(floor(value / pow(2.0, bitIndex)), 2.0) == 1.0 && mod(floor(mask / pow(2.0, bitIndex)), 2.0) == 1.0; -} -#endif -bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) -{ - #ifdef GRAPHICS_API_WEBGL2 - return !((rendererLayer.x & lightCullingMask.x) != 0 || (rendererLayer.y & lightCullingMask.y) != 0); - #else - for (int i = 0; i < 16; i++) { - if (isBitSet( float(rendererLayer.x), float(lightCullingMask.x), float(i)) || isBitSet( float(rendererLayer.y), float(lightCullingMask.y), float(i))) { - return false; - } - } - return true; - #endif -} #endif diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 37782c5c..ec2cde3b 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -7,22 +7,22 @@ #include "Shadow.glsl" -void diffuseLobe(BRDFData brdfData, vec3 irradiance, float attenuation, inout vec3 Fd){ - Fd += attenuation * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); +void diffuseLobe(BRDFData brdfData, vec3 attenuationIrradiance, inout vec3 diffuseColor){ + diffuseColor += attenuationIrradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -void specularLobe(BRDFData brdfData, vec3 incidentDirection, vec3 irradiance, float attenuation, inout vec3 Fs){ - Fs += attenuation * irradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.normal, brdfData.specularColor, brdfData.roughness); +void specularLobe(BRDFData brdfData, vec3 incidentDirection, vec3 attenuationIrradiance, inout vec3 specularColor){ + specularColor += attenuationIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.normal, brdfData.specularColor, brdfData.roughness); } -float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout vec3 Fs){ +float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout vec3 specularColor){ float attenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT float clearCoatDotNL = saturate( dot( brdfData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; - Fs += brdfData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.clearCoatNormal, vec3( 0.04 ), brdfData.clearCoatRoughness ); + specularColor += brdfData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.clearCoatNormal, vec3( 0.04 ), brdfData.clearCoatRoughness ); attenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif @@ -32,19 +32,21 @@ float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { - vec3 Fd = vec3(0); - vec3 Fs = vec3(0); + vec3 diffuseColor = vec3(0); + vec3 specularColor = vec3(0); float dotNL = saturate( dot( brdfData.normal, incidentDirection ) ); vec3 irradiance = dotNL * lightColor * PI; // ClearCoat Lobe - float attenuation = clearCoatLobe(incidentDirection, lightColor, brdfData, Fs); + float attenuation = clearCoatLobe(incidentDirection, lightColor, brdfData, specularColor); + + vec3 attenuationIrradiance = irradiance * irradiance; // Diffuse Lobe - diffuseLobe(brdfData, irradiance, attenuation, Fd); + diffuseLobe(brdfData, attenuationIrradiance, diffuseColor); // Specular Lobe - specularLobe(brdfData, incidentDirection, irradiance, attenuation, Fs); + specularLobe(brdfData, incidentDirection, attenuationIrradiance, specularColor); - color += Fd + Fs; + color += diffuseColor + specularColor; } @@ -106,20 +108,18 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color shadowAttenuation = 1.0; #ifdef SCENE_IS_CALCULATE_SHADOWS shadowAttenuation *= sampleShadowMap(v); - // int sunIndex = int(scene_ShadowInfo.z); #endif DirectLight directionalLight; for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { + getLightInfo(i, directionalLight); // warning: use `continue` syntax may trigger flickering bug in safri 16.1. - if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){ - directionalLight.color = scene_DirectLightColor[i]; + if(!directionalLight.isCulled){ #ifdef SCENE_IS_CALCULATE_SHADOWS if (i == 0) { // Sun light index is always 0 directionalLight.color *= shadowAttenuation; } #endif - directionalLight.direction = scene_DirectLightDirection[i]; addDirectionalDirectLightRadiance( directionalLight, brdfData, color ); } } @@ -129,13 +129,9 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color #ifdef SCENE_POINT_LIGHT_COUNT PointLight pointLight; - for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) { - if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){ - pointLight.color = scene_PointLightColor[i]; - pointLight.position = scene_PointLightPosition[i]; - pointLight.distance = scene_PointLightDistance[i]; - + getLightInfo(i, pointLight); + if(!pointLight.isCulled){ addPointDirectLightRadiance( pointLight, brdfData, color ); } } @@ -145,16 +141,9 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color #ifdef SCENE_SPOT_LIGHT_COUNT SpotLight spotLight; - for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) { - if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){ - spotLight.color = scene_SpotLightColor[i]; - spotLight.position = scene_SpotLightPosition[i]; - spotLight.direction = scene_SpotLightDirection[i]; - spotLight.distance = scene_SpotLightDistance[i]; - spotLight.angleCos = scene_SpotLightAngleCos[i]; - spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; - + getLightInfo(i, spotLight); + if(!spotLight.isCulled){ addSpotDirectLightRadiance( spotLight, brdfData, color ); } } diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index 11b3de6d..d704f168 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -94,7 +94,7 @@ vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { } -void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 Fd){ +void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 diffuseColor){ #ifdef SCENE_USE_SH vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, brdfData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA @@ -106,24 +106,24 @@ void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 Fd){ irradiance *= PI; #endif - Fd += diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); + diffuseColor += diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -float evaluateClearCoatIBL(BRDFData brdfData, float specularAO, inout vec3 Fs){ +float evaluateClearCoatIBL(BRDFData brdfData, float specularAO, inout vec3 specularColor){ float radianceAttenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT vec3 clearCoatRadiance = getLightProbeRadiance( brdfData, brdfData.clearCoatNormal, brdfData.clearCoatRoughness); - Fs += specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); + specularColor += specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); radianceAttenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif return radianceAttenuation; } -void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAttenuation, inout vec3 Fs){ +void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAttenuation, inout vec3 specularColor){ vec3 radiance = getLightProbeRadiance(brdfData, brdfData.normal, brdfData.roughness); - Fs += specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, brdfData.dotNV ); + specularColor += specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, brdfData.dotNV ); } @@ -154,21 +154,21 @@ float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ } void evaluateIBL(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ - vec3 Fd = vec3(0); - vec3 Fs = vec3(0); + vec3 diffuseColor = vec3(0); + vec3 specularColor = vec3(0); float diffuseAO = evaluateDiffuseAO(v); float specularAO = evaluateSpecularAO(diffuseAO, brdfData.roughness, brdfData.dotNV); // IBL diffuse - evaluateDiffuseIBL(brdfData, diffuseAO, Fd); + evaluateDiffuseIBL(brdfData, diffuseAO, diffuseColor); // IBL ClearCoat - float radianceAttenuation = evaluateClearCoatIBL(brdfData, specularAO, Fs); + float radianceAttenuation = evaluateClearCoatIBL(brdfData, specularAO, specularColor); // IBL specular - evaluateSpecularIBL(brdfData, specularAO, radianceAttenuation, Fs); + evaluateSpecularIBL(brdfData, specularAO, radianceAttenuation, specularColor); - color += Fd + Fs; + color += diffuseColor + specularColor; } From 9e07c00bfbe88b07ab0ba27c2a5b910ce392d4a6 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 15:42:30 +0800 Subject: [PATCH 49/89] "v0.0.0-experimental-shaderlab-thin.7" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 12bbc631..3cfccc87 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 07d33f07..01203d6e 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index b9b918d0..e0c3426b 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 4e403d0b..c78ebf2a 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 1844d8ca..6ad9af10 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 6c0fd14a..c227fcac 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 5ec8b239..6a48bbbe 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index a8d963f4..85463b2b 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 32962ba0..0d6f3a42 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 215c286f..368ef83c 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 3dcd7a21..ae2835e6 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 61885f6c..dabbdd88 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index a8abdd75..dc839d96 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index f863a4e4..650b561b 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index b0fc9093..f513a17f 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index a2b2806a..2fea1c3f 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 03752695..53d2f8c2 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 80cba8fd..0ae42a8b 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index d7834e9f..dd44f2f9 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.6", + "version": "0.0.0-experimental-shaderlab-thin.7", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 7aba925f9da865e7ead93b12fe64ba7375a372fe Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 15:52:13 +0800 Subject: [PATCH 50/89] refactor: revert culled logic --- packages/shaderlab/src/shaders/Light.glsl | 6 ------ .../shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl | 6 +++--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/shaderlab/src/shaders/Light.glsl b/packages/shaderlab/src/shaders/Light.glsl index 9db02d55..05085feb 100644 --- a/packages/shaderlab/src/shaders/Light.glsl +++ b/packages/shaderlab/src/shaders/Light.glsl @@ -29,7 +29,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) struct DirectLight { vec3 color; vec3 direction; - bool isCulled; } ivec2 scene_DirectLightCullingMask[SCENE_DIRECT_LIGHT_COUNT]; @@ -39,7 +38,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) void getLightInfo(int index, out DirectLight light){ light.color = scene_DirectLightColor[index]; light.direction = scene_DirectLightDirection[index]; - light.isCulled = isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[index]); } #endif @@ -52,7 +50,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) vec3 color; vec3 position; float distance; - bool isCulled; } ivec2 scene_PointLightCullingMask[ SCENE_POINT_LIGHT_COUNT ]; @@ -64,7 +61,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) light.color = scene_PointLightColor[index]; light.position = scene_PointLightPosition[index]; light.distance = scene_PointLightDistance[index]; - light.isCulled = isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[index]); } #endif @@ -80,7 +76,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) float distance; float angleCos; float penumbraCos; - bool isCulled; } ivec2 scene_SpotLightCullingMask[ SCENE_SPOT_LIGHT_COUNT ]; @@ -98,7 +93,6 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) light.distance = scene_SpotLightDistance[index]; light.angleCos = scene_SpotLightAngleCos[index]; light.penumbraCos = scene_SpotLightPenumbraCos[index]; - light.isCulled = isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[index]); } diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index ec2cde3b..6d45b882 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -114,7 +114,7 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { getLightInfo(i, directionalLight); // warning: use `continue` syntax may trigger flickering bug in safri 16.1. - if(!directionalLight.isCulled){ + if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){ #ifdef SCENE_IS_CALCULATE_SHADOWS if (i == 0) { // Sun light index is always 0 directionalLight.color *= shadowAttenuation; @@ -131,7 +131,7 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color PointLight pointLight; for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) { getLightInfo(i, pointLight); - if(!pointLight.isCulled){ + if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){ addPointDirectLightRadiance( pointLight, brdfData, color ); } } @@ -143,7 +143,7 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color SpotLight spotLight; for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) { getLightInfo(i, spotLight); - if(!spotLight.isCulled){ + if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){ addSpotDirectLightRadiance( spotLight, brdfData, color ); } } From 562c12810581954ed6e14e38513c6ad53f2f2654 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 15:53:45 +0800 Subject: [PATCH 51/89] "v0.0.0-experimental-shaderlab-thin.8" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 3cfccc87..ad578a69 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 01203d6e..695a151b 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index e0c3426b..7ab68d18 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index c78ebf2a..bee5acfe 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 6ad9af10..080c093d 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index c227fcac..360f6232 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 6a48bbbe..f39dbb5e 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 85463b2b..4d5bae31 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 0d6f3a42..fe711aca 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 368ef83c..615aefae 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index ae2835e6..af072f0c 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index dabbdd88..b56d1dd1 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index dc839d96..f9d13be3 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 650b561b..07492a37 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index f513a17f..682992b4 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 2fea1c3f..65a14b7f 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 53d2f8c2..7009ff71 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 0ae42a8b..ced60822 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index dd44f2f9..ca71de7e 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.7", + "version": "0.0.0-experimental-shaderlab-thin.8", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 0c8faa51be677812454952094eacc0e8a3f6b9b0 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 16:26:16 +0800 Subject: [PATCH 52/89] "v0.0.0-experimental-shaderlab-pbr.0" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index ad578a69..d05701b4 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 695a151b..ea2d663b 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 7ab68d18..e094e8b7 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index bee5acfe..fa46b86a 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 080c093d..9cd16bbe 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 360f6232..dafc6f22 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index f39dbb5e..aa4e224f 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 4d5bae31..68eaefe2 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index fe711aca..c65a9e83 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 615aefae..c7e88395 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index af072f0c..378b39ea 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index b56d1dd1..9965bf6b 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index f9d13be3..d2a15777 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 07492a37..62eed8f2 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 682992b4..c708dada 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 65a14b7f..d71fe16a 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 7009ff71..817737e4 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index ced60822..8feef81b 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index ca71de7e..2a627075 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-thin.8", + "version": "0.0.0-experimental-shaderlab-pbr.0", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From e78d604b885db4ccf1e35ee4397229f42c116802 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 17:12:07 +0800 Subject: [PATCH 53/89] refactor: rename --- packages/shaderlab/src/shaders/Light.glsl | 18 +++++++++++++----- .../src/shaders/shadingPBR/LightDirectPBR.glsl | 11 ++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/shaderlab/src/shaders/Light.glsl b/packages/shaderlab/src/shaders/Light.glsl index 05085feb..2d2284ce 100644 --- a/packages/shaderlab/src/shaders/Light.glsl +++ b/packages/shaderlab/src/shaders/Light.glsl @@ -9,8 +9,7 @@ ivec4 renderer_Layer; } #endif -bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) -{ +bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ #ifdef GRAPHICS_API_WEBGL2 return !((rendererLayer.x & lightCullingMask.x) != 0 || (rendererLayer.y & lightCullingMask.y) != 0); #else @@ -35,9 +34,12 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) vec3 scene_DirectLightColor[SCENE_DIRECT_LIGHT_COUNT]; vec3 scene_DirectLightDirection[SCENE_DIRECT_LIGHT_COUNT]; - void getLightInfo(int index, out DirectLight light){ + DirectLight getDirectLight(int index){ + DirectLight light; light.color = scene_DirectLightColor[index]; light.direction = scene_DirectLightDirection[index]; + + return light; } #endif @@ -57,10 +59,13 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) vec3 scene_PointLightPosition[ SCENE_POINT_LIGHT_COUNT ]; float scene_PointLightDistance[ SCENE_POINT_LIGHT_COUNT ]; - void getLightInfo(int index, out PointLight light){ + PointLight getPointLight(int index){ + PointLight light; light.color = scene_PointLightColor[index]; light.position = scene_PointLightPosition[index]; light.distance = scene_PointLightDistance[index]; + + return light; } #endif @@ -86,13 +91,16 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask) float scene_SpotLightAngleCos[ SCENE_SPOT_LIGHT_COUNT ]; float scene_SpotLightPenumbraCos[ SCENE_SPOT_LIGHT_COUNT ]; - void getLightInfo(int index, out SpotLight light){ + SpotLight getSpotLight(int index){ + SpotLight light; light.color = scene_SpotLightColor[index]; light.position = scene_SpotLightPosition[index]; light.direction = scene_SpotLightDirection[index]; light.distance = scene_SpotLightDistance[index]; light.angleCos = scene_SpotLightAngleCos[index]; light.penumbraCos = scene_SpotLightPenumbraCos[index]; + + return light; } diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 6d45b882..d9314811 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -110,11 +110,10 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color shadowAttenuation *= sampleShadowMap(v); #endif - DirectLight directionalLight; for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { - getLightInfo(i, directionalLight); // warning: use `continue` syntax may trigger flickering bug in safri 16.1. if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){ + DirectLight directionalLight = getDirectLight(i); #ifdef SCENE_IS_CALCULATE_SHADOWS if (i == 0) { // Sun light index is always 0 directionalLight.color *= shadowAttenuation; @@ -128,10 +127,9 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color #ifdef SCENE_POINT_LIGHT_COUNT - PointLight pointLight; for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) { - getLightInfo(i, pointLight); if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){ + PointLight pointLight = getPointLight(i); addPointDirectLightRadiance( pointLight, brdfData, color ); } } @@ -139,11 +137,10 @@ void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color #endif #ifdef SCENE_SPOT_LIGHT_COUNT - - SpotLight spotLight; + for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) { - getLightInfo(i, spotLight); if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){ + SpotLight spotLight = getSpotLight(i); addSpotDirectLightRadiance( spotLight, brdfData, color ); } } From 054c43e18fd450a9c0779c36da0c3b4af9a12d33 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Mon, 24 Jun 2024 17:13:19 +0800 Subject: [PATCH 54/89] "v0.0.0-experimental-shaderlab-pbr.1" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index d05701b4..915c4220 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index ea2d663b..bbb01b1a 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index e094e8b7..5646c7d6 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index fa46b86a..de81ca7c 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 9cd16bbe..2d01c2c4 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index dafc6f22..03ce5e6a 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index aa4e224f..3dc3464c 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index 68eaefe2..a13e17e5 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index c65a9e83..a836d7ce 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index c7e88395..f277f0f8 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 378b39ea..8d7de4ec 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 9965bf6b..d8ace551 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index d2a15777..54c32515 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 62eed8f2..ab108927 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index c708dada..1f04eee9 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index d71fe16a..8140d12f 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 817737e4..4c48770f 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 8feef81b..73ecf7aa 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 2a627075..6d42c8ac 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-pbr.0", + "version": "0.0.0-experimental-shaderlab-pbr.1", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 544c5d4f4841fe9b36610ef436024f94af833af0 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 13:50:15 +0800 Subject: [PATCH 55/89] refactor: add shading function macro --- .../src/shaders/shadingPBR/ForwardPassPBR.glsl | 2 +- .../src/shaders/shadingPBR/LightDirectPBR.glsl | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index cac55e47..f819f4cc 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -38,7 +38,7 @@ void PBRFragment(Varyings v) { vec4 color = vec4(0, 0, 0, surfaceData.opacity); // Direct Light - evaluateDirectRadiance(temp_varyings, brdfData, color.rgb); + FUNCTION_SURFACE_SHADING(temp_varyings, brdfData, color.rgb); // IBL evaluateIBL(temp_varyings, brdfData, color.rgb); // Emissive diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index d9314811..3a773817 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -2,6 +2,11 @@ // #ifndef LIGHT_DIRECT_PBR_INCLUDED // #define LIGHT_DIRECT_PBR_INCLUDED 1 +#define FUNCTION_SURFACE_SHADING surfaceShading +#define FUNCTION_DIFFUSE_LOBE diffuseLobe +#define FUNCTION_SPECULAR_LOBE specularLobe +#define FUNCTION_CLEAR_COAT_LOBE clearCoatLobe + #include "BRDF.glsl" #include "Light.glsl" #include "Shadow.glsl" @@ -38,13 +43,13 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, ino vec3 irradiance = dotNL * lightColor * PI; // ClearCoat Lobe - float attenuation = clearCoatLobe(incidentDirection, lightColor, brdfData, specularColor); + float attenuation = FUNCTION_CLEAR_COAT_LOBE(incidentDirection, lightColor, brdfData, specularColor); vec3 attenuationIrradiance = irradiance * irradiance; // Diffuse Lobe - diffuseLobe(brdfData, attenuationIrradiance, diffuseColor); + FUNCTION_DIFFUSE_LOBE(brdfData, attenuationIrradiance, diffuseColor); // Specular Lobe - specularLobe(brdfData, incidentDirection, attenuationIrradiance, specularColor); + FUNCTION_SPECULAR_LOBE(brdfData, incidentDirection, attenuationIrradiance, specularColor); color += diffuseColor + specularColor; @@ -101,7 +106,7 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, ino #endif -void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ +void surfaceShading(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT From 9e7f2d20d476056cf5be3cf176afcb293fc0c17e Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 13:50:52 +0800 Subject: [PATCH 56/89] "v0.0.0-experimental-shaderlab-pbr.2" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index 915c4220..aa80171d 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index bbb01b1a..a2736cff 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 5646c7d6..299050cb 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index de81ca7c..99df7327 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 2d01c2c4..7cc2b91a 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 03ce5e6a..5636f65d 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 3dc3464c..2ca911bd 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index a13e17e5..cf4af311 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index a836d7ce..6be82da3 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index f277f0f8..a4783bab 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 8d7de4ec..a9e017ea 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index d8ace551..a95822ca 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 54c32515..a115a50f 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index ab108927..9376c992 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 1f04eee9..55557fa2 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 8140d12f..fd3ab01f 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 4c48770f..bfb07e53 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 73ecf7aa..785ab88e 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 6d42c8ac..44b78ea0 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-pbr.1", + "version": "0.0.0-experimental-shaderlab-pbr.2", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 761f10785dcaa25b1639d81614e93b234f262992 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 14:28:52 +0800 Subject: [PATCH 57/89] refactor: add surfaceShading --- .../src/shaders/shadingPBR/ForwardPassPBR.glsl | 2 +- .../src/shaders/shadingPBR/LightDirectPBR.glsl | 16 ++++++++-------- .../src/shaders/shadingPBR/LightIndirectPBR.glsl | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index f819f4cc..cac55e47 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -38,7 +38,7 @@ void PBRFragment(Varyings v) { vec4 color = vec4(0, 0, 0, surfaceData.opacity); // Direct Light - FUNCTION_SURFACE_SHADING(temp_varyings, brdfData, color.rgb); + evaluateDirectRadiance(temp_varyings, brdfData, color.rgb); // IBL evaluateIBL(temp_varyings, brdfData, color.rgb); // Emissive diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 3a773817..08f9627f 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -1,6 +1,6 @@ -// #ifndef LIGHT_DIRECT_PBR_INCLUDED -// #define LIGHT_DIRECT_PBR_INCLUDED 1 +#ifndef LIGHT_DIRECT_PBR_INCLUDED +#define LIGHT_DIRECT_PBR_INCLUDED #define FUNCTION_SURFACE_SHADING surfaceShading #define FUNCTION_DIFFUSE_LOBE diffuseLobe @@ -35,7 +35,7 @@ float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout } -void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { +void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); @@ -61,7 +61,7 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, ino vec3 lightColor = directionalLight.color; vec3 direction = -directionalLight.direction; - addRadiance( direction, lightColor, brdfData, color ); + FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); } @@ -78,7 +78,7 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, ino vec3 lightColor = pointLight.color; lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - addRadiance( direction, lightColor, brdfData, color ); + FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); } @@ -99,14 +99,14 @@ void addRadiance(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, ino vec3 lightColor = spotLight.color; lightColor *= spotEffect * decayEffect; - addRadiance( direction, lightColor, brdfData, color ); + FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); } #endif -void surfaceShading(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ +void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT @@ -154,4 +154,4 @@ void surfaceShading(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ } -// #endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index d704f168..969b33cb 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -1,6 +1,6 @@ // #ifndef LIGHT_INDIRECT_PBR_INCLUDED -// #define LIGHT_INDIRECT_PBR_INCLUDED 1 +// #define LIGHT_INDIRECT_PBR_INCLUDED #include "BRDF.glsl" #include "Light.glsl" From d86f3f4722a245698563ef6c0465dd3d4fb30cdf Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 14:29:30 +0800 Subject: [PATCH 58/89] "v0.0.0-experimental-shaderlab-pbr.3" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index aa80171d..fa2ac892 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index a2736cff..f9081230 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 299050cb..76b4385c 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 99df7327..f942ff7a 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 7cc2b91a..85a1b18b 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 5636f65d..da0c537a 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 2ca911bd..74d0d003 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index cf4af311..c7c13c6f 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 6be82da3..62c862a8 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index a4783bab..0b725118 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index a9e017ea..9c5fe51f 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index a95822ca..a6931174 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index a115a50f..ee296a9a 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index 9376c992..e7d521a6 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 55557fa2..c1b6f411 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index fd3ab01f..c1dec043 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index bfb07e53..fd3ed697 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index 785ab88e..d1e9ee8f 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 44b78ea0..dd2a7a40 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-pbr.2", + "version": "0.0.0-experimental-shaderlab-pbr.3", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 15adf57277d1558f0ed672560c245bd7222fd47d Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 15:30:27 +0800 Subject: [PATCH 59/89] refactor: move out macro --- .../shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl | 5 +++++ .../shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index cac55e47..8ead2168 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -1,3 +1,8 @@ +#define FUNCTION_SURFACE_SHADING surfaceShading +#define FUNCTION_DIFFUSE_LOBE diffuseLobe +#define FUNCTION_SPECULAR_LOBE specularLobe +#define FUNCTION_CLEAR_COAT_LOBE clearCoatLobe + #include "AttributesPBR.glsl" #include "VaryingsPBR.glsl" #include "Common.glsl" diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 08f9627f..591d79a6 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -2,11 +2,6 @@ #ifndef LIGHT_DIRECT_PBR_INCLUDED #define LIGHT_DIRECT_PBR_INCLUDED -#define FUNCTION_SURFACE_SHADING surfaceShading -#define FUNCTION_DIFFUSE_LOBE diffuseLobe -#define FUNCTION_SPECULAR_LOBE specularLobe -#define FUNCTION_CLEAR_COAT_LOBE clearCoatLobe - #include "BRDF.glsl" #include "Light.glsl" #include "Shadow.glsl" From b196c78debd4c5696d670e45736b44504b2cfcb6 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 15:31:10 +0800 Subject: [PATCH 60/89] "v0.0.0-experimental-shaderlab-pbr.4" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index fa2ac892..f48a999e 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index f9081230..93a813ae 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index 76b4385c..a4fcca52 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index f942ff7a..285e0c2b 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 85a1b18b..0aaa8739 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index da0c537a..1220879a 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 74d0d003..29b49f1c 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index c7c13c6f..eb6f5626 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 62c862a8..6a41ea41 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 0b725118..651221db 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index 9c5fe51f..efc621a3 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index a6931174..9ce58e05 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index ee296a9a..3cdd477c 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index e7d521a6..d2ceae73 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index c1b6f411..819f8ccb 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index c1dec043..82f019e5 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index fd3ed697..823ee8a1 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index d1e9ee8f..d90ec6d2 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index dd2a7a40..56791433 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-pbr.3", + "version": "0.0.0-experimental-shaderlab-pbr.4", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 7d87e38522022d2260f10c31d83edec27775be7e Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 15:52:35 +0800 Subject: [PATCH 61/89] refactor: override function macro --- .../src/shaders/shadingPBR/ForwardPassPBR.glsl | 5 ----- .../src/shaders/shadingPBR/LightDirectPBR.glsl | 13 +++++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 8ead2168..cac55e47 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -1,8 +1,3 @@ -#define FUNCTION_SURFACE_SHADING surfaceShading -#define FUNCTION_DIFFUSE_LOBE diffuseLobe -#define FUNCTION_SPECULAR_LOBE specularLobe -#define FUNCTION_CLEAR_COAT_LOBE clearCoatLobe - #include "AttributesPBR.glsl" #include "VaryingsPBR.glsl" #include "Common.glsl" diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 591d79a6..01a2b290 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -2,6 +2,19 @@ #ifndef LIGHT_DIRECT_PBR_INCLUDED #define LIGHT_DIRECT_PBR_INCLUDED +#ifndef FUNCTION_SURFACE_SHADING + #define FUNCTION_SURFACE_SHADING surfaceShading +#endif +#ifndef FUNCTION_DIFFUSE_LOBE + #define FUNCTION_DIFFUSE_LOBE diffuseLobe +#endif +#ifndef FUNCTION_SPECULAR_LOBE + #define FUNCTION_SPECULAR_LOBE specularLobe +#endif +#ifndef FUNCTION_CLEAR_COAT_LOBE + #define FUNCTION_CLEAR_COAT_LOBE clearCoatLobe +#endif + #include "BRDF.glsl" #include "Light.glsl" #include "Shadow.glsl" From 94d142f6ed02bc152552749583aafcedca0760d4 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 25 Jun 2024 15:53:24 +0800 Subject: [PATCH 62/89] "v0.0.0-experimental-shaderlab-pbr.5" --- packages/auxiliary-lines/package.json | 2 +- packages/controls/package.json | 2 +- packages/custom-gltf-parser/package.json | 2 +- packages/custom-material/package.json | 2 +- packages/draco/package.json | 2 +- packages/dynamic-bone/package.json | 2 +- packages/framebuffer-picker/package.json | 2 +- packages/galacean-engine-toolkit/package.json | 2 +- packages/geometry-sketch/package.json | 2 +- packages/gizmo/package.json | 2 +- packages/input-logger/package.json | 2 +- packages/lines/package.json | 2 +- packages/navigation-gizmo/package.json | 2 +- packages/outline/package.json | 2 +- packages/shaderlab/package.json | 2 +- packages/skeleton-viewer/package.json | 2 +- packages/stats/package.json | 2 +- packages/tween/package.json | 2 +- packages/way-point/package.json | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/auxiliary-lines/package.json b/packages/auxiliary-lines/package.json index f48a999e..d7c034dd 100644 --- a/packages/auxiliary-lines/package.json +++ b/packages/auxiliary-lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-auxiliary-lines", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/controls/package.json b/packages/controls/package.json index 93a813ae..f6b17d65 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-controls", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "publishConfig": { "access": "public", diff --git a/packages/custom-gltf-parser/package.json b/packages/custom-gltf-parser/package.json index a4fcca52..dfd0dcc3 100755 --- a/packages/custom-gltf-parser/package.json +++ b/packages/custom-gltf-parser/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-gltf-parser", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/custom-material/package.json b/packages/custom-material/package.json index 285e0c2b..e103f139 100644 --- a/packages/custom-material/package.json +++ b/packages/custom-material/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-custom-material", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc", diff --git a/packages/draco/package.json b/packages/draco/package.json index 0aaa8739..72835edd 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-draco", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/dynamic-bone/package.json b/packages/dynamic-bone/package.json index 1220879a..ee2106dd 100644 --- a/packages/dynamic-bone/package.json +++ b/packages/dynamic-bone/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-dynamic-bone", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index 29b49f1c..fe2d4047 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-framebuffer-picker", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/galacean-engine-toolkit/package.json b/packages/galacean-engine-toolkit/package.json index eb6f5626..6835bf2c 100644 --- a/packages/galacean-engine-toolkit/package.json +++ b/packages/galacean-engine-toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/geometry-sketch/package.json b/packages/geometry-sketch/package.json index 6a41ea41..d4a8ef18 100644 --- a/packages/geometry-sketch/package.json +++ b/packages/geometry-sketch/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-geometry-sketch", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/gizmo/package.json b/packages/gizmo/package.json index 651221db..daae589d 100644 --- a/packages/gizmo/package.json +++ b/packages/gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/input-logger/package.json b/packages/input-logger/package.json index efc621a3..4d7f3b20 100644 --- a/packages/input-logger/package.json +++ b/packages/input-logger/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-input-logger", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/lines/package.json b/packages/lines/package.json index 9ce58e05..a65eb8cf 100644 --- a/packages/lines/package.json +++ b/packages/lines/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-lines", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/navigation-gizmo/package.json b/packages/navigation-gizmo/package.json index 3cdd477c..9b4f0dab 100644 --- a/packages/navigation-gizmo/package.json +++ b/packages/navigation-gizmo/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-navigation-gizmo", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" diff --git a/packages/outline/package.json b/packages/outline/package.json index d2ceae73..01b011f4 100755 --- a/packages/outline/package.json +++ b/packages/outline/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-outline", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "module": "dist/es/index.js", "main": "dist/commonjs/browser.js", diff --git a/packages/shaderlab/package.json b/packages/shaderlab/package.json index 819f8ccb..09d26050 100755 --- a/packages/shaderlab/package.json +++ b/packages/shaderlab/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-shader-lab", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/skeleton-viewer/package.json b/packages/skeleton-viewer/package.json index 82f019e5..a6e76df7 100755 --- a/packages/skeleton-viewer/package.json +++ b/packages/skeleton-viewer/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-skeleton-viewer", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/stats/package.json b/packages/stats/package.json index 823ee8a1..0b8ebb11 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-stats", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/tween/package.json b/packages/tween/package.json index d90ec6d2..75af18b4 100644 --- a/packages/tween/package.json +++ b/packages/tween/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-tween", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "license": "MIT", "scripts": { "b:types": "tsc" diff --git a/packages/way-point/package.json b/packages/way-point/package.json index 56791433..861cd34e 100755 --- a/packages/way-point/package.json +++ b/packages/way-point/package.json @@ -1,6 +1,6 @@ { "name": "@galacean/engine-toolkit-way-point", - "version": "0.0.0-experimental-shaderlab-pbr.4", + "version": "0.0.0-experimental-shaderlab-pbr.5", "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org" From 178ef1d2d776cc6cb4ad422d8a13d66f549c543c Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 27 Jun 2024 17:13:51 +0800 Subject: [PATCH 63/89] refactor: format change --- packages/shaderlab/src/shaders/Light.glsl | 8 ++++---- packages/shaderlab/src/shaders/PBR.gs | 6 +++--- .../shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl | 4 ++-- .../src/shaders/shadingPBR/MaterialInputPBR.glsl | 4 ++-- .../shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/shaderlab/src/shaders/Light.glsl b/packages/shaderlab/src/shaders/Light.glsl index 2d2284ce..8f52e514 100644 --- a/packages/shaderlab/src/shaders/Light.glsl +++ b/packages/shaderlab/src/shaders/Light.glsl @@ -28,7 +28,7 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ struct DirectLight { vec3 color; vec3 direction; - } + }; ivec2 scene_DirectLightCullingMask[SCENE_DIRECT_LIGHT_COUNT]; vec3 scene_DirectLightColor[SCENE_DIRECT_LIGHT_COUNT]; @@ -52,7 +52,7 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ vec3 color; vec3 position; float distance; - } + }; ivec2 scene_PointLightCullingMask[ SCENE_POINT_LIGHT_COUNT ]; vec3 scene_PointLightColor[ SCENE_POINT_LIGHT_COUNT ]; @@ -81,7 +81,7 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ float distance; float angleCos; float penumbraCos; - } + }; ivec2 scene_SpotLightCullingMask[ SCENE_SPOT_LIGHT_COUNT ]; vec3 scene_SpotLightColor[ SCENE_SPOT_LIGHT_COUNT ]; @@ -112,7 +112,7 @@ struct EnvMapLight { float mipMapLevel; float diffuseIntensity; float specularIntensity; -} +}; EnvMapLight scene_EnvMapLight; diff --git a/packages/shaderlab/src/shaders/PBR.gs b/packages/shaderlab/src/shaders/PBR.gs index ca5988f7..db1eb381 100644 --- a/packages/shaderlab/src/shaders/PBR.gs +++ b/packages/shaderlab/src/shaders/PBR.gs @@ -78,11 +78,11 @@ Shader "PBR.gs" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW - - VertexShader = PBRVertex; - FragmentShader = PBRFragment; #include "ForwardPassPBR.glsl" + + VertexShader = PBRVertex; + FragmentShader = PBRFragment; } } diff --git a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl index a1dd59e0..b1ae307c 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl @@ -68,7 +68,7 @@ struct Attributes{ vec4 TANGENT; #endif #endif -} +}; struct Temp_Attributes{ @@ -140,4 +140,4 @@ struct Temp_Attributes{ vec4 TANGENT; #endif #endif -} +}; diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index b69ee8b1..8600eb88 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -23,7 +23,7 @@ struct SurfaceData{ #endif vec3 viewDir; -} +}; struct BRDFData{ // common @@ -58,7 +58,7 @@ struct BRDFData{ vec3 clearCoatNormal; float clearCoatDotNV; #endif -} +}; #define MIN_PERCEPTUAL_ROUGHNESS 0.045 #define MIN_ROUGHNESS 0.002025 diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index d064f0db..6adf0fc6 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -31,7 +31,7 @@ struct Varyings{ #ifdef MATERIAL_NEED_WORLD_POS vec3 v_pos; #endif -} +}; struct Temp_Varyings{ @@ -67,4 +67,4 @@ struct Temp_Varyings{ #ifdef MATERIAL_NEED_WORLD_POS vec3 v_pos; #endif -} +}; From b1874b223612e8603530bbba09dc2d2cabe92810 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 27 Jun 2024 17:30:45 +0800 Subject: [PATCH 64/89] refactor: delete todo --- packages/shaderlab/src/shaders/Fog.glsl | 2 +- packages/shaderlab/src/shaders/Normal.glsl | 4 +- packages/shaderlab/src/shaders/Shadow.glsl | 2 +- packages/shaderlab/src/shaders/Vertex.glsl | 2 +- packages/shaderlab/src/shaders/index.ts | 4 - .../src/shaders/shadingPBR/AttributesPBR.glsl | 74 +------------------ .../shaders/shadingPBR/ForwardPassPBR.glsl | 23 ++---- .../shaders/shadingPBR/LightDirectPBR.glsl | 2 +- .../shaders/shadingPBR/LightIndirectPBR.glsl | 4 +- .../shaders/shadingPBR/MaterialInputPBR.glsl | 8 +- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 38 +--------- .../src/shaders/temp/transformAttributes.glsl | 69 ----------------- .../src/shaders/temp/transformVaryings.glsl | 33 --------- 13 files changed, 20 insertions(+), 245 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/temp/transformAttributes.glsl delete mode 100644 packages/shaderlab/src/shaders/temp/transformVaryings.glsl diff --git a/packages/shaderlab/src/shaders/Fog.glsl b/packages/shaderlab/src/shaders/Fog.glsl index 444e4648..d6417a9a 100644 --- a/packages/shaderlab/src/shaders/Fog.glsl +++ b/packages/shaderlab/src/shaders/Fog.glsl @@ -27,4 +27,4 @@ #endif -#endif +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index fe7d4383..83bc646e 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -4,7 +4,7 @@ // gl_FrontFacing has random value on Adreno GPUs // the Adreno bug is only when gl_FrontFacing is inside a function // https://bugs.chromium.org/p/chromium/issues/detail?id=1154842 -vec3 getNormal(Temp_Varyings v, bool isFrontFacing){ +vec3 getNormal(Varyings v, bool isFrontFacing){ #ifdef RENDERER_HAS_NORMAL vec3 normal = normalize(v.v_normal); #elif defined(HAS_DERIVATIVES) @@ -27,7 +27,7 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt return normal; } -mat3 getTBN(Temp_Varyings v, bool isFrontFacing){ +mat3 getTBN(Varyings v, bool isFrontFacing){ #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) mat3 tbn = mat3(v.v_tangent, v.v_bitangent, v.v_normal); #else diff --git a/packages/shaderlab/src/shaders/Shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl index c49d29d5..174a696a 100644 --- a/packages/shaderlab/src/shaders/Shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -151,7 +151,7 @@ } - float sampleShadowMap(Temp_Varyings v) { + float sampleShadowMap(Varyings v) { #if SCENE_SHADOW_CASCADED_COUNT == 1 vec3 shadowCoord = v.v_shadowCoord; #else diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 84cbf769..14ba049f 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -9,7 +9,7 @@ vec4 material_TilingOffset; -void initVertex(){ +void initVertex(Attributes attr, out Varyings v){ vec4 position = vec4( attr.POSITION , 1.0 ); #ifndef MATERIAL_OMIT_NORMAL diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index 8dc3f96e..f1730f97 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -10,8 +10,6 @@ import Skin from "./Skin.glsl"; import Transform from "./Transform.glsl"; import Vertex from "./Vertex.glsl"; import shadingPBR from "./shadingPBR"; -import temp_transformAttributes from "./temp/transformAttributes.glsl"; -import temp_transformVaryings from "./temp/transformVaryings.glsl"; interface IShaderFragment { includeKey: string; @@ -28,8 +26,6 @@ const fragmentList: IShaderFragment[] = [ { source: Shadow, includeKey: "Shadow.glsl" }, { source: Transform, includeKey: "Transform.glsl" }, { source: Vertex, includeKey: "Vertex.glsl" }, - { source: temp_transformAttributes, includeKey: "temp_transformAttributes.glsl" }, - { source: temp_transformVaryings, includeKey: "temp_transformVaryings.glsl" }, { source: Skin, includeKey: "Skin.glsl" }, ...shadingPBR diff --git a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl index b1ae307c..a552a331 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl @@ -68,76 +68,4 @@ struct Attributes{ vec4 TANGENT; #endif #endif -}; - - -struct Temp_Attributes{ - #ifdef RENDERER_HAS_BLENDSHAPE - #ifndef RENDERER_BLENDSHAPE_USE_TEXTURE - vec3 POSITION_BS0; - vec3 POSITION_BS1; - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - vec3 NORMAL_BS0; - vec3 NORMAL_BS1; - vec3 TANGENT_BS0; - vec3 TANGENT_BS1; - #else - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - vec3 POSITION_BS2; - vec3 POSITION_BS3; - - #ifdef RENDERER_BLENDSHAPE_HAS_NORMAL - vec3 NORMAL_BS0; - vec3 NORMAL_BS1; - vec3 NORMAL_BS2; - vec3 NORMAL_BS3; - #endif - - #ifdef RENDERER_BLENDSHAPE_HAS_TANGENT - vec3 TANGENT_BS0; - vec3 TANGENT_BS1; - vec3 TANGENT_BS2; - vec3 TANGENT_BS3; - #endif - - #else - vec3 POSITION_BS2; - vec3 POSITION_BS3; - vec3 POSITION_BS4; - vec3 POSITION_BS5; - vec3 POSITION_BS6; - vec3 POSITION_BS7; - #endif - #endif - #endif - #endif - - vec3 POSITION; - - #ifdef RENDERER_HAS_UV - vec2 TEXCOORD_0; - #endif - - #ifdef RENDERER_HAS_UV1 - vec2 TEXCOORD_1; - #endif - - #ifdef RENDERER_HAS_SKIN - vec4 JOINTS_0; - vec4 WEIGHTS_0; - #endif - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 COLOR_0; - #endif - - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 NORMAL; - #endif - - #ifdef RENDERER_HAS_TANGENT - vec4 TANGENT; - #endif - #endif -}; +}; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index cac55e47..e5450291 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -11,14 +11,7 @@ Varyings PBRVertex(Attributes attr) { Varyings v; - // @todo: delete - Temp_Attributes temp_attributes; - Temp_Varyings temp_varyings; - #include "temp_transformAttributes.glsl" - #include "temp_transformVaryings.glsl" - - // @todo: use initVertex(attr, v); - initVertex(); + initVertex(attr, v); return v; } @@ -27,20 +20,16 @@ void PBRFragment(Varyings v) { SurfaceData surfaceData; BRDFData brdfData; - // @todo: delete - Temp_Varyings temp_varyings; - #include "temp_transformVaryings.glsl" - - initSurfaceData(temp_varyings, surfaceData, gl_FrontFacing); - // Can modify surfaceData here. - initBRDFData(temp_varyings, surfaceData, brdfData, gl_FrontFacing); + initSurfaceData(v, surfaceData, gl_FrontFacing); + // // Can modify surfaceData here. + initBRDFData(v, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); // Direct Light - evaluateDirectRadiance(temp_varyings, brdfData, color.rgb); + evaluateDirectRadiance(v, brdfData, color.rgb); // IBL - evaluateIBL(temp_varyings, brdfData, color.rgb); + evaluateIBL(v, brdfData, color.rgb); // Emissive color.rgb += surfaceData.emissiveColor; diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 01a2b290..015b5ef2 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -114,7 +114,7 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, #endif -void evaluateDirectRadiance(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ +void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ float shadowAttenuation = 1.0; #ifdef SCENE_DIRECT_LIGHT_COUNT diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index 969b33cb..3c333cba 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -127,7 +127,7 @@ void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAtte } -float evaluateDiffuseAO(Temp_Varyings v){ +float evaluateDiffuseAO(Varyings v){ float diffuseAO = 1.0; #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE @@ -153,7 +153,7 @@ float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ return specularAO; } -void evaluateIBL(Temp_Varyings v, BRDFData brdfData, inout vec3 color){ +void evaluateIBL(Varyings v, BRDFData brdfData, inout vec3 color){ vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); float diffuseAO = evaluateDiffuseAO(v); diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index 8600eb88..13daa9d4 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -154,7 +154,7 @@ float getAARoughnessFactor(vec3 normal) { #endif -void initSurfaceData(Temp_Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ +void initSurfaceData(Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ // common vec4 baseColor = material_BaseColor; float metallic = material_Metal; @@ -275,7 +275,7 @@ void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.roughness = max(roughness, getAARoughnessFactor(brdfData.normal)); } -void initClearCoatBRDFData(Temp_Varyings v, inout BRDFData brdfData, bool isFrontFacing){ +void initClearCoatBRDFData(Varyings v, inout BRDFData brdfData, bool isFrontFacing){ #ifdef MATERIAL_ENABLE_CLEAR_COAT #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); @@ -302,7 +302,7 @@ void initClearCoatBRDFData(Temp_Varyings v, inout BRDFData brdfData, bool isFron } -void initAnisotropyBRDFData(Temp_Varyings v, inout BRDFData brdfData){ +void initAnisotropyBRDFData(Varyings v, inout BRDFData brdfData){ #ifdef MATERIAL_ENABLE_ANISOTROPY float anisotropy = material_AnisotropyInfo.z; vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); @@ -321,7 +321,7 @@ void initAnisotropyBRDFData(Temp_Varyings v, inout BRDFData brdfData){ } -void initBRDFData(Temp_Varyings v, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ +void initBRDFData(Varyings v, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ initGeometryData(surfaceData, brdfData); initCommonBRDFData(surfaceData, brdfData); initClearCoatBRDFData(v, brdfData, isFrontFacing); diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index 6adf0fc6..9c712b3b 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -31,40 +31,4 @@ struct Varyings{ #ifdef MATERIAL_NEED_WORLD_POS vec3 v_pos; #endif -}; - - -struct Temp_Varyings{ - #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 v_color; - #endif - - #if SCENE_FOG_MODE != 0 - vec3 v_positionVS; - #endif - - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 v_normal; - #ifdef RENDERER_HAS_TANGENT - vec3 v_tangent; - vec3 v_bitangent; - #endif - #endif - #endif - - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 - vec3 v_shadowCoord; - #endif - #endif - - vec2 v_uv; - - #ifdef RENDERER_HAS_UV1 - vec2 v_uv1; - #endif - #ifdef MATERIAL_NEED_WORLD_POS - vec3 v_pos; - #endif -}; +}; \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/temp/transformAttributes.glsl b/packages/shaderlab/src/shaders/temp/transformAttributes.glsl deleted file mode 100644 index b994f4da..00000000 --- a/packages/shaderlab/src/shaders/temp/transformAttributes.glsl +++ /dev/null @@ -1,69 +0,0 @@ -temp_attributes.POSITION = attr.POSITION; - - #ifdef RENDERER_HAS_BLENDSHAPE - #ifndef RENDERER_BLENDSHAPE_USE_TEXTURE - temp_attributes.POSITION_BS0 = attr.POSITION_BS0; - temp_attributes.POSITION_BS1 = attr.POSITION_BS1; - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - temp_attributes.NORMAL_BS0 = attr.NORMAL_BS0; - temp_attributes.NORMAL_BS1 = attr.NORMAL_BS1; - temp_attributes.TANGENT_BS0 = attr.TANGENT_BS0; - temp_attributes.TANGENT_BS1 = attr.TANGENT_BS1; - #else - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - temp_attributes.POSITION_BS2 = attr.POSITION_BS2; - temp_attributes.POSITION_BS3 = attr.POSITION_BS3; - - #ifdef RENDERER_BLENDSHAPE_HAS_NORMAL - temp_attributes.NORMAL_BS0 = attr.NORMAL_BS0; - temp_attributes.NORMAL_BS1 = attr.NORMAL_BS1; - temp_attributes.NORMAL_BS2 = attr.NORMAL_BS2; - temp_attributes.NORMAL_BS3 = attr.NORMAL_BS3; - #endif - - #ifdef RENDERER_BLENDSHAPE_HAS_TANGENT - temp_attributes.TANGENT_BS0 = attr.TANGENT_BS0; - temp_attributes.TANGENT_BS1 = attr.TANGENT_BS1; - temp_attributes.TANGENT_BS2 = attr.TANGENT_BS2; - temp_attributes.TANGENT_BS3 = attr.TANGENT_BS3; - #endif - - #else - temp_attributes.POSITION_BS2 = attr.POSITION_BS2; - temp_attributes.POSITION_BS3 = attr.POSITION_BS3; - temp_attributes.POSITION_BS4 = attr.POSITION_BS4; - temp_attributes.POSITION_BS5 = attr.POSITION_BS5; - temp_attributes.POSITION_BS6 = attr.POSITION_BS6; - temp_attributes.POSITION_BS7 = attr.POSITION_BS7; - #endif - #endif - #endif - #endif - - - #ifdef RENDERER_HAS_UV - temp_attributes.TEXCOORD_0 = attr.TEXCOORD_0; - #endif - - #ifdef RENDERER_HAS_UV1 - temp_attributes.TEXCOORD_1 = attr.TEXCOORD_1; - #endif - - #ifdef RENDERER_HAS_SKIN - temp_attributes.JOINTS_0 = attr.JOINTS_0; - temp_attributes.WEIGHTS_0 = attr.WEIGHTS_0; - #endif - - #ifdef RENDERER_ENABLE_VERTEXCOLOR - temp_attributes.COLOR_0 = attr.COLOR_0; - #endif - - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - temp_attributes.NORMAL = attr.NORMAL; - #endif - - #ifdef RENDERER_HAS_TANGENT - temp_attributes.TANGENT = attr.TANGENT; - #endif - #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/temp/transformVaryings.glsl b/packages/shaderlab/src/shaders/temp/transformVaryings.glsl deleted file mode 100644 index b0389d51..00000000 --- a/packages/shaderlab/src/shaders/temp/transformVaryings.glsl +++ /dev/null @@ -1,33 +0,0 @@ - -#ifdef RENDERER_ENABLE_VERTEXCOLOR - temp_varyings.v_color = v.v_color; -#endif - -#if SCENE_FOG_MODE != 0 - temp_varyings.v_positionVS = v.v_positionVS; -#endif - -#ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - temp_varyings.v_normal = v.v_normal; - #ifdef RENDERER_HAS_TANGENT - temp_varyings.v_tangent = v.v_tangent; - temp_varyings.v_bitangent = v.v_bitangent; - #endif - #endif -#endif - -#ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 - temp_varyings.v_shadowCoord = v.v_shadowCoord; - #endif -#endif - -temp_varyings.v_uv = v.v_uv; - -#ifdef RENDERER_HAS_UV1 - temp_varyings.v_uv1 = v.v_uv1; -#endif -#ifdef MATERIAL_NEED_WORLD_POS - temp_varyings.v_pos = v.v_pos; -#endif \ No newline at end of file From f358db77e1b540c2433d7fd189058a2ba33c2a24 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 27 Jun 2024 18:20:39 +0800 Subject: [PATCH 65/89] fix: clamp roughness --- packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl | 2 +- packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index e5450291..03112f21 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -21,7 +21,7 @@ void PBRFragment(Varyings v) { BRDFData brdfData; initSurfaceData(v, surfaceData, gl_FrontFacing); - // // Can modify surfaceData here. + // Can modify surfaceData here. initBRDFData(v, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index 13daa9d4..e777c166 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -133,7 +133,7 @@ float getAARoughnessFactor(vec3 normal) { // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" #ifdef HAS_DERIVATIVES vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ); + return min(MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ), 1.0); #else return MIN_PERCEPTUAL_ROUGHNESS; #endif From 0fadeeab2b26f0ca645988646bb3d88301a9bff4 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 27 Jun 2024 18:21:40 +0800 Subject: [PATCH 66/89] refactor: delete override function cause of shaderLab --- packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index d5131a7e..ff551c9d 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -2,9 +2,9 @@ #ifndef BRDF_INCLUDED #define BRDF_INCLUDED 1 -float F_Schlick(float f0, float dotLH) { - return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); -} +// float F_Schlick(float f0, float dotLH) { +// return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); +// } vec3 F_Schlick(vec3 specularColor, float dotLH ) { From 8791680221445d670cca18504c2eae3a772fb50f Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 27 Jun 2024 18:25:05 +0800 Subject: [PATCH 67/89] refactor: support repeat macro now cause of shaderlab --- packages/shaderlab/src/shaders/BlendShape.glsl | 2 +- packages/shaderlab/src/shaders/Common.glsl | 2 +- packages/shaderlab/src/shaders/Fog.glsl | 2 +- packages/shaderlab/src/shaders/Light.glsl | 2 +- packages/shaderlab/src/shaders/Normal.glsl | 4 ++-- packages/shaderlab/src/shaders/Shadow.glsl | 2 +- packages/shaderlab/src/shaders/ShadowSampleTent.glsl | 2 +- packages/shaderlab/src/shaders/Skin.glsl | 2 +- packages/shaderlab/src/shaders/Transform.glsl | 2 +- packages/shaderlab/src/shaders/Vertex.glsl | 6 +++--- packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl | 2 +- .../shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl | 6 +++--- .../shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl | 2 +- 13 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/shaderlab/src/shaders/BlendShape.glsl b/packages/shaderlab/src/shaders/BlendShape.glsl index 022d08a5..81950d0c 100644 --- a/packages/shaderlab/src/shaders/BlendShape.glsl +++ b/packages/shaderlab/src/shaders/BlendShape.glsl @@ -1,5 +1,5 @@ #ifndef BLENDSHAPE_INCLUDED -#define BLENDSHAPE_INCLUDED 1 +#define BLENDSHAPE_INCLUDED #ifdef RENDERER_HAS_BLENDSHAPE #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE diff --git a/packages/shaderlab/src/shaders/Common.glsl b/packages/shaderlab/src/shaders/Common.glsl index 0889d0ce..2f89d06d 100644 --- a/packages/shaderlab/src/shaders/Common.glsl +++ b/packages/shaderlab/src/shaders/Common.glsl @@ -1,5 +1,5 @@ #ifndef COMMON_INCLUDED -#define COMMON_INCLUDED 1 +#define COMMON_INCLUDED #define PI 3.14159265359 #define RECIPROCAL_PI 0.31830988618 diff --git a/packages/shaderlab/src/shaders/Fog.glsl b/packages/shaderlab/src/shaders/Fog.glsl index d6417a9a..a9d920c9 100644 --- a/packages/shaderlab/src/shaders/Fog.glsl +++ b/packages/shaderlab/src/shaders/Fog.glsl @@ -1,5 +1,5 @@ #ifndef FOG_INCLUDED -#define FOG_INCLUDED 1 +#define FOG_INCLUDED #if SCENE_FOG_MODE != 0 vec4 scene_FogColor; diff --git a/packages/shaderlab/src/shaders/Light.glsl b/packages/shaderlab/src/shaders/Light.glsl index 8f52e514..2c4eeefc 100644 --- a/packages/shaderlab/src/shaders/Light.glsl +++ b/packages/shaderlab/src/shaders/Light.glsl @@ -1,5 +1,5 @@ #ifndef LIGHT_INCLUDED -#define LIGHT_INCLUDED 1 +#define LIGHT_INCLUDED ivec4 renderer_Layer; diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index 83bc646e..bc182278 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -1,5 +1,5 @@ -#ifndef NORMAL_GET_INCLUDED -#define NORMAL_GET_INCLUDED 1 +#ifndef NORMAL_INCLUDED +#define NORMAL_INCLUDED // gl_FrontFacing has random value on Adreno GPUs // the Adreno bug is only when gl_FrontFacing is inside a function diff --git a/packages/shaderlab/src/shaders/Shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl index 174a696a..73891a69 100644 --- a/packages/shaderlab/src/shaders/Shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -1,5 +1,5 @@ #ifndef SHADOW_INCLUDED -#define SHADOW_INCLUDED 1 +#define SHADOW_INCLUDED #if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) #define SCENE_IS_CALCULATE_SHADOWS diff --git a/packages/shaderlab/src/shaders/ShadowSampleTent.glsl b/packages/shaderlab/src/shaders/ShadowSampleTent.glsl index 05272b78..7fc570c7 100644 --- a/packages/shaderlab/src/shaders/ShadowSampleTent.glsl +++ b/packages/shaderlab/src/shaders/ShadowSampleTent.glsl @@ -1,5 +1,5 @@ #ifndef SHADOW_SAMPLE_TENT_INCLUDED -#define SHADOW_SAMPLE_TENT_INCLUDED 1 +#define SHADOW_SAMPLE_TENT_INCLUDED // ------------------------------------------------------------------ // PCF Filtering Tent Functions diff --git a/packages/shaderlab/src/shaders/Skin.glsl b/packages/shaderlab/src/shaders/Skin.glsl index defdcae6..35bef11a 100644 --- a/packages/shaderlab/src/shaders/Skin.glsl +++ b/packages/shaderlab/src/shaders/Skin.glsl @@ -1,5 +1,5 @@ #ifndef SKIN_INCLUDED -#define SKIN_INCLUDED 1 +#define SKIN_INCLUDED #ifdef RENDERER_HAS_SKIN diff --git a/packages/shaderlab/src/shaders/Transform.glsl b/packages/shaderlab/src/shaders/Transform.glsl index e26ed34c..6ae50a24 100644 --- a/packages/shaderlab/src/shaders/Transform.glsl +++ b/packages/shaderlab/src/shaders/Transform.glsl @@ -1,5 +1,5 @@ #ifndef TRANSFORM_INCLUDED -#define TRANSFORM_INCLUDED 1 +#define TRANSFORM_INCLUDED mat4 renderer_LocalMat; mat4 renderer_ModelMat; diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 14ba049f..0bfe4411 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -1,5 +1,5 @@ -// #ifndef VERTEX_INCLUDE -// #define VERTEX_INCLUDE 1 +#ifndef VERTEX_INCLUDE +#define VERTEX_INCLUDE #include "Skin.glsl" #include "BlendShape.glsl" @@ -195,4 +195,4 @@ void initVertex(Attributes attr, out Varyings v){ gl_Position = renderer_MVPMat * position; } -// #endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index ff551c9d..a11270ca 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -1,6 +1,6 @@ #ifndef BRDF_INCLUDED -#define BRDF_INCLUDED 1 +#define BRDF_INCLUDED // float F_Schlick(float f0, float dotLH) { // return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index 3c333cba..221e6119 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -1,6 +1,6 @@ -// #ifndef LIGHT_INDIRECT_PBR_INCLUDED -// #define LIGHT_INDIRECT_PBR_INCLUDED +#ifndef LIGHT_INDIRECT_PBR_INCLUDED +#define LIGHT_INDIRECT_PBR_INCLUDED #include "BRDF.glsl" #include "Light.glsl" @@ -172,4 +172,4 @@ void evaluateIBL(Varyings v, BRDFData brdfData, inout vec3 color){ } -// #endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index e777c166..8a42eb21 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -1,5 +1,5 @@ #ifndef MATERIAL_INPUT_PBR_INCLUDED -#define MATERIAL_INPUT_PBR_INCLUDED 1 +#define MATERIAL_INPUT_PBR_INCLUDED #include "Normal.glsl" From 460bc0369b38aa028f7ebedda2ad3523f0afc3e4 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 27 Jun 2024 18:49:15 +0800 Subject: [PATCH 68/89] fix: attenuation error --- packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 015b5ef2..9e6ed568 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -53,7 +53,7 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, // ClearCoat Lobe float attenuation = FUNCTION_CLEAR_COAT_LOBE(incidentDirection, lightColor, brdfData, specularColor); - vec3 attenuationIrradiance = irradiance * irradiance; + vec3 attenuationIrradiance = attenuation * irradiance; // Diffuse Lobe FUNCTION_DIFFUSE_LOBE(brdfData, attenuationIrradiance, diffuseColor); // Specular Lobe @@ -87,7 +87,6 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); - } #endif From 14b03d1e4c728fc10772bd65d5f0e16baaee7794 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 11 Jul 2024 17:27:27 +0800 Subject: [PATCH 69/89] fix: clamp specular aa --- .../src/shaders/shadingPBR/MaterialInputPBR.glsl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index 8a42eb21..52875f2f 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -133,9 +133,9 @@ float getAARoughnessFactor(vec3 normal) { // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" #ifdef HAS_DERIVATIVES vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return min(MIN_PERCEPTUAL_ROUGHNESS + max( max(dxy.x, dxy.y), dxy.z ), 1.0); + return max( max(dxy.x, dxy.y), dxy.z ); #else - return MIN_PERCEPTUAL_ROUGHNESS; + return 0.0; #endif } @@ -272,7 +272,7 @@ void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.specularColor = specularColor; #endif - brdfData.roughness = max(roughness, getAARoughnessFactor(brdfData.normal)); + brdfData.roughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(roughness + getAARoughnessFactor(brdfData.normal), 1.0)); } void initClearCoatBRDFData(Varyings v, inout BRDFData brdfData, bool isFrontFacing){ @@ -296,7 +296,8 @@ void initClearCoatBRDFData(Varyings v, inout BRDFData brdfData, bool isFrontFaci #endif brdfData.clearCoat = saturate( brdfData.clearCoat ); - brdfData.clearCoatRoughness = max(brdfData.clearCoatRoughness, getAARoughnessFactor(brdfData.clearCoatNormal)); + + brdfData.clearCoatRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(brdfData.clearCoatRoughness + getAARoughnessFactor(brdfData.clearCoatNormal), 1.0)); #endif From 99b02e7aec61ebc30346e5e271f783adab7a242f Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 11:57:04 +0800 Subject: [PATCH 70/89] ci: add registerShader function --- packages/shaderlab/src/GSLPBRMaterial.ts | 10 ---------- packages/shaderlab/src/index.ts | 7 +++---- packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl | 6 +++--- .../src/shaders/shadingPBR/LightIndirectPBR.glsl | 3 ++- 4 files changed, 8 insertions(+), 18 deletions(-) delete mode 100644 packages/shaderlab/src/GSLPBRMaterial.ts diff --git a/packages/shaderlab/src/GSLPBRMaterial.ts b/packages/shaderlab/src/GSLPBRMaterial.ts deleted file mode 100644 index e4df9039..00000000 --- a/packages/shaderlab/src/GSLPBRMaterial.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Engine, PBRMaterial, Shader } from "@galacean/engine"; - -export class GSLPBRMaterial extends PBRMaterial { - constructor(engine: Engine) { - const shader = Shader.find("PBR.gs"); - super(engine); - - this.shader = shader; - } -} diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index 717ac7ef..8f138238 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1,4 +1,3 @@ -export { GSLPBRMaterial } from "./GSLPBRMaterial"; import { Shader, ShaderFactory } from "@galacean/engine"; import { PBRSource, fragmentList } from "./shaders"; @@ -11,9 +10,9 @@ export function registerIncludes() { ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source); } - Shader.create(PBRSource); - registered = true; } -export { PBRSource }; +export function registerShader() { + Shader.create(PBRSource); +} diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index a11270ca..949f3f7a 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -2,9 +2,9 @@ #ifndef BRDF_INCLUDED #define BRDF_INCLUDED -// float F_Schlick(float f0, float dotLH) { -// return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); -// } +float F_Schlick(float f0, float dotLH) { + return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); +} vec3 F_Schlick(vec3 specularColor, float dotLH ) { diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index 221e6119..92907513 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -41,7 +41,8 @@ vec3 envBRDFApprox(vec3 specularColor, float roughness, float dotNV ) { vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; - return specularColor * AB.x + AB.y; + // AB may less than 0 at high roughness, ref: https://github.com/galacean/engine/pull/2173 + return max(specularColor * AB.x + AB.y, 0.0); } From 0d28e973abcf0edb95f8c8ba4e5518590188005d Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 14:02:56 +0800 Subject: [PATCH 71/89] refactor: include editorProps --- packages/shaderlab/src/index.ts | 11 ++- packages/shaderlab/src/shaders/PBR.gs | 79 +------------------ .../src/shaders/shadingPBR/AttributesPBR.glsl | 8 +- .../src/shaders/shadingPBR/EditorProps.glsl | 71 +++++++++++++++++ .../shaders/shadingPBR/ForwardPassPBR.glsl | 8 +- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 8 +- .../shaderlab/src/shaders/shadingPBR/index.ts | 4 +- 7 files changed, 107 insertions(+), 82 deletions(-) create mode 100644 packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl diff --git a/packages/shaderlab/src/index.ts b/packages/shaderlab/src/index.ts index 8f138238..df47aed1 100644 --- a/packages/shaderlab/src/index.ts +++ b/packages/shaderlab/src/index.ts @@ -1,18 +1,23 @@ import { Shader, ShaderFactory } from "@galacean/engine"; import { PBRSource, fragmentList } from "./shaders"; -let registered = false; +let includeRegistered = false; +let shaderRegistered = false; export function registerIncludes() { - if (registered) return; + if (includeRegistered) return; for (const sourceFragment of fragmentList) { ShaderFactory.registerInclude(sourceFragment.includeKey, sourceFragment.source); } - registered = true; + includeRegistered = true; } export function registerShader() { + if (shaderRegistered) return; + Shader.create(PBRSource); + + shaderRegistered = true; } diff --git a/packages/shaderlab/src/shaders/PBR.gs b/packages/shaderlab/src/shaders/PBR.gs index db1eb381..04a9cd5c 100644 --- a/packages/shaderlab/src/shaders/PBR.gs +++ b/packages/shaderlab/src/shaders/PBR.gs @@ -1,89 +1,18 @@ Shader "PBR.gs" { - EditorProperties { - Header("Base"){ - material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; - material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); - material_BaseTexture("BaseTexture", Texture2D); - } - - Header("Metal Roughness") { - material_Metal( "Metal", Range(0,1,0.01) ) = 1; - material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; - material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); - } - - Header("Anisotropy") { - material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); - material_AnisotropyTexture("AnisotropyTexture", Texture2D); - } - - Header("Normal") { - material_NormalTexture("NormalTexture", Texture2D); - material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; - } + #include "EditorProps.glsl" - Header("Emissive") { - material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); - material_EmissiveTexture("EmissiveTexture", Texture2D); - } - - Header("Occlusion") { - material_OcclusionTexture("OcclusionTexture", Texture2D); - material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; - material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; - } - - Header("Clear Coat") { - material_ClearCoat("ClearCoat", Range(0, 1, 0.01)) = 0; - material_ClearCoatTexture("ClearCoatTexture", Texture2D); - material_ClearCoatRoughness("ClearCoatRoughness", Range(0, 1, 0.01)) = 0; - material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); - material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); - } - - Header("Common") { - material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; - material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); - } - } - - EditorMacros { - Header("Conditional Macors") { - MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); - MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); - MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY"); - MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE") - MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); - MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); - MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); - MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); - MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); - MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); - MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); - MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); - MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); - } - - Header("Enabled Macros") { - [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); - [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); - } - } - SubShader "Default" { - UsePass "pbr/Default/ShadowCaster" Pass "Forward Pass" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW - - #include "ForwardPassPBR.glsl" - + VertexShader = PBRVertex; FragmentShader = PBRFragment; - + + #include "ForwardPassPBR.glsl" } } } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl index a552a331..59480c87 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl @@ -1,3 +1,6 @@ +#ifndef ATTRIBUTES_PBR_INCLUDED +#define ATTRIBUTES_PBR_INCLUDED + struct Attributes{ vec3 POSITION; @@ -68,4 +71,7 @@ struct Attributes{ vec4 TANGENT; #endif #endif -}; \ No newline at end of file +}; + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl b/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl new file mode 100644 index 00000000..caf7f9ec --- /dev/null +++ b/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl @@ -0,0 +1,71 @@ + EditorProperties { + Header("Base"){ + material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; + material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); + material_BaseTexture("BaseTexture", Texture2D); + } + + Header("Metal Roughness") { + material_Metal( "Metal", Range(0,1,0.01) ) = 1; + material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; + material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); + } + + Header("Anisotropy") { + material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); + material_AnisotropyTexture("AnisotropyTexture", Texture2D); + } + + Header("Normal") { + material_NormalTexture("NormalTexture", Texture2D); + material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; + } + + Header("Emissive") { + material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); + material_EmissiveTexture("EmissiveTexture", Texture2D); + } + + Header("Occlusion") { + material_OcclusionTexture("OcclusionTexture", Texture2D); + material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; + material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; + } + + Header("Clear Coat") { + material_ClearCoat("ClearCoat", Range(0, 1, 0.01)) = 0; + material_ClearCoatTexture("ClearCoatTexture", Texture2D); + material_ClearCoatRoughness("ClearCoatRoughness", Range(0, 1, 0.01)) = 0; + material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); + material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); + } + + Header("Common") { + material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; + material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); + } + } + + EditorMacros { + Header("Conditional Macors") { + MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); + MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); + MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY"); + MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE") + MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); + MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); + MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); + MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); + MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); + MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); + MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); + } + + Header("Enabled Macros") { + [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); + [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); + } +} + \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 03112f21..5c6b177a 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -1,3 +1,6 @@ +#ifndef FORWARD_PASS_PBR_INCLUDED +#define FORWARD_PASS_PBR_INCLUDED + #include "AttributesPBR.glsl" #include "VaryingsPBR.glsl" #include "Common.glsl" @@ -43,4 +46,7 @@ void PBRFragment(Varyings v) { #endif gl_FragColor = color; -} \ No newline at end of file +} + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index 9c712b3b..124a2aa9 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -1,3 +1,6 @@ +#ifndef VARYINGS_PBR_INCLUDED +#define VARYINGS_PBR_INCLUDED + struct Varyings{ vec2 v_uv; @@ -31,4 +34,7 @@ struct Varyings{ #ifdef MATERIAL_NEED_WORLD_POS vec3 v_pos; #endif -}; \ No newline at end of file +}; + + +#endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index 3abeef71..da4fb618 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -5,6 +5,7 @@ import LightDirectPBR from "./LightDirectPBR.glsl"; import LightIndirectPBR from "./LightIndirectPBR.glsl"; import MaterialInputPBR from "./MaterialInputPBR.glsl"; import VaryingsPBR from "./VaryingsPBR.glsl"; +import EditorProps from "./EditorProps.glsl"; export default [ { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" }, @@ -13,5 +14,6 @@ export default [ { source: MaterialInputPBR, includeKey: "MaterialInputPBR.glsl" }, { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" }, { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" }, - { source: BRDF, includeKey: "BRDF.glsl" } + { source: BRDF, includeKey: "BRDF.glsl" }, + { source: EditorProps, includeKey: "EditorProps.glsl" } ]; From f85cc8d98a9bbd907c97b1e6f968c627e2a6023e Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 14:57:23 +0800 Subject: [PATCH 72/89] refactor: add NEED_TANGENT --- packages/shaderlab/src/shaders/PBR.gs | 4 ++-- .../src/shaders/shadingPBR/MaterialInputPBR.glsl | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/shaderlab/src/shaders/PBR.gs b/packages/shaderlab/src/shaders/PBR.gs index 04a9cd5c..eaaebfcd 100644 --- a/packages/shaderlab/src/shaders/PBR.gs +++ b/packages/shaderlab/src/shaders/PBR.gs @@ -11,8 +11,8 @@ Shader "PBR.gs" { VertexShader = PBRVertex; FragmentShader = PBRFragment; - + #include "ForwardPassPBR.glsl" } } - } \ No newline at end of file +} \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index 52875f2f..f0aa5ac4 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -3,6 +3,10 @@ #include "Normal.glsl" +#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) + #define NEED_TANGENT +#endif + struct SurfaceData{ // common vec3 albedoColor; @@ -17,7 +21,7 @@ struct SurfaceData{ vec3 position; vec3 normal; - #ifdef RENDERER_HAS_TANGENT + #ifdef NEED_TANGENT vec3 tangent; vec3 bitangent; #endif @@ -35,7 +39,7 @@ struct BRDFData{ vec3 position; vec3 normal; - #ifdef RENDERER_HAS_TANGENT + #ifdef NEED_TANGENT vec3 tangent; vec3 bitangent; #endif @@ -55,7 +59,7 @@ struct BRDFData{ #ifdef MATERIAL_ENABLE_CLEAR_COAT float clearCoat; float clearCoatRoughness; - vec3 clearCoatNormal; + vec3 clearCoatNormal; float clearCoatDotNV; #endif }; @@ -230,7 +234,7 @@ void initSurfaceData(Varyings v, out SurfaceData surfaceData, bool isFrontFacing surfaceData.viewDir = normalize(camera_Position - v.v_pos); #endif - #ifdef RENDERER_HAS_TANGENT + #ifdef NEED_TANGENT mat3 tbn = getTBN(v, isFrontFacing); surfaceData.tangent = tbn[0]; surfaceData.bitangent = tbn[1]; @@ -247,7 +251,7 @@ void initSurfaceData(Varyings v, out SurfaceData surfaceData, bool isFrontFacing void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.position = surfaceData.position; brdfData.normal = surfaceData.normal; - #ifdef RENDERER_HAS_TANGENT + #ifdef NEED_TANGENT brdfData.tangent = surfaceData.tangent; brdfData.bitangent = surfaceData.bitangent; #endif From 71c39db79e2a89d6ec5b8be63516b9654dfe94f5 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 16:36:07 +0800 Subject: [PATCH 73/89] refactor: add ibl function macro --- .../src/shaders/shadingPBR/EditorProps.glsl | 2 +- .../shaders/shadingPBR/LightIndirectPBR.glsl | 27 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl b/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl index caf7f9ec..254e0e73 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl @@ -44,7 +44,7 @@ material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); } - } + } EditorMacros { Header("Conditional Macors") { diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index 92907513..b46b4c63 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -2,6 +2,23 @@ #ifndef LIGHT_INDIRECT_PBR_INCLUDED #define LIGHT_INDIRECT_PBR_INCLUDED +#ifndef FUNCTION_DIFFUSE_AO + #define FUNCTION_DIFFUSE_AO evaluateDiffuseAO +#endif +#ifndef FUNCTION_SPECULAR_AO + #define FUNCTION_SPECULAR_AO evaluateSpecularAO +#endif +#ifndef FUNCTION_DIFFUSE_IBL + #define FUNCTION_DIFFUSE_IBL evaluateDiffuseIBL +#endif +#ifndef FUNCTION_SPECULAR_IBL + #define FUNCTION_SPECULAR_IBL evaluateSpecularIBL +#endif +#ifndef FUNCTION_CLEAR_COAT_IBL + #define FUNCTION_CLEAR_COAT_IBL evaluateClearCoatIBL +#endif + + #include "BRDF.glsl" #include "Light.glsl" @@ -157,17 +174,17 @@ float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ void evaluateIBL(Varyings v, BRDFData brdfData, inout vec3 color){ vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); - float diffuseAO = evaluateDiffuseAO(v); - float specularAO = evaluateSpecularAO(diffuseAO, brdfData.roughness, brdfData.dotNV); + float diffuseAO = FUNCTION_DIFFUSE_AO(v); + float specularAO = FUNCTION_SPECULAR_AO(diffuseAO, brdfData.roughness, brdfData.dotNV); // IBL diffuse - evaluateDiffuseIBL(brdfData, diffuseAO, diffuseColor); + FUNCTION_DIFFUSE_IBL(brdfData, diffuseAO, diffuseColor); // IBL ClearCoat - float radianceAttenuation = evaluateClearCoatIBL(brdfData, specularAO, specularColor); + float radianceAttenuation = FUNCTION_CLEAR_COAT_IBL(brdfData, specularAO, specularColor); // IBL specular - evaluateSpecularIBL(brdfData, specularAO, radianceAttenuation, specularColor); + FUNCTION_SPECULAR_IBL(brdfData, specularAO, radianceAttenuation, specularColor); color += diffuseColor + specularColor; } From 7384531192f3d73d5e7d33ecc9de7b9fc8cbb359 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 16:48:22 +0800 Subject: [PATCH 74/89] refactor: remove eidtor props outside --- packages/shaderlab/src/shaders/PBR.gs | 75 ++++++++++++++++++- .../src/shaders/shadingPBR/EditorProps.glsl | 71 ------------------ .../shaderlab/src/shaders/shadingPBR/index.ts | 4 +- 3 files changed, 73 insertions(+), 77 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl diff --git a/packages/shaderlab/src/shaders/PBR.gs b/packages/shaderlab/src/shaders/PBR.gs index eaaebfcd..c0313af6 100644 --- a/packages/shaderlab/src/shaders/PBR.gs +++ b/packages/shaderlab/src/shaders/PBR.gs @@ -1,6 +1,75 @@ Shader "PBR.gs" { - #include "EditorProps.glsl" + EditorProperties { + Header("Base"){ + material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; + material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); + material_BaseTexture("BaseTexture", Texture2D); + } + + Header("Metal Roughness") { + material_Metal( "Metal", Range(0,1,0.01) ) = 1; + material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; + material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); + } + + Header("Anisotropy") { + material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); + material_AnisotropyTexture("AnisotropyTexture", Texture2D); + } + + Header("Normal") { + material_NormalTexture("NormalTexture", Texture2D); + material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; + } + + Header("Emissive") { + material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); + material_EmissiveTexture("EmissiveTexture", Texture2D); + } + + Header("Occlusion") { + material_OcclusionTexture("OcclusionTexture", Texture2D); + material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; + material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; + } + + Header("Clear Coat") { + material_ClearCoat("ClearCoat", Range(0, 1, 0.01)) = 0; + material_ClearCoatTexture("ClearCoatTexture", Texture2D); + material_ClearCoatRoughness("ClearCoatRoughness", Range(0, 1, 0.01)) = 0; + material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); + material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); + } + + Header("Common") { + material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; + material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); + } + } + EditorMacros { + Header("Conditional Macors") { + MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); + MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); + MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY"); + MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE") + MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); + MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); + MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); + MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); + MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); + MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); + MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); + MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); + } + + Header("Enabled Macros") { + [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); + [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); + } + } + SubShader "Default" { UsePass "pbr/Default/ShadowCaster" @@ -8,11 +77,11 @@ Shader "PBR.gs" { Tags { pipelineStage = "Forward"} #define IS_METALLIC_WORKFLOW - + VertexShader = PBRVertex; FragmentShader = PBRFragment; #include "ForwardPassPBR.glsl" } } -} \ No newline at end of file + } \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl b/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl deleted file mode 100644 index 254e0e73..00000000 --- a/packages/shaderlab/src/shaders/shadingPBR/EditorProps.glsl +++ /dev/null @@ -1,71 +0,0 @@ - EditorProperties { - Header("Base"){ - material_IOR("IOR", Range(0, 5, 0.01)) = 1.5; - material_BaseColor("BaseColor", Color) = (1, 1, 1, 1); - material_BaseTexture("BaseTexture", Texture2D); - } - - Header("Metal Roughness") { - material_Metal( "Metal", Range(0,1,0.01) ) = 1; - material_Roughness( "Roughness", Range( 0, 1, 0.01 ) ) = 1; - material_RoughnessMetallicTexture("RoughnessMetallicTexture", Texture2D); - } - - Header("Anisotropy") { - material_AnisotropyInfo("AnisotropyInfo", Vector3) = (1, 0, 0); - material_AnisotropyTexture("AnisotropyTexture", Texture2D); - } - - Header("Normal") { - material_NormalTexture("NormalTexture", Texture2D); - material_NormalIntensity("NormalIntensity", Range(0, 5, 0.01)) = 1; - } - - Header("Emissive") { - material_EmissiveColor("EmissiveColor", Color ) = (0, 0, 0, 1); - material_EmissiveTexture("EmissiveTexture", Texture2D); - } - - Header("Occlusion") { - material_OcclusionTexture("OcclusionTexture", Texture2D); - material_OcclusionIntensity("OcclusionIntensity", Range(0, 5, 0.01)) = 1; - material_OcclusionTextureCoord("OcclusionTextureCoord", Float) = 0; - } - - Header("Clear Coat") { - material_ClearCoat("ClearCoat", Range(0, 1, 0.01)) = 0; - material_ClearCoatTexture("ClearCoatTexture", Texture2D); - material_ClearCoatRoughness("ClearCoatRoughness", Range(0, 1, 0.01)) = 0; - material_ClearCoatRoughnessTexture("ClearCoatRoughnessTexture", Texture2D); - material_ClearCoatNormalTexture("ClearCoatNormalTexture", Texture2D); - } - - Header("Common") { - material_AlphaCutoff( "AlphaCutoff", Range(0, 1, 0.01) ) = 0; - material_TilingOffset("TilingOffset", Vector4) = (1, 1, 0, 0); - } - } - - EditorMacros { - Header("Conditional Macors") { - MATERIAL_HAS_BASETEXTURE("HAS_BASETEXTURE"); - MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE("HAS_ROUGHNESS_METALLIC_TEXTURE"); - MATERIAL_ENABLE_ANISOTROPY("ENABLE_ANISOTROPY"); - MATERIAL_HAS_ANISOTROPY_TEXTURE("HAS_ANISOTROPY_TEXTURE") - MATERIAL_HAS_NORMALTEXTURE("HAS_NORMALTEXTURE"); - MATERIAL_HAS_EMISSIVETEXTURE("HAS_EMISSIVETEXTURE"); - MATERIAL_HAS_OCCLUSION_TEXTURE("HAS_OCCLUSION_TEXTURE"); - MATERIAL_ENABLE_CLEAR_COAT("ENABLE_CLEAR_COAT"); - MATERIAL_HAS_CLEAR_COAT_TEXTURE("HAS_CLEAR_COAT_TEXTURE"); - MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE("HAS_CLEAR_COAT_ROUGHNESS_TEXTURE"); - MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE("HAS_CLEAR_COAT_NORMAL_TEXTURE"); - MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); - MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); - } - - Header("Enabled Macros") { - [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); - [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); - } -} - \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index da4fb618..3abeef71 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -5,7 +5,6 @@ import LightDirectPBR from "./LightDirectPBR.glsl"; import LightIndirectPBR from "./LightIndirectPBR.glsl"; import MaterialInputPBR from "./MaterialInputPBR.glsl"; import VaryingsPBR from "./VaryingsPBR.glsl"; -import EditorProps from "./EditorProps.glsl"; export default [ { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" }, @@ -14,6 +13,5 @@ export default [ { source: MaterialInputPBR, includeKey: "MaterialInputPBR.glsl" }, { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" }, { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" }, - { source: BRDF, includeKey: "BRDF.glsl" }, - { source: EditorProps, includeKey: "EditorProps.glsl" } + { source: BRDF, includeKey: "BRDF.glsl" } ]; From 7802c98347b7318ae77111c92a77107e4f0db41b Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 17:37:46 +0800 Subject: [PATCH 75/89] refactor: abstract some function --- .../shaderlab/src/shaders/BlendShape.glsl | 97 +++++++++++++-- packages/shaderlab/src/shaders/Skin.glsl | 53 +++++--- packages/shaderlab/src/shaders/Vertex.glsl | 113 +++--------------- 3 files changed, 141 insertions(+), 122 deletions(-) diff --git a/packages/shaderlab/src/shaders/BlendShape.glsl b/packages/shaderlab/src/shaders/BlendShape.glsl index 81950d0c..4a140211 100644 --- a/packages/shaderlab/src/shaders/BlendShape.glsl +++ b/packages/shaderlab/src/shaders/BlendShape.glsl @@ -6,6 +6,13 @@ mediump sampler2DArray renderer_BlendShapeTexture; ivec3 renderer_BlendShapeTextureInfo; float renderer_BlendShapeWeights[RENDERER_BLENDSHAPE_COUNT]; + + vec3 getBlendShapeVertexElement(int blendShapeIndex, int vertexElementIndex){ + int y = vertexElementIndex / renderer_BlendShapeTextureInfo.y; + int x = vertexElementIndex - y * renderer_BlendShapeTextureInfo.y; + ivec3 uv = ivec3(x, y , blendShapeIndex); + return (texelFetch(renderer_BlendShapeTexture, uv, 0)).xyz; + } #else #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) float renderer_BlendShapeWeights[2]; @@ -18,15 +25,87 @@ #endif #endif - #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE - vec3 getBlendShapeVertexElement(int blendShapeIndex, int vertexElementIndex) - { - int y = vertexElementIndex / renderer_BlendShapeTextureInfo.y; - int x = vertexElementIndex - y * renderer_BlendShapeTextureInfo.y; - ivec3 uv = ivec3(x, y , blendShapeIndex); - return (texelFetch(renderer_BlendShapeTexture, uv, 0)).xyz; - } - #endif + void calculateBlendShape(Attributes attr, inout vec4 position + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + ,inout vec3 normal + #endif + #ifdef RENDERER_HAS_TANGENT + ,inout vec4 tangent + #endif + #endif + ){ + #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE + int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; + for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){ + int vertexElementOffset = vertexOffset; + float weight = renderer_BlendShapeWeights[i]; + // Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13 + if(weight != 0.0){ + position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + + #ifndef MATERIAL_OMIT_NORMAL + #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) + vertexElementOffset += 1; + normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif + + #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) + vertexElementOffset += 1; + tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif + #endif + } + + } + #else + position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; + position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; + + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + #endif + + #ifdef RENDERER_HAS_TANGENT + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + #endif + #endif + #else + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) + #ifndef MATERIAL_OMIT_NORMAL + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; + normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; + #endif + + #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; + tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; + #endif + #endif + #else + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; + position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; + position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; + position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; + #endif + #endif + #endif + } + #endif diff --git a/packages/shaderlab/src/shaders/Skin.glsl b/packages/shaderlab/src/shaders/Skin.glsl index 35bef11a..48b832ea 100644 --- a/packages/shaderlab/src/shaders/Skin.glsl +++ b/packages/shaderlab/src/shaders/Skin.glsl @@ -3,27 +3,44 @@ #ifdef RENDERER_HAS_SKIN - #ifdef RENDERER_USE_JOINT_TEXTURE - sampler2D renderer_JointSampler; - float renderer_JointCount; - - mat4 getJointMatrix(sampler2D smp, float index) - { - float base = index / renderer_JointCount; - float hf = 0.5 / renderer_JointCount; - float v = base + hf; - - vec4 m0 = texture2D(smp, vec2(0.125, v )); - vec4 m1 = texture2D(smp, vec2(0.375, v )); - vec4 m2 = texture2D(smp, vec2(0.625, v )); - vec4 m3 = texture2D(smp, vec2(0.875, v )); - - return mat4(m0, m1, m2, m3); - } - + #ifdef RENDERER_USE_JOINT_TEXTURE + sampler2D renderer_JointSampler; + float renderer_JointCount; + + mat4 getJointMatrix(sampler2D smp, float index){ + float base = index / renderer_JointCount; + float hf = 0.5 / renderer_JointCount; + float v = base + hf; + + vec4 m0 = texture2D(smp, vec2(0.125, v )); + vec4 m1 = texture2D(smp, vec2(0.375, v )); + vec4 m2 = texture2D(smp, vec2(0.625, v )); + vec4 m3 = texture2D(smp, vec2(0.875, v )); + + return mat4(m0, m1, m2, m3); + } #else mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; #endif + + mat4 getSkinMatrix(Attributes attr){ + #ifdef RENDERER_USE_JOINT_TEXTURE + mat4 skinMatrix = + attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + + attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + + attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + + attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); + #else + mat4 skinMatrix = + attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + + attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + + attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + + attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; + #endif + + return skinMatrix; + } + #endif diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 0bfe4411..b6ce3d07 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -25,113 +25,36 @@ void initVertex(Attributes attr, out Varyings v){ // blendShape #ifdef RENDERER_HAS_BLENDSHAPE - #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE - int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; - for(int i = 0; i < RENDERER_BLENDSHAPE_COUNT; i++){ - int vertexElementOffset = vertexOffset; - float weight = renderer_BlendShapeWeights[i]; - // Warnning: Multiplying by 0 creates weird precision issues, causing rendering anomalies in Ace2 Android13 - if(weight != 0.0){ - position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - - #ifndef MATERIAL_OMIT_NORMAL - #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) - vertexElementOffset += 1; - normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif - - #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) - vertexElementOffset += 1; - tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif - #endif - } - - } - #else - position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; - position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; - - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - #endif - - #ifdef RENDERER_HAS_TANGENT - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - #endif - #endif - #else - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; - normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; - #endif - - #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; - tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; - #endif - #endif - #else - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; - position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; - position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; - position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; - #endif - #endif - #endif + calculateBlendShape(attr, position + #ifndef MATERIAL_OMIT_NORMAL + #ifdef RENDERER_HAS_NORMAL + ,normal + #endif + #ifdef RENDERER_HAS_TANGENT + ,tangent + #endif + #endif + ); #endif // skin #ifdef RENDERER_HAS_SKIN + mat4 skinMatrix = getSkinMatrix(attr); + position = skinMatrix * position; - #ifdef RENDERER_USE_JOINT_TEXTURE - mat4 skinMatrix = - attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + - attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + - attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + - attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); - - #else - mat4 skinMatrix = - attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + - attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + - attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + - attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; - #endif - - position = skinMatrix * position; - - #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) - mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); - normal = normal * skinNormalMatrix; - #ifdef RENDERER_HAS_TANGENT - tangent.xyz = tangent.xyz * skinNormalMatrix; - #endif - + #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) + mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); + normal = normal * skinNormalMatrix; + #ifdef RENDERER_HAS_TANGENT + tangent.xyz = tangent.xyz * skinNormalMatrix; #endif + #endif #endif - - // fog #if SCENE_FOG_MODE != 0 vec4 positionVS = renderer_MVMat * position; From 459e8d72cb0728fec6a61acc33e2b47c6c6d1a8b Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 18:24:39 +0800 Subject: [PATCH 76/89] refactor: init vetex shader --- packages/shaderlab/src/shaders/Vertex.glsl | 64 ++++++++++--------- .../shaders/shadingPBR/ForwardPassPBR.glsl | 5 +- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 16 ++--- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index b6ce3d07..64e44274 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -9,7 +9,32 @@ vec4 material_TilingOffset; -void initVertex(Attributes attr, out Varyings v){ + +void initUV(Attributes attr, inout Varyings v){ + #ifdef RENDERER_HAS_UV + v.v_uv = attr.TEXCOORD_0; + #else + // may need this calculate normal + v.v_uv = vec2( 0.0, 0.0 ); + #endif + + #ifdef RENDERER_HAS_UV1 + v.v_uv1 = attr.TEXCOORD_1; + #endif + + #ifdef MATERIAL_NEED_TILING_OFFSET + v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; + #endif +} + +void initVertexColor(Attributes attr, inout Varyings v){ + #ifdef RENDERER_ENABLE_VERTEXCOLOR + v.v_color = attr.COLOR_0; + #endif +} + + +void initTransform(Attributes attr, out Varyings v){ vec4 position = vec4( attr.POSITION , 1.0 ); #ifndef MATERIAL_OMIT_NORMAL @@ -62,30 +87,7 @@ void initVertex(Attributes attr, out Varyings v){ #endif - // uv_vert - #ifdef RENDERER_HAS_UV - v.v_uv = attr.TEXCOORD_0; - #else - // may need this calculate normal - v.v_uv = vec2( 0.0, 0.0 ); - #endif - - #ifdef RENDERER_HAS_UV1 - v.v_uv1 = attr.TEXCOORD_1; - #endif - - #ifdef MATERIAL_NEED_TILING_OFFSET - v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; - #endif - - - // color_vert - #ifdef RENDERER_ENABLE_VERTEXCOLOR - v.v_color = attr.COLOR_0; - #endif - - - // normal_vert + // normal and tangent #ifndef MATERIAL_OMIT_NORMAL #ifdef RENDERER_HAS_NORMAL v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); @@ -107,15 +109,19 @@ void initVertex(Attributes attr, out Varyings v){ v.v_pos = temp_pos.xyz / temp_pos.w; #endif - // shadow + + // position_vert + gl_Position = renderer_MVPMat * position; +} + + +void initShadowCorrd(Attributes attr, out Varyings v){ #ifdef SCENE_IS_CALCULATE_SHADOWS #if SCENE_SHADOW_CASCADED_COUNT == 1 v.v_shadowCoord = getShadowCoord(v.v_pos); #endif #endif - - // position_vert - gl_Position = renderer_MVPMat * position; } + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 5c6b177a..8d1719b8 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -14,7 +14,10 @@ Varyings PBRVertex(Attributes attr) { Varyings v; - initVertex(attr, v); + initUV(attr, v); + initVertexColor(attr, v); + initTransform(attr, v); + initShadowCorrd(attr, v); return v; } diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index 124a2aa9..c34bc1c8 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -3,6 +3,9 @@ struct Varyings{ vec2 v_uv; + #ifdef RENDERER_HAS_UV1 + vec2 v_uv1; + #endif #ifdef RENDERER_ENABLE_VERTEXCOLOR vec4 v_color; @@ -22,18 +25,15 @@ struct Varyings{ #endif #endif - #ifdef SCENE_IS_CALCULATE_SHADOWS + #ifdef MATERIAL_NEED_WORLD_POS + vec3 v_pos; + #endif + + #ifdef SCENE_IS_CALCULATE_SHADOWS #if SCENE_SHADOW_CASCADED_COUNT==1 vec3 v_shadowCoord; #endif #endif - - #ifdef RENDERER_HAS_UV1 - vec2 v_uv1; - #endif - #ifdef MATERIAL_NEED_WORLD_POS - vec3 v_pos; - #endif }; From dfca3ab23d052e9ddf9bbc9f2d7cab9ffec40779 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 18:39:06 +0800 Subject: [PATCH 77/89] refactor: delete MATERIAL_NEED_TILING_OFFSET and MATERIAL_NEED_WORLD_POS --- packages/shaderlab/src/shaders/PBR.gs | 5 ----- packages/shaderlab/src/shaders/Vertex.glsl | 12 +++--------- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 8 +++----- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/packages/shaderlab/src/shaders/PBR.gs b/packages/shaderlab/src/shaders/PBR.gs index c0313af6..f53f7e5c 100644 --- a/packages/shaderlab/src/shaders/PBR.gs +++ b/packages/shaderlab/src/shaders/PBR.gs @@ -63,11 +63,6 @@ Shader "PBR.gs" { MATERIAL_IS_TRANSPARENT("IS_TRANSPARENT"); MATERIAL_IS_ALPHA_CUTOFF("IS_ALPHA_CUTOFF"); } - - Header("Enabled Macros") { - [On] MATERIAL_NEED_WORLD_POS("WORLD POS"); - [On] MATERIAL_NEED_TILING_OFFSET("TILING_OFFSET"); - } } SubShader "Default" { diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 64e44274..4aa6dae6 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -8,8 +8,6 @@ vec4 material_TilingOffset; - - void initUV(Attributes attr, inout Varyings v){ #ifdef RENDERER_HAS_UV v.v_uv = attr.TEXCOORD_0; @@ -22,9 +20,7 @@ void initUV(Attributes attr, inout Varyings v){ v.v_uv1 = attr.TEXCOORD_1; #endif - #ifdef MATERIAL_NEED_TILING_OFFSET - v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; - #endif + v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; } void initVertexColor(Attributes attr, inout Varyings v){ @@ -104,10 +100,8 @@ void initTransform(Attributes attr, out Varyings v){ // worldpos_vert - #ifdef MATERIAL_NEED_WORLD_POS - vec4 temp_pos = renderer_ModelMat * position; - v.v_pos = temp_pos.xyz / temp_pos.w; - #endif + vec4 temp_pos = renderer_ModelMat * position; + v.v_pos = temp_pos.xyz / temp_pos.w; // position_vert diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index c34bc1c8..fa69cff6 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -25,12 +25,10 @@ struct Varyings{ #endif #endif - #ifdef MATERIAL_NEED_WORLD_POS - vec3 v_pos; - #endif + vec3 v_pos; - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 + #ifdef SCENE_IS_CALCULATE_SHADOWS + #if SCENE_SHADOW_CASCADED_COUNT==1 vec3 v_shadowCoord; #endif #endif From 561c531af0e55224537f5478512164db01417cf0 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Tue, 16 Jul 2024 19:37:58 +0800 Subject: [PATCH 78/89] refactor: delete MATERIAL_OMIT_NORMAL --- .../shaderlab/src/shaders/BlendShape.glsl | 74 +++++++++---------- packages/shaderlab/src/shaders/Vertex.glsl | 45 +++++------ .../src/shaders/shadingPBR/AttributesPBR.glsl | 14 ++-- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 12 ++- 4 files changed, 62 insertions(+), 83 deletions(-) diff --git a/packages/shaderlab/src/shaders/BlendShape.glsl b/packages/shaderlab/src/shaders/BlendShape.glsl index 4a140211..5021d544 100644 --- a/packages/shaderlab/src/shaders/BlendShape.glsl +++ b/packages/shaderlab/src/shaders/BlendShape.glsl @@ -26,13 +26,11 @@ #endif void calculateBlendShape(Attributes attr, inout vec4 position - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - ,inout vec3 normal - #endif - #ifdef RENDERER_HAS_TANGENT - ,inout vec4 tangent - #endif + #ifdef RENDERER_HAS_NORMAL + ,inout vec3 normal + #endif + #ifdef RENDERER_HAS_TANGENT + ,inout vec4 tangent #endif ){ #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE @@ -44,16 +42,14 @@ if(weight != 0.0){ position.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #ifndef MATERIAL_OMIT_NORMAL - #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) - vertexElementOffset += 1; - normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif + #if defined( RENDERER_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) + vertexElementOffset += 1; + normal += getBlendShapeVertexElement(i, vertexElementOffset) * weight; + #endif - #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) - vertexElementOffset += 1; - tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; - #endif + #if defined( RENDERER_HAS_TANGENT ) && defined(RENDERER_BLENDSHAPE_HAS_TANGENT) + vertexElementOffset += 1; + tangent.xyz += getBlendShapeVertexElement(i, vertexElementOffset) * weight; #endif } @@ -63,36 +59,32 @@ position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - #endif - - #ifdef RENDERER_HAS_TANGENT - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - #endif + #ifdef RENDERER_HAS_NORMAL + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; #endif + + #ifdef RENDERER_HAS_TANGENT + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + #endif #else #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - #ifndef MATERIAL_OMIT_NORMAL - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; - normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; - #endif + #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) + normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; + normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; + #endif - #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; - tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; - #endif + #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) + tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; + tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; #endif #else position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 4aa6dae6..6b3d9c91 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -33,45 +33,38 @@ void initVertexColor(Attributes attr, inout Varyings v){ void initTransform(Attributes attr, out Varyings v){ vec4 position = vec4( attr.POSITION , 1.0 ); - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 normal = vec3( attr.NORMAL ); - #endif + #ifdef RENDERER_HAS_NORMAL + vec3 normal = vec3( attr.NORMAL ); + #endif - #ifdef RENDERER_HAS_TANGENT - vec4 tangent = vec4( attr.TANGENT ); - #endif + #ifdef RENDERER_HAS_TANGENT + vec4 tangent = vec4( attr.TANGENT ); #endif // blendShape #ifdef RENDERER_HAS_BLENDSHAPE calculateBlendShape(attr, position - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - ,normal - #endif - #ifdef RENDERER_HAS_TANGENT - ,tangent - #endif + #ifdef RENDERER_HAS_NORMAL + ,normal + #endif + #ifdef RENDERER_HAS_TANGENT + ,tangent #endif ); #endif - - // skin #ifdef RENDERER_HAS_SKIN mat4 skinMatrix = getSkinMatrix(attr); position = skinMatrix * position; - #if defined(RENDERER_HAS_NORMAL) && !defined(MATERIAL_OMIT_NORMAL) + #if defined(RENDERER_HAS_NORMAL) mat3 skinNormalMatrix = INVERSE_MAT(mat3(skinMatrix)); normal = normal * skinNormalMatrix; #ifdef RENDERER_HAS_TANGENT tangent.xyz = tangent.xyz * skinNormalMatrix; #endif - #endif #endif @@ -84,17 +77,15 @@ void initTransform(Attributes attr, out Varyings v){ // normal and tangent - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); + #ifdef RENDERER_HAS_NORMAL + v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); - #ifdef RENDERER_HAS_TANGENT - vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); - vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; + #ifdef RENDERER_HAS_TANGENT + vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); + vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; - v.v_tangent = tangentW; - v.v_bitangent = bitangentW; - #endif + v.v_tangent = tangentW; + v.v_bitangent = bitangentW; #endif #endif diff --git a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl index 59480c87..96569034 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl @@ -62,15 +62,13 @@ struct Attributes{ vec4 COLOR_0; #endif - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 NORMAL; - #endif + #ifdef RENDERER_HAS_NORMAL + vec3 NORMAL; + #endif - #ifdef RENDERER_HAS_TANGENT - vec4 TANGENT; - #endif - #endif + #ifdef RENDERER_HAS_TANGENT + vec4 TANGENT; + #endif }; diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index fa69cff6..da1da354 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -15,13 +15,11 @@ struct Varyings{ vec3 v_positionVS; #endif - #ifndef MATERIAL_OMIT_NORMAL - #ifdef RENDERER_HAS_NORMAL - vec3 v_normal; - #ifdef RENDERER_HAS_TANGENT - vec3 v_tangent; - vec3 v_bitangent; - #endif + #ifdef RENDERER_HAS_NORMAL + vec3 v_normal; + #ifdef RENDERER_HAS_TANGENT + vec3 v_tangent; + vec3 v_bitangent; #endif #endif From de4af120702daddd6c315d68a8d515c14b2ceb5d Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 17 Jul 2024 16:18:52 +0800 Subject: [PATCH 79/89] chore: test compile bug --- .../shaderlab/src/shaders/BlendShape.glsl | 46 ++++++++-------- packages/shaderlab/src/shaders/Normal.glsl | 18 +++--- packages/shaderlab/src/shaders/Skin.glsl | 18 +++--- packages/shaderlab/src/shaders/Vertex.glsl | 55 ++++++------------- .../shaders/shadingPBR/ForwardPassPBR.glsl | 36 +++++++----- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 6 +- 6 files changed, 83 insertions(+), 96 deletions(-) diff --git a/packages/shaderlab/src/shaders/BlendShape.glsl b/packages/shaderlab/src/shaders/BlendShape.glsl index 5021d544..c91e502f 100644 --- a/packages/shaderlab/src/shaders/BlendShape.glsl +++ b/packages/shaderlab/src/shaders/BlendShape.glsl @@ -25,7 +25,7 @@ #endif #endif - void calculateBlendShape(Attributes attr, inout vec4 position + void calculateBlendShape(Attributes attributes, inout vec4 position #ifdef RENDERER_HAS_NORMAL ,inout vec3 normal #endif @@ -55,44 +55,44 @@ } #else - position.xyz += attr.POSITION_BS0 * renderer_BlendShapeWeights[0]; - position.xyz += attr.POSITION_BS1 * renderer_BlendShapeWeights[1]; + position.xyz += attributes.POSITION_BS0 * renderer_BlendShapeWeights[0]; + position.xyz += attributes.POSITION_BS1 * renderer_BlendShapeWeights[1]; #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) #ifdef RENDERER_HAS_NORMAL - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + normal += attributes.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attributes.NORMAL_BS1 * renderer_BlendShapeWeights[1]; #endif #ifdef RENDERER_HAS_TANGENT - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + tangent.xyz += attributes.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attributes.TANGENT_BS1 * renderer_BlendShapeWeights[1]; #endif #else #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) || defined( RENDERER_BLENDSHAPE_HAS_TANGENT ) - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; + position.xyz += attributes.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attributes.POSITION_BS3 * renderer_BlendShapeWeights[3]; #if defined( RENDERER_BLENDSHAPE_HAS_NORMAL ) && defined( RENDERER_HAS_NORMAL ) - normal += attr.NORMAL_BS0 * renderer_BlendShapeWeights[0]; - normal += attr.NORMAL_BS1 * renderer_BlendShapeWeights[1]; - normal += attr.NORMAL_BS2 * renderer_BlendShapeWeights[2]; - normal += attr.NORMAL_BS3 * renderer_BlendShapeWeights[3]; + normal += attributes.NORMAL_BS0 * renderer_BlendShapeWeights[0]; + normal += attributes.NORMAL_BS1 * renderer_BlendShapeWeights[1]; + normal += attributes.NORMAL_BS2 * renderer_BlendShapeWeights[2]; + normal += attributes.NORMAL_BS3 * renderer_BlendShapeWeights[3]; #endif #if defined(RENDERER_BLENDSHAPE_HAS_TANGENT) && defined( RENDERER_HAS_TANGENT ) - tangent.xyz += attr.TANGENT_BS0 * renderer_BlendShapeWeights[0]; - tangent.xyz += attr.TANGENT_BS1 * renderer_BlendShapeWeights[1]; - tangent.xyz += attr.TANGENT_BS2 * renderer_BlendShapeWeights[2]; - tangent.xyz += attr.TANGENT_BS3 * renderer_BlendShapeWeights[3]; + tangent.xyz += attributes.TANGENT_BS0 * renderer_BlendShapeWeights[0]; + tangent.xyz += attributes.TANGENT_BS1 * renderer_BlendShapeWeights[1]; + tangent.xyz += attributes.TANGENT_BS2 * renderer_BlendShapeWeights[2]; + tangent.xyz += attributes.TANGENT_BS3 * renderer_BlendShapeWeights[3]; #endif #else - position.xyz += attr.POSITION_BS2 * renderer_BlendShapeWeights[2]; - position.xyz += attr.POSITION_BS3 * renderer_BlendShapeWeights[3]; - position.xyz += attr.POSITION_BS4 * renderer_BlendShapeWeights[4]; - position.xyz += attr.POSITION_BS5 * renderer_BlendShapeWeights[5]; - position.xyz += attr.POSITION_BS6 * renderer_BlendShapeWeights[6]; - position.xyz += attr.POSITION_BS7 * renderer_BlendShapeWeights[7]; + position.xyz += attributes.POSITION_BS2 * renderer_BlendShapeWeights[2]; + position.xyz += attributes.POSITION_BS3 * renderer_BlendShapeWeights[3]; + position.xyz += attributes.POSITION_BS4 * renderer_BlendShapeWeights[4]; + position.xyz += attributes.POSITION_BS5 * renderer_BlendShapeWeights[5]; + position.xyz += attributes.POSITION_BS6 * renderer_BlendShapeWeights[6]; + position.xyz += attributes.POSITION_BS7 * renderer_BlendShapeWeights[7]; #endif #endif #endif diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index bc182278..ac26729d 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -4,12 +4,12 @@ // gl_FrontFacing has random value on Adreno GPUs // the Adreno bug is only when gl_FrontFacing is inside a function // https://bugs.chromium.org/p/chromium/issues/detail?id=1154842 -vec3 getNormal(Varyings v, bool isFrontFacing){ +vec3 getNormal(Varyings varyings, bool isFrontFacing){ #ifdef RENDERER_HAS_NORMAL - vec3 normal = normalize(v.v_normal); + vec3 normal = normalize(varyings.v_normal); #elif defined(HAS_DERIVATIVES) - vec3 pos_dx = dFdx(v.v_pos); - vec3 pos_dy = dFdy(v.v_pos); + vec3 pos_dx = dFdx(varyings.v_pos); + vec3 pos_dy = dFdy(varyings.v_pos); vec3 normal = normalize( cross(pos_dx, pos_dy) ); #else vec3 normal = vec3(0, 0, 1); @@ -27,13 +27,13 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt return normal; } -mat3 getTBN(Varyings v, bool isFrontFacing){ +mat3 getTBN(Varyings varyings, bool isFrontFacing){ #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) - mat3 tbn = mat3(v.v_tangent, v.v_bitangent, v.v_normal); + mat3 tbn = mat3(varyings.v_tangent, varyings.v_bitangent, varyings.v_normal); #else - vec3 normal = getNormal(v, isFrontFacing); - vec3 position = v.v_pos; - vec2 uv = isFrontFacing? v.v_uv: -v.v_uv; + vec3 normal = getNormal(varyings, isFrontFacing); + vec3 position = varyings.v_pos; + vec2 uv = isFrontFacing? varyings.v_uv: -varyings.v_uv; #ifdef HAS_DERIVATIVES // ref: http://www.thetenthplanet.de/archives/1180 diff --git a/packages/shaderlab/src/shaders/Skin.glsl b/packages/shaderlab/src/shaders/Skin.glsl index 48b832ea..3f8e1cb1 100644 --- a/packages/shaderlab/src/shaders/Skin.glsl +++ b/packages/shaderlab/src/shaders/Skin.glsl @@ -23,19 +23,19 @@ mat4 renderer_JointMatrix[ RENDERER_JOINTS_NUM ]; #endif - mat4 getSkinMatrix(Attributes attr){ + mat4 getSkinMatrix(Attributes attributes){ #ifdef RENDERER_USE_JOINT_TEXTURE mat4 skinMatrix = - attr.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.x ) + - attr.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.y ) + - attr.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.z ) + - attr.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attr.JOINTS_0.w ); + attributes.WEIGHTS_0.x * getJointMatrix(renderer_JointSampler, attributes.JOINTS_0.x ) + + attributes.WEIGHTS_0.y * getJointMatrix(renderer_JointSampler, attributes.JOINTS_0.y ) + + attributes.WEIGHTS_0.z * getJointMatrix(renderer_JointSampler, attributes.JOINTS_0.z ) + + attributes.WEIGHTS_0.w * getJointMatrix(renderer_JointSampler, attributes.JOINTS_0.w ); #else mat4 skinMatrix = - attr.WEIGHTS_0.x * renderer_JointMatrix[ int( attr.JOINTS_0.x ) ] + - attr.WEIGHTS_0.y * renderer_JointMatrix[ int( attr.JOINTS_0.y ) ] + - attr.WEIGHTS_0.z * renderer_JointMatrix[ int( attr.JOINTS_0.z ) ] + - attr.WEIGHTS_0.w * renderer_JointMatrix[ int( attr.JOINTS_0.w ) ]; + attributes.WEIGHTS_0.x * renderer_JointMatrix[ int( attributes.JOINTS_0.x ) ] + + attributes.WEIGHTS_0.y * renderer_JointMatrix[ int( attributes.JOINTS_0.y ) ] + + attributes.WEIGHTS_0.z * renderer_JointMatrix[ int( attributes.JOINTS_0.z ) ] + + attributes.WEIGHTS_0.w * renderer_JointMatrix[ int( attributes.JOINTS_0.w ) ]; #endif return skinMatrix; diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 6b3d9c91..0e1424fc 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -8,43 +8,32 @@ vec4 material_TilingOffset; -void initUV(Attributes attr, inout Varyings v){ - #ifdef RENDERER_HAS_UV - v.v_uv = attr.TEXCOORD_0; - #else - // may need this calculate normal - v.v_uv = vec2( 0.0, 0.0 ); - #endif +vec2 getUV0(Attributes attributes){ + vec2 uv0 = vec2(0); - #ifdef RENDERER_HAS_UV1 - v.v_uv1 = attr.TEXCOORD_1; + #ifdef RENDERER_HAS_UV + uv0 = attributes.TEXCOORD_0; #endif - v.v_uv = v.v_uv * material_TilingOffset.xy + material_TilingOffset.zw; -} - -void initVertexColor(Attributes attr, inout Varyings v){ - #ifdef RENDERER_ENABLE_VERTEXCOLOR - v.v_color = attr.COLOR_0; - #endif + return uv0 * material_TilingOffset.xy + material_TilingOffset.zw; } -void initTransform(Attributes attr, out Varyings v){ - vec4 position = vec4( attr.POSITION , 1.0 ); +void initTransform(Attributes attributes, inout Varyings varyings){ + vec4 position = vec4( attributes.POSITION , 1.0 ); #ifdef RENDERER_HAS_NORMAL - vec3 normal = vec3( attr.NORMAL ); + vec3 normal = vec3( attributes.NORMAL ); #endif #ifdef RENDERER_HAS_TANGENT - vec4 tangent = vec4( attr.TANGENT ); + vec4 tangent = vec4( attributes.TANGENT ); #endif // blendShape #ifdef RENDERER_HAS_BLENDSHAPE - calculateBlendShape(attr, position + calculateBlendShape(attributes, position #ifdef RENDERER_HAS_NORMAL ,normal #endif @@ -56,7 +45,7 @@ void initTransform(Attributes attr, out Varyings v){ // skin #ifdef RENDERER_HAS_SKIN - mat4 skinMatrix = getSkinMatrix(attr); + mat4 skinMatrix = getSkinMatrix(attributes); position = skinMatrix * position; #if defined(RENDERER_HAS_NORMAL) @@ -72,41 +61,31 @@ void initTransform(Attributes attr, out Varyings v){ // fog #if SCENE_FOG_MODE != 0 vec4 positionVS = renderer_MVMat * position; - v.v_positionVS = positionVS.xyz / positionVS.w; + varyings.v_positionVS = positionVS.xyz / positionVS.w; #endif // normal and tangent #ifdef RENDERER_HAS_NORMAL - v.v_normal = normalize( mat3(renderer_NormalMat) * normal ); + varyings.v_normal = normalize( mat3(renderer_NormalMat) * normal ); #ifdef RENDERER_HAS_TANGENT vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); - vec3 bitangentW = cross( v.v_normal, tangentW ) * tangent.w; + vec3 bitangentW = cross( varyings.v_normal, tangentW ) * tangent.w; - v.v_tangent = tangentW; - v.v_bitangent = bitangentW; + varyings.v_tangent = tangentW; + varyings.v_bitangent = bitangentW; #endif #endif // worldpos_vert vec4 temp_pos = renderer_ModelMat * position; - v.v_pos = temp_pos.xyz / temp_pos.w; + varyings.v_pos = temp_pos.xyz / temp_pos.w; // position_vert gl_Position = renderer_MVPMat * position; } - -void initShadowCorrd(Attributes attr, out Varyings v){ - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT == 1 - v.v_shadowCoord = getShadowCoord(v.v_pos); - #endif - #endif -} - - #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 8d1719b8..4e6413cf 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -11,37 +11,47 @@ #include "LightDirectPBR.glsl" #include "LightIndirectPBR.glsl" -Varyings PBRVertex(Attributes attr) { - Varyings v; +Varyings PBRVertex(Attributes attributes) { + Varyings varyings; - initUV(attr, v); - initVertexColor(attr, v); - initTransform(attr, v); - initShadowCorrd(attr, v); + varyings.v_uv = getUV0(attributes); + #ifdef RENDERER_HAS_UV1 + varyings.v_uv1 = attributes.TEXCOORD_1; + #endif + + #ifdef RENDERER_ENABLE_VERTEXCOLOR + varyings.v_color = attributes.COLOR_0; + #endif + + initTransform(attributes, varyings); + + #if defined(SCENE_SHADOW_CASCADED_COUNT) && (SCENE_SHADOW_CASCADED_COUNT == 1) + // varyings.v_shadowCoord = getShadowCoord(varyings.v_pos); + #endif - return v; + return varyings; } -void PBRFragment(Varyings v) { +void PBRFragment(Varyings varyings) { SurfaceData surfaceData; BRDFData brdfData; - initSurfaceData(v, surfaceData, gl_FrontFacing); + initSurfaceData(varyings, surfaceData, gl_FrontFacing); // Can modify surfaceData here. - initBRDFData(v, surfaceData, brdfData, gl_FrontFacing); + initBRDFData(varyings, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); // Direct Light - evaluateDirectRadiance(v, brdfData, color.rgb); + evaluateDirectRadiance(varyings, brdfData, color.rgb); // IBL - evaluateIBL(v, brdfData, color.rgb); + evaluateIBL(varyings, brdfData, color.rgb); // Emissive color.rgb += surfaceData.emissiveColor; #if SCENE_FOG_MODE != 0 - color = fog(color, v.v_positionVS); + color = fog(color, varyings.v_positionVS); #endif #ifndef ENGINE_IS_COLORSPACE_GAMMA diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index da1da354..d065e3f9 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -25,10 +25,8 @@ struct Varyings{ vec3 v_pos; - #ifdef SCENE_IS_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 - vec3 v_shadowCoord; - #endif + #if defined(SCENE_SHADOW_CASCADED_COUNT) && (SCENE_SHADOW_CASCADED_COUNT == 1) + vec3 v_shadowCoord; #endif }; From a6a215151942cecff224d0f831cb318ab069ac12 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 17 Jul 2024 19:16:41 +0800 Subject: [PATCH 80/89] refactor: init vertex --- .../shaderlab/src/shaders/BlendShape.glsl | 7 +- packages/shaderlab/src/shaders/Shadow.glsl | 2 + packages/shaderlab/src/shaders/Vertex.glsl | 77 +++++++++++-------- .../shaders/shadingPBR/ForwardPassPBR.glsl | 28 ++++++- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 5 +- 5 files changed, 78 insertions(+), 41 deletions(-) diff --git a/packages/shaderlab/src/shaders/BlendShape.glsl b/packages/shaderlab/src/shaders/BlendShape.glsl index c91e502f..05a69f02 100644 --- a/packages/shaderlab/src/shaders/BlendShape.glsl +++ b/packages/shaderlab/src/shaders/BlendShape.glsl @@ -28,10 +28,11 @@ void calculateBlendShape(Attributes attributes, inout vec4 position #ifdef RENDERER_HAS_NORMAL ,inout vec3 normal + #ifdef RENDERER_HAS_TANGENT + ,inout vec4 tangent + #endif #endif - #ifdef RENDERER_HAS_TANGENT - ,inout vec4 tangent - #endif + ){ #ifdef RENDERER_BLENDSHAPE_USE_TEXTURE int vertexOffset = gl_VertexID * renderer_BlendShapeTextureInfo.x; diff --git a/packages/shaderlab/src/shaders/Shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl index 73891a69..335b0706 100644 --- a/packages/shaderlab/src/shaders/Shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -1,6 +1,8 @@ #ifndef SHADOW_INCLUDED #define SHADOW_INCLUDED +#include "Transform.glsl" + #if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) #define SCENE_IS_CALCULATE_SHADOWS #endif diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 0e1424fc..7641114c 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -1,12 +1,29 @@ #ifndef VERTEX_INCLUDE #define VERTEX_INCLUDE +#include "Transform.glsl" #include "Skin.glsl" #include "BlendShape.glsl" #include "Shadow.glsl" -#include "Transform.glsl" +struct VertexInputs{ + vec4 positionOS; + vec3 positionWS; + + #if SCENE_FOG_MODE != 0 + vec3 positionVS; + #endif + + #ifdef RENDERER_HAS_NORMAL + vec3 normalWS; + #ifdef RENDERER_HAS_TANGENT + vec3 tangentWS; + vec3 bitangentWS; + #endif + #endif +}; + vec4 material_TilingOffset; vec2 getUV0(Attributes attributes){ vec2 uv0 = vec2(0); @@ -18,32 +35,31 @@ vec2 getUV0(Attributes attributes){ return uv0 * material_TilingOffset.xy + material_TilingOffset.zw; } - -void initTransform(Attributes attributes, inout Varyings varyings){ - vec4 position = vec4( attributes.POSITION , 1.0 ); +VertexInputs getVertexInputs(Attributes attributes){ + VertexInputs inputs; + vec4 position = vec4(attributes.POSITION, 1.0); #ifdef RENDERER_HAS_NORMAL vec3 normal = vec3( attributes.NORMAL ); + #ifdef RENDERER_HAS_TANGENT + vec4 tangent = vec4( attributes.TANGENT ); + #endif #endif - #ifdef RENDERER_HAS_TANGENT - vec4 tangent = vec4( attributes.TANGENT ); - #endif - - - // blendShape + + // BlendShape #ifdef RENDERER_HAS_BLENDSHAPE calculateBlendShape(attributes, position #ifdef RENDERER_HAS_NORMAL ,normal - #endif - #ifdef RENDERER_HAS_TANGENT - ,tangent + #ifdef RENDERER_HAS_TANGENT + ,tangent + #endif #endif ); #endif - // skin + // Skin #ifdef RENDERER_HAS_SKIN mat4 skinMatrix = getSkinMatrix(attributes); position = skinMatrix * position; @@ -57,35 +73,30 @@ void initTransform(Attributes attributes, inout Varyings varyings){ #endif #endif - - // fog - #if SCENE_FOG_MODE != 0 - vec4 positionVS = renderer_MVMat * position; - varyings.v_positionVS = positionVS.xyz / positionVS.w; - #endif - - - // normal and tangent + // TBN world space #ifdef RENDERER_HAS_NORMAL - varyings.v_normal = normalize( mat3(renderer_NormalMat) * normal ); + inputs.normalWS = normalize( mat3(renderer_NormalMat) * normal ); #ifdef RENDERER_HAS_TANGENT - vec3 tangentW = normalize( mat3(renderer_NormalMat) * tangent.xyz ); - vec3 bitangentW = cross( varyings.v_normal, tangentW ) * tangent.w; + vec3 tangentWS = normalize( mat3(renderer_NormalMat) * tangent.xyz ); + vec3 bitangentWS = cross( inputs.normalWS, tangentWS ) * tangent.w; - varyings.v_tangent = tangentW; - varyings.v_bitangent = bitangentW; + inputs.tangentWS = tangentWS; + inputs.bitangentWS = bitangentWS; #endif #endif - // worldpos_vert - vec4 temp_pos = renderer_ModelMat * position; - varyings.v_pos = temp_pos.xyz / temp_pos.w; + inputs.positionOS = position; + vec4 positionWS = renderer_ModelMat * position; + inputs.positionWS = positionWS.xyz / positionWS.w; + #if SCENE_FOG_MODE != 0 + vec4 positionVS = renderer_MVMat * position; + inputs.positionVS = positionVS.xyz / positionVS.w; + #endif - // position_vert - gl_Position = renderer_MVPMat * position; + return inputs; } #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 4e6413cf..b0be96a2 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -11,6 +11,7 @@ #include "LightDirectPBR.glsl" #include "LightIndirectPBR.glsl" + Varyings PBRVertex(Attributes attributes) { Varyings varyings; @@ -23,12 +24,33 @@ Varyings PBRVertex(Attributes attributes) { varyings.v_color = attributes.COLOR_0; #endif - initTransform(attributes, varyings); - #if defined(SCENE_SHADOW_CASCADED_COUNT) && (SCENE_SHADOW_CASCADED_COUNT == 1) - // varyings.v_shadowCoord = getShadowCoord(varyings.v_pos); + VertexInputs vertexInputs = getVertexInputs(attributes); + + // positionWS + varyings.v_pos = vertexInputs.positionWS; + + // positionVS + #if SCENE_FOG_MODE != 0 + varyings.v_positionVS = vertexInputs.positionVS; + #endif + + // normalWS、tangentWS、bitangentWS + #ifdef RENDERER_HAS_NORMAL + varyings.v_normal = vertexInputs.normalWS; + #ifdef RENDERER_HAS_TANGENT + varyings.v_tangent = vertexInputs.tangentWS; + varyings.v_bitangent = vertexInputs.bitangentWS; + #endif #endif + // ShadowCoord + #if defined(SCENE_IS_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) + varyings.v_shadowCoord = getShadowCoord(vertexInputs.positionWS); + #endif + + gl_Position = renderer_MVPMat * vertexInputs.positionOS; + return varyings; } diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index d065e3f9..b216512f 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -11,6 +11,8 @@ struct Varyings{ vec4 v_color; #endif + vec3 v_pos; + #if SCENE_FOG_MODE != 0 vec3 v_positionVS; #endif @@ -23,9 +25,8 @@ struct Varyings{ #endif #endif - vec3 v_pos; - #if defined(SCENE_SHADOW_CASCADED_COUNT) && (SCENE_SHADOW_CASCADED_COUNT == 1) + #if defined(SCENE_IS_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) vec3 v_shadowCoord; #endif }; From 57945a5b9a5fafbba7be15c8035e086c6c9f9e50 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 17 Jul 2024 19:40:58 +0800 Subject: [PATCH 81/89] fix: pre SCENE_IS_CALCULATE_SHADOWS --- packages/shaderlab/src/shaders/Shadow.glsl | 5 +---- packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/shaderlab/src/shaders/Shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl index 335b0706..ae34d0fa 100644 --- a/packages/shaderlab/src/shaders/Shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -2,10 +2,7 @@ #define SHADOW_INCLUDED #include "Transform.glsl" - -#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) - #define SCENE_IS_CALCULATE_SHADOWS -#endif +#include "Common.glsl" #ifdef SCENE_IS_CALCULATE_SHADOWS #if SCENE_SHADOW_CASCADED_COUNT==1 diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index b216512f..d5c88e00 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -1,6 +1,10 @@ #ifndef VARYINGS_PBR_INCLUDED #define VARYINGS_PBR_INCLUDED +#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) + #define SCENE_IS_CALCULATE_SHADOWS +#endif + struct Varyings{ vec2 v_uv; #ifdef RENDERER_HAS_UV1 From 67fdc58729f76d46dbfbbcc45ea2b10af1bed403 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 17 Jul 2024 19:58:26 +0800 Subject: [PATCH 82/89] refactor: add Macros.glsl --- packages/shaderlab/src/shaders/Macros.glsl | 14 ++++++++++ packages/shaderlab/src/shaders/index.ts | 2 ++ .../src/shaders/shadingPBR/AttributesPBR.glsl | 27 ++++++++++--------- .../shaders/shadingPBR/ForwardPassPBR.glsl | 1 + .../shaders/shadingPBR/MaterialInputPBR.glsl | 4 --- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 4 --- 6 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 packages/shaderlab/src/shaders/Macros.glsl diff --git a/packages/shaderlab/src/shaders/Macros.glsl b/packages/shaderlab/src/shaders/Macros.glsl new file mode 100644 index 00000000..d7caf04b --- /dev/null +++ b/packages/shaderlab/src/shaders/Macros.glsl @@ -0,0 +1,14 @@ +#ifndef MACROS_INCLUDED +#define MACROS_INCLUDED + +#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) + #define SCENE_IS_CALCULATE_SHADOWS +#endif + + +#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) + #define NEED_TANGENT +#endif + + +#endif diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index f1730f97..a5612e00 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -2,6 +2,7 @@ import BlendShape from "./BlendShape.glsl"; import Common from "./Common.glsl"; import Fog from "./Fog.glsl"; import Light from "./Light.glsl"; +import Macros from "./Macros.glsl"; import Normal from "./Normal.glsl"; import PBRSource from "./PBR.gs"; import Shadow from "./Shadow.glsl"; @@ -27,6 +28,7 @@ const fragmentList: IShaderFragment[] = [ { source: Transform, includeKey: "Transform.glsl" }, { source: Vertex, includeKey: "Vertex.glsl" }, { source: Skin, includeKey: "Skin.glsl" }, + { source: Macros, includeKey: "Macros.glsl" }, ...shadingPBR ]; diff --git a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl index 96569034..df8b010b 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/AttributesPBR.glsl @@ -1,6 +1,7 @@ #ifndef ATTRIBUTES_PBR_INCLUDED #define ATTRIBUTES_PBR_INCLUDED + struct Attributes{ vec3 POSITION; @@ -45,22 +46,22 @@ struct Attributes{ #endif - #ifdef RENDERER_HAS_UV - vec2 TEXCOORD_0; - #endif + #ifdef RENDERER_HAS_UV + vec2 TEXCOORD_0; + #endif - #ifdef RENDERER_HAS_UV1 - vec2 TEXCOORD_1; - #endif + #ifdef RENDERER_HAS_UV1 + vec2 TEXCOORD_1; + #endif - #ifdef RENDERER_HAS_SKIN - vec4 JOINTS_0; - vec4 WEIGHTS_0; - #endif + #ifdef RENDERER_HAS_SKIN + vec4 JOINTS_0; + vec4 WEIGHTS_0; + #endif - #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 COLOR_0; - #endif + #ifdef RENDERER_ENABLE_VERTEXCOLOR + vec4 COLOR_0; + #endif #ifdef RENDERER_HAS_NORMAL vec3 NORMAL; diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index b0be96a2..2945bd8a 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -1,6 +1,7 @@ #ifndef FORWARD_PASS_PBR_INCLUDED #define FORWARD_PASS_PBR_INCLUDED +#include "Macros.glsl" #include "AttributesPBR.glsl" #include "VaryingsPBR.glsl" #include "Common.glsl" diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl index f0aa5ac4..c121a9c4 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl @@ -3,10 +3,6 @@ #include "Normal.glsl" -#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) - #define NEED_TANGENT -#endif - struct SurfaceData{ // common vec3 albedoColor; diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index d5c88e00..b216512f 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -1,10 +1,6 @@ #ifndef VARYINGS_PBR_INCLUDED #define VARYINGS_PBR_INCLUDED -#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) - #define SCENE_IS_CALCULATE_SHADOWS -#endif - struct Varyings{ vec2 v_uv; #ifdef RENDERER_HAS_UV1 From c8ac01544af900ff32b18eae8141f5a9a52e2737 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 17 Jul 2024 21:16:57 +0800 Subject: [PATCH 83/89] refactor: adapter to webgl1 --- packages/shaderlab/src/shaders/Light.glsl | 56 ++++++++++--------- packages/shaderlab/src/shaders/Vertex.glsl | 2 +- .../shaders/shadingPBR/LightDirectPBR.glsl | 30 +++++++++- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/packages/shaderlab/src/shaders/Light.glsl b/packages/shaderlab/src/shaders/Light.glsl index 2c4eeefc..3101ae1c 100644 --- a/packages/shaderlab/src/shaders/Light.glsl +++ b/packages/shaderlab/src/shaders/Light.glsl @@ -34,13 +34,15 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ vec3 scene_DirectLightColor[SCENE_DIRECT_LIGHT_COUNT]; vec3 scene_DirectLightDirection[SCENE_DIRECT_LIGHT_COUNT]; - DirectLight getDirectLight(int index){ - DirectLight light; - light.color = scene_DirectLightColor[index]; - light.direction = scene_DirectLightDirection[index]; - - return light; - } + #ifdef GRAPHICS_API_WEBGL2 + DirectLight getDirectLight(int index){ + DirectLight light; + light.color = scene_DirectLightColor[index]; + light.direction = scene_DirectLightDirection[index]; + + return light; + } + #endif #endif @@ -59,14 +61,16 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ vec3 scene_PointLightPosition[ SCENE_POINT_LIGHT_COUNT ]; float scene_PointLightDistance[ SCENE_POINT_LIGHT_COUNT ]; - PointLight getPointLight(int index){ - PointLight light; - light.color = scene_PointLightColor[index]; - light.position = scene_PointLightPosition[index]; - light.distance = scene_PointLightDistance[index]; + #ifdef GRAPHICS_API_WEBGL2 + PointLight getPointLight(int index){ + PointLight light; + light.color = scene_PointLightColor[index]; + light.position = scene_PointLightPosition[index]; + light.distance = scene_PointLightDistance[index]; - return light; - } + return light; + } + #endif #endif @@ -91,17 +95,19 @@ bool isRendererCulledByLight(ivec2 rendererLayer, ivec2 lightCullingMask){ float scene_SpotLightAngleCos[ SCENE_SPOT_LIGHT_COUNT ]; float scene_SpotLightPenumbraCos[ SCENE_SPOT_LIGHT_COUNT ]; - SpotLight getSpotLight(int index){ - SpotLight light; - light.color = scene_SpotLightColor[index]; - light.position = scene_SpotLightPosition[index]; - light.direction = scene_SpotLightDirection[index]; - light.distance = scene_SpotLightDistance[index]; - light.angleCos = scene_SpotLightAngleCos[index]; - light.penumbraCos = scene_SpotLightPenumbraCos[index]; - - return light; - } + #ifdef GRAPHICS_API_WEBGL2 + SpotLight getSpotLight(int index){ + SpotLight light; + light.color = scene_SpotLightColor[index]; + light.position = scene_SpotLightPosition[index]; + light.direction = scene_SpotLightDirection[index]; + light.distance = scene_SpotLightDistance[index]; + light.angleCos = scene_SpotLightAngleCos[index]; + light.penumbraCos = scene_SpotLightPenumbraCos[index]; + + return light; + } + #endif #endif diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/Vertex.glsl index 7641114c..b35eb033 100644 --- a/packages/shaderlab/src/shaders/Vertex.glsl +++ b/packages/shaderlab/src/shaders/Vertex.glsl @@ -47,7 +47,7 @@ VertexInputs getVertexInputs(Attributes attributes){ #endif - // BlendShape + // BlendShape #ifdef RENDERER_HAS_BLENDSHAPE calculateBlendShape(attributes, position #ifdef RENDERER_HAS_NORMAL diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 9e6ed568..d16dc661 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -125,7 +125,14 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { // warning: use `continue` syntax may trigger flickering bug in safri 16.1. if(!isRendererCulledByLight(renderer_Layer.xy, scene_DirectLightCullingMask[i])){ - DirectLight directionalLight = getDirectLight(i); + #ifdef GRAPHICS_API_WEBGL2 + DirectLight directionalLight = getDirectLight(i); + #else + DirectLight directionalLight; + directionalLight.color = scene_DirectLightColor[i]; + directionalLight.direction = scene_DirectLightDirection[i]; + #endif + #ifdef SCENE_IS_CALCULATE_SHADOWS if (i == 0) { // Sun light index is always 0 directionalLight.color *= shadowAttenuation; @@ -141,7 +148,14 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ for ( int i = 0; i < SCENE_POINT_LIGHT_COUNT; i ++ ) { if(!isRendererCulledByLight(renderer_Layer.xy, scene_PointLightCullingMask[i])){ - PointLight pointLight = getPointLight(i); + #ifdef GRAPHICS_API_WEBGL2 + PointLight pointLight = getPointLight(i); + #else + PointLight pointLight; + pointLight.color = scene_PointLightColor[i]; + pointLight.position = scene_PointLightPosition[i]; + pointLight.distance = scene_PointLightDistance[i]; + #endif addPointDirectLightRadiance( pointLight, brdfData, color ); } } @@ -152,7 +166,17 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ for ( int i = 0; i < SCENE_SPOT_LIGHT_COUNT; i ++ ) { if(!isRendererCulledByLight(renderer_Layer.xy, scene_SpotLightCullingMask[i])){ - SpotLight spotLight = getSpotLight(i); + #ifdef GRAPHICS_API_WEBGL2 + SpotLight spotLight = getSpotLight(i); + #else + SpotLight spotLight; + spotLight.color = scene_SpotLightColor[i]; + spotLight.position = scene_SpotLightPosition[i]; + spotLight.direction = scene_SpotLightDirection[i]; + spotLight.distance = scene_SpotLightDistance[i]; + spotLight.angleCos = scene_SpotLightAngleCos[i]; + spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; + #endif addSpotDirectLightRadiance( spotLight, brdfData, color ); } } From 74034300148186bbf8080108bdabacdbac6c22d9 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 17 Jul 2024 21:22:28 +0800 Subject: [PATCH 84/89] refactor: rename --- packages/shaderlab/src/shaders/Macros.glsl | 2 +- packages/shaderlab/src/shaders/Shadow.glsl | 4 ++-- packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl | 2 +- packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl | 4 ++-- packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/shaderlab/src/shaders/Macros.glsl b/packages/shaderlab/src/shaders/Macros.glsl index d7caf04b..04fa7e52 100644 --- a/packages/shaderlab/src/shaders/Macros.glsl +++ b/packages/shaderlab/src/shaders/Macros.glsl @@ -2,7 +2,7 @@ #define MACROS_INCLUDED #if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) - #define SCENE_IS_CALCULATE_SHADOWS + #define NEED_CALCULATE_SHADOWS #endif diff --git a/packages/shaderlab/src/shaders/Shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl index ae34d0fa..d224bdba 100644 --- a/packages/shaderlab/src/shaders/Shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -4,7 +4,7 @@ #include "Transform.glsl" #include "Common.glsl" -#ifdef SCENE_IS_CALCULATE_SHADOWS +#ifdef NEED_CALCULATE_SHADOWS #if SCENE_SHADOW_CASCADED_COUNT==1 mat4 scene_ShadowMatrices[SCENE_SHADOW_CASCADED_COUNT + 1]; @@ -77,7 +77,7 @@ #endif -#ifdef SCENE_IS_CALCULATE_SHADOWS +#ifdef NEED_CALCULATE_SHADOWS // intensity, null, fadeScale, fadeBias vec4 scene_ShadowInfo; vec4 scene_ShadowMapSize; diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 2945bd8a..2d7f3529 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -46,7 +46,7 @@ Varyings PBRVertex(Attributes attributes) { #endif // ShadowCoord - #if defined(SCENE_IS_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) + #if defined(NEED_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) varyings.v_shadowCoord = getShadowCoord(vertexInputs.positionWS); #endif diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index d16dc661..04063c8a 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -118,7 +118,7 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ #ifdef SCENE_DIRECT_LIGHT_COUNT shadowAttenuation = 1.0; - #ifdef SCENE_IS_CALCULATE_SHADOWS + #ifdef NEED_CALCULATE_SHADOWS shadowAttenuation *= sampleShadowMap(v); #endif @@ -133,7 +133,7 @@ void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ directionalLight.direction = scene_DirectLightDirection[i]; #endif - #ifdef SCENE_IS_CALCULATE_SHADOWS + #ifdef NEED_CALCULATE_SHADOWS if (i == 0) { // Sun light index is always 0 directionalLight.color *= shadowAttenuation; } diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index b216512f..f7fda0e4 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -26,7 +26,7 @@ struct Varyings{ #endif - #if defined(SCENE_IS_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) + #if defined(NEED_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) vec3 v_shadowCoord; #endif }; From 1cbf9becb63df73e6f2cee4cced2015e969365e1 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 18 Jul 2024 00:31:01 +0800 Subject: [PATCH 85/89] refactor: split varyings and attributes from light --- packages/shaderlab/src/shaders/Macros.glsl | 14 -- packages/shaderlab/src/shaders/Shadow.glsl | 17 +-- packages/shaderlab/src/shaders/index.ts | 4 - .../src/shaders/shadingPBR/BRDF.glsl | 69 +++++++++ .../shaders/shadingPBR/ForwardPassPBR.glsl | 35 +++-- ...MaterialInputPBR.glsl => FragmentPBR.glsl} | 144 +++++++----------- .../shaders/shadingPBR/LightDirectPBR.glsl | 8 +- .../shaders/shadingPBR/LightIndirectPBR.glsl | 54 ++----- .../VertexPBR.glsl} | 0 .../shaderlab/src/shaders/shadingPBR/index.ts | 6 +- 10 files changed, 171 insertions(+), 180 deletions(-) delete mode 100644 packages/shaderlab/src/shaders/Macros.glsl rename packages/shaderlab/src/shaders/shadingPBR/{MaterialInputPBR.glsl => FragmentPBR.glsl} (89%) rename packages/shaderlab/src/shaders/{Vertex.glsl => shadingPBR/VertexPBR.glsl} (100%) diff --git a/packages/shaderlab/src/shaders/Macros.glsl b/packages/shaderlab/src/shaders/Macros.glsl deleted file mode 100644 index 04fa7e52..00000000 --- a/packages/shaderlab/src/shaders/Macros.glsl +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef MACROS_INCLUDED -#define MACROS_INCLUDED - -#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) - #define NEED_CALCULATE_SHADOWS -#endif - - -#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) - #define NEED_TANGENT -#endif - - -#endif diff --git a/packages/shaderlab/src/shaders/Shadow.glsl b/packages/shaderlab/src/shaders/Shadow.glsl index d224bdba..0fee182c 100644 --- a/packages/shaderlab/src/shaders/Shadow.glsl +++ b/packages/shaderlab/src/shaders/Shadow.glsl @@ -4,8 +4,13 @@ #include "Transform.glsl" #include "Common.glsl" +#if defined(SCENE_SHADOW_TYPE) && defined(RENDERER_IS_RECEIVE_SHADOWS) + #define NEED_CALCULATE_SHADOWS +#endif + + #ifdef NEED_CALCULATE_SHADOWS - #if SCENE_SHADOW_CASCADED_COUNT==1 + #if SCENE_SHADOW_CASCADED_COUNT == 1 mat4 scene_ShadowMatrices[SCENE_SHADOW_CASCADED_COUNT + 1]; vec4 scene_ShadowSplitSpheres[4]; @@ -150,13 +155,7 @@ } - float sampleShadowMap(Varyings v) { - #if SCENE_SHADOW_CASCADED_COUNT == 1 - vec3 shadowCoord = v.v_shadowCoord; - #else - vec3 shadowCoord = getShadowCoord(v.v_pos); - #endif - + float sampleShadowMap(vec3 positionWS, vec3 shadowCoord) { float attenuation = 1.0; if(shadowCoord.z > 0.0 && shadowCoord.z < 1.0) { #if SCENE_SHADOW_TYPE == 1 @@ -173,7 +172,7 @@ attenuation = mix(1.0, attenuation, scene_ShadowInfo.x); } - float shadowFade = getShadowFade(v.v_pos); + float shadowFade = getShadowFade(positionWS); attenuation = mix(1.0, mix(attenuation, 1.0, shadowFade), scene_ShadowInfo.x); return attenuation; diff --git a/packages/shaderlab/src/shaders/index.ts b/packages/shaderlab/src/shaders/index.ts index a5612e00..de23d2e3 100644 --- a/packages/shaderlab/src/shaders/index.ts +++ b/packages/shaderlab/src/shaders/index.ts @@ -2,14 +2,12 @@ import BlendShape from "./BlendShape.glsl"; import Common from "./Common.glsl"; import Fog from "./Fog.glsl"; import Light from "./Light.glsl"; -import Macros from "./Macros.glsl"; import Normal from "./Normal.glsl"; import PBRSource from "./PBR.gs"; import Shadow from "./Shadow.glsl"; import ShadowSampleTent from "./ShadowSampleTent.glsl"; import Skin from "./Skin.glsl"; import Transform from "./Transform.glsl"; -import Vertex from "./Vertex.glsl"; import shadingPBR from "./shadingPBR"; interface IShaderFragment { @@ -26,9 +24,7 @@ const fragmentList: IShaderFragment[] = [ { source: ShadowSampleTent, includeKey: "ShadowSampleTent.glsl" }, { source: Shadow, includeKey: "Shadow.glsl" }, { source: Transform, includeKey: "Transform.glsl" }, - { source: Vertex, includeKey: "Vertex.glsl" }, { source: Skin, includeKey: "Skin.glsl" }, - { source: Macros, includeKey: "Macros.glsl" }, ...shadingPBR ]; diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index 949f3f7a..46bc63e5 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -2,6 +2,75 @@ #ifndef BRDF_INCLUDED #define BRDF_INCLUDED +#define MIN_PERCEPTUAL_ROUGHNESS 0.045 +#define MIN_ROUGHNESS 0.002025 + +#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) + #define NEED_TANGENT +#endif + +struct SurfaceData{ + // common + vec3 albedoColor; + vec3 specularColor; + vec3 emissiveColor; + float metallic; + float roughness; + float f0; + float opacity; + + // geometry + vec3 position; + vec3 normal; + + #ifdef NEED_TANGENT + vec3 tangent; + vec3 bitangent; + #endif + + vec3 viewDir; +}; + + +struct BRDFData{ + // common + vec3 diffuseColor; + vec3 specularColor; + float roughness; + float diffuseAO; + float specularAO; + + // geometry + vec3 position; + vec3 normal; + + #ifdef NEED_TANGENT + vec3 tangent; + vec3 bitangent; + #endif + + vec3 viewDir; + float dotNV; + + // Anisotropy + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy; + vec3 anisotropicT; + vec3 anisotropicB; + vec3 anisotropicN; + #endif + + // Clear coat + #ifdef MATERIAL_ENABLE_CLEAR_COAT + float clearCoat; + float clearCoatRoughness; + vec3 clearCoatNormal; + float clearCoatDotNV; + #endif +}; + + + float F_Schlick(float f0, float dotLH) { return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); } diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 2d7f3529..b7212a99 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -1,17 +1,18 @@ #ifndef FORWARD_PASS_PBR_INCLUDED #define FORWARD_PASS_PBR_INCLUDED -#include "Macros.glsl" -#include "AttributesPBR.glsl" -#include "VaryingsPBR.glsl" #include "Common.glsl" -#include "Vertex.glsl" #include "Fog.glsl" -#include "MaterialInputPBR.glsl" #include "LightDirectPBR.glsl" #include "LightIndirectPBR.glsl" +#include "AttributesPBR.glsl" +#include "VaryingsPBR.glsl" +#include "VertexPBR.glsl" +#include "FragmentPBR.glsl" + + Varyings PBRVertex(Attributes attributes) { Varyings varyings; @@ -56,19 +57,31 @@ Varyings PBRVertex(Attributes attributes) { } void PBRFragment(Varyings varyings) { - SurfaceData surfaceData; - BRDFData brdfData; + SurfaceData surfaceData = getSurfaceData(varyings, gl_FrontFacing); - initSurfaceData(varyings, surfaceData, gl_FrontFacing); + BRDFData brdfData; // Can modify surfaceData here. initBRDFData(varyings, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); - // Direct Light - evaluateDirectRadiance(varyings, brdfData, color.rgb); + // Get shadow attenuation + float shadowAttenuation = 1.0; + #if defined(SCENE_DIRECT_LIGHT_COUNT) && defined(NEED_CALCULATE_SHADOWS) + #if SCENE_SHADOW_CASCADED_COUNT == 1 + vec3 shadowCoord = varyings.v_shadowCoord; + #else + vec3 shadowCoord = getShadowCoord(varyings.v_pos); + #endif + shadowAttenuation *= sampleShadowMap(varyings.v_pos, shadowCoord); + #endif + + // Evaluate direct lighting + evaluateDirectRadiance(shadowAttenuation, brdfData, color.rgb); + // IBL - evaluateIBL(varyings, brdfData, color.rgb); + evaluateIBL(brdfData, color.rgb); + // Emissive color.rgb += surfaceData.emissiveColor; diff --git a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl similarity index 89% rename from packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl rename to packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl index c121a9c4..68e91c79 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/MaterialInputPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl @@ -3,66 +3,6 @@ #include "Normal.glsl" -struct SurfaceData{ - // common - vec3 albedoColor; - vec3 specularColor; - vec3 emissiveColor; - float metallic; - float roughness; - float f0; - float opacity; - - // geometry - vec3 position; - vec3 normal; - - #ifdef NEED_TANGENT - vec3 tangent; - vec3 bitangent; - #endif - - vec3 viewDir; -}; - -struct BRDFData{ - // common - vec3 diffuseColor; - vec3 specularColor; - float roughness; - - // geometry - vec3 position; - vec3 normal; - - #ifdef NEED_TANGENT - vec3 tangent; - vec3 bitangent; - #endif - - vec3 viewDir; - float dotNV; - - // Anisotropy - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy; - vec3 anisotropicT; - vec3 anisotropicB; - vec3 anisotropicN; - #endif - - // Clear coat - #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoat; - float clearCoatRoughness; - vec3 clearCoatNormal; - float clearCoatDotNV; - #endif -}; - -#define MIN_PERCEPTUAL_ROUGHNESS 0.045 -#define MIN_ROUGHNESS 0.002025 - float material_AlphaCutoff; vec4 material_BaseColor; float material_Metal; @@ -126,35 +66,8 @@ float material_OcclusionTextureCoord; #endif - -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return max( max(dxy.x, dxy.y), dxy.z ); - #else - return 0.0; - #endif -} - -#ifdef MATERIAL_ENABLE_ANISOTROPY - // Aniso Bent Normals - // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(BRDFData brdfData) { - vec3 anisotropyDirection = (brdfData.anisotropy >= 0.0) ? brdfData.anisotropicB : brdfData.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, brdfData.viewDir); - vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); - // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(brdfData.normal, anisotropicNormal, abs(brdfData.anisotropy) * saturate( 5.0 * brdfData.roughness)) ); - - return bentNormal; - } -#endif - - -void initSurfaceData(Varyings v, out SurfaceData surfaceData, bool isFrontFacing){ +SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ + SurfaceData surfaceData; // common vec4 baseColor = material_BaseColor; float metallic = material_Metal; @@ -242,8 +155,38 @@ void initSurfaceData(Varyings v, out SurfaceData surfaceData, bool isFrontFacing #else surfaceData.normal = getNormal(v, isFrontFacing); #endif + + return surfaceData; +} + +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return max( max(dxy.x, dxy.y), dxy.z ); + #else + return 0.0; + #endif } +#ifdef MATERIAL_ENABLE_ANISOTROPY + // Aniso Bent Normals + // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far + vec3 getAnisotropicBentNormal(BRDFData brdfData) { + vec3 anisotropyDirection = (brdfData.anisotropy >= 0.0) ? brdfData.anisotropicB : brdfData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, brdfData.viewDir); + vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); + // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 + vec3 bentNormal = normalize( mix(brdfData.normal, anisotropicNormal, abs(brdfData.anisotropy) * saturate( 5.0 * brdfData.roughness)) ); + + return bentNormal; + } +#endif + + + void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.position = surfaceData.position; brdfData.normal = surfaceData.normal; @@ -322,11 +265,34 @@ void initAnisotropyBRDFData(Varyings v, inout BRDFData brdfData){ } +void initAO(Varyings v, inout BRDFData brdfData){ + float diffuseAO = 1.0; + float specularAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + vec2 aoUV = v.v_uv; + #ifdef RENDERER_HAS_UV1 + if(material_OcclusionTextureCoord == 1.0){ + aoUV = v.v_uv1; + } + #endif + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( brdfData.dotNV + diffuseAO, exp2( - 16.0 * brdfData.roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + brdfData.diffuseAO = diffuseAO; + brdfData.specularAO = specularAO; +} + void initBRDFData(Varyings v, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ initGeometryData(surfaceData, brdfData); initCommonBRDFData(surfaceData, brdfData); initClearCoatBRDFData(v, brdfData, isFrontFacing); initAnisotropyBRDFData(v, brdfData); + initAO(v, brdfData); } diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index 04063c8a..d014e789 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -113,14 +113,8 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, #endif -void evaluateDirectRadiance(Varyings v, BRDFData brdfData, inout vec3 color){ - float shadowAttenuation = 1.0; - +void evaluateDirectRadiance(float shadowAttenuation, BRDFData brdfData, inout vec3 color){ #ifdef SCENE_DIRECT_LIGHT_COUNT - shadowAttenuation = 1.0; - #ifdef NEED_CALCULATE_SHADOWS - shadowAttenuation *= sampleShadowMap(v); - #endif for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { // warning: use `continue` syntax may trigger flickering bug in safri 16.1. diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index b46b4c63..a8e5ae15 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -2,12 +2,6 @@ #ifndef LIGHT_INDIRECT_PBR_INCLUDED #define LIGHT_INDIRECT_PBR_INCLUDED -#ifndef FUNCTION_DIFFUSE_AO - #define FUNCTION_DIFFUSE_AO evaluateDiffuseAO -#endif -#ifndef FUNCTION_SPECULAR_AO - #define FUNCTION_SPECULAR_AO evaluateSpecularAO -#endif #ifndef FUNCTION_DIFFUSE_IBL #define FUNCTION_DIFFUSE_IBL evaluateDiffuseIBL #endif @@ -112,7 +106,7 @@ vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { } -void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 diffuseColor){ +void evaluateDiffuseIBL(BRDFData brdfData, inout vec3 diffuseColor){ #ifdef SCENE_USE_SH vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, brdfData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA @@ -124,67 +118,39 @@ void evaluateDiffuseIBL(BRDFData brdfData, float diffuseAO, inout vec3 diffuseCo irradiance *= PI; #endif - diffuseColor += diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); + diffuseColor += brdfData.diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -float evaluateClearCoatIBL(BRDFData brdfData, float specularAO, inout vec3 specularColor){ +float evaluateClearCoatIBL(BRDFData brdfData, inout vec3 specularColor){ float radianceAttenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT vec3 clearCoatRadiance = getLightProbeRadiance( brdfData, brdfData.clearCoatNormal, brdfData.clearCoatRoughness); - specularColor += specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); + specularColor += brdfData.specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); radianceAttenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); #endif return radianceAttenuation; } -void evaluateSpecularIBL(BRDFData brdfData, float specularAO, float radianceAttenuation, inout vec3 specularColor){ +void evaluateSpecularIBL(BRDFData brdfData, float radianceAttenuation, inout vec3 specularColor){ vec3 radiance = getLightProbeRadiance(brdfData, brdfData.normal, brdfData.roughness); - specularColor += specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, brdfData.dotNV ); + specularColor += brdfData.specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, brdfData.dotNV ); } -float evaluateDiffuseAO(Varyings v){ - float diffuseAO = 1.0; - - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; - } - #endif - diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - #endif - - return diffuseAO; -} - -float evaluateSpecularAO(float diffuseAO, float roughness, float dotNV){ - float specularAO = 1.0; - - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) - specularAO = saturate( pow( dotNV + diffuseAO, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + diffuseAO ); - #endif - - return specularAO; -} - -void evaluateIBL(Varyings v, BRDFData brdfData, inout vec3 color){ +void evaluateIBL(BRDFData brdfData, inout vec3 color){ vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); - float diffuseAO = FUNCTION_DIFFUSE_AO(v); - float specularAO = FUNCTION_SPECULAR_AO(diffuseAO, brdfData.roughness, brdfData.dotNV); // IBL diffuse - FUNCTION_DIFFUSE_IBL(brdfData, diffuseAO, diffuseColor); + FUNCTION_DIFFUSE_IBL(brdfData, diffuseColor); // IBL ClearCoat - float radianceAttenuation = FUNCTION_CLEAR_COAT_IBL(brdfData, specularAO, specularColor); + float radianceAttenuation = FUNCTION_CLEAR_COAT_IBL(brdfData, specularColor); // IBL specular - FUNCTION_SPECULAR_IBL(brdfData, specularAO, radianceAttenuation, specularColor); + FUNCTION_SPECULAR_IBL(brdfData, radianceAttenuation, specularColor); color += diffuseColor + specularColor; } diff --git a/packages/shaderlab/src/shaders/Vertex.glsl b/packages/shaderlab/src/shaders/shadingPBR/VertexPBR.glsl similarity index 100% rename from packages/shaderlab/src/shaders/Vertex.glsl rename to packages/shaderlab/src/shaders/shadingPBR/VertexPBR.glsl diff --git a/packages/shaderlab/src/shaders/shadingPBR/index.ts b/packages/shaderlab/src/shaders/shadingPBR/index.ts index 3abeef71..d30ee8b5 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/index.ts +++ b/packages/shaderlab/src/shaders/shadingPBR/index.ts @@ -3,15 +3,17 @@ import BRDF from "./BRDF.glsl"; import ForwardPassPBR from "./ForwardPassPBR.glsl"; import LightDirectPBR from "./LightDirectPBR.glsl"; import LightIndirectPBR from "./LightIndirectPBR.glsl"; -import MaterialInputPBR from "./MaterialInputPBR.glsl"; +import FragmentPBR from "./FragmentPBR.glsl"; import VaryingsPBR from "./VaryingsPBR.glsl"; +import VertexPBR from "./VertexPBR.glsl"; export default [ { source: ForwardPassPBR, includeKey: "ForwardPassPBR.glsl" }, { source: AttributesPBR, includeKey: "AttributesPBR.glsl" }, { source: VaryingsPBR, includeKey: "VaryingsPBR.glsl" }, - { source: MaterialInputPBR, includeKey: "MaterialInputPBR.glsl" }, + { source: FragmentPBR, includeKey: "FragmentPBR.glsl" }, { source: LightDirectPBR, includeKey: "LightDirectPBR.glsl" }, { source: LightIndirectPBR, includeKey: "LightIndirectPBR.glsl" }, + { source: VertexPBR, includeKey: "VertexPBR.glsl" }, { source: BRDF, includeKey: "BRDF.glsl" } ]; From 0ad84df519893f52edde9825bc8e496de6747c98 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 18 Jul 2024 00:45:28 +0800 Subject: [PATCH 86/89] refactor: rename varyings --- packages/shaderlab/src/shaders/Normal.glsl | 12 ++++---- .../shaders/shadingPBR/ForwardPassPBR.glsl | 26 ++++++++--------- .../src/shaders/shadingPBR/FragmentPBR.glsl | 28 +++++++++---------- .../src/shaders/shadingPBR/VaryingsPBR.glsl | 18 ++++++------ 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index ac26729d..9a5b2430 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -6,10 +6,10 @@ // https://bugs.chromium.org/p/chromium/issues/detail?id=1154842 vec3 getNormal(Varyings varyings, bool isFrontFacing){ #ifdef RENDERER_HAS_NORMAL - vec3 normal = normalize(varyings.v_normal); + vec3 normal = normalize(varyings.normalWS); #elif defined(HAS_DERIVATIVES) - vec3 pos_dx = dFdx(varyings.v_pos); - vec3 pos_dy = dFdy(varyings.v_pos); + vec3 pos_dx = dFdx(varyings.positionWS); + vec3 pos_dy = dFdy(varyings.positionWS); vec3 normal = normalize( cross(pos_dx, pos_dy) ); #else vec3 normal = vec3(0, 0, 1); @@ -29,11 +29,11 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt mat3 getTBN(Varyings varyings, bool isFrontFacing){ #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) - mat3 tbn = mat3(varyings.v_tangent, varyings.v_bitangent, varyings.v_normal); + mat3 tbn = mat3(varyings.tangentWS, varyings.bitangentWS, varyings.normalWS); #else vec3 normal = getNormal(varyings, isFrontFacing); - vec3 position = varyings.v_pos; - vec2 uv = isFrontFacing? varyings.v_uv: -varyings.v_uv; + vec3 position = varyings.positionWS; + vec2 uv = isFrontFacing? varyings.uv: -varyings.uv; #ifdef HAS_DERIVATIVES // ref: http://www.thetenthplanet.de/archives/1180 diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index b7212a99..90c0035d 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -17,38 +17,38 @@ Varyings PBRVertex(Attributes attributes) { Varyings varyings; - varyings.v_uv = getUV0(attributes); + varyings.uv = getUV0(attributes); #ifdef RENDERER_HAS_UV1 - varyings.v_uv1 = attributes.TEXCOORD_1; + varyings.uv1 = attributes.TEXCOORD_1; #endif #ifdef RENDERER_ENABLE_VERTEXCOLOR - varyings.v_color = attributes.COLOR_0; + varyings.vertexColor = attributes.COLOR_0; #endif VertexInputs vertexInputs = getVertexInputs(attributes); // positionWS - varyings.v_pos = vertexInputs.positionWS; + varyings.positionWS = vertexInputs.positionWS; // positionVS #if SCENE_FOG_MODE != 0 - varyings.v_positionVS = vertexInputs.positionVS; + varyings.positionVS = vertexInputs.positionVS; #endif // normalWS、tangentWS、bitangentWS #ifdef RENDERER_HAS_NORMAL - varyings.v_normal = vertexInputs.normalWS; + varyings.normalWS = vertexInputs.normalWS; #ifdef RENDERER_HAS_TANGENT - varyings.v_tangent = vertexInputs.tangentWS; - varyings.v_bitangent = vertexInputs.bitangentWS; + varyings.tangentWS = vertexInputs.tangentWS; + varyings.bitangentWS = vertexInputs.bitangentWS; #endif #endif // ShadowCoord #if defined(NEED_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) - varyings.v_shadowCoord = getShadowCoord(vertexInputs.positionWS); + varyings.shadowCoord = getShadowCoord(vertexInputs.positionWS); #endif gl_Position = renderer_MVPMat * vertexInputs.positionOS; @@ -69,11 +69,11 @@ void PBRFragment(Varyings varyings) { float shadowAttenuation = 1.0; #if defined(SCENE_DIRECT_LIGHT_COUNT) && defined(NEED_CALCULATE_SHADOWS) #if SCENE_SHADOW_CASCADED_COUNT == 1 - vec3 shadowCoord = varyings.v_shadowCoord; + vec3 shadowCoord = varyings.shadowCoord; #else - vec3 shadowCoord = getShadowCoord(varyings.v_pos); + vec3 shadowCoord = getShadowCoord(varyings.positionWS); #endif - shadowAttenuation *= sampleShadowMap(varyings.v_pos, shadowCoord); + shadowAttenuation *= sampleShadowMap(varyings.positionWS, shadowCoord); #endif // Evaluate direct lighting @@ -87,7 +87,7 @@ void PBRFragment(Varyings varyings) { #if SCENE_FOG_MODE != 0 - color = fog(color, varyings.v_positionVS); + color = fog(color, varyings.positionVS); #endif #ifndef ENGINE_IS_COLORSPACE_GAMMA diff --git a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl index 68e91c79..a4bb2a94 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl @@ -78,7 +78,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ vec3 emissiveRadiance = material_EmissiveColor; #ifdef MATERIAL_HAS_BASETEXTURE - vec4 baseTextureColor = texture2D(material_BaseTexture, v.v_uv); + vec4 baseTextureColor = texture2D(material_BaseTexture, v.uv); #ifndef ENGINE_IS_COLORSPACE_GAMMA baseTextureColor = gammaToLinear(baseTextureColor); #endif @@ -86,7 +86,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ #endif #ifdef RENDERER_ENABLE_VERTEXCOLOR - baseColor *= v.v_color; + baseColor *= v.vertexColor; #endif @@ -97,13 +97,13 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ #endif #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE - vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.v_uv ); + vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.uv ); roughness *= metalRoughMapColor.g; metallic *= metalRoughMapColor.b; #endif #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE - vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.v_uv ); + vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.uv ); #ifndef ENGINE_IS_COLORSPACE_GAMMA specularGlossinessColor = gammaToLinear(specularGlossinessColor); #endif @@ -113,7 +113,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ #endif #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.v_uv); + vec4 emissiveColor = texture2D(material_EmissiveTexture, v.uv); #ifndef ENGINE_IS_COLORSPACE_GAMMA emissiveColor = gammaToLinear(emissiveColor); #endif @@ -135,12 +135,12 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ // geometry - surfaceData.position = v.v_pos; + surfaceData.position = v.positionWS; #ifdef CAMERA_ORTHOGRAPHIC surfaceData.viewDir = -camera_Forward; #else - surfaceData.viewDir = normalize(camera_Position - v.v_pos); + surfaceData.viewDir = normalize(camera_Position - v.positionWS); #endif #ifdef NEED_TANGENT @@ -148,7 +148,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ surfaceData.tangent = tbn[0]; surfaceData.bitangent = tbn[1]; #ifdef MATERIAL_HAS_NORMALTEXTURE - surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.uv, isFrontFacing); #else surfaceData.normal = tbn[2]; #endif @@ -221,7 +221,7 @@ void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ void initClearCoatBRDFData(Varyings v, inout BRDFData brdfData, bool isFrontFacing){ #ifdef MATERIAL_ENABLE_CLEAR_COAT #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, v.v_uv, isFrontFacing); + brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, v.uv, isFrontFacing); #else brdfData.clearCoatNormal = getNormal(v, isFrontFacing); #endif @@ -231,11 +231,11 @@ void initClearCoatBRDFData(Varyings v, inout BRDFData brdfData, bool isFrontFaci brdfData.clearCoatRoughness = material_ClearCoatRoughness; #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - brdfData.clearCoat *= (texture2D( material_ClearCoatTexture, v.v_uv )).r; + brdfData.clearCoat *= (texture2D( material_ClearCoatTexture, v.uv )).r; #endif #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - brdfData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.v_uv )).g; + brdfData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.uv )).g; #endif brdfData.clearCoat = saturate( brdfData.clearCoat ); @@ -251,7 +251,7 @@ void initAnisotropyBRDFData(Varyings v, inout BRDFData brdfData){ float anisotropy = material_AnisotropyInfo.z; vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.v_uv )).rgb; + vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.uv )).rgb; anisotropy *= anisotropyTextureInfo.b; anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; #endif @@ -270,10 +270,10 @@ void initAO(Varyings v, inout BRDFData brdfData){ float specularAO = 1.0; #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.v_uv; + vec2 aoUV = v.uv; #ifdef RENDERER_HAS_UV1 if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.v_uv1; + aoUV = v.uv1; } #endif diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; diff --git a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl index f7fda0e4..bf111d43 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/VaryingsPBR.glsl @@ -2,32 +2,32 @@ #define VARYINGS_PBR_INCLUDED struct Varyings{ - vec2 v_uv; + vec2 uv; #ifdef RENDERER_HAS_UV1 - vec2 v_uv1; + vec2 uv1; #endif #ifdef RENDERER_ENABLE_VERTEXCOLOR - vec4 v_color; + vec4 vertexColor; #endif - vec3 v_pos; + vec3 positionWS; #if SCENE_FOG_MODE != 0 - vec3 v_positionVS; + vec3 positionVS; #endif #ifdef RENDERER_HAS_NORMAL - vec3 v_normal; + vec3 normalWS; #ifdef RENDERER_HAS_TANGENT - vec3 v_tangent; - vec3 v_bitangent; + vec3 tangentWS; + vec3 bitangentWS; #endif #endif #if defined(NEED_CALCULATE_SHADOWS) && (SCENE_SHADOW_CASCADED_COUNT == 1) - vec3 v_shadowCoord; + vec3 shadowCoord; #endif }; From 9eac5152a7b2c4727e4108e8768a5b10debec4cc Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 18 Jul 2024 01:41:34 +0800 Subject: [PATCH 87/89] fix: tbn refactor --- packages/shaderlab/src/shaders/Normal.glsl | 59 ++--- .../src/shaders/shadingPBR/BRDF.glsl | 168 +++++++++++++- .../shaders/shadingPBR/ForwardPassPBR.glsl | 12 +- .../src/shaders/shadingPBR/FragmentPBR.glsl | 210 ++++-------------- 4 files changed, 242 insertions(+), 207 deletions(-) diff --git a/packages/shaderlab/src/shaders/Normal.glsl b/packages/shaderlab/src/shaders/Normal.glsl index 9a5b2430..3aefec6a 100644 --- a/packages/shaderlab/src/shaders/Normal.glsl +++ b/packages/shaderlab/src/shaders/Normal.glsl @@ -4,17 +4,7 @@ // gl_FrontFacing has random value on Adreno GPUs // the Adreno bug is only when gl_FrontFacing is inside a function // https://bugs.chromium.org/p/chromium/issues/detail?id=1154842 -vec3 getNormal(Varyings varyings, bool isFrontFacing){ - #ifdef RENDERER_HAS_NORMAL - vec3 normal = normalize(varyings.normalWS); - #elif defined(HAS_DERIVATIVES) - vec3 pos_dx = dFdx(varyings.positionWS); - vec3 pos_dy = dFdy(varyings.positionWS); - vec3 normal = normalize( cross(pos_dx, pos_dy) ); - #else - vec3 normal = vec3(0, 0, 1); - #endif - +vec3 getNormal(vec3 normal, bool isFrontFacing){ normal *= float( isFrontFacing ) * 2.0 - 1.0; return normal; } @@ -27,37 +17,26 @@ vec3 getNormalByNormalTexture(mat3 tbn, sampler2D normalTexture, float normalInt return normal; } -mat3 getTBN(Varyings varyings, bool isFrontFacing){ - #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) - mat3 tbn = mat3(varyings.tangentWS, varyings.bitangentWS, varyings.normalWS); +mat3 getTBNByDerivatives(vec2 uv, vec3 normal, vec3 position, bool isFrontFacing){ + #ifdef HAS_DERIVATIVES + uv = isFrontFacing? uv: -uv; + // ref: http://www.thetenthplanet.de/archives/1180 + // get edge vectors of the pixel triangle + vec3 dp1 = dFdx(position); + vec3 dp2 = dFdy(position); + vec2 duv1 = dFdx(uv); + vec2 duv2 = dFdy(uv); + // solve the linear system + vec3 dp2perp = cross(dp2, normal); + vec3 dp1perp = cross(normal, dp1); + vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x; + vec3 bitangent = dp2perp * duv1.y + dp1perp * duv2.y; + // construct a scale-invariant frame + float invmax = inversesqrt(max(dot(tangent, tangent), dot(bitangent, bitangent))); + return mat3(tangent * invmax, bitangent * invmax, normal); #else - vec3 normal = getNormal(varyings, isFrontFacing); - vec3 position = varyings.positionWS; - vec2 uv = isFrontFacing? varyings.uv: -varyings.uv; - - #ifdef HAS_DERIVATIVES - // ref: http://www.thetenthplanet.de/archives/1180 - // get edge vectors of the pixel triangle - vec3 dp1 = dFdx(position); - vec3 dp2 = dFdy(position); - vec2 duv1 = dFdx(uv); - vec2 duv2 = dFdy(uv); - - // solve the linear system - vec3 dp2perp = cross(dp2, normal); - vec3 dp1perp = cross(normal, dp1); - vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x; - vec3 bitangent = dp2perp * duv1.y + dp1perp * duv2.y; - - // construct a scale-invariant frame - float invmax = inversesqrt(max(dot(tangent, tangent), dot(bitangent, bitangent))); - mat3 tbn = mat3(tangent * invmax, bitangent * invmax, normal); - #else - mat3 tbn = mat3(vec3(0.0), vec3(0.0), normal); - #endif + return mat3(vec3(0.0), vec3(0.0), normal); #endif - - return tbn; } diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index 46bc63e5..464b9f70 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -5,10 +5,41 @@ #define MIN_PERCEPTUAL_ROUGHNESS 0.045 #define MIN_ROUGHNESS 0.002025 -#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) +#if defined(RENDERER_HAS_TANGENT) || defined(MATERIAL_ENABLE_ANISOTROPY) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE) || defined(MATERIAL_HAS_NORMALTEXTURE) #define NEED_TANGENT #endif + + +#ifdef MATERIAL_ENABLE_CLEAR_COAT + float material_ClearCoat; + float material_ClearCoatRoughness; + + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + sampler2D material_ClearCoatTexture; + #endif + + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + sampler2D material_ClearCoatRoughnessTexture; + #endif + + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + sampler2D material_ClearCoatNormalTexture; + #endif +#endif + +#ifdef MATERIAL_ENABLE_ANISOTROPY + vec3 material_AnisotropyInfo; + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + sampler2D material_AnisotropyTexture; + #endif +#endif + +#ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + sampler2D material_OcclusionTexture; +#endif + + struct SurfaceData{ // common vec3 albedoColor; @@ -69,6 +100,9 @@ struct BRDFData{ #endif }; +float material_OcclusionIntensity; +float material_OcclusionTextureCoord; + float F_Schlick(float f0, float dotLH) { @@ -196,5 +230,137 @@ vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor) { return RECIPROCAL_PI * diffuseColor; } +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return max( max(dxy.x, dxy.y), dxy.z ); + #else + return 0.0; + #endif +} + +#ifdef MATERIAL_ENABLE_ANISOTROPY + // Aniso Bent Normals + // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far + vec3 getAnisotropicBentNormal(BRDFData brdfData) { + vec3 anisotropyDirection = (brdfData.anisotropy >= 0.0) ? brdfData.anisotropicB : brdfData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, brdfData.viewDir); + vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); + // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 + vec3 bentNormal = normalize( mix(brdfData.normal, anisotropicNormal, abs(brdfData.anisotropy) * saturate( 5.0 * brdfData.roughness)) ); + + return bentNormal; + } +#endif + + + +void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ + brdfData.position = surfaceData.position; + brdfData.normal = surfaceData.normal; + #ifdef NEED_TANGENT + brdfData.tangent = surfaceData.tangent; + brdfData.bitangent = surfaceData.bitangent; + #endif + brdfData.viewDir = surfaceData.viewDir; + + brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); +} + +void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ + vec3 albedoColor = surfaceData.albedoColor; + vec3 specularColor = surfaceData.specularColor; + float metallic = surfaceData.metallic; + float roughness = surfaceData.roughness; + float f0 = surfaceData.f0; + + #ifdef IS_METALLIC_WORKFLOW + brdfData.diffuseColor = albedoColor * ( 1.0 - metallic ); + brdfData.specularColor = mix( vec3(f0), albedoColor, metallic ); + #else + float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); + brdfData.diffuseColor = albedoColor * ( 1.0 - specularStrength ); + brdfData.specularColor = specularColor; + #endif + + brdfData.roughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(roughness + getAARoughnessFactor(brdfData.normal), 1.0)); +} + +void initClearCoatBRDFData(vec2 uv, vec3 normal, inout BRDFData brdfData, bool isFrontFacing){ + #ifdef MATERIAL_ENABLE_CLEAR_COAT + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, uv, isFrontFacing); + #else + brdfData.clearCoatNormal = normal; + #endif + brdfData.clearCoatDotNV = saturate( dot(brdfData.clearCoatNormal, brdfData.viewDir) ); + + brdfData.clearCoat = material_ClearCoat; + brdfData.clearCoatRoughness = material_ClearCoatRoughness; + + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + brdfData.clearCoat *= (texture2D( material_ClearCoatTexture, uv )).r; + #endif + + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + brdfData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, uv )).g; + #endif + + brdfData.clearCoat = saturate( brdfData.clearCoat ); + + brdfData.clearCoatRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(brdfData.clearCoatRoughness + getAARoughnessFactor(brdfData.clearCoatNormal), 1.0)); + + #endif + +} + +void initAnisotropyBRDFData(vec2 uv, inout BRDFData brdfData){ + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy = material_AnisotropyInfo.z; + vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, uv )).rgb; + anisotropy *= anisotropyTextureInfo.b; + anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; + #endif + + brdfData.anisotropy = anisotropy; + brdfData.anisotropicT = normalize(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal) * anisotropicDirection); + brdfData.anisotropicB = normalize(cross(brdfData.normal, brdfData.anisotropicT)); + brdfData.anisotropicN = getAnisotropicBentNormal(brdfData); + + #endif + +} + +void initAO(vec2 aoUV, inout BRDFData brdfData){ + float diffuseAO = 1.0; + float specularAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( brdfData.dotNV + diffuseAO, exp2( - 16.0 * brdfData.roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + brdfData.diffuseAO = diffuseAO; + brdfData.specularAO = specularAO; +} + +void initBRDFData(vec2 uv, vec2 aoUV, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ + initGeometryData(surfaceData, brdfData); + initCommonBRDFData(surfaceData, brdfData); + initClearCoatBRDFData(uv, surfaceData.normal, brdfData, isFrontFacing); + initAnisotropyBRDFData(uv, brdfData); + initAO(aoUV, brdfData); +} + + + #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 90c0035d..27ee0646 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -60,8 +60,18 @@ void PBRFragment(Varyings varyings) { SurfaceData surfaceData = getSurfaceData(varyings, gl_FrontFacing); BRDFData brdfData; + + + // Get aoUV + vec2 aoUV = varyings.uv; + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(RENDERER_HAS_UV1) + if(material_OcclusionTextureCoord == 1.0){ + aoUV = varyings.uv1; + } + #endif + // Can modify surfaceData here. - initBRDFData(varyings, surfaceData, brdfData, gl_FrontFacing); + initBRDFData(varyings.uv, aoUV, surfaceData, brdfData, gl_FrontFacing); vec4 color = vec4(0, 0, 0, surfaceData.opacity); diff --git a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl index a4bb2a94..4970e04f 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl @@ -12,32 +12,32 @@ vec3 material_PBRSpecularColor; float material_Glossiness; vec3 material_EmissiveColor; float material_NormalIntensity; -float material_OcclusionIntensity; -float material_OcclusionTextureCoord; +// float material_OcclusionIntensity; +// float material_OcclusionTextureCoord; -#ifdef MATERIAL_ENABLE_CLEAR_COAT - float material_ClearCoat; - float material_ClearCoatRoughness; +// #ifdef MATERIAL_ENABLE_CLEAR_COAT +// float material_ClearCoat; +// float material_ClearCoatRoughness; - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - sampler2D material_ClearCoatTexture; - #endif +// #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE +// sampler2D material_ClearCoatTexture; +// #endif - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - sampler2D material_ClearCoatRoughnessTexture; - #endif +// #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE +// sampler2D material_ClearCoatRoughnessTexture; +// #endif - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - sampler2D material_ClearCoatNormalTexture; - #endif -#endif +// #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE +// sampler2D material_ClearCoatNormalTexture; +// #endif +// #endif -#ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 material_AnisotropyInfo; - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - sampler2D material_AnisotropyTexture; - #endif -#endif +// #ifdef MATERIAL_ENABLE_ANISOTROPY +// vec3 material_AnisotropyInfo; +// #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE +// sampler2D material_AnisotropyTexture; +// #endif +// #endif // Texture #ifdef MATERIAL_HAS_BASETEXTURE @@ -61,9 +61,9 @@ float material_OcclusionTextureCoord; sampler2D material_SpecularGlossinessTexture; #endif -#ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - sampler2D material_OcclusionTexture; -#endif +// #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE +// sampler2D material_OcclusionTexture; +// #endif SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ @@ -134,7 +134,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ #endif - // geometry + // Geometry surfaceData.position = v.positionWS; #ifdef CAMERA_ORTHOGRAPHIC @@ -143,156 +143,36 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ surfaceData.viewDir = normalize(camera_Position - v.positionWS); #endif - #ifdef NEED_TANGENT - mat3 tbn = getTBN(v, isFrontFacing); - surfaceData.tangent = tbn[0]; - surfaceData.bitangent = tbn[1]; - #ifdef MATERIAL_HAS_NORMALTEXTURE - surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.uv, isFrontFacing); - #else - surfaceData.normal = tbn[2]; - #endif - #else - surfaceData.normal = getNormal(v, isFrontFacing); - #endif - - return surfaceData; -} - -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return max( max(dxy.x, dxy.y), dxy.z ); + // Normal + #ifdef RENDERER_HAS_NORMAL + vec3 normal = normalize(v.normalWS); + #elif defined(HAS_DERIVATIVES) + vec3 pos_dx = dFdx(v.positionWS); + vec3 pos_dy = dFdy(v.positionWS); + vec3 normal = normalize( cross(pos_dx, pos_dy) ); #else - return 0.0; + vec3 normal = vec3(0, 0, 1); #endif -} - -#ifdef MATERIAL_ENABLE_ANISOTROPY - // Aniso Bent Normals - // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(BRDFData brdfData) { - vec3 anisotropyDirection = (brdfData.anisotropy >= 0.0) ? brdfData.anisotropicB : brdfData.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, brdfData.viewDir); - vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); - // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(brdfData.normal, anisotropicNormal, abs(brdfData.anisotropy) * saturate( 5.0 * brdfData.roughness)) ); - - return bentNormal; - } -#endif - - + + surfaceData.normal = normal; -void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ - brdfData.position = surfaceData.position; - brdfData.normal = surfaceData.normal; #ifdef NEED_TANGENT - brdfData.tangent = surfaceData.tangent; - brdfData.bitangent = surfaceData.bitangent; - #endif - brdfData.viewDir = surfaceData.viewDir; - - brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); -} - -void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ - vec3 albedoColor = surfaceData.albedoColor; - vec3 specularColor = surfaceData.specularColor; - float metallic = surfaceData.metallic; - float roughness = surfaceData.roughness; - float f0 = surfaceData.f0; - - #ifdef IS_METALLIC_WORKFLOW - brdfData.diffuseColor = albedoColor * ( 1.0 - metallic ); - brdfData.specularColor = mix( vec3(f0), albedoColor, metallic ); - #else - float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b ); - brdfData.diffuseColor = albedoColor * ( 1.0 - specularStrength ); - brdfData.specularColor = specularColor; - #endif - - brdfData.roughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(roughness + getAARoughnessFactor(brdfData.normal), 1.0)); -} - -void initClearCoatBRDFData(Varyings v, inout BRDFData brdfData, bool isFrontFacing){ - #ifdef MATERIAL_ENABLE_CLEAR_COAT - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, v.uv, isFrontFacing); + #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) + surfaceData.tangent = v.tangentWS; + surfaceData.bitangent = v.bitangentWS; + mat3 tbn = mat3(v.tangentWS, v.bitangentWS, v.normalWS); #else - brdfData.clearCoatNormal = getNormal(v, isFrontFacing); + mat3 tbn = getTBNByDerivatives(v.uv, normal, v.positionWS, isFrontFacing); + surfaceData.tangent = tbn[0]; + surfaceData.bitangent = tbn[1]; #endif - brdfData.clearCoatDotNV = saturate( dot(brdfData.clearCoatNormal, brdfData.viewDir) ); - brdfData.clearCoat = material_ClearCoat; - brdfData.clearCoatRoughness = material_ClearCoatRoughness; - - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - brdfData.clearCoat *= (texture2D( material_ClearCoatTexture, v.uv )).r; - #endif - - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - brdfData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, v.uv )).g; - #endif - - brdfData.clearCoat = saturate( brdfData.clearCoat ); - - brdfData.clearCoatRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(brdfData.clearCoatRoughness + getAARoughnessFactor(brdfData.clearCoatNormal), 1.0)); - - #endif - -} - -void initAnisotropyBRDFData(Varyings v, inout BRDFData brdfData){ - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy = material_AnisotropyInfo.z; - vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, v.uv )).rgb; - anisotropy *= anisotropyTextureInfo.b; - anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; - #endif - - brdfData.anisotropy = anisotropy; - brdfData.anisotropicT = normalize(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal) * anisotropicDirection); - brdfData.anisotropicB = normalize(cross(brdfData.normal, brdfData.anisotropicT)); - brdfData.anisotropicN = getAnisotropicBentNormal(brdfData); - - #endif - -} - -void initAO(Varyings v, inout BRDFData brdfData){ - float diffuseAO = 1.0; - float specularAO = 1.0; - - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - vec2 aoUV = v.uv; - #ifdef RENDERER_HAS_UV1 - if(material_OcclusionTextureCoord == 1.0){ - aoUV = v.uv1; - } + #ifdef MATERIAL_HAS_NORMALTEXTURE + surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.uv, isFrontFacing); #endif - diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; - #endif - - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) - specularAO = saturate( pow( brdfData.dotNV + diffuseAO, exp2( - 16.0 * brdfData.roughness - 1.0 ) ) - 1.0 + diffuseAO ); #endif - brdfData.diffuseAO = diffuseAO; - brdfData.specularAO = specularAO; -} - -void initBRDFData(Varyings v, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ - initGeometryData(surfaceData, brdfData); - initCommonBRDFData(surfaceData, brdfData); - initClearCoatBRDFData(v, brdfData, isFrontFacing); - initAnisotropyBRDFData(v, brdfData); - initAO(v, brdfData); + return surfaceData; } From f6ebf9739dce68fb45f31d26ee98bd1a9839f386 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 18 Jul 2024 02:21:15 +0800 Subject: [PATCH 88/89] refactor: move function to brdf.glsl --- .../src/shaders/shadingPBR/BRDF.glsl | 175 ++++++------------ .../shaders/shadingPBR/ForwardPassPBR.glsl | 8 +- .../src/shaders/shadingPBR/FragmentPBR.glsl | 138 ++++++++++---- 3 files changed, 166 insertions(+), 155 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index 464b9f70..2efd808d 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -10,36 +10,6 @@ #endif - -#ifdef MATERIAL_ENABLE_CLEAR_COAT - float material_ClearCoat; - float material_ClearCoatRoughness; - - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - sampler2D material_ClearCoatTexture; - #endif - - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - sampler2D material_ClearCoatRoughnessTexture; - #endif - - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - sampler2D material_ClearCoatNormalTexture; - #endif -#endif - -#ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 material_AnisotropyInfo; - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - sampler2D material_AnisotropyTexture; - #endif -#endif - -#ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - sampler2D material_OcclusionTexture; -#endif - - struct SurfaceData{ // common vec3 albedoColor; @@ -47,6 +17,8 @@ struct SurfaceData{ vec3 emissiveColor; float metallic; float roughness; + float diffuseAO; + float specularAO; float f0; float opacity; @@ -60,6 +32,23 @@ struct SurfaceData{ #endif vec3 viewDir; + float dotNV; + + // Anisotropy + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy; + vec3 anisotropicT; + vec3 anisotropicB; + vec3 anisotropicN; + #endif + + // Clear coat + #ifdef MATERIAL_ENABLE_CLEAR_COAT + float clearCoat; + float clearCoatRoughness; + vec3 clearCoatNormal; + float clearCoatDotNV; + #endif }; @@ -100,10 +89,20 @@ struct BRDFData{ #endif }; -float material_OcclusionIntensity; -float material_OcclusionTextureCoord; +float getAARoughnessFactor(vec3 normal) { + // Kaplanyan 2016, "Stable specular highlights" + // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" + // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" + #ifdef HAS_DERIVATIVES + vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); + return max( max(dxy.x, dxy.y), dxy.z ); + #else + return 0.0; + #endif +} + float F_Schlick(float f0, float dotLH) { return f0 + 0.96 * (pow(1.0 - dotLH, 5.0)); @@ -214,7 +213,7 @@ vec3 BRDF_Specular_GGX(vec3 incidentDirection, BRDFData brdfData, vec3 normal, v vec3 halfDir = normalize( incidentDirection + brdfData.viewDir ); float dotNL = saturate( dot( normal, incidentDirection ) ); - float dotNV = saturate( dot( normal, brdfData.viewDir ) ); + float dotNV = brdfData.dotNV; float dotNH = saturate( dot( normal, halfDir ) ); float dotLH = saturate( dot( incidentDirection, halfDir ) ); @@ -230,33 +229,6 @@ vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor) { return RECIPROCAL_PI * diffuseColor; } -float getAARoughnessFactor(vec3 normal) { - // Kaplanyan 2016, "Stable specular highlights" - // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" - // Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" - #ifdef HAS_DERIVATIVES - vec3 dxy = max( abs(dFdx(normal)), abs(dFdy(normal)) ); - return max( max(dxy.x, dxy.y), dxy.z ); - #else - return 0.0; - #endif -} - -#ifdef MATERIAL_ENABLE_ANISOTROPY - // Aniso Bent Normals - // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far - vec3 getAnisotropicBentNormal(BRDFData brdfData) { - vec3 anisotropyDirection = (brdfData.anisotropy >= 0.0) ? brdfData.anisotropicB : brdfData.anisotropicT; - vec3 anisotropicTangent = cross(anisotropyDirection, brdfData.viewDir); - vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); - // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 - vec3 bentNormal = normalize( mix(brdfData.normal, anisotropicNormal, abs(brdfData.anisotropy) * saturate( 5.0 * brdfData.roughness)) ); - - return bentNormal; - } -#endif - - void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.position = surfaceData.position; @@ -267,7 +239,7 @@ void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ #endif brdfData.viewDir = surfaceData.viewDir; - brdfData.dotNV = saturate( dot(brdfData.normal, brdfData.viewDir) ); + brdfData.dotNV = surfaceData.dotNV; } void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ @@ -289,75 +261,38 @@ void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.roughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(roughness + getAARoughnessFactor(brdfData.normal), 1.0)); } -void initClearCoatBRDFData(vec2 uv, vec3 normal, inout BRDFData brdfData, bool isFrontFacing){ - #ifdef MATERIAL_ENABLE_CLEAR_COAT - #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE - brdfData.clearCoatNormal = getNormalByNormalTexture(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, uv, isFrontFacing); - #else - brdfData.clearCoatNormal = normal; - #endif - brdfData.clearCoatDotNV = saturate( dot(brdfData.clearCoatNormal, brdfData.viewDir) ); - - brdfData.clearCoat = material_ClearCoat; - brdfData.clearCoatRoughness = material_ClearCoatRoughness; - - #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE - brdfData.clearCoat *= (texture2D( material_ClearCoatTexture, uv )).r; - #endif - - #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE - brdfData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, uv )).g; - #endif - - brdfData.clearCoat = saturate( brdfData.clearCoat ); - - brdfData.clearCoatRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(brdfData.clearCoatRoughness + getAARoughnessFactor(brdfData.clearCoatNormal), 1.0)); - - #endif - +void initClearCoatBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ + brdfData.clearCoatNormal = surfaceData.clearCoatNormal; + brdfData.clearCoatDotNV = surfaceData.clearCoatDotNV; + brdfData.clearCoat = surfaceData.clearCoat; + brdfData.clearCoatRoughness = surfaceData.clearCoatRoughness; } -void initAnisotropyBRDFData(vec2 uv, inout BRDFData brdfData){ - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy = material_AnisotropyInfo.z; - vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); - #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE - vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, uv )).rgb; - anisotropy *= anisotropyTextureInfo.b; - anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; - #endif - - brdfData.anisotropy = anisotropy; - brdfData.anisotropicT = normalize(mat3(brdfData.tangent, brdfData.bitangent, brdfData.normal) * anisotropicDirection); - brdfData.anisotropicB = normalize(cross(brdfData.normal, brdfData.anisotropicT)); - brdfData.anisotropicN = getAnisotropicBentNormal(brdfData); - - #endif +void initAnisotropyBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ + brdfData.anisotropy = surfaceData.anisotropy; + brdfData.anisotropicT = surfaceData.anisotropicT; + brdfData.anisotropicB = surfaceData.anisotropicB; + brdfData.anisotropicN = surfaceData.anisotropicN; +} +void initAO(SurfaceData surfaceData, inout BRDFData brdfData){ + brdfData.diffuseAO = surfaceData.diffuseAO; + brdfData.specularAO = surfaceData.specularAO; } -void initAO(vec2 aoUV, inout BRDFData brdfData){ - float diffuseAO = 1.0; - float specularAO = 1.0; +void initBRDFData(SurfaceData surfaceData, out BRDFData brdfData){ + initGeometryData(surfaceData, brdfData); + initCommonBRDFData(surfaceData, brdfData); - #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE - diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #ifdef MATERIAL_ENABLE_CLEAR_COAT + initClearCoatBRDFData(surfaceData, brdfData); #endif - #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) - specularAO = saturate( pow( brdfData.dotNV + diffuseAO, exp2( - 16.0 * brdfData.roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #ifdef MATERIAL_ENABLE_ANISOTROPY + initAnisotropyBRDFData(surfaceData, brdfData); #endif - brdfData.diffuseAO = diffuseAO; - brdfData.specularAO = specularAO; -} - -void initBRDFData(vec2 uv, vec2 aoUV, SurfaceData surfaceData, out BRDFData brdfData, bool isFrontFacing){ - initGeometryData(surfaceData, brdfData); - initCommonBRDFData(surfaceData, brdfData); - initClearCoatBRDFData(uv, surfaceData.normal, brdfData, isFrontFacing); - initAnisotropyBRDFData(uv, brdfData); - initAO(aoUV, brdfData); + initAO(surfaceData, brdfData); } diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index 27ee0646..c7dc98d3 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -56,12 +56,10 @@ Varyings PBRVertex(Attributes attributes) { return varyings; } -void PBRFragment(Varyings varyings) { - SurfaceData surfaceData = getSurfaceData(varyings, gl_FrontFacing); +void PBRFragment(Varyings varyings) { BRDFData brdfData; - // Get aoUV vec2 aoUV = varyings.uv; #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(RENDERER_HAS_UV1) @@ -70,8 +68,10 @@ void PBRFragment(Varyings varyings) { } #endif + SurfaceData surfaceData = getSurfaceData(varyings, aoUV, gl_FrontFacing); + // Can modify surfaceData here. - initBRDFData(varyings.uv, aoUV, surfaceData, brdfData, gl_FrontFacing); + initBRDFData(surfaceData, brdfData); vec4 color = vec4(0, 0, 0, surfaceData.opacity); diff --git a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl index 4970e04f..44be24ce 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl @@ -12,32 +12,32 @@ vec3 material_PBRSpecularColor; float material_Glossiness; vec3 material_EmissiveColor; float material_NormalIntensity; -// float material_OcclusionIntensity; -// float material_OcclusionTextureCoord; +float material_OcclusionIntensity; +float material_OcclusionTextureCoord; -// #ifdef MATERIAL_ENABLE_CLEAR_COAT -// float material_ClearCoat; -// float material_ClearCoatRoughness; +#ifdef MATERIAL_ENABLE_CLEAR_COAT + float material_ClearCoat; + float material_ClearCoatRoughness; -// #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE -// sampler2D material_ClearCoatTexture; -// #endif + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + sampler2D material_ClearCoatTexture; + #endif -// #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE -// sampler2D material_ClearCoatRoughnessTexture; -// #endif + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + sampler2D material_ClearCoatRoughnessTexture; + #endif -// #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE -// sampler2D material_ClearCoatNormalTexture; -// #endif -// #endif + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + sampler2D material_ClearCoatNormalTexture; + #endif +#endif -// #ifdef MATERIAL_ENABLE_ANISOTROPY -// vec3 material_AnisotropyInfo; -// #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE -// sampler2D material_AnisotropyTexture; -// #endif -// #endif +#ifdef MATERIAL_ENABLE_ANISOTROPY + vec3 material_AnisotropyInfo; + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + sampler2D material_AnisotropyTexture; + #endif +#endif // Texture #ifdef MATERIAL_HAS_BASETEXTURE @@ -61,13 +61,31 @@ float material_NormalIntensity; sampler2D material_SpecularGlossinessTexture; #endif -// #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE -// sampler2D material_OcclusionTexture; -// #endif +#ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + sampler2D material_OcclusionTexture; +#endif + +#ifdef MATERIAL_ENABLE_ANISOTROPY + // Aniso Bent Normals + // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far + vec3 getAnisotropicBentNormal(SurfaceData surfaceData) { + vec3 anisotropyDirection = (surfaceData.anisotropy >= 0.0) ? surfaceData.anisotropicB : surfaceData.anisotropicT; + vec3 anisotropicTangent = cross(anisotropyDirection, surfaceData.viewDir); + vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection); + // reduce stretching for (roughness < 0.2), refer to https://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf 80 + vec3 bentNormal = normalize( mix(surfaceData.normal, anisotropicNormal, abs(surfaceData.anisotropy) * saturate( 5.0 * surfaceData.roughness)) ); -SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ + return bentNormal; + } +#endif + + +SurfaceData getSurfaceData(Varyings v, vec2 aoUV, bool isFrontFacing){ SurfaceData surfaceData; + + vec2 uv = v.uv; + // common vec4 baseColor = material_BaseColor; float metallic = material_Metal; @@ -78,7 +96,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ vec3 emissiveRadiance = material_EmissiveColor; #ifdef MATERIAL_HAS_BASETEXTURE - vec4 baseTextureColor = texture2D(material_BaseTexture, v.uv); + vec4 baseTextureColor = texture2D(material_BaseTexture, uv); #ifndef ENGINE_IS_COLORSPACE_GAMMA baseTextureColor = gammaToLinear(baseTextureColor); #endif @@ -97,13 +115,13 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ #endif #ifdef MATERIAL_HAS_ROUGHNESS_METALLIC_TEXTURE - vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, v.uv ); + vec4 metalRoughMapColor = texture2D( material_RoughnessMetallicTexture, uv ); roughness *= metalRoughMapColor.g; metallic *= metalRoughMapColor.b; #endif #ifdef MATERIAL_HAS_SPECULAR_GLOSSINESS_TEXTURE - vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, v.uv ); + vec4 specularGlossinessColor = texture2D(material_SpecularGlossinessTexture, uv ); #ifndef ENGINE_IS_COLORSPACE_GAMMA specularGlossinessColor = gammaToLinear(specularGlossinessColor); #endif @@ -113,7 +131,7 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ #endif #ifdef MATERIAL_HAS_EMISSIVETEXTURE - vec4 emissiveColor = texture2D(material_EmissiveTexture, v.uv); + vec4 emissiveColor = texture2D(material_EmissiveTexture, uv); #ifndef ENGINE_IS_COLORSPACE_GAMMA emissiveColor = gammaToLinear(emissiveColor); #endif @@ -156,22 +174,80 @@ SurfaceData getSurfaceData(Varyings v, bool isFrontFacing){ surfaceData.normal = normal; + // Tangent #ifdef NEED_TANGENT #if defined(RENDERER_HAS_NORMAL) && defined(RENDERER_HAS_TANGENT) surfaceData.tangent = v.tangentWS; surfaceData.bitangent = v.bitangentWS; mat3 tbn = mat3(v.tangentWS, v.bitangentWS, v.normalWS); #else - mat3 tbn = getTBNByDerivatives(v.uv, normal, v.positionWS, isFrontFacing); + mat3 tbn = getTBNByDerivatives(uv, normal, v.positionWS, isFrontFacing); surfaceData.tangent = tbn[0]; surfaceData.bitangent = tbn[1]; #endif #ifdef MATERIAL_HAS_NORMALTEXTURE - surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, v.uv, isFrontFacing); + surfaceData.normal = getNormalByNormalTexture(tbn, material_NormalTexture, material_NormalIntensity, uv, isFrontFacing); + #endif + #endif + + surfaceData.dotNV = saturate( dot(surfaceData.normal, surfaceData.viewDir) ); + + // Clear Coat + #ifdef MATERIAL_ENABLE_CLEAR_COAT + #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE + surfaceData.clearCoatNormal = getNormalByNormalTexture(mat3(surfaceData.tangent, surfaceData.bitangent, surfaceData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, uv, isFrontFacing); + #else + surfaceData.clearCoatNormal = surfaceData.normal; #endif + surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); + + surfaceData.clearCoat = material_ClearCoat; + surfaceData.clearCoatRoughness = material_ClearCoatRoughness; + + #ifdef MATERIAL_HAS_CLEAR_COAT_TEXTURE + surfaceData.clearCoat *= (texture2D( material_ClearCoatTexture, uv )).r; + #endif + + #ifdef MATERIAL_HAS_CLEAR_COAT_ROUGHNESS_TEXTURE + surfaceData.clearCoatRoughness *= (texture2D( material_ClearCoatRoughnessTexture, uv )).g; + #endif + + surfaceData.clearCoat = saturate( surfaceData.clearCoat ); + surfaceData.clearCoatRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(surfaceData.clearCoatRoughness + getAARoughnessFactor(surfaceData.clearCoatNormal), 1.0)); #endif + // Anisotropy + #ifdef MATERIAL_ENABLE_ANISOTROPY + float anisotropy = material_AnisotropyInfo.z; + vec3 anisotropicDirection = vec3(material_AnisotropyInfo.xy, 0.0); + #ifdef MATERIAL_HAS_ANISOTROPY_TEXTURE + vec3 anisotropyTextureInfo = (texture2D( material_AnisotropyTexture, uv )).rgb; + anisotropy *= anisotropyTextureInfo.b; + anisotropicDirection.xy *= anisotropyTextureInfo.rg * 2.0 - 1.0; + #endif + + surfaceData.anisotropy = anisotropy; + surfaceData.anisotropicT = normalize(mat3(surfaceData.tangent, surfaceData.bitangent, surfaceData.normal) * anisotropicDirection); + surfaceData.anisotropicB = normalize(cross(surfaceData.normal, surfaceData.anisotropicT)); + surfaceData.anisotropicN = getAnisotropicBentNormal(surfaceData); + #endif + + // AO + float diffuseAO = 1.0; + float specularAO = 1.0; + + #ifdef MATERIAL_HAS_OCCLUSION_TEXTURE + diffuseAO = ((texture2D(material_OcclusionTexture, aoUV)).r - 1.0) * material_OcclusionIntensity + 1.0; + #endif + + #if defined(MATERIAL_HAS_OCCLUSION_TEXTURE) && defined(SCENE_USE_SPECULAR_ENV) + specularAO = saturate( pow( surfaceData.dotNV + diffuseAO, exp2( - 16.0 * surfaceData.roughness - 1.0 ) ) - 1.0 + diffuseAO ); + #endif + + surfaceData.diffuseAO = diffuseAO; + surfaceData.specularAO = specularAO; + return surfaceData; } From d21e3c6c1f1c0f22864ad00678c0ef59796b2efa Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 18 Jul 2024 03:27:54 +0800 Subject: [PATCH 89/89] refactor: split surfaceData and brdfData --- .../src/shaders/shadingPBR/BRDF.glsl | 101 +++--------------- .../shaders/shadingPBR/ForwardPassPBR.glsl | 6 +- .../src/shaders/shadingPBR/FragmentPBR.glsl | 2 +- .../shaders/shadingPBR/LightDirectPBR.glsl | 45 ++++---- .../shaders/shadingPBR/LightIndirectPBR.glsl | 38 +++---- 5 files changed, 60 insertions(+), 132 deletions(-) diff --git a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl index 2efd808d..dbfb122c 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/BRDF.glsl @@ -53,44 +53,17 @@ struct SurfaceData{ struct BRDFData{ - // common vec3 diffuseColor; vec3 specularColor; float roughness; - float diffuseAO; - float specularAO; - - // geometry - vec3 position; - vec3 normal; - #ifdef NEED_TANGENT - vec3 tangent; - vec3 bitangent; - #endif - - vec3 viewDir; - float dotNV; - - // Anisotropy - #ifdef MATERIAL_ENABLE_ANISOTROPY - float anisotropy; - vec3 anisotropicT; - vec3 anisotropicB; - vec3 anisotropicN; - #endif - - // Clear coat #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoat; + vec3 clearCoatSpecularColor; float clearCoatRoughness; - vec3 clearCoatNormal; - float clearCoatDotNV; #endif }; - float getAARoughnessFactor(vec3 normal) { // Kaplanyan 2016, "Stable specular highlights" // Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" @@ -179,10 +152,10 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl } #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 anisotropicLobe(vec3 h, vec3 l, BRDFData brdfData, vec3 specularColor, float alpha, float dotNV, float dotNL, float dotNH, float dotLH) { - vec3 t = brdfData.anisotropicT; - vec3 b = brdfData.anisotropicB; - vec3 v = brdfData.viewDir; + vec3 anisotropicLobe(vec3 h, vec3 l, SurfaceData surfaceData, vec3 specularColor, float alpha, float dotNV, float dotNL, float dotNH, float dotLH) { + vec3 t = surfaceData.anisotropicT; + vec3 b = surfaceData.anisotropicB; + vec3 v = surfaceData.viewDir; float dotTV = dot(t, v); float dotBV = dot(b, v); @@ -193,8 +166,8 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl // Aniso parameter remapping // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24 - float at = max(alpha * (1.0 + brdfData.anisotropy), MIN_ROUGHNESS); - float ab = max(alpha * (1.0 - brdfData.anisotropy), MIN_ROUGHNESS); + float at = max(alpha * (1.0 + surfaceData.anisotropy), MIN_ROUGHNESS); + float ab = max(alpha * (1.0 - surfaceData.anisotropy), MIN_ROUGHNESS); // specular anisotropic BRDF vec3 F = F_Schlick( specularColor, dotLH ); @@ -206,19 +179,19 @@ vec3 isotropicLobe(vec3 specularColor, float alpha, float dotNV, float dotNL, fl #endif // GGX Distribution, Schlick Fresnel, GGX-Smith Visibility -vec3 BRDF_Specular_GGX(vec3 incidentDirection, BRDFData brdfData, vec3 normal, vec3 specularColor, float roughness ) { +vec3 BRDF_Specular_GGX(vec3 incidentDirection, SurfaceData surfaceData, vec3 normal, vec3 specularColor, float roughness ) { float alpha = pow2( roughness ); // UE4's roughness - vec3 halfDir = normalize( incidentDirection + brdfData.viewDir ); + vec3 halfDir = normalize( incidentDirection + surfaceData.viewDir ); float dotNL = saturate( dot( normal, incidentDirection ) ); - float dotNV = brdfData.dotNV; + float dotNV = saturate( dot( normal, surfaceData.viewDir ) ); float dotNH = saturate( dot( normal, halfDir ) ); float dotLH = saturate( dot( incidentDirection, halfDir ) ); #ifdef MATERIAL_ENABLE_ANISOTROPY - return anisotropicLobe(halfDir, incidentDirection, brdfData, specularColor, alpha, dotNV, dotNL, dotNH, dotLH); + return anisotropicLobe(halfDir, incidentDirection, surfaceData, specularColor, alpha, dotNV, dotNL, dotNH, dotLH); #else return isotropicLobe(specularColor, alpha, dotNV, dotNL, dotNH, dotLH); #endif @@ -230,19 +203,7 @@ vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor) { } -void initGeometryData(SurfaceData surfaceData, inout BRDFData brdfData){ - brdfData.position = surfaceData.position; - brdfData.normal = surfaceData.normal; - #ifdef NEED_TANGENT - brdfData.tangent = surfaceData.tangent; - brdfData.bitangent = surfaceData.bitangent; - #endif - brdfData.viewDir = surfaceData.viewDir; - - brdfData.dotNV = surfaceData.dotNV; -} - -void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ +void initBRDFData(SurfaceData surfaceData, out BRDFData brdfData){ vec3 albedoColor = surfaceData.albedoColor; vec3 specularColor = surfaceData.specularColor; float metallic = surfaceData.metallic; @@ -258,44 +219,12 @@ void initCommonBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ brdfData.specularColor = specularColor; #endif - brdfData.roughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(roughness + getAARoughnessFactor(brdfData.normal), 1.0)); -} - -void initClearCoatBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ - brdfData.clearCoatNormal = surfaceData.clearCoatNormal; - brdfData.clearCoatDotNV = surfaceData.clearCoatDotNV; - brdfData.clearCoat = surfaceData.clearCoat; - brdfData.clearCoatRoughness = surfaceData.clearCoatRoughness; -} - -void initAnisotropyBRDFData(SurfaceData surfaceData, inout BRDFData brdfData){ - brdfData.anisotropy = surfaceData.anisotropy; - brdfData.anisotropicT = surfaceData.anisotropicT; - brdfData.anisotropicB = surfaceData.anisotropicB; - brdfData.anisotropicN = surfaceData.anisotropicN; -} - -void initAO(SurfaceData surfaceData, inout BRDFData brdfData){ - brdfData.diffuseAO = surfaceData.diffuseAO; - brdfData.specularAO = surfaceData.specularAO; -} - -void initBRDFData(SurfaceData surfaceData, out BRDFData brdfData){ - initGeometryData(surfaceData, brdfData); - initCommonBRDFData(surfaceData, brdfData); + brdfData.roughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(roughness + getAARoughnessFactor(surfaceData.normal), 1.0)); #ifdef MATERIAL_ENABLE_CLEAR_COAT - initClearCoatBRDFData(surfaceData, brdfData); - #endif - - #ifdef MATERIAL_ENABLE_ANISOTROPY - initAnisotropyBRDFData(surfaceData, brdfData); + brdfData.clearCoatRoughness = max(MIN_PERCEPTUAL_ROUGHNESS, min(surfaceData.clearCoatRoughness + getAARoughnessFactor(surfaceData.clearCoatNormal), 1.0)); + brdfData.clearCoatSpecularColor = vec3(0.04); #endif - - initAO(surfaceData, brdfData); } - - - #endif \ No newline at end of file diff --git a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl index c7dc98d3..c996b928 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/ForwardPassPBR.glsl @@ -70,7 +70,7 @@ void PBRFragment(Varyings varyings) { SurfaceData surfaceData = getSurfaceData(varyings, aoUV, gl_FrontFacing); - // Can modify surfaceData here. + // Can modify surfaceData here initBRDFData(surfaceData, brdfData); vec4 color = vec4(0, 0, 0, surfaceData.opacity); @@ -87,10 +87,10 @@ void PBRFragment(Varyings varyings) { #endif // Evaluate direct lighting - evaluateDirectRadiance(shadowAttenuation, brdfData, color.rgb); + evaluateDirectRadiance(shadowAttenuation, surfaceData, brdfData, color.rgb); // IBL - evaluateIBL(brdfData, color.rgb); + evaluateIBL(surfaceData, brdfData, color.rgb); // Emissive color.rgb += surfaceData.emissiveColor; diff --git a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl index 44be24ce..05526aef 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl @@ -198,7 +198,7 @@ SurfaceData getSurfaceData(Varyings v, vec2 aoUV, bool isFrontFacing){ #ifdef MATERIAL_HAS_CLEAR_COAT_NORMAL_TEXTURE surfaceData.clearCoatNormal = getNormalByNormalTexture(mat3(surfaceData.tangent, surfaceData.bitangent, surfaceData.normal), material_ClearCoatNormalTexture, material_NormalIntensity, uv, isFrontFacing); #else - surfaceData.clearCoatNormal = surfaceData.normal; + surfaceData.clearCoatNormal = normal; #endif surfaceData.clearCoatDotNV = saturate( dot(surfaceData.clearCoatNormal, surfaceData.viewDir) ); diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl index d014e789..38c2b6da 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightDirectPBR.glsl @@ -24,40 +24,40 @@ void diffuseLobe(BRDFData brdfData, vec3 attenuationIrradiance, inout vec3 diffu diffuseColor += attenuationIrradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -void specularLobe(BRDFData brdfData, vec3 incidentDirection, vec3 attenuationIrradiance, inout vec3 specularColor){ - specularColor += attenuationIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.normal, brdfData.specularColor, brdfData.roughness); +void specularLobe(SurfaceData surfaceData, BRDFData brdfData, vec3 incidentDirection, vec3 attenuationIrradiance, inout vec3 specularColor){ + specularColor += attenuationIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.normal, brdfData.specularColor, brdfData.roughness); } -float clearCoatLobe(vec3 incidentDirection, vec3 color, BRDFData brdfData, inout vec3 specularColor){ +float clearCoatLobe(vec3 incidentDirection, vec3 color, SurfaceData surfaceData, BRDFData brdfData, inout vec3 specularColor){ float attenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - float clearCoatDotNL = saturate( dot( brdfData.clearCoatNormal, incidentDirection ) ); + float clearCoatDotNL = saturate( dot( surfaceData.clearCoatNormal, incidentDirection ) ); vec3 clearCoatIrradiance = clearCoatDotNL * color; - specularColor += brdfData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, brdfData, brdfData.clearCoatNormal, vec3( 0.04 ), brdfData.clearCoatRoughness ); - attenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); + specularColor += surfaceData.clearCoat * clearCoatIrradiance * BRDF_Specular_GGX( incidentDirection, surfaceData, surfaceData.clearCoatNormal, brdfData.clearCoatSpecularColor, brdfData.clearCoatRoughness ); + attenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); #endif return attenuation; } -void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, inout vec3 color) { +void surfaceShading(vec3 incidentDirection, vec3 lightColor, SurfaceData surfaceData, BRDFData brdfData, inout vec3 color) { vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); - float dotNL = saturate( dot( brdfData.normal, incidentDirection ) ); + float dotNL = saturate( dot( surfaceData.normal, incidentDirection ) ); vec3 irradiance = dotNL * lightColor * PI; // ClearCoat Lobe - float attenuation = FUNCTION_CLEAR_COAT_LOBE(incidentDirection, lightColor, brdfData, specularColor); + float attenuation = FUNCTION_CLEAR_COAT_LOBE(incidentDirection, lightColor, surfaceData, brdfData, specularColor); vec3 attenuationIrradiance = attenuation * irradiance; // Diffuse Lobe FUNCTION_DIFFUSE_LOBE(brdfData, attenuationIrradiance, diffuseColor); // Specular Lobe - FUNCTION_SPECULAR_LOBE(brdfData, incidentDirection, attenuationIrradiance, specularColor); + FUNCTION_SPECULAR_LOBE(surfaceData, brdfData, incidentDirection, attenuationIrradiance, specularColor); color += diffuseColor + specularColor; @@ -65,11 +65,11 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, #ifdef SCENE_DIRECT_LIGHT_COUNT - void addDirectionalDirectLightRadiance(DirectLight directionalLight, BRDFData brdfData, inout vec3 color) { + void addDirectionalDirectLightRadiance(DirectLight directionalLight, SurfaceData surfaceData, BRDFData brdfData, inout vec3 color) { vec3 lightColor = directionalLight.color; vec3 direction = -directionalLight.direction; - FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); + FUNCTION_SURFACE_SHADING( direction, lightColor, surfaceData, brdfData, color ); } @@ -77,25 +77,24 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, #ifdef SCENE_POINT_LIGHT_COUNT - void addPointDirectLightRadiance(PointLight pointLight, BRDFData brdfData, inout vec3 color) { - - vec3 lVector = pointLight.position - brdfData.position; + void addPointDirectLightRadiance(PointLight pointLight, SurfaceData surfaceData, BRDFData brdfData, inout vec3 color) { + vec3 lVector = pointLight.position - surfaceData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); vec3 lightColor = pointLight.color; lightColor *= clamp(1.0 - pow(lightDistance/pointLight.distance, 4.0), 0.0, 1.0); - FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); + FUNCTION_SURFACE_SHADING( direction, lightColor, surfaceData, brdfData, color ); } #endif #ifdef SCENE_SPOT_LIGHT_COUNT - void addSpotDirectLightRadiance(SpotLight spotLight, BRDFData brdfData, inout vec3 color) { + void addSpotDirectLightRadiance(SpotLight spotLight, SurfaceData surfaceData, BRDFData brdfData, inout vec3 color) { - vec3 lVector = spotLight.position - brdfData.position; + vec3 lVector = spotLight.position - surfaceData.position; vec3 direction = normalize( lVector ); float lightDistance = length( lVector ); float angleCos = dot( direction, -spotLight.direction ); @@ -106,14 +105,14 @@ void surfaceShading(vec3 incidentDirection, vec3 lightColor, BRDFData brdfData, vec3 lightColor = spotLight.color; lightColor *= spotEffect * decayEffect; - FUNCTION_SURFACE_SHADING( direction, lightColor, brdfData, color ); + FUNCTION_SURFACE_SHADING( direction, lightColor, surfaceData, brdfData, color ); } #endif -void evaluateDirectRadiance(float shadowAttenuation, BRDFData brdfData, inout vec3 color){ +void evaluateDirectRadiance(float shadowAttenuation, SurfaceData surfaceData, BRDFData brdfData, inout vec3 color){ #ifdef SCENE_DIRECT_LIGHT_COUNT for ( int i = 0; i < SCENE_DIRECT_LIGHT_COUNT; i ++ ) { @@ -132,7 +131,7 @@ void evaluateDirectRadiance(float shadowAttenuation, BRDFData brdfData, inout ve directionalLight.color *= shadowAttenuation; } #endif - addDirectionalDirectLightRadiance( directionalLight, brdfData, color ); + addDirectionalDirectLightRadiance( directionalLight, surfaceData, brdfData, color ); } } @@ -150,7 +149,7 @@ void evaluateDirectRadiance(float shadowAttenuation, BRDFData brdfData, inout ve pointLight.position = scene_PointLightPosition[i]; pointLight.distance = scene_PointLightDistance[i]; #endif - addPointDirectLightRadiance( pointLight, brdfData, color ); + addPointDirectLightRadiance( pointLight, surfaceData, brdfData, color ); } } @@ -171,7 +170,7 @@ void evaluateDirectRadiance(float shadowAttenuation, BRDFData brdfData, inout ve spotLight.angleCos = scene_SpotLightAngleCos[i]; spotLight.penumbraCos = scene_SpotLightPenumbraCos[i]; #endif - addSpotDirectLightRadiance( spotLight, brdfData, color ); + addSpotDirectLightRadiance( spotLight, surfaceData, brdfData, color ); } } diff --git a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl index a8e5ae15..c9226d2a 100644 --- a/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl +++ b/packages/shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl @@ -62,22 +62,22 @@ float getSpecularMIPLevel(float roughness, int maxMIPLevel ) { return roughness * float(maxMIPLevel); } -vec3 getReflectedVector(BRDFData brdfData, vec3 n) { +vec3 getReflectedVector(SurfaceData surfaceData, vec3 n) { #ifdef MATERIAL_ENABLE_ANISOTROPY - vec3 r = reflect(-brdfData.viewDir, brdfData.anisotropicN); + vec3 r = reflect(-surfaceData.viewDir, surfaceData.anisotropicN); #else - vec3 r = reflect(-brdfData.viewDir, n); + vec3 r = reflect(-surfaceData.viewDir, n); #endif return r; } -vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { +vec3 getLightProbeRadiance(SurfaceData surfaceData, vec3 normal, float roughness) { #ifndef SCENE_USE_SPECULAR_ENV return vec3(0); #else - vec3 reflectVec = getReflectedVector(brdfData, normal); + vec3 reflectVec = getReflectedVector(surfaceData, normal); reflectVec.x = -reflectVec.x; // TextureCube is left-hand,so x need inverse float specularMIPLevel = getSpecularMIPLevel(roughness, int(scene_EnvMapLight.mipMapLevel) ); @@ -106,9 +106,9 @@ vec3 getLightProbeRadiance(BRDFData brdfData, vec3 normal, float roughness) { } -void evaluateDiffuseIBL(BRDFData brdfData, inout vec3 diffuseColor){ +void evaluateDiffuseIBL(SurfaceData surfaceData, BRDFData brdfData, inout vec3 diffuseColor){ #ifdef SCENE_USE_SH - vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, brdfData.normal); + vec3 irradiance = getLightProbeIrradiance(scene_EnvSH, surfaceData.normal); #ifdef ENGINE_IS_COLORSPACE_GAMMA irradiance = (linearToGamma(vec4(irradiance, 1.0))).rgb; #endif @@ -118,39 +118,39 @@ void evaluateDiffuseIBL(BRDFData brdfData, inout vec3 diffuseColor){ irradiance *= PI; #endif - diffuseColor += brdfData.diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); + diffuseColor += surfaceData.diffuseAO * irradiance * BRDF_Diffuse_Lambert( brdfData.diffuseColor ); } -float evaluateClearCoatIBL(BRDFData brdfData, inout vec3 specularColor){ +float evaluateClearCoatIBL(SurfaceData surfaceData, BRDFData brdfData, inout vec3 specularColor){ float radianceAttenuation = 1.0; #ifdef MATERIAL_ENABLE_CLEAR_COAT - vec3 clearCoatRadiance = getLightProbeRadiance( brdfData, brdfData.clearCoatNormal, brdfData.clearCoatRoughness); - specularColor += brdfData.specularAO * clearCoatRadiance * brdfData.clearCoat * envBRDFApprox(vec3( 0.04 ), brdfData.clearCoatRoughness, brdfData.clearCoatDotNV); - radianceAttenuation -= brdfData.clearCoat * F_Schlick(0.04, brdfData.clearCoatDotNV); + vec3 clearCoatRadiance = getLightProbeRadiance(surfaceData, surfaceData.clearCoatNormal, brdfData.clearCoatRoughness); + specularColor += surfaceData.specularAO * clearCoatRadiance * surfaceData.clearCoat * envBRDFApprox(brdfData.clearCoatSpecularColor, brdfData.clearCoatRoughness, surfaceData.clearCoatDotNV); + radianceAttenuation -= surfaceData.clearCoat * F_Schlick(0.04, surfaceData.clearCoatDotNV); #endif return radianceAttenuation; } -void evaluateSpecularIBL(BRDFData brdfData, float radianceAttenuation, inout vec3 specularColor){ - vec3 radiance = getLightProbeRadiance(brdfData, brdfData.normal, brdfData.roughness); - specularColor += brdfData.specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, brdfData.dotNV ); +void evaluateSpecularIBL(SurfaceData surfaceData, BRDFData brdfData, float radianceAttenuation, inout vec3 specularColor){ + vec3 radiance = getLightProbeRadiance(surfaceData, surfaceData.normal, brdfData.roughness); + specularColor += surfaceData.specularAO * radianceAttenuation * radiance * envBRDFApprox(brdfData.specularColor, brdfData.roughness, surfaceData.dotNV ); } -void evaluateIBL(BRDFData brdfData, inout vec3 color){ +void evaluateIBL(SurfaceData surfaceData, BRDFData brdfData, inout vec3 color){ vec3 diffuseColor = vec3(0); vec3 specularColor = vec3(0); // IBL diffuse - FUNCTION_DIFFUSE_IBL(brdfData, diffuseColor); + FUNCTION_DIFFUSE_IBL(surfaceData, brdfData, diffuseColor); // IBL ClearCoat - float radianceAttenuation = FUNCTION_CLEAR_COAT_IBL(brdfData, specularColor); + float radianceAttenuation = FUNCTION_CLEAR_COAT_IBL(surfaceData, brdfData, specularColor); // IBL specular - FUNCTION_SPECULAR_IBL(brdfData, radianceAttenuation, specularColor); + FUNCTION_SPECULAR_IBL(surfaceData, brdfData, radianceAttenuation, specularColor); color += diffuseColor + specularColor; }