Spin - Done!

This commit is contained in:
_Redstone_c_ 2021-02-01 11:33:00 +08:00
parent e987949600
commit d635eeadd2

View File

@ -13,8 +13,8 @@ public class Fractal : MonoBehaviour
[BurstCompile(FloatPrecision.Standard, FloatMode.Fast, CompileSynchronously = true)] [BurstCompile(FloatPrecision.Standard, FloatMode.Fast, CompileSynchronously = true)]
private struct UpdateFractalLevelJob : IJobFor private struct UpdateFractalLevelJob : IJobFor
{ {
public float spinAngleDelta;
public float scale; public float scale;
public float deltaTime;
[ReadOnly] [ReadOnly]
public NativeArray<FractalPart> parents; public NativeArray<FractalPart> parents;
@ -28,7 +28,8 @@ public class Fractal : MonoBehaviour
{ {
var parent = parents[i / 5]; var parent = parents[i / 5];
var part = parts[i]; var part = parts[i];
part.spinAngle += spinAngleDelta; part.spinAngle += part.spinVelocity * deltaTime;
float3 upAxis = float3 upAxis =
mul(mul(parent.worldRotation, part.rotation), up()); mul(mul(parent.worldRotation, part.rotation), up());
@ -66,8 +67,7 @@ public class Fractal : MonoBehaviour
{ {
public float3 worldPosition; public float3 worldPosition;
public quaternion rotation, worldRotation; public quaternion rotation, worldRotation;
public float maxSagAngle; public float maxSagAngle, spinAngle, spinVelocity;
public float spinAngle;
} }
NativeArray<FractalPart>[] parts; NativeArray<FractalPart>[] parts;
@ -78,31 +78,25 @@ public class Fractal : MonoBehaviour
int depth = 4; int depth = 4;
[SerializeField] [SerializeField]
Mesh mesh = default; Mesh mesh = default, leafMesh = default;
[SerializeField]
Mesh leafMesh = default;
[SerializeField] [SerializeField]
Material material = default; Material material = default;
[SerializeField] [SerializeField]
Gradient gradientA = default; Gradient gradientA = default, gradientB = default;
[SerializeField] [SerializeField]
Gradient gradientB = default; Color leafColorA = default, leafColorB = default;
[SerializeField]
Color leafColorA = default;
[SerializeField]
Color leafColorB = default;
[SerializeField, Range(0f, 90f)] [SerializeField, Range(0f, 90f)]
float maxSagAngleA = 15f; float maxSagAngleA = 15f, maxSagAngleB = 25f;
[SerializeField, Range(0f, 90f)] [SerializeField, Range(0f, 90f)]
float maxSagAngleB = 25f; float spinSpeedA = 20f, spinSpeedB = 25f;
[SerializeField, Range(0f, 1f)]
float reverseSpinChance = 0.25f;
static float3[] directions = static float3[] directions =
{ {
@ -121,7 +115,10 @@ public class Fractal : MonoBehaviour
return new FractalPart() return new FractalPart()
{ {
maxSagAngle = radians(Random.Range(maxSagAngleA, maxSagAngleB)), maxSagAngle = radians(Random.Range(maxSagAngleA, maxSagAngleB)),
rotation = rotations[childIndex] rotation = rotations[childIndex],
spinVelocity =
(Random.value < reverseSpinChance ? -1f : 1f) *
radians(Random.Range(spinSpeedA, spinSpeedB))
}; };
} }
@ -195,9 +192,9 @@ public class Fractal : MonoBehaviour
private void Update() private void Update()
{ {
float spinAngleDelta = 0.125f * PI * Time.deltaTime; float deltaTime = Time.deltaTime;
FractalPart rootPart = parts[0][0]; FractalPart rootPart = parts[0][0];
rootPart.spinAngle += spinAngleDelta; rootPart.spinAngle += rootPart.spinVelocity * deltaTime;
rootPart.worldRotation = mul(transform.rotation, rootPart.worldRotation = mul(transform.rotation,
mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle)) mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle))
); );
@ -214,7 +211,7 @@ public class Fractal : MonoBehaviour
scale *= 0.5f; scale *= 0.5f;
jobHandle = new UpdateFractalLevelJob jobHandle = new UpdateFractalLevelJob
{ {
spinAngleDelta = spinAngleDelta, deltaTime = deltaTime,
scale = scale, scale = scale,
parents = parts[li - 1], parents = parts[li - 1],
parts = parts[li], parts = parts[li],