浏览代码

Added CullingGroup API functionality for culling decal objects

/prototype-decals
Paul Melamed 7 年前
当前提交
6a91487b
共有 4 个文件被更改,包括 99 次插入19 次删除
  1. 14
      ScriptableRenderPipeline/Core/CoreUtils.cs
  2. 21
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs
  3. 75
      ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
  4. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs

14
ScriptableRenderPipeline/Core/CoreUtils.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;

mesh.triangles = triangles;
return mesh;
}
public static BoundingSphere GetDecalMeshBoundingSphere(Matrix4x4 decalToWorld)
{
Vector4 min = new Vector4(-0.5f, -1.0f, -0.5f, 1.0f);
Vector4 max = new Vector4(0.5f, 0.0f, 0.5f, 1.0f);
min = decalToWorld * min;
max = decalToWorld * max;
BoundingSphere res = new BoundingSphere();
res.position = (max + min) / 2;
res.radius = ((Vector3)(max - min)).magnitude / 2;
return res;
}
}
}

21
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalProjectorComponent.cs


using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.
namespace UnityEngine.Experimental.Rendering.HDPipeline
{

private static readonly int m_DecalToWorldR = Shader.PropertyToID("_DecalToWorldR");
public Material m_Material;
private MaterialPropertyBlock m_PropertyBlock;
public const int kInvalidIndex = -1;
private int m_CullIndex = kInvalidIndex;
private MaterialPropertyBlock m_PropertyBlock;
public int CullIndex
{
get
{
return this.m_CullIndex;
}
set
{
this.m_CullIndex = value;
}
}
public void OnEnable()
{

public void OnValidate()
{
BoundingSphere sphere = CoreUtils.GetDecalMeshBoundingSphere(transform.localToWorldMatrix);
if (m_Material != null)
{
Shader shader = m_Material.shader;

}
}
}
private void DrawGizmo(bool selected)
{

public void OnDrawGizmosSelected()
{
DrawGizmo(true);
// if this object is selected there is a chance the transform was changed so update culling info
DecalSystem.instance.UpdateCulling(this);
}
public void UpdatePropertyBlock(Vector3 cameraPos)

75
ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs


}
}
internal HashSet<DecalProjectorComponent> m_Decals = new HashSet<DecalProjectorComponent>();
internal List<DecalProjectorComponent> m_Decals = new List<DecalProjectorComponent>();
CullingGroup m_CullingGroup;
private BoundingSphere[] m_BoundingSpheres;
m_DecalMesh = CoreUtils.CreateDecalMesh();
m_DecalMesh = CoreUtils.CreateDecalMesh();
public void AddDecal(DecalProjectorComponent d)
// updates bounding spheres for all decals, also refreshes cull indices
void UpdateCulling()
// If no decal material assign, don't add it
if (d.m_Material == null)
return;
m_BoundingSpheres = new BoundingSphere[m_Decals.Count];
int cullIndex = 0;
foreach (var decal in m_Decals)
{
decal.CullIndex = cullIndex;
m_BoundingSpheres[cullIndex] = CoreUtils.GetDecalMeshBoundingSphere(decal.transform.localToWorldMatrix);
cullIndex++;
}
}
if (d.m_Material.GetTexture("_BaseColorMap") || d.m_Material.GetTexture("_NormalMap"))
// update bounding sphere for a single decal
public void UpdateCulling(DecalProjectorComponent decal)
{
int cullIndex = decal.CullIndex;
m_BoundingSpheres[cullIndex] = CoreUtils.GetDecalMeshBoundingSphere(decal.transform.localToWorldMatrix);
}
public void AddDecal(DecalProjectorComponent d)
{
if (d.CullIndex != DecalProjectorComponent.kInvalidIndex)
m_Decals.Add(d);
d.CullIndex = m_Decals.Count;
m_Decals.Add(d);
UpdateCulling();
UpdateCulling();
public void Render(ScriptableRenderContext renderContext, Vector3 cameraPos, CommandBuffer cmd)
public void Cull(Camera camera)
{
m_CullingGroup = new CullingGroup();
m_CullingGroup.targetCamera = camera;
m_CullingGroup.SetBoundingSpheres(m_BoundingSpheres);
}
public void Render(ScriptableRenderContext renderContext, HDCamera camera, CommandBuffer cmd)
foreach (var decal in m_Decals)
int numResults = 0;
int[] resultIndices = new int[m_Decals.Count];
numResults = m_CullingGroup.QueryIndices(true, resultIndices, 0);
for (int resultIndex = 0; resultIndex < numResults; resultIndex++)
decal.UpdatePropertyBlock(cameraPos);
cmd.DrawMesh(m_DecalMesh, decal.transform.localToWorldMatrix, decal.m_Material, 0, 0, decal.GetPropertyBlock());
int decalIndex = resultIndices[resultIndex];
// If no decal material assigned, don't draw it
if (m_Decals[decalIndex].m_Material == null)
continue;
// don't draw decals that do not have textures assigned
if (!m_Decals[decalIndex].m_Material.GetTexture("_BaseColorMap") && !m_Decals[decalIndex].m_Material.GetTexture("_NormalMap") &&
!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());
m_CullingGroup.Dispose();
m_CullingGroup = null;
}
}
}

8
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
}
#endif
// decal system needs to be updated with current camera
DecalSystem.instance.Cull(camera);
using (new ProfilingSample(cmd, "CullResults.Cull", GetSampler(CustomSamplerId.CullResultsCull)))
{

RenderVelocity(m_CullResults, hdCamera, renderContext, cmd);
RenderDBuffer(hdCamera.cameraPos, renderContext, cmd);
RenderDBuffer(hdCamera, renderContext, cmd);
RenderGBuffer(m_CullResults, hdCamera, renderContext, cmd);

}
}
void RenderDBuffer(Vector3 cameraPos, ScriptableRenderContext renderContext, CommandBuffer cmd)
void RenderDBuffer(HDCamera camera, ScriptableRenderContext renderContext, CommandBuffer cmd)
DecalSystem.instance.Render(renderContext, cameraPos, cmd);
DecalSystem.instance.Render(renderContext, camera, cmd);
}
}

正在加载...
取消
保存