浏览代码

color map and normal maps working for decals

/Add-support-for-light-specular-color-tint
Paul Melamed 7 年前
当前提交
c4761310
共有 19 个文件被更改,包括 176 次插入39 次删除
  1. 23
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
  2. 6
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalComponent.cs
  3. 7
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  4. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs
  5. 6
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl
  6. 15
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader
  7. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl
  8. 8
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/LitDataInternal.hlsl
  9. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl
  10. 21
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl
  11. 1
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassGBuffer.hlsl
  12. 7
      ScriptableRenderPipeline/HDRenderPipeline/ShaderVariables.hlsl
  13. 22
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs
  14. 39
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl
  15. 23
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalData.hlsl
  16. 7
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass/DecalSharePass.hlsl
  17. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/OutputBuffers.hlsl
  18. 0
      /ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalComponent.cs

23
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs


using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline.Decal
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public class DecalSystem
{

}
}
internal HashSet<Decal> m_DecalsDiffuse = new HashSet<Decal>();
internal HashSet<Decal> m_DecalsNormals = new HashSet<Decal>();
internal HashSet<Decal> m_DecalsBoth = new HashSet<Decal>();
internal HashSet<DecalComponent> m_DecalsDiffuse = new HashSet<DecalComponent>();
internal HashSet<DecalComponent> m_DecalsNormals = new HashSet<DecalComponent>();
internal HashSet<DecalComponent> m_DecalsBoth = new HashSet<DecalComponent>();
private static readonly int m_DecalToWorldR = Shader.PropertyToID("_DecalToWorldR");
public DecalSystem()
{

m_CubeMesh.triangles = triangles;
}
public void AddDecal(Decal d)
public void AddDecal(DecalComponent d)
if (d.m_Kind == Decal.Kind.DiffuseOnly)
if (d.m_Kind == DecalComponent.Kind.DiffuseOnly)
if (d.m_Kind == Decal.Kind.NormalsOnly)
if (d.m_Kind == DecalComponent.Kind.NormalsOnly)
if (d.m_Kind == Decal.Kind.Both)
if (d.m_Kind == DecalComponent.Kind.Both)
public void RemoveDecal(Decal d)
public void RemoveDecal(DecalComponent d)
{
m_DecalsDiffuse.Remove(d);
m_DecalsNormals.Remove(d);

}
foreach (var decal in m_DecalsDiffuse)
{
Matrix4x4 final = decal.transform.localToWorldMatrix;
Matrix4x4 final = decal.transform.localToWorldMatrix;
Matrix4x4 decalToWorldR = Matrix4x4.Rotate(decal.transform.localRotation);
cmd.SetGlobalMatrix(m_DecalToWorldR, decalToWorldR);
//DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int submeshIndex, int shaderPass);
cmd.DrawMesh(m_CubeMesh, final, decal.m_Material, 0, 0);
}

6
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalComponent.cs


using UnityEditor;
using UnityEngine;
namespace UnityEngine.Experimental.Rendering.HDPipeline.Decal
namespace UnityEngine.Experimental.Rendering.HDPipeline
public class Decal : MonoBehaviour
public class DecalComponent : MonoBehaviour
{
public enum Kind
{

{
// Create a custom game object
GameObject go = new GameObject("Decal");
go.AddComponent<Decal>();
go.AddComponent<DecalComponent>();
// Ensure it gets re-parented if this was a context click (otherwise does nothing)
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
// Register the creation in the undo system

7
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


using System.Linq;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.Experimental.Rendering.HDPipeline.TilePass;
using UnityEngine.Experimental.Rendering.HDPipeline.Decal;
#if UNITY_EDITOR
using UnityEditor;

m_GbufferManager.SetBufferDescription(gbufferIndex, "_GBufferTexture" + gbufferIndex, rtFormat[gbufferIndex], rtReadWrite[gbufferIndex]);
}
m_DbufferManager.gbufferCount = 1;
m_DbufferManager.gbufferCount = 2;
m_DbufferManager.SetBufferDescription(1, "_DBufferTexture1", RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
m_VelocityBuffer = HDShaderIDs._VelocityTexture;
if (ShaderConfig.s_VelocityInGbuffer == 1)

CoreUtils.SetRenderTarget(cmd, m_DbufferManager.GetGBuffers(), m_CameraDepthStencilBufferRT);
cmd.SetGlobalTexture(HDShaderIDs._CameraDepthTexture, GetDepthTexture());
DecalSystem.instance.Render(renderContext, camera, cmd);
cmd.SetGlobalTexture(HDShaderIDs._DBufferTexture0, m_DbufferManager.GetGBuffers()[0]);
cmd.SetGlobalTexture(HDShaderIDs._DBufferTexture1, m_DbufferManager.GetGBuffers()[1]);
}
RenderGBuffer(m_CullResults, camera, renderContext, cmd);

3
ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs


public static readonly int _Size = Shader.PropertyToID("_Size");
public static readonly int _Source4 = Shader.PropertyToID("_Source4");
public static readonly int _Result1 = Shader.PropertyToID("_Result1");
public static readonly int _DBufferTexture0 = Shader.PropertyToID("_DBufferTexture0");
public static readonly int _DBufferTexture1 = Shader.PropertyToID("_DBufferTexture1");
}
}

6
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl


CBUFFER_START(Decal)
float4x4 _WorldToDecal;
CBUFFER_END
#include "Decal.cs.hlsl"
#define DBufferType0 float4

15
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader


Properties
{
_BaseColorMap("BaseColorMap", 2D) = "white" {}
_NormalMap("NormalMap", 2D) = "bump" {} // Tangent space normal map
_DecalBlend("_DecalBlend", Range(0.0, 1.0)) = 0.5
}
HLSLINCLUDE

//-------------------------------------------------------------------------------------
// Define
//-------------------------------------------------------------------------------------
#define UNITY_MATERIAL_DECAL // Need to be define before including Material.hlsl
/*
#define UNITY_MATERIAL_LIT // Need to be define before including Material.hlsl
// Use surface gradient normal mapping as it handle correctly triplanar normal mapping and multiple UVSet

HLSLPROGRAM
#include "../../ShaderVariables.hlsl"
#include "OutputBuffers.hlsl"
#include "Decal.hlsl"
// #include "../../ShaderVariables.hlsl"
#include "../../ShaderPass/VaryingMesh.hlsl"
// #include "../../ShaderPass/ShaderPassGBuffer.hlsl"
#define SHADERPASS SHADERPASS_DBUFFER
#include "../../ShaderVariables.hlsl"
#include "../../Material/Material.hlsl"
#include "ShaderPass/DecalSharePass.hlsl"
#include "DecalData.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl


TEXTURE2D(_BaseColorMap);
SAMPLER2D(sampler_BaseColorMap);
TEXTURE2D(_NormalMap);
SAMPLER2D(sampler_NormalMap);
float _DecalBlend;
CBUFFER_START(Decal)
float4x4 _WorldToDecal;
float4x4 _DecalToWorldR;
CBUFFER_END
#endif

8
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));
}

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Material.hlsl


#include "Unlit/Unlit.hlsl"
#elif defined(UNITY_MATERIAL_IRIDESCENCE)
//#include "Iridescence/Iridescence.hlsl"
#elif defined(UNITY_MATERIAL_DECAL)
#include "Decal/Decal.hlsl"
#endif
//-----------------------------------------------------------------------------

#endif
#endif // #ifdef GBUFFERMATERIAL_COUNT
// define for dbuffer management
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0, \
out DBufferType0 MERGE_NAME(NAME, 1) : SV_Target1 \
#endif // UNITY_MATERIAL_INCLUDED

21
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl


#if SHADERPASS != SHADERPASS_GBUFFER
#if SHADERPASS != SHADERPASS_DBUFFER
#error SHADERPASS_is_not_correctly_define
#endif

FragInputs input = UnpackVaryingsMeshToFragInputs(packedInput.vmesh);
// input.unPositionSS is SV_Position
PositionInputs posInput = GetPositionInput(input.unPositionSS.xy, _ScreenSize.zw);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
SurfaceData surfaceData;
BuiltinData builtinData;
GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);

*/
float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, posInput.positionSS.xy);
UpdatePositionInput(d, _InvViewProjMatrix, _ViewProjMatrix, posInput);
float3 positionWS = posInput.positionWS;
float3 positionDS = mul(_WorldToDecal, float4(positionWS, 1.0f)).xyz;
clip(positionDS < 0 ? -1 : 1);
clip(positionDS > 1 ? -1 : 1);
outDBuffer0.xyzw = float4(positionDS, 1.0f);
SurfaceData surfaceData;
BuiltinData builtinData;
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);

7
ScriptableRenderPipeline/HDRenderPipeline/ShaderVariables.hlsl


