浏览代码

HDRenderLoop: Make transparent object working + doublesided mode work

/main
sebastienlagarde 8 年前
当前提交
b7ac9b37
共有 11 个文件被更改,包括 75 次插入47 次删除
  1. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 8
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.cs
  3. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader
  4. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl
  5. 4
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl
  6. 44
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/TemplateDisneyGGX.hlsl
  7. 4
      Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl
  8. 18
      Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl
  9. 2
      Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl
  10. 18
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl
  11. 8
      Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl

4
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


renderLoop.ExecuteCommandBuffer(cmd);
cmd.Dispose();
// render opaque objects into GBuffer
DrawRendererSettings settings = new DrawRendererSettings(cull, camera, new ShaderPassName("GBuffer"));
settings.sorting.sortOptions = SortOptions.SortByMaterialThenMesh;

DrawRendererSettings settings = new DrawRendererSettings(cullResults, camera, new ShaderPassName("Forward"));
settings.rendererConfiguration = RendererConfiguration.ConfigureOneLightProbePerRenderer | RendererConfiguration.ConfigureReflectionProbesProbePerRenderer;
settings.sorting.sortOptions = SortOptions.SortByMaterialThenMesh;
settings.inputCullingOptions.SetQueuesTransparent();
renderLoop.DrawRenderers(ref settings);
}

RenderDeferredLighting(camera, renderLoop);
// RenderForward(cullResults, camera, renderLoop);
RenderForward(cullResults, camera, renderLoop);
FinalPass(renderLoop);

8
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.cs


material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
break;
}
}
}
if (doubleSidedMode != DoubleSidedMode.None)
if (doubleSidedMode != DoubleSidedMode.None || surfaceType == SurfaceType.Transparent)
}
else
{
material.SetInt("_CullMode", (int)UnityEngine.Rendering.CullMode.Back);
}
if (doubleSidedMode == DoubleSidedMode.DoubleSidedLightingFlip)

6
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/DisneyGGX.shader


[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
[HideInInspector] _CullMode("__cullmode", Float) = 2.0 // Back by default: MEGA WARNING - if we override this, how it work with MIRROR ? (to check if the engine correctly flip stuff)
[HideInInspector] _CullMode("__cullmode", Float) = 2.0 // Back by default: MEGA WARNING - if we override this, how it work with reflection plane ? (to check if the engine correctly flip stuff)
// Material Id
[HideInInspector] _MaterialId("_MaterialId", FLoat) = 0

float4 specularLighting;
ForwardLighting(V, positionWS, bsdfData, diffuseLighting, specularLighting);
return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
return float4(diffuseLighting.rgb + specularLighting.rgb, surfaceData.opacity);
}
ENDHLSL

Name "GBuffer" // Name is not used
Tags { "LightMode" = "GBuffer" } // This will be only for opaque object based on the RenderQueue index
Cull[_CullMode]
Cull [_CullMode]
HLSLPROGRAM
#pragma target 5.0

6
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl


// float distortionBlur; // Define the mipmap level to use
// float2 velocityVector;
float opacity; // Not store, use for blending
};
struct BSDFData

attenuation *= GetAngleAttenuation(L, light.forward, light.angleScale, light.angleOffset);
float illuminance = saturate(dot(bsdfData.normalWS, L)) * attenuation;
diffuseLighting = float4(0.0f, 0.0f, 0.0f, 1.0f);
specularLighting = float4(0.0f, 0.0f, 0.0f, 1.0f);
diffuseLighting = float4(0.0, 0.0, 0.0, 1.0);
specularLighting = float4(0.0, 0.0, 0.0, 1.0);
if (illuminance > 0.0f)
{

4
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/ShaderVariables.hlsl


float3 TransformObjectToWorld(float3 positionOS)
{
return mul(GetObjectToWorldMatrix(), float4(positionOS, 1.0));
return mul(GetObjectToWorldMatrix(), float4(positionOS, 1.0)).xyz;
return mul(GetObjectToWorldViewMatrix(), float4(positionOS, 1.0));
return mul(GetObjectToWorldViewMatrix(), float4(positionOS, 1.0)).xyz;
}
float3 TransformObjectToWorldDir(float3 dirOS)

44
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/TemplateDisneyGGX.hlsl


float2 texCoord0;
float4 tangentToWorld[3]; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
float cullFace;
/*
#ifdef SHADER_STAGE_FRAGMENT
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
FRONT_FACE_TYPE cullFace;
#endif
*/
};
struct PackedVaryings

#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
float cullFace : FACE;
/*
#ifdef SHADER_STAGE_FRAGMENT
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMATIC;
#endif
*/
};
// Function to pack data to use as few interpolator as possible, the ShaderGraph should generate these functions

output.tangentToWorld[1] = input.interpolators[2];
output.tangentToWorld[2] = input.interpolators[3];
/*
*/
return output;
}

output.tangentToWorld[1].w = 0;
output.tangentToWorld[2].w = 0;
/*
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
output.cullFace = FRONT_FACE_TYPE(0); // To avoid a warning
#endif
*/
return PackVaryings(output);
}

clip(alpha - _Cutoff);
#endif
data.opacity = alpha;
float mettalic = 1.0f;
float mettalic = 1.0;
data.diffuseColor = baseColor * (1.0f - mettalic);
data.diffuseColor = baseColor * (1.0 - mettalic);
float f0_dieletric = 0.04;
data.specularColor = lerp(float3(f0_dieletric, f0_dieletric, f0_dieletric), baseColor, mettalic);

/*
#if defined(_DOUBLESIDED_LIGHTING_FLIP) || defined(_DOUBLESIDED_LIGHTING_MIRROR)
#if defined(_DOUBLESIDED_LIGHTING_FLIP)
float oppositeNormalWS = -data.normalWS;
#ifdef _DOUBLESIDED_LIGHTING_FLIP
float3 oppositeNormalWS = -data.normalWS;
float oppositeNormalWS = reflect(data.normalWS, vertexNormalWS);
float3 oppositeNormalWS = reflect(data.normalWS, vertexNormalWS);
data.normalWS = (input.cullFace > 0.0 ? GetOdddNegativeScale() : -GetOdddNegativeScale()) >= 0.0 ? data.normalWS : oppositeNormalWS;
//data.normalWS = IS_FRONT_VFACE(GetOdddNegativeScale() : -GetOdddNegativeScale()) >= 0.0 ? data.normalWS : oppositeNormalWS;
//data.normalWS = input.cullFace > 0.0 ? data.normalWS : data.normalWS;
data.normalWS = IS_FRONT_VFACE(input.cullFace, data.normalWS, -data.normalWS);
#endif
*/

#elif _MASKMAP // If we have a MaskMap, use emissive slot as a mask on baseColor
data.emissiveColor = data.baseColor * tex2D(_MaskMap, uv).b;
#else
data.emissiveColor = float3(0.0f, 0.0f, 0.0f);
data.emissiveColor = float3(0.0, 0.0, 0.0);
data.subSurfaceRadius = 1.0f; // tex2D(_SubSurfaceRadiusMap, input.texCoord0).r * _SubSurfaceRadius;
data.subSurfaceRadius = 1.0; // tex2D(_SubSurfaceRadiusMap, input.texCoord0).r * _SubSurfaceRadius;
// TODO
/*

4
Assets/ScriptableRenderLoop/ShaderLibrary/API/D3D11.hlsl


#define UNITY_UV_STARTS_AT_TOP 1
#define UNITY_REVERSED_Z 1
#define UNITY_NEAR_CLIP_VALUE (1.0)
#define VFACE FACE
#define FRONT_FACE_SEMATIC SV_IsFrontFace
#define FRONT_FACE_TYPE bool
#define IS_FRONT_VFACE(VAL, FRONT, BACK) ((VAL) ? (FRONT) : (BACK))
#define CBUFFER_START(name) cbuffer name {
#define CBUFFER_END };

18
Assets/ScriptableRenderLoop/ShaderLibrary/BSDF.hlsl


float F_Schlick(float f0, float f90, float u)
{
float x = 1.0f - u;
float x = 1.0 - u;
float x5 = x * x;
x5 = x5 * x5 * x;
return (f90 - f0) * x5 + f0; // sub mul mul mul sub mad

{
return F_Schlick(f0, 1.0f, u);
return F_Schlick(f0, 1.0, u);
float x = 1.0f - u;
float x = 1.0 - u;
return (f90 - f0) * x5 + f0; // sub mul mul mul sub mad
return (float3(f90, f90, f90) - f0) * x5 + f0; // sub mul mul mul sub mad
return F_Schlick(f0, float3(1.0f, 1.0f, 1.0f), u);
return F_Schlick(f0, 1.0, u);
}
//-----------------------------------------------------------------------------

{
roughness = max(roughness, UNITY_MIN_ROUGHNESS);
float a2 = roughness * roughness;
float f = (NdotH * a2 - NdotH) * NdotH + 1.0f;
float f = (NdotH * a2 - NdotH) * NdotH + 1.0;
return INV_PI * a2 / (f * f);
}

half lambdaV = NdotL * (NdotV * (1 - a) + a);
half lambdaL = NdotV * (NdotL * (1 - a) + a);
return 0.5f / (lambdaV + lambdaL);
return 0.5 / (lambdaV + lambdaL);
#endif
}

{
float fd90 = 0.5 + 2 * LdotH * LdotH * perceptualRoughness;
// Two schlick fresnel term
float lightScatter = F_Schlick(1.0f, fd90, NdotL);
float viewScatter = F_Schlick(1.0f, fd90, NdotV);
float lightScatter = F_Schlick(1.0, fd90, NdotL);
float viewScatter = F_Schlick(1.0, fd90, NdotV);
return INV_PI * lightScatter * viewScatter;
}

2
Assets/ScriptableRenderLoop/ShaderLibrary/Color.hlsl


float kOneOverRGBMMaxRange = 1.0 / maxRGBM;
const float kMinMultiplier = 2.0 * 1e-2;
float4 rgbm = float4(rgb * kOneOverRGBMMaxRange, 1.0f);
float4 rgbm = float4(rgb * kOneOverRGBMMaxRange, 1.0);
rgbm.a = max(max(rgbm.r, rgbm.g), max(rgbm.b, kMinMultiplier));
rgbm.a = ceil(rgbm.a * 255.0) / 255.0;

18
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


float FastACos(float inX)
{
float x = abs(inX);
float res = -0.156583f * x + HALF_PI;
res *= sqrt(1.0f - x);
float res = -0.156583 * x + HALF_PI;
res *= sqrt(1.0 - x);
return (inX >= 0) ? res : PI - res;
}

// input [0, infinity] and output [0, PI/2]
float FastATanPos(float x)
{
float t0 = (x < 1.0f) ? x : 1.0f / x;
float t0 = (x < 1.0) ? x : 1.0 / x;
float poly = 0.0872929f;
poly = -0.301895f + poly * t1;
poly = 1.0f + poly * t1;
float poly = 0.0872929;
poly = -0.301895 + poly * t1;
poly = 1.0 + poly * t1;
return (x < 1.0f) ? poly : HALF_PI - poly;
return (x < 1.0) ? poly : HALF_PI - poly;
}
// 4 VGPR, 16 FR (12 FR, 1 QR), 2 scalar

float t0 = FastATanPos(abs(x));
return (x < 0.0f) ? -t0: t0;
return (x < 0.0f) ? -t0 : t0;
}
// ----------------------------------------------------------------------------

// TODO: How to detect automatically that we are a compute shader ?
#if 0
// In case of compute shader an extra half offset is added to the screenPos to shift the integer position to pixel center.
coord.positionSS.xy += float2(0.5f, 0.5f);
coord.positionSS.xy += float2(0.5, 0.5);
#endif
coord.positionSS *= invScreenSize;

8
Assets/ScriptableRenderLoop/ShaderLibrary/Filtering.hlsl


// Compute the Catmull-Rom weights using the fractional offset that we calculated earlier.
// These equations are pre-expanded based on our knowledge of where the texels will be located,
// which lets us avoid having to evaluate a piece-wise function.
float2 w0 = (1.0f / 6.0f) * (-3.0f * f3 + 6.0f * f2 - 3.0f * f);
float2 w1 = (1.0f / 6.0f) * (9.0f * f3 - 15.0f * f2 + 6.0f);
float2 w2 = (1.0f / 6.0f) * (-9.0f * f3 + 12.0f * f2 + 3.0f * f);
float2 w3 = (1.0f / 6.0f) * (3.0f * f3 - 3.0f * f2);
float2 w0 = (1.0f / 6.0) * (-3.0 * f3 + 6.0 * f2 - 3.0 * f);
float2 w1 = (1.0f / 6.0) * (9.0 * f3 - 15.0 * f2 + 6.0);
float2 w2 = (1.0f / 6.0) * (-9.0 * f3 + 12.0 * f2 + 3.0 * f);
float2 w3 = (1.0f / 6.0) * (3.0 * f3 - 3.0 * f2);
// Otim by Vlad, to test
// float2 w0 = (1.0 / 2.0) * f * (-1.0 + f * (2.0 - f));

正在加载...
取消
保存