浏览代码

debugging projecting a decal onto geometry

/Add-support-for-light-specular-color-tint
Paul Melamed 7 年前
当前提交
650a7ab4
共有 11 个文件被更改,包括 109 次插入19 次删除
  1. 2
      ScriptableRenderPipeline/Core/ShaderLibrary/API/D3D11.hlsl
  2. 26
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
  3. 7
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  4. 15
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.shader
  5. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/DecalProperties.hlsl
  6. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/OutputBuffers.hlsl
  7. 3
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs
  8. 1
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs.hlsl
  9. 3
      ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl
  10. 67
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl

2
ScriptableRenderPipeline/Core/ShaderLibrary/API/D3D11.hlsl


#define GATHER_TEXTURE2D_ARRAY(textureName, samplerName, coord2, index) textureName.Gather(samplerName, float3(coord2, index))
#define GATHER_TEXTURECUBE(textureName, samplerName, coord3) textureName.Gather(samplerName, coord3)
#define GATHER_TEXTURECUBE_ARRAY(textureName, samplerName, coord3, index) textureName.Gather(samplerName, float4(coord3, index))
#define SAMPLE_DEPTH_TEXTURE(textureName, samplerName, coord2) SAMPLE_TEXTURE2D(textureName, samplerName, coord2).r

26
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs


internal HashSet<Decal> m_DecalsBoth = new HashSet<Decal>();
Mesh m_CubeMesh;
private readonly static int m_WorldToDecal = Shader.PropertyToID("_WorldToDecal");
{
CreateCubeMesh();
}
void CreateCubeMesh()
{
m_CubeMesh = new Mesh();

int[] triangles = new int[36];
triangles[0] = 0; triangles[1] = 1; triangles[2] = 2;
triangles[3] = 0; triangles[4] = 2; triangles[5] = 3;
triangles[6] = 1; triangles[7] = 5; triangles[8] = 6;
triangles[9] = 1; triangles[10] = 6; triangles[11] = 2;
triangles[12] = 5; triangles[13] = 3; triangles[14] = 7;
triangles[0] = 0; triangles[1] = 1; triangles[2] = 2;
triangles[3] = 0; triangles[4] = 2; triangles[5] = 3;
triangles[6] = 1; triangles[7] = 5; triangles[8] = 6;
triangles[9] = 1; triangles[10] = 6; triangles[11] = 2;
triangles[12] = 5; triangles[13] = 4; triangles[14] = 7;
triangles[15] = 5; triangles[16] = 7; triangles[17] = 6;
triangles[18] = 4; triangles[19] = 0; triangles[20] = 3;
triangles[21] = 4; triangles[22] = 3; triangles[23] = 7;

triangles[33] = 4; triangles[34] = 1; triangles[35] = 0;
m_CubeMesh.triangles = triangles;
}
public void AddDecal(Decal d)

public void Render(ScriptableRenderContext renderContext, Camera camera, CommandBuffer cmd)
{
if (m_CubeMesh == null)
CreateCubeMesh();
Matrix4x4 final = decal.transform.localToWorldMatrix * offset;
Vector4 positionWS = new Vector4(0,5,0,1);
Vector4 positionDS = final.inverse * positionWS;
cmd.SetGlobalMatrix(m_WorldToDecal, final.inverse);
cmd.DrawMesh(m_CubeMesh, decal.transform.localToWorldMatrix * offset, decal.m_Material, 0, 0);
cmd.DrawMesh(m_CubeMesh, final, decal.m_Material, 0, 0);
}
}
}

7
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


InitAndClearBuffer(hdCamera, cmd);
RenderDepthPrepass(m_CullResults, camera, renderContext, cmd);
RenderDepthPrepass(m_CullResults, camera, renderContext, cmd, true);
cmd.SetGlobalTexture(HDShaderIDs._CameraDepthTexture, GetDepthTexture());
DecalSystem.instance.Render(renderContext, camera, cmd);
}

// Forward only renderer: We always render everything
// Deferred renderer: We render a depth prepass only if engine request it. We can decide if we render everything or only opaque alpha tested object.
// Forward opaque with deferred renderer (DepthForwardOnly pass): We always render everything
void RenderDepthPrepass(CullResults cull, Camera camera, ScriptableRenderContext renderContext, CommandBuffer cmd)
void RenderDepthPrepass(CullResults cull, Camera camera, ScriptableRenderContext renderContext, CommandBuffer cmd, bool hasDecals)
{
// In case of deferred renderer, we can have forward opaque material. These materials need to be render in the depth buffer to correctly build the light list.
// And they will tag the stencil to not be lit during the deferred lighting pass.

using (new ProfilingSample(cmd, addAlphaTestedOnly ? "Depth Prepass alpha test" : "Depth Prepass"))
{
CoreUtils.SetRenderTarget(cmd, m_CameraDepthStencilBufferRT);
if (addFullDepthPrepass && !addAlphaTestedOnly) // Always true in case of forward rendering, use in case of deferred rendering if requesting a full depth prepass
if (hasDecals || (addFullDepthPrepass && !addAlphaTestedOnly)) // Always true in case of forward rendering, use in case of deferred rendering if requesting a full depth prepass
{
// We render first the opaque object as opaque alpha tested are more costly to render and could be reject by early-z (but not Hi-z as it is disable with clip instruction)
// This is handled automatically with the RenderQueue value (OpaqueAlphaTested have a different value and thus are sorted after Opaque)

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


#include "../../../Core/ShaderLibrary/Common.hlsl"
// #include "../../../Core/ShaderLibrary/Wind.hlsl"
#include "../../../Core/ShaderLibrary/Wind.hlsl"
// #include "../../ShaderPass/ShaderPass.cs.hlsl"
#include "../../ShaderPass/ShaderPass.cs.hlsl"
//-------------------------------------------------------------------------------------

Pass
{
Name "DBuffer" // Name is not used
Tags { "LightMode" = "GBuffer" } // This will be only for opaque object based on the RenderQueue index
Tags { "LightMode" = "dBuffer" } // This will be only for opaque object based on the RenderQueue index
ZWrite Off
ZTest [_ZTestMode]
Pass Replace
Pass Replace
#define SHADERPASS_DBUFFER
#include "Decal.hlsl"
#include "ShaderPass/ShaderPassDBuffer.hlsl"
#include "../../ShaderPass/ShaderPassDBuffer.hlsl"
ENDHLSL
}

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


TEXTURE2D(_BaseColorMap);
SAMPLER2D(sampler_BaseColorMap);
TEXTURE2D(_CameraDepthTexture);
SAMPLER2D(sampler_CameraDepthTexture);
#endif

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


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

3
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs


LightTransport,
Shadows,
SubsurfaceScattering,
VolumetricLighting
VolumetricLighting,
DBuffer
}
}

1
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPass.cs.hlsl


#define SHADERPASS_SHADOWS (7)
#define SHADERPASS_SUBSURFACE_SCATTERING (8)
#define SHADERPASS_VOLUMETRIC_LIGHTING (9)
#define SHADERPASS_DBUFFER (10)
#endif

3
ScriptableRenderPipeline/HDRenderPipeline/Material/Decal/Decal.hlsl


CBUFFER_START(Decal)
float4x4 _WorldToDecal;
CBUFFER_END

67
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDBuffer.hlsl


#if SHADERPASS != SHADERPASS_GBUFFER
#error SHADERPASS_is_not_correctly_define
#endif
#include "VertMesh.hlsl"
PackedVaryingsType Vert(AttributesMesh inputMesh)
{
VaryingsType varyingsType;
varyingsType.vmesh = VertMesh(inputMesh);
return PackVaryingsType(varyingsType);
}
void Frag( PackedVaryingsToPS packedInput,
OUTPUT_DBUFFER(outDBuffer)
)
{
FragInputs input = UnpackVaryingsMeshToFragInputs(packedInput.vmesh);
// input.unPositionSS is SV_Position
PositionInputs posInput = GetPositionInput(input.unPositionSS.xy, _ScreenSize.zw);
// UpdatePositionInput(input.unPositionSS.z, input.unPositionSS.w, input.positionWS, posInput);
/*
float3 V = GetWorldSpaceNormalizeViewDir(input.positionWS);
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);
*/
// float d = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, posInput.positionSS.xy), _ZBufferParams);
float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, posInput.positionSS.xy);
UpdatePositionInput(d, _InvViewProjMatrix, _ViewProjMatrix, posInput);
posInput.positionWS.xyz += _WorldSpaceCameraPos;
clip(posInput.positionWS.y < 0 ? -1 : 1);
//clip(posInput.positionWS.y > 0.0001 ? -1 : 1);
outDBuffer0.xyzw = float4(posInput.positionWS.xyz + float3(0.5, 0.0, 0.5), 1.0f);
/*
// float d = Linear01Depth(SAMPLE_TEXTURE_2D(_CameraDepthTexture, sampler_CameraDepthTexture, posInput.positionSS.xy).z);
float4 res;
res = float4(posInput.positionSS.x,posInput.positionSS.y,0,0);
res = float4(0,0,posInput.depthRaw,0);
res = float4(0,0,d,0);
res = float4(posInput.positionWS.xyz,1);
res = mul(_WorldToDecal, res);
clip(res.y < 0.5 ? -1 : 1);
clip(res.x < 0.5 ? -1 : 1);
clip(res.z < 0.5 ? -1 : 1);
clip(res.y > 0.5 ? -1 : 1);
clip(res.x > 0.5 ? -1 : 1);
clip(res.z > 0.5 ? -1 : 1);
// clip(float3(1.0f, 1.0f, 1.0f) - res.xyz);
// float4 res = packedInput.vmesh.positionCS;
outDBuffer0.xyzw = float4(res.xyz, 1.0f);
*/
}
正在加载...
取消
保存