TEXTURE3D_FLOAT(unity_ProbeVolumeSH);
SAMPLER3D(samplerunity_ProbeVolumeSH);
// Decals textures
TEXTURE2D(_DBufferTexture0);
SAMPLER2D(sampler_DBufferTexture0);
TEXTURE2D(_DBufferTexture1);
SAMPLER2D(sampler_DBufferTexture1);
CBUFFER_START(UnityVelocityPass)
float4x4 unity_MatrixNonJitteredVP;
float4x4 unity_MatrixPreviousVP;

22
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs


using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
public partial class Decal : RenderPipelineMaterial
{
// Main structure that store the user data (i.e user input of master node in material graph)
[GenerateHLSL(PackingRules.Exact, false, true, 2000)]
public struct SurfaceData
{
[SurfaceDataAttributes("Base Color", false, true)]
public Vector4 baseColor;
[SurfaceDataAttributes("Normal", true)]
public Vector4 normalWS;
};
[GenerateHLSL(PackingRules.Exact, false, true, 2100)]
public struct BSDFData
{
};
}
}

39
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.cs.hlsl


//
// This file was automatically generated. Please don't edit by hand.
//
#ifndef DECAL_CS_HLSL
#define DECAL_CS_HLSL
//
// UnityEngine.Experimental.Rendering.HDPipeline.Decal+SurfaceData: static fields
//
#define DEBUGVIEW_DECAL_SURFACEDATA_BASE_COLOR (2000)
#define DEBUGVIEW_DECAL_SURFACEDATA_NORMAL_WS (2001)
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.Decal+SurfaceData
// PackingRules = Exact
struct SurfaceData
{
float4 baseColor;
float4 normalWS;
};
//
// Debug functions
//
void GetGeneratedSurfaceDataDebug(uint paramId, SurfaceData surfacedata, inout float3 result, inout bool needLinearToSRGB)
{
switch (paramId)
{
case DEBUGVIEW_DECAL_SURFACEDATA_BASE_COLOR:
result = surfacedata.baseColor.xyz;
needLinearToSRGB = true;
break;
case DEBUGVIEW_DECAL_SURFACEDATA_NORMAL_WS:
result = surfacedata.normalWS.xyz;
break;
}
}
#endif

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


//-------------------------------------------------------------------------------------
// Fill SurfaceData/Builtin data function
//-------------------------------------------------------------------------------------
#include "../../../Core/ShaderLibrary/SampleUVMapping.hlsl"
#include "../MaterialUtilities.hlsl"
void GetSurfaceAndBuiltinData(FragInputs input, float3 V, inout PositionInputs posInput, out SurfaceData surfaceData, out BuiltinData builtinData)
{
float3 positionWS = posInput.positionWS;
float3 positionDS = mul(_WorldToDecal, float4(positionWS, 1.0f)).xyz;
clip(positionDS < 0 ? -1 : 1);
clip(positionDS > 1 ? -1 : 1);
surfaceData.baseColor = SAMPLE_TEXTURE2D(_BaseColorMap, sampler_BaseColorMap, positionDS.xz);
surfaceData.baseColor.w *= _DecalBlend;
UVMapping texCoord;
ZERO_INITIALIZE(UVMapping, texCoord);
ZERO_INITIALIZE(BuiltinData, builtinData);
texCoord.uv = positionDS.xz;
surfaceData.normalWS.xyz = mul((float3x3)_DecalToWorldR, SAMPLE_UVMAPPING_NORMALMAP(_NormalMap, sampler_NormalMap, texCoord, 1)) * 0.5f + 0.5f;
surfaceData.normalWS.w = surfaceData.baseColor.w;
}

7
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/ShaderPass/DecalSharePass.hlsl


#ifndef SHADERPASS
#error Undefine_SHADERPASS
#endif
// This include will define the various Attributes/Varyings structure
#include "../../ShaderPass/VaryingMesh.hlsl"

9
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/OutputBuffers.hlsl


#ifndef UNITY_DECALOUTPUTBUFFERS_INCLUDED
#define UNITY_DECALOUTPUTBUFFERS_INCLUDED
#define DBufferType0 float4
#define OUTPUT_DBUFFER(NAME) \
out DBufferType0 MERGE_NAME(NAME, 0) : SV_Target0
#endif

/ScriptableRenderPipeline/HDRenderPipeline/Decal/Decal.cs → /ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalComponent.cs

正在加载...
取消
保存