Arbitrary Colors

This commit is contained in:
_Redstone_c_ 2021-02-01 09:25:22 +08:00
parent 9e6b1592ef
commit dd11842558
4 changed files with 82 additions and 34 deletions

File diff suppressed because one or more lines are too long

View File

@ -153,18 +153,18 @@ MonoBehaviour:
depth: 8
mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
material: {fileID: 2100000, guid: 8f3914ac4291e664ba60ae208ae66460, type: 2}
gradient:
gradientA:
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}
key2: {r: 0, g: 0, b: 0, a: 0}
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
ctime1: 32768
ctime2: 65535
ctime3: 0
ctime4: 0
@ -173,7 +173,36 @@ MonoBehaviour:
ctime7: 0
atime0: 0
atime1: 65535
atime2: 65535
atime2: 0
atime3: 0
atime4: 0
atime5: 0
atime6: 0
atime7: 0
m_Mode: 0
m_NumColorKeys: 3
m_NumAlphaKeys: 2
gradientB:
serializedVersion: 2
key0: {r: 0, g: 0, b: 1, a: 1}
key1: {r: 1, g: 1, b: 1, a: 1}
key2: {r: 1, g: 1, b: 0, a: 0}
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: 32768
ctime2: 65535
ctime3: 0
ctime4: 0
ctime5: 0
ctime6: 0
ctime7: 0
atime0: 0
atime1: 65535
atime2: 0
atime3: 0
atime4: 0
atime5: 0

View File

@ -3,6 +3,7 @@ using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using Random = UnityEngine.Random;
using static Unity.Mathematics.math;
using quaternion = Unity.Mathematics.quaternion;
@ -62,7 +63,10 @@ public class Fractal : MonoBehaviour
Material material = default;
[SerializeField]
Gradient gradient = default;
Gradient gradientA = default;
[SerializeField]
Gradient gradientB = default;
static float3[] directions =
{
@ -87,8 +91,12 @@ public class Fractal : MonoBehaviour
ComputeBuffer[] matricesBuffers;
static readonly int colorId = Shader.PropertyToID("_Color");
Vector4[] sequenceNumbers;
static readonly int colorAId = Shader.PropertyToID("_ColorA");
static readonly int colorBId = Shader.PropertyToID("_ColorB");
static readonly int matricesId = Shader.PropertyToID("_Matrices");
static readonly int sequenceNumbersId = Shader.PropertyToID("_SequenceNumbers");
static MaterialPropertyBlock propertyBlock;
@ -97,12 +105,14 @@ public class Fractal : MonoBehaviour
parts = new NativeArray<FractalPart>[depth];
matrices = new NativeArray<float3x4>[depth];
matricesBuffers = new ComputeBuffer[depth];
sequenceNumbers = new Vector4[depth];
int stride = 12 * 4;
for (int i = 0, length = 1; i < parts.Length; i++, length *= 5)
{
parts[i] = new NativeArray<FractalPart>(length, Allocator.Persistent);
matrices[i] = new NativeArray<float3x4>(length, Allocator.Persistent);
matricesBuffers[i] = new ComputeBuffer(length, stride);
sequenceNumbers[i] = new Vector4(Random.value, Random.value);
}
parts[0][0] = CreatePart(0);
@ -135,6 +145,7 @@ public class Fractal : MonoBehaviour
parts = null;
matrices = null;
matricesBuffers = null;
sequenceNumbers = null;
}
void OnValidate()
@ -181,8 +192,11 @@ public class Fractal : MonoBehaviour
{
ComputeBuffer buffer = matricesBuffers[i];
buffer.SetData(matrices[i]);
propertyBlock.SetColor(colorId, gradient.Evaluate(i / (matricesBuffers.Length - 1f)));
float gradientInterpolator = i / (matricesBuffers.Length - 1f);
propertyBlock.SetColor(colorAId, gradientA.Evaluate(gradientInterpolator));
propertyBlock.SetColor(colorBId, gradientB.Evaluate(gradientInterpolator));
propertyBlock.SetBuffer(matricesId, buffer);
propertyBlock.SetVector(sequenceNumbersId, sequenceNumbers[i]);
Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock);
}
}

View File

@ -2,6 +2,21 @@
StructuredBuffer<float3x4> _Matrices;
#endif
float4 _ColorA, _ColorB;
float2 _SequenceNumbers;
float4 GetFractalColor () {
#if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
return lerp(
_ColorA, _ColorB,
frac(unity_InstanceID * _SequenceNumbers.x + _SequenceNumbers.y)
);
#else
return _ColorA;
#endif
}
void ConfigureProcedural () {
#if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
float3x4 m = _Matrices[unity_InstanceID];
@ -12,10 +27,12 @@ void ConfigureProcedural () {
#endif
}
void ShaderGraphFunction_float (float3 In, out float3 Out) {
void ShaderGraphFunction_float (float3 In, out float3 Out, out float4 FractalColor) {
Out = In;
FractalColor = GetFractalColor();
}
void ShaderGraphFunction_half (half3 In, out half3 Out) {
void ShaderGraphFunction_half (half3 In, out half3 Out, out half4 FractalColor) {
Out = In;
FractalColor = GetFractalColor();
}