Color Gradient

This commit is contained in:
_Redstone_c_ 2021-02-01 08:54:15 +08:00
parent 4d861c50dd
commit 9e6b1592ef
3 changed files with 36 additions and 2 deletions

View File

@ -4,7 +4,7 @@
"typeInfo": { "typeInfo": {
"fullName": "UnityEditor.ShaderGraph.Internal.Vector3ShaderProperty" "fullName": "UnityEditor.ShaderGraph.Internal.Vector3ShaderProperty"
}, },
"JSONnodeData": "{\n \"m_Guid\": {\n \"m_GuidSerialized\": \"1a852ecf-3bd7-467e-a62a-9b13b2667e2f\"\n },\n \"m_Name\": \"Albedo\",\n \"m_DefaultReferenceName\": \"Vector3_32FB817D\",\n \"m_OverrideReferenceName\": \"\",\n \"m_GeneratePropertyBlock\": true,\n \"m_Precision\": 0,\n \"m_GPUInstanced\": false,\n \"m_Hidden\": false,\n \"m_Value\": {\n \"x\": 0.0,\n \"y\": 0.0,\n \"z\": 0.0,\n \"w\": 0.0\n }\n}" "JSONnodeData": "{\n \"m_Guid\": {\n \"m_GuidSerialized\": \"1a852ecf-3bd7-467e-a62a-9b13b2667e2f\"\n },\n \"m_Name\": \"Albedo\",\n \"m_DefaultReferenceName\": \"Vector3_32FB817D\",\n \"m_OverrideReferenceName\": \"_Color\",\n \"m_GeneratePropertyBlock\": true,\n \"m_Precision\": 0,\n \"m_GPUInstanced\": false,\n \"m_Hidden\": false,\n \"m_Value\": {\n \"x\": 0.0,\n \"y\": 0.0,\n \"z\": 0.0,\n \"w\": 0.0\n }\n}"
}, },
{ {
"typeInfo": { "typeInfo": {

View File

@ -153,6 +153,35 @@ MonoBehaviour:
depth: 8 depth: 8
mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
material: {fileID: 2100000, guid: 8f3914ac4291e664ba60ae208ae66460, type: 2} material: {fileID: 2100000, guid: 8f3914ac4291e664ba60ae208ae66460, type: 2}
gradient:
serializedVersion: 2
key0: {r: 1, g: 1, b: 1, a: 1}
key1: {r: 1, g: 0, b: 0, a: 1}
key2: {r: 0, g: 0, b: 0, a: 1}
key3: {r: 0, g: 0, b: 0, a: 0}
key4: {r: 0, g: 0, b: 0, a: 0}
key5: {r: 0, g: 0, b: 0, a: 0}
key6: {r: 0, g: 0, b: 0, a: 0}
key7: {r: 0, g: 0, b: 0, a: 0}
ctime0: 0
ctime1: 39514
ctime2: 65535
ctime3: 0
ctime4: 0
ctime5: 0
ctime6: 0
ctime7: 0
atime0: 0
atime1: 65535
atime2: 65535
atime3: 0
atime4: 0
atime5: 0
atime6: 0
atime7: 0
m_Mode: 0
m_NumColorKeys: 3
m_NumAlphaKeys: 2
--- !u!4 &197491476 --- !u!4 &197491476
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -52,7 +52,7 @@ public class Fractal : MonoBehaviour
NativeArray<float3x4>[] matrices; NativeArray<float3x4>[] matrices;
[SerializeField, Range(1, 8)] [SerializeField, Range(2, 8)]
int depth = 4; int depth = 4;
[SerializeField] [SerializeField]
@ -61,6 +61,9 @@ public class Fractal : MonoBehaviour
[SerializeField] [SerializeField]
Material material = default; Material material = default;
[SerializeField]
Gradient gradient = default;
static float3[] directions = static float3[] directions =
{ {
up(), right(), left(), forward(), back() up(), right(), left(), forward(), back()
@ -84,6 +87,7 @@ public class Fractal : MonoBehaviour
ComputeBuffer[] matricesBuffers; ComputeBuffer[] matricesBuffers;
static readonly int colorId = Shader.PropertyToID("_Color");
static readonly int matricesId = Shader.PropertyToID("_Matrices"); static readonly int matricesId = Shader.PropertyToID("_Matrices");
static MaterialPropertyBlock propertyBlock; static MaterialPropertyBlock propertyBlock;
@ -177,6 +181,7 @@ public class Fractal : MonoBehaviour
{ {
ComputeBuffer buffer = matricesBuffers[i]; ComputeBuffer buffer = matricesBuffers[i];
buffer.SetData(matrices[i]); buffer.SetData(matrices[i]);
propertyBlock.SetColor(colorId, gradient.Evaluate(i / (matricesBuffers.Length - 1f)));
propertyBlock.SetBuffer(matricesId, buffer); propertyBlock.SetBuffer(matricesId, buffer);
Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock); Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock);
} }