This commit is contained in:
_Redstone_c_ 2021-02-01 11:10:33 +08:00
parent dd11842558
commit 7ee4755a6a
4 changed files with 61 additions and 27 deletions

File diff suppressed because one or more lines are too long

View File

@ -152,11 +152,12 @@ MonoBehaviour:
m_EditorClassIdentifier:
depth: 8
mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
leafMesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
material: {fileID: 2100000, guid: 8f3914ac4291e664ba60ae208ae66460, type: 2}
gradientA:
serializedVersion: 2
key0: {r: 1, g: 1, b: 1, a: 1}
key1: {r: 1, g: 0, b: 0, a: 1}
key0: {r: 1, g: 1, b: 1, a: 0.3137255}
key1: {r: 1, g: 0, b: 0, a: 0.3529412}
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}
@ -184,8 +185,8 @@ MonoBehaviour:
m_NumAlphaKeys: 2
gradientB:
serializedVersion: 2
key0: {r: 0, g: 0, b: 1, a: 1}
key1: {r: 1, g: 1, b: 1, a: 1}
key0: {r: 0, g: 0, b: 1, a: 0.54901963}
key1: {r: 1, g: 1, b: 1, a: 0.627451}
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}
@ -211,6 +212,8 @@ MonoBehaviour:
m_Mode: 0
m_NumColorKeys: 3
m_NumAlphaKeys: 2
leafColorA: {r: 0.45882356, g: 0.97647065, b: 0.29803923, a: 0.49803922}
leafColorB: {r: 0.21568629, g: 0.4901961, b: 0.13333334, a: 0.9019608}
--- !u!4 &197491476
Transform:
m_ObjectHideFlags: 0

View File

@ -53,12 +53,15 @@ public class Fractal : MonoBehaviour
NativeArray<float3x4>[] matrices;
[SerializeField, Range(2, 8)]
[SerializeField, Range(3, 8)]
int depth = 4;
[SerializeField]
Mesh mesh = default;
[SerializeField]
Mesh leafMesh = default;
[SerializeField]
Material material = default;
@ -68,6 +71,12 @@ public class Fractal : MonoBehaviour
[SerializeField]
Gradient gradientB = default;
[SerializeField]
Color leafColorA = default;
[SerializeField]
Color leafColorB = default;
static float3[] directions =
{
up(), right(), left(), forward(), back()
@ -112,7 +121,7 @@ public class Fractal : MonoBehaviour
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);
sequenceNumbers[i] = new Vector4(Random.value, Random.value, Random.value, Random.value);
}
parts[0][0] = CreatePart(0);
@ -188,8 +197,25 @@ public class Fractal : MonoBehaviour
jobHandle.Complete();
var bounds = new Bounds(rootPart.worldPosition, float3(3f * objectScale));
int leafIndex = matricesBuffers.Length - 1;
for (int i = 0; i < matricesBuffers.Length; i++)
{
Mesh instanceMesh;
Color colorA, colorB;
if (i == leafIndex)
{
colorA = leafColorA;
colorB = leafColorB;
instanceMesh = leafMesh;
}
else
{
colorA = gradientA.Evaluate(i / (matricesBuffers.Length - 2f));
colorB = gradientB.Evaluate(i / (matricesBuffers.Length - 2f));
instanceMesh = mesh;
}
propertyBlock.SetColor(colorAId, colorA);
propertyBlock.SetColor(colorBId, colorB);
ComputeBuffer buffer = matricesBuffers[i];
buffer.SetData(matrices[i]);
float gradientInterpolator = i / (matricesBuffers.Length - 1f);
@ -197,7 +223,7 @@ public class Fractal : MonoBehaviour
propertyBlock.SetColor(colorBId, gradientB.Evaluate(gradientInterpolator));
propertyBlock.SetBuffer(matricesId, buffer);
propertyBlock.SetVector(sequenceNumbersId, sequenceNumbers[i]);
Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock);
Graphics.DrawMeshInstancedProcedural(instanceMesh, 0, material, bounds, buffer.count, propertyBlock);
}
}
}

View File

@ -4,14 +4,20 @@
float4 _ColorA, _ColorB;
float2 _SequenceNumbers;
float4 _SequenceNumbers;
float4 GetFractalColor () {
#if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED)
return lerp(
_ColorA, _ColorB,
float4 color;
color.rgb = lerp(
_ColorA.rgb, _ColorB.rgb,
frac(unity_InstanceID * _SequenceNumbers.x + _SequenceNumbers.y)
);
color.a = lerp(
_ColorA.a, _ColorB.a,
frac(unity_InstanceID * _SequenceNumbers.z + _SequenceNumbers.w)
);
return color;
#else
return _ColorA;
#endif