From 9e6b1592ef106340a8f6bbfa93c63e65bdd7d410 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 1 Feb 2021 08:54:15 +0800 Subject: [PATCH] Color Gradient --- Assets/Fractal/Fractal URP.shadergraph | 2 +- Assets/Scenes/SampleScene.unity | 29 ++++++++++++++++++++++++++ Assets/Script/Fractal.cs | 7 ++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Assets/Fractal/Fractal URP.shadergraph b/Assets/Fractal/Fractal URP.shadergraph index 952ba0d..953ee60 100644 --- a/Assets/Fractal/Fractal URP.shadergraph +++ b/Assets/Fractal/Fractal URP.shadergraph @@ -4,7 +4,7 @@ "typeInfo": { "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": { diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 2a71772..64cbada 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -153,6 +153,35 @@ MonoBehaviour: depth: 8 mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} 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 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Script/Fractal.cs b/Assets/Script/Fractal.cs index a26f3cd..0d00670 100644 --- a/Assets/Script/Fractal.cs +++ b/Assets/Script/Fractal.cs @@ -52,7 +52,7 @@ public class Fractal : MonoBehaviour NativeArray[] matrices; - [SerializeField, Range(1, 8)] + [SerializeField, Range(2, 8)] int depth = 4; [SerializeField] @@ -61,6 +61,9 @@ public class Fractal : MonoBehaviour [SerializeField] Material material = default; + [SerializeField] + Gradient gradient = default; + static float3[] directions = { up(), right(), left(), forward(), back() @@ -84,6 +87,7 @@ public class Fractal : MonoBehaviour ComputeBuffer[] matricesBuffers; + static readonly int colorId = Shader.PropertyToID("_Color"); static readonly int matricesId = Shader.PropertyToID("_Matrices"); static MaterialPropertyBlock propertyBlock; @@ -177,6 +181,7 @@ public class Fractal : MonoBehaviour { ComputeBuffer buffer = matricesBuffers[i]; buffer.SetData(matrices[i]); + propertyBlock.SetColor(colorId, gradient.Evaluate(i / (matricesBuffers.Length - 1f))); propertyBlock.SetBuffer(matricesId, buffer); Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock); }