-
-
Save shelllee/dd81627ab43c8d3a3fda5741dc5319ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Rendering; | |
using Unity.Mathematics; | |
[RequireComponent(typeof(Camera))] | |
public class Draw : MonoBehaviour | |
{ | |
[SerializeField] public Mesh Mesh; | |
[SerializeField] public Material Material; | |
private Camera mainCamera; | |
private Matrix4x4[] matrices; | |
private ComputeBuffer buffer; | |
unsafe void Start() | |
{ | |
matrices = new Matrix4x4[1023]; | |
for (int i = 0; i < matrices.Length; i++) | |
{ | |
matrices[i] = Matrix4x4.identity; | |
matrices[i].m03 = i * 0.1f; | |
matrices[i].m23 = 50f; | |
} | |
mainCamera = GetComponent<Camera>(); | |
buffer = new ComputeBuffer(1023, sizeof(float4), ComputeBufferType.Constant); | |
var cols = new float4[matrices.Length]; | |
for (int i = 0; i < cols.Length; i++) | |
{ | |
switch (i % 4) | |
{ | |
case 0: | |
cols[i] = new float4(0, 1, 0, 1); | |
break; | |
case 1: | |
cols[i] = new float4(1, 0, 0, 1); | |
break; | |
case 2: | |
cols[i] = new float4(1, 1, 0, 1); | |
break; | |
default: | |
cols[i] = new float4(0, 0, 1, 1); | |
break; | |
} | |
} | |
buffer.SetData(cols); | |
Material.SetConstantBuffer("UnityInstancing_Props", buffer, 0, matrices.Length * sizeof(float4)); | |
} | |
void OnDestroy() | |
{ | |
buffer.Release(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
for (int i = 0; i < matrices.Length; i++) | |
{ | |
matrices[i].m13 += Time.deltaTime * i * 0.1f; | |
} | |
Graphics.DrawMeshInstanced(Mesh, 0, Material, matrices, matrices.Length, null, ShadowCastingMode.Off, false, 0, mainCamera, LightProbeUsage.Off, null); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax. | |
Shader "Unlit/ConstantUnlit" | |
{ | |
Properties | |
{ | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma multi_compile_instancing | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float4 col : COLOR0; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct v2f | |
{ | |
float4 col : COLOR0; | |
float4 vertex : SV_POSITION; | |
}; | |
#if defined(UNITY_INSTANCING_ENABLED) | |
#if defined(SHADER_API_GLES3) | |
cbuffer UnityInstancing_Props{ | |
#else | |
CBUFFER_START(UnityInstancing_Props) | |
#endif | |
struct { | |
float4 _Color; | |
} PropsArray[UNITY_INSTANCED_ARRAY_SIZE]; | |
#if defined(SHADER_API_GLES3) | |
} | |
#else | |
CBUFFER_END | |
#endif | |
#endif | |
v2f vert (appdata v) | |
{ | |
UNITY_SETUP_INSTANCE_ID(v); | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
#if defined(UNITY_INSTANCING_ENABLED) | |
#if defined(SHADER_API_GLES3) | |
o.col = float4(1, 0, 0, 1); | |
#else | |
o.col = PropsArray[unity_InstanceID]._Color; | |
#endif | |
#else | |
o.col = float4(1, 1, 1, 1); | |
#endif | |
return o; | |
} | |
float4 frag (v2f i) : SV_Target | |
{ | |
return i.col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment