Paul Melamed
7 年前
当前提交
0be6b369
共有 4 个文件被更改,包括 168 次插入 和 0 次删除
-
12ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
-
68ScriptableRenderPipeline/HDRenderPipeline/Decal/Decal.cs
-
88ScriptableRenderPipeline/HDRenderPipeline/Decal/DecalSystem.cs
|
|||
using UnityEditor; |
|||
using UnityEngine; |
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline.Decal |
|||
{ |
|||
[ExecuteInEditMode] |
|||
public class Decal : MonoBehaviour |
|||
{ |
|||
public enum Kind |
|||
{ |
|||
DiffuseOnly, |
|||
NormalsOnly, |
|||
Both |
|||
} |
|||
|
|||
public Kind m_Kind; |
|||
public Material m_Material; |
|||
|
|||
public void OnEnable() |
|||
{ |
|||
DecalSystem.instance.AddDecal(this); |
|||
} |
|||
|
|||
public void Start() |
|||
{ |
|||
DecalSystem.instance.AddDecal(this); |
|||
} |
|||
|
|||
public void OnDisable() |
|||
{ |
|||
DecalSystem.instance.RemoveDecal(this); |
|||
} |
|||
|
|||
private void DrawGizmo(bool selected) |
|||
{ |
|||
var col = new Color(0.0f, 0.7f, 1f, 1.0f); |
|||
col.a = selected ? 0.3f : 0.1f; |
|||
Gizmos.color = col; |
|||
Matrix4x4 offset = Matrix4x4.Translate(new Vector3(0.0f, -0.5f, 0.0f)); |
|||
Gizmos.matrix = transform.localToWorldMatrix * offset; |
|||
Gizmos.DrawCube(Vector3.zero, Vector3.one); |
|||
col.a = selected ? 0.5f : 0.2f; |
|||
Gizmos.color = col; |
|||
Gizmos.DrawWireCube(Vector3.zero, Vector3.one); |
|||
} |
|||
|
|||
public void OnDrawGizmos() |
|||
{ |
|||
//DrawGizmo(false);
|
|||
} |
|||
public void OnDrawGizmosSelected() |
|||
{ |
|||
DrawGizmo(true); |
|||
} |
|||
|
|||
[MenuItem("GameObject/Effects/Decal", false, 0)] |
|||
static void CreateDecal(MenuCommand menuCommand) |
|||
{ |
|||
// Create a custom game object
|
|||
GameObject go = new GameObject("Decal"); |
|||
go.AddComponent<Decal>(); |
|||
// 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
|
|||
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name); |
|||
Selection.activeObject = go; |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine.Experimental.Rendering; |
|||
using UnityEngine.Rendering; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline.Decal |
|||
{ |
|||
public class DecalSystem |
|||
{ |
|||
static DecalSystem m_Instance; |
|||
static public DecalSystem instance |
|||
{ |
|||
get |
|||
{ |
|||
if (m_Instance == null) |
|||
m_Instance = new DecalSystem(); |
|||
return m_Instance; |
|||
} |
|||
} |
|||
|
|||
internal HashSet<Decal> m_DecalsDiffuse = new HashSet<Decal>(); |
|||
internal HashSet<Decal> m_DecalsNormals = new HashSet<Decal>(); |
|||
internal HashSet<Decal> m_DecalsBoth = new HashSet<Decal>(); |
|||
Mesh m_CubeMesh; |
|||
|
|||
public DecalSystem() |
|||
{ |
|||
m_CubeMesh = new Mesh(); |
|||
|
|||
Vector3[] vertices = new Vector3[8]; |
|||
|
|||
vertices[0] = new Vector3(-0.5f, -0.5f, -0.5f); |
|||
vertices[1] = new Vector3(0.5f, -0.5f, -0.5f); |
|||
vertices[2] = new Vector3(0.5f, 0.5f, -0.5f); |
|||
vertices[3] = new Vector3(-0.5f, 0.5f, -0.5f); |
|||
vertices[4] = new Vector3(-0.5f, -0.5f, 0.5f); |
|||
vertices[5] = new Vector3(0.5f, -0.5f, 0.5f); |
|||
vertices[6] = new Vector3(0.5f, 0.5f, 0.5f); |
|||
vertices[7] = new Vector3(-0.5f, 0.5f, 0.5f); |
|||
|
|||
m_CubeMesh.vertices = vertices; |
|||
|
|||
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[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[24] = 3; triangles[25] = 2; triangles[26] = 6; |
|||
triangles[27] = 3; triangles[28] = 6; triangles[29] = 7; |
|||
triangles[30] = 4; triangles[31] = 5; triangles[32] = 1; |
|||
triangles[33] = 4; triangles[34] = 1; triangles[35] = 0; |
|||
|
|||
m_CubeMesh.triangles = triangles; |
|||
|
|||
} |
|||
|
|||
public void AddDecal(Decal d) |
|||
{ |
|||
RemoveDecal(d); |
|||
if (d.m_Kind == Decal.Kind.DiffuseOnly) |
|||
m_DecalsDiffuse.Add(d); |
|||
if (d.m_Kind == Decal.Kind.NormalsOnly) |
|||
m_DecalsNormals.Add(d); |
|||
if (d.m_Kind == Decal.Kind.Both) |
|||
m_DecalsBoth.Add(d); |
|||
} |
|||
public void RemoveDecal(Decal d) |
|||
{ |
|||
m_DecalsDiffuse.Remove(d); |
|||
m_DecalsNormals.Remove(d); |
|||
m_DecalsBoth.Remove(d); |
|||
} |
|||
|
|||
public void Render(ScriptableRenderContext renderContext, Camera camera, CommandBuffer cmd) |
|||
{ |
|||
foreach (var decal in m_DecalsDiffuse) |
|||
{ |
|||
Matrix4x4 offset = Matrix4x4.Translate(new Vector3(0.0f, -0.5f, 0.0f)); |
|||
//DrawMesh(Mesh mesh, Matrix4x4 matrix, Material material, int submeshIndex, int shaderPass);
|
|||
cmd.DrawMesh(m_CubeMesh, decal.transform.localToWorldMatrix * offset, decal.m_Material, 0, 0); |
|||
} |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue