浏览代码

Move world to decal calculation to shaders, remove the need to set property block in c#

/main
Paul Melamed 7 年前
当前提交
2479dda2
共有 3 个文件被更改,包括 4 次插入41 次删除
  1. 36
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Decal/DecalProjectorComponent.cs
  2. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Decal/DecalSystem.cs
  3. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassDBuffer.hlsl

36
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Decal/DecalProjectorComponent.cs


private static readonly int m_DecalToWorldR = Shader.PropertyToID("_DecalToWorldR");
public Material m_Material;
private MaterialPropertyBlock m_PropertyBlock;
// normal space __x to decal space __x
// |\ |\
// y z z y
//
private static Matrix4x4 m_NormalToDecal = Matrix4x4.Scale(new Vector3(1.0f, 1.0f, -1.0f)) * Matrix4x4.Rotate(Quaternion.AngleAxis(-90.0f, new Vector3(1, 0, 0)));
public int CullIndex
{

public void Start()
{
m_PropertyBlock = new MaterialPropertyBlock();
DecalSystem.instance.AddDecal(this);
}

DrawGizmo(true);
// if this object is selected there is a chance the transform was changed so update culling info
DecalSystem.instance.UpdateBoundingSphere(this);
}
public void UpdatePropertyBlock(Vector3 cameraPos)
{
Matrix4x4 CRWStoAWS = new Matrix4x4();
if (ShaderConfig.s_CameraRelativeRendering == 1)
{
CRWStoAWS = Matrix4x4.Translate(cameraPos);
}
else
{
CRWStoAWS = Matrix4x4.identity;
}
Matrix4x4 final = transform.localToWorldMatrix;
Matrix4x4 decalToWorldR = Matrix4x4.Rotate(transform.rotation) * m_NormalToDecal;
Matrix4x4 worldToDecal = Matrix4x4.Translate(new Vector3(0.5f, 0.0f, 0.5f)) * Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f)) * final.inverse;
if (m_PropertyBlock == null)
{
m_PropertyBlock = new MaterialPropertyBlock();
}
m_PropertyBlock.SetMatrix(m_DecalToWorldR, decalToWorldR);
m_PropertyBlock.SetMatrix(m_WorldToDecal, worldToDecal * CRWStoAWS);
}
public MaterialPropertyBlock GetPropertyBlock()
{
return m_PropertyBlock;
}
}
}

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Decal/DecalSystem.cs


!m_Decals[decalIndex].m_Material.GetTexture("_MaskMap"))
continue;
m_Decals[decalIndex].UpdatePropertyBlock(camera.cameraPos);
cmd.DrawMesh(m_DecalMesh, m_Decals[decalIndex].transform.localToWorldMatrix, m_Decals[decalIndex].m_Material, 0, 0,
m_Decals[decalIndex].GetPropertyBlock());
cmd.DrawMesh(m_DecalMesh, m_Decals[decalIndex].transform.localToWorldMatrix, m_Decals[decalIndex].m_Material, 0, 0);
}
}

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassDBuffer.hlsl


float d = LOAD_TEXTURE2D(_MainDepthTexture, posInput.positionSS).x;
UpdatePositionInput(d, UNITY_MATRIX_I_VP, UNITY_MATRIX_VP, posInput);
float3 positionWS = posInput.positionWS;
float3 positionDS = mul(_WorldToDecal, float4(positionWS, 1.0f)).xyz;
float3 positionWS = GetAbsolutePositionWS(posInput.positionWS);
float3 positionDS = mul(UNITY_MATRIX_I_M, float4(positionWS, 1.0f)).xyz;
positionDS = positionDS * float3(1.0f, -1.0f, 1.0f) + float3(0.5f, 0.0f, 0.5f);
clip(positionDS < 0 ? -1 : 1);
clip(positionDS > 1 ? -1 : 1);

正在加载...
取消
保存