From e987949600ba1801fbff9e8cf9cde9ade6f311f3 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 1 Feb 2021 11:25:34 +0800 Subject: [PATCH] Sagging --- Assets/Scenes/SampleScene.unity | 22 +++++++++++---------- Assets/Script/Fractal.cs | 35 +++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 39da8b6..5193a60 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -156,8 +156,8 @@ MonoBehaviour: material: {fileID: 2100000, guid: 8f3914ac4291e664ba60ae208ae66460, type: 2} gradientA: serializedVersion: 2 - key0: {r: 1, g: 1, b: 1, a: 0.3137255} - key1: {r: 1, g: 0, b: 0, a: 0.3529412} + key0: {r: 0.764151, g: 0.18413278, b: 0, a: 0.3137255} + key1: {r: 1, g: 1, 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} @@ -165,7 +165,7 @@ MonoBehaviour: key6: {r: 0, g: 0, b: 0, a: 0} key7: {r: 0, g: 0, b: 0, a: 0} ctime0: 0 - ctime1: 32768 + ctime1: 65535 ctime2: 65535 ctime3: 0 ctime4: 0 @@ -181,12 +181,12 @@ MonoBehaviour: atime6: 0 atime7: 0 m_Mode: 0 - m_NumColorKeys: 3 + m_NumColorKeys: 2 m_NumAlphaKeys: 2 gradientB: serializedVersion: 2 - key0: {r: 0, g: 0, b: 1, a: 0.54901963} - key1: {r: 1, g: 1, b: 1, a: 0.627451} + key0: {r: 0.40392157, g: 0.14109339, b: 0, a: 0.54901963} + key1: {r: 0, g: 0.6320754, b: 0, 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} @@ -194,7 +194,7 @@ MonoBehaviour: key6: {r: 0, g: 0, b: 0, a: 0} key7: {r: 0, g: 0, b: 0, a: 0} ctime0: 0 - ctime1: 32768 + ctime1: 65535 ctime2: 65535 ctime3: 0 ctime4: 0 @@ -210,10 +210,12 @@ MonoBehaviour: atime6: 0 atime7: 0 m_Mode: 0 - m_NumColorKeys: 3 + m_NumColorKeys: 2 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} + maxSagAngleA: 15 + maxSagAngleB: 25 --- !u!4 &197491476 Transform: m_ObjectHideFlags: 0 @@ -221,13 +223,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 197491472} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalRotation: {x: 0, y: 0, z: 0.17364816, w: 0.9848078} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 4 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 20} --- !u!1 &705507993 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Script/Fractal.cs b/Assets/Script/Fractal.cs index 3f9e11e..de7e753 100644 --- a/Assets/Script/Fractal.cs +++ b/Assets/Script/Fractal.cs @@ -29,12 +29,32 @@ public class Fractal : MonoBehaviour var parent = parents[i / 5]; var part = parts[i]; part.spinAngle += spinAngleDelta; - part.worldRotation = mul(parent.worldRotation, + + float3 upAxis = + mul(mul(parent.worldRotation, part.rotation), up()); + float3 sagAxis = cross(up(), upAxis); + + float sagMagnitude = length(sagAxis); + quaternion baseRotation; + if (sagMagnitude > 0f) + { + sagAxis /= sagMagnitude; + quaternion sagRotation = + quaternion.AxisAngle(sagAxis, part.maxSagAngle * sagMagnitude); + baseRotation = mul(sagRotation, parent.worldRotation); + } + else + { + baseRotation = parent.worldRotation; + } + + part.worldRotation = mul(baseRotation, mul(part.rotation, quaternion.RotateY(part.spinAngle)) ); + part.worldPosition = parent.worldPosition + - mul(parent.worldRotation, 1.5f * scale * part.direction); + mul(part.worldRotation, float3(0f, 1.5f * scale, 0f)); parts[i] = part; float3x3 r = float3x3(part.worldRotation) * scale; @@ -44,8 +64,9 @@ public class Fractal : MonoBehaviour private struct FractalPart { - public float3 direction, worldPosition; + public float3 worldPosition; public quaternion rotation, worldRotation; + public float maxSagAngle; public float spinAngle; } @@ -77,6 +98,12 @@ public class Fractal : MonoBehaviour [SerializeField] Color leafColorB = default; + [SerializeField, Range(0f, 90f)] + float maxSagAngleA = 15f; + + [SerializeField, Range(0f, 90f)] + float maxSagAngleB = 25f; + static float3[] directions = { up(), right(), left(), forward(), back() @@ -93,7 +120,7 @@ public class Fractal : MonoBehaviour { return new FractalPart() { - direction = directions[childIndex], + maxSagAngle = radians(Random.Range(maxSagAngleA, maxSagAngleB)), rotation = rotations[childIndex] }; }