浏览代码

cleaned up decal shader, added custom UI for decal shader

/Add-support-for-light-specular-color-tint
Paul Melamed 7 年前
当前提交
a5eb21ff
共有 9 个文件被更改,包括 146 次插入61 次删除
  1. 23
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
  2. 5
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader
  3. 14
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl
  4. 21
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl
  5. 23
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitDataInternal.hlsl
  6. 15
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl
  7. 1
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl
  8. 105
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs

23
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs


}
}
internal HashSet<DecalComponent> m_DecalsDiffuse = new HashSet<DecalComponent>();
internal HashSet<DecalComponent> m_DecalsNormals = new HashSet<DecalComponent>();
internal HashSet<DecalComponent> m_DecalsBoth = new HashSet<DecalComponent>();
internal HashSet<DecalComponent> m_Decals = new HashSet<DecalComponent>();
Mesh m_CubeMesh;
private static readonly int m_WorldToDecal = Shader.PropertyToID("_WorldToDecal");

public void AddDecal(DecalComponent d)
{
RemoveDecal(d);
if (d.m_Kind == DecalComponent.Kind.DiffuseOnly)
m_DecalsDiffuse.Add(d);
if (d.m_Kind == DecalComponent.Kind.NormalsOnly)
m_DecalsNormals.Add(d);
if (d.m_Kind == DecalComponent.Kind.Both)
m_DecalsBoth.Add(d);
if (d.m_Material.GetTexture("_BaseColorMap") || d.m_Material.GetTexture("_NormalMap"))
{
RemoveDecal(d);
m_Decals.Add(d);
}
m_DecalsDiffuse.Remove(d);
m_DecalsNormals.Remove(d);
m_DecalsBoth.Remove(d);
m_Decals.Remove(d);
}
public void Render(ScriptableRenderContext renderContext, Camera camera, CommandBuffer cmd)

{
CRWStoAWS = Matrix4x4.identity;
}
foreach (var decal in m_DecalsDiffuse)
foreach (var decal in m_Decals)
{
Matrix4x4 final = decal.transform.localToWorldMatrix;
Matrix4x4 decalToWorldR = Matrix4x4.Rotate(decal.transform.localRotation);

5
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader


//-------------------------------------------------------------------------------------
// Variant
//-------------------------------------------------------------------------------------
#pragma shader_feature _COLORMAP
#pragma shader_feature _NORMALMAP
/*
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _DEPTHOFFSET_ON

ENDHLSL
}
}
// CustomEditor "Experimental.Rendering.HDPipeline.LitGUI"
CustomEditor "Experimental.Rendering.HDPipeline.DecalUI"
}

14
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl


float3 positionDS = mul(_WorldToDecal, float4(positionWS, 1.0f)).xyz;
clip(positionDS < 0 ? -1 : 1);
clip(positionDS > 1 ? -1 : 1);
surfaceData.baseColor = float4(0,0,0,0);
surfaceData.normalWS = float4(0,0,0,0);
float totalBlend = _DecalBlend;
#if _COLORMAP
surfaceData.baseColor.w *= _DecalBlend;
totalBlend *= surfaceData.baseColor.w;
surfaceData.baseColor.w = totalBlend;
#endif
//surfaceData.normalWS.xyz = mul((float3x3)_DecalToWorldR, SAMPLE_UVMAPPING_NORMALMAP(_NormalMap, sampler_NormalMap, texCoord, 1)) * 0.5f + 0.5f;
#if _NORMALMAP
surfaceData.normalWS.w = surfaceData.baseColor.w;
surfaceData.normalWS.w = totalBlend;
#endif
}

21
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitData.hlsl


#endif
}
void AddDecalBaseColor(float2 texCoords, inout float3 baseColor)
{
float4 decalBaseColor = SAMPLE_TEXTURE2D(_DBufferTexture0, sampler_DBufferTexture0, texCoords);
baseColor = lerp(baseColor, decalBaseColor.xyz, decalBaseColor.w);
}
void AddDecalNormal(float2 texCoords, inout float3 normalTS)
{
float4 decalNormalTS = SAMPLE_TEXTURE2D(_DBufferTexture1, sampler_DBufferTexture1, texCoords) * float4(2.0f, 2.0f, 2.0f, 1.0f) - float4(1.0f, 1.0f, 1.0f, 0.0f);
if(decalNormalTS.w > 0.0f)
normalTS = normalize(lerp(normalTS, decalNormalTS.xyz, decalNormalTS.w));
}
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
#ifdef LOD_FADE_CROSSFADE // enable dithering LOD transition if user select CrossFade transition in LOD group

float3 bentNormalTS;
float3 bentNormalWS;
float alpha = GetSurfaceData(input, layerTexCoord, surfaceData, normalTS, bentNormalTS);
AddDecalBaseColor(posInput.positionSS.xy, surfaceData.baseColor);
AddDecalBaseColor(posInput.positionSS.xy, surfaceData.baseColor);
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
// Use bent normal to sample GI if available
surfaceData.specularOcclusion = 1.0;

surfaceData.atDistance = 1000000.0;
surfaceData.transmittanceMask = 0.0;
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
AddDecalBaseColor(posInput.positionSS.xy, surfaceData.baseColor);
AddDecalBaseColor(posInput.positionSS.xy, surfaceData.baseColor);
GetNormalWS(input, V, normalTS, surfaceData.normalWS);
// Use bent normal to sample GI if available
// If any layer use a bent normal map, then bentNormalTS contain the interpolated result of bentnormal and normalmap (in case no bent normal are available)
// Note: the code in LitDataInternal ensure that we fallback on normal map for layer that have no bentnormal

23
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitDataInternal.hlsl


return alpha;
}
void AddDecalToSurfaceData(float2 texCoords, inout SurfaceData surfaceData)
{
float4 decalBaseColor = SAMPLE_TEXTURE2D(_DBufferTexture0, sampler_DBufferTexture0, texCoords);
surfaceData.baseColor.xyz = lerp(surfaceData.baseColor.xyz, decalBaseColor.xyz, decalBaseColor.w);
float4 decalNormalWS = SAMPLE_TEXTURE2D(_DBufferTexture1, sampler_DBufferTexture1, texCoords) * float4(2.0f, 2.0f, 2.0f, 1.0f) - float4(1.0f, 1.0f, 1.0f, 0.0f);
surfaceData.normalWS = normalize(lerp(surfaceData.normalWS, decalNormalWS.xyz, decalNormalWS.w));
}
void AddDecalBaseColor(float2 texCoords, inout float3 baseColor)
{
float4 decalBaseColor = SAMPLE_TEXTURE2D(_DBufferTexture0, sampler_DBufferTexture0, texCoords);
baseColor = lerp(baseColor, decalBaseColor.xyz, decalBaseColor.w);
}
void AddDecalNormal(float2 texCoords, inout float3 normalTS)
{
float4 decalNormalTS = SAMPLE_TEXTURE2D(_DBufferTexture1, sampler_DBufferTexture1, texCoords) * float4(2.0f, 2.0f, 2.0f, 1.0f) - float4(1.0f, 1.0f, 1.0f, 0.0f);
if(decalNormalTS.w > 0.0f)
normalTS = normalize(lerp(normalTS, decalNormalTS.xyz, decalNormalTS.w));
//normalTS = (lerp(normalTS, decalNormalTS.xyz, decalNormalTS.w));
}

15
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl


PositionInputs posInput = GetPositionInput(input.unPositionSS.xy, _ScreenSize.zw);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
// UpdatePositionInput(input.unPositionSS.z, input.unPositionSS.w, input.positionWS, posInput);
SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);
float3 bakeDiffuseLighting = GetBakedDiffuseLigthing(surfaceData, builtinData, bsdfData, preLightData);
ENCODE_INTO_GBUFFER(surfaceData, bakeDiffuseLighting, outGBuffer);
ENCODE_VELOCITY_INTO_GBUFFER(builtinData.velocity, outVelocityBuffer);
*/

GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
outDBuffer0 = surfaceData.baseColor;
outDBuffer1 = surfaceData.normalWS;
// outDBuffer0.xyzw = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, positionDS.xz); // float4(positionDS, 1.0f);
// texCoord.uv = positionDS.xz;
// outDBuffer1.xyz = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, positionDS.xz).xyz; //SAMPLE_UVMAPPING_NORMALMAP(_NormalMap, sampler_NormalMap, texCoord, 1); //SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, positionDS.xz).xyz;
// outDBuffer1.w = outDBuffer0.w;
}

