-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using UnityEngine; | ||
using UnityEngine.Rendering; | ||
|
||
/// <summary> | ||
/// This demo shows the use of the procedural instancing features to render objects | ||
/// without need of any position buffer. The values are calculated direclty inside the | ||
/// shader. | ||
/// Shadowing is broken when more than one DrawMeshInstancedIndirect is made. | ||
/// The color buffer is used for debug only. | ||
/// </summary> | ||
public class InstancedIndirectShadowsIssue : MonoBehaviour | ||
{ | ||
public int gridDim = 1000; | ||
public int instanceCount = 0; | ||
public Material instanceMaterial; | ||
|
||
public ShadowCastingMode castShadows = ShadowCastingMode.Off; | ||
public bool receiveShadows = false; | ||
|
||
private ComputeBuffer colorBuffer; | ||
|
||
private uint[] args = new uint[5] { 0, 0, 0, 0, 0 }; | ||
private Material[] materials; | ||
|
||
public Mesh[] meshes; | ||
private ComputeBuffer[] argsBuffers; | ||
|
||
void Start() | ||
{ | ||
instanceCount = gridDim * gridDim; | ||
|
||
argsBuffers = new ComputeBuffer[meshes.Length]; | ||
for (int i = 0; i < meshes.Length; i++) | ||
{ | ||
argsBuffers[i] = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments); | ||
} | ||
|
||
materials = new Material[meshes.Length]; | ||
for (int i = 0; i < materials.Length; i++) | ||
{ | ||
materials[i] = new Material(instanceMaterial); | ||
} | ||
|
||
CreateBuffers(); | ||
} | ||
|
||
void Update() | ||
{ | ||
for (int i = 0; i < meshes.Length; i++) | ||
{ | ||
materials[i].SetFloat("_Dim", gridDim); | ||
materials[i].SetVector("_Pos", new Vector4(i * (gridDim + 5), 0, 0, 0)); | ||
materials[i].SetBuffer("colorBuffer", colorBuffer); | ||
|
||
Graphics.DrawMeshInstancedIndirect(meshes[i], 0, materials[i], meshes[i].bounds, argsBuffers[i], 0, null, castShadows, receiveShadows); | ||
} | ||
} | ||
|
||
void CreateBuffers() | ||
{ | ||
/// Colors - for debug only | ||
if (colorBuffer != null) | ||
colorBuffer.Release(); | ||
|
||
colorBuffer = new ComputeBuffer(instanceCount, 16); | ||
|
||
Vector4[] colors = new Vector4[instanceCount]; | ||
for (int i = 0; i < instanceCount; i++) | ||
colors[i] = Random.ColorHSV(); | ||
|
||
colorBuffer.SetData(colors); | ||
|
||
// avoid culling | ||
for (int i = 0; i < meshes.Length; i++) | ||
{ | ||
meshes[i].bounds = new Bounds(Vector3.zero, Vector3.one * 10000f); | ||
} | ||
|
||
// indirect args | ||
for (int i = 0; i < argsBuffers.Length; i++) | ||
{ | ||
args[0] = meshes[i].GetIndexCount(0); | ||
args[1] = (uint)instanceCount; | ||
argsBuffers[i].SetData(args); | ||
} | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
if (colorBuffer != null) | ||
colorBuffer.Release(); | ||
colorBuffer = null; | ||
|
||
for (int i = 0; i < argsBuffers.Length; i++) | ||
{ | ||
if(argsBuffers[i] != null) | ||
argsBuffers[i].Release(); | ||
} | ||
argsBuffers = null; | ||
} | ||
|
||
void OnGUI() | ||
{ | ||
GUI.Label(new Rect(265, 12, 200, 30), "Instance Count: " + instanceCount.ToString("N0")); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
Shader "Instanced/InstancedIndirectShadowsIssue" | ||
{ | ||
Properties{ | ||
_MainTex("Albedo (RGB)", 2D) = "white" {} | ||
_Glossiness("Smoothness", Range(0,1)) = 0.5 | ||
_Metallic("Metallic", Range(0,1)) = 0.0 | ||
} | ||
SubShader{ | ||
Tags{ "RenderType" = "Opaque" } | ||
LOD 200 | ||
|
||
CGPROGRAM | ||
// Physically based Standard lighting model | ||
#pragma surface surf Standard addshadow | ||
#pragma multi_compile_instancing | ||
#pragma instancing_options procedural:setup | ||
|
||
sampler2D _MainTex; | ||
|
||
struct Input { | ||
float2 uv_MainTex; | ||
}; | ||
|
||
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED | ||
StructuredBuffer<float4> colorBuffer; | ||
#endif | ||
float _Dim; | ||
float4 _Pos; | ||
|
||
float rand(in float2 uv) | ||
{ | ||
float2 noise = (frac(sin(dot(uv ,float2(12.9898,78.233)*2.0)) * 43758.5453)); | ||
return abs(noise.x + noise.y) * 0.5; | ||
} | ||
|
||
void rotate2D(inout float2 v, float r) | ||
{ | ||
float s, c; | ||
sincos(r, s, c); | ||
v = float2(v.x * c - v.y * s, v.x * s + v.y * c); | ||
} | ||
|
||
void setup() | ||
{ | ||
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED | ||
// this uv assumes the # of instances is _Dim * _Dim. | ||
// so we calculate the uv inside a grid of _Dim x _Dim elements. | ||
float2 uv = float2( floor(unity_InstanceID / _Dim) / _Dim, (unity_InstanceID % (int)_Dim) / _Dim); | ||
// in this case, _Dim can be replaced by the size in the world | ||
float4 position = _Pos + float4(uv.x * _Dim, 0, uv.y * _Dim, 1); | ||
float scale = position.w; | ||
|
||
//float rotation = scale * scale * _Time.y * 0.5f; | ||
//rotate2D(position.xz, rotation); | ||
|
||
unity_ObjectToWorld._11_21_31_41 = float4(scale, 0, 0, 0); | ||
unity_ObjectToWorld._12_22_32_42 = float4(0, scale, 0, 0); | ||
unity_ObjectToWorld._13_23_33_43 = float4(0, 0, scale, 0); | ||
unity_ObjectToWorld._14_24_34_44 = float4(position.xyz, 1); | ||
unity_WorldToObject = unity_ObjectToWorld; | ||
unity_WorldToObject._14_24_34 *= -1; | ||
unity_WorldToObject._11_22_33 = 1.0f / unity_WorldToObject._11_22_33; | ||
#endif | ||
} | ||
|
||
half _Glossiness; | ||
half _Metallic; | ||
|
||
void surf(Input IN, inout SurfaceOutputStandard o) | ||
{ | ||
float4 col = 1.0f; | ||
|
||
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED | ||
col = colorBuffer[unity_InstanceID]; | ||
#else | ||
col = float4(0, 0, 1, 1); | ||
#endif | ||
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * col; | ||
o.Albedo = c.rgb; | ||
o.Metallic = _Metallic; | ||
o.Smoothness = _Glossiness; | ||
o.Alpha = c.a; | ||
} | ||
ENDCG | ||
} | ||
FallBack "Diffuse" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
m_EditorVersion: 5.6.0b10 | ||
m_EditorVersion: 5.6.0b11 |