1
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl


SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
// AddDecalToSurfaceData(posInput.positionSS.xy, surfaceData);
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);

105
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Editor/DecalUI.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class DecalUI : ShaderGUI
{
protected static class Styles
{
public static string InputsText = "Inputs";
public static GUIContent baseColorText = new GUIContent("Base Color + Blend", "Albedo (RGB) and Blend Factor (A)");
public static GUIContent normalMapText = new GUIContent("Normal Map", "Normal Map (BC7/BC5/DXT5(nm))");
public static GUIContent decalBlendText = new GUIContent("Decal Blend", "Combines with Base Color (A)");
}
protected MaterialProperty baseColorMap = new MaterialProperty();
protected const string kBaseColorMap = "_BaseColorMap";
protected MaterialProperty normalMap = new MaterialProperty();
protected const string kNormalMap = "_NormalMap";
protected MaterialProperty decalBlend = new MaterialProperty();
protected const string kDecalBlend = "_DecalBlend";
protected MaterialEditor m_MaterialEditor;
// This is call by the inspector
void FindMaterialProperties(MaterialProperty[] props)
{
baseColorMap = FindProperty(kBaseColorMap, props);
normalMap = FindProperty(kNormalMap, props);
decalBlend = FindProperty(kDecalBlend, props);
}
static public void SetKeyword(Material m, string keyword, bool state)
{
if (state)
m.EnableKeyword(keyword);
else
m.DisableKeyword(keyword);
}
// All Setup Keyword functions must be static. It allow to create script to automatically update the shaders with a script if code change
static public void SetupMaterialKeywordsAndPass(Material material)
{
SetKeyword(material, "_COLORMAP", material.GetTexture(kBaseColorMap));
SetKeyword(material, "_NORMALMAP", material.GetTexture(kNormalMap));
}
protected void SetupMaterialKeywordsAndPassInternal(Material material)
{
SetupMaterialKeywordsAndPass(material);
}
public void ShaderPropertiesGUI(Material material)
{
// Use default labelWidth
EditorGUIUtility.labelWidth = 0f;
// Detect any changes to the material
EditorGUI.BeginChangeCheck();
{
EditorGUILayout.LabelField(Styles.InputsText, EditorStyles.boldLabel);
EditorGUI.indentLevel++;
m_MaterialEditor.TexturePropertySingleLine(Styles.baseColorText, baseColorMap);
m_MaterialEditor.TexturePropertySingleLine(Styles.normalMapText, normalMap);
m_MaterialEditor.ShaderProperty(decalBlend, Styles.decalBlendText);
EditorGUI.indentLevel--;
}
if (EditorGUI.EndChangeCheck())
{
foreach (var obj in m_MaterialEditor.targets)
SetupMaterialKeywordsAndPassInternal((Material)obj);
}
}
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
{
m_MaterialEditor = materialEditor;
// We should always do this call at the beginning
m_MaterialEditor.serializedObject.Update();
FindMaterialProperties(props);
Material material = materialEditor.target as Material;
ShaderPropertiesGUI(material);
// We should always do this call at the end
m_MaterialEditor.serializedObject.ApplyModifiedProperties();
}
}
}
正在加载...
取消
保存