This commit is contained in:
SoulliesOfficial
2026-06-05 04:45:57 -04:00
parent 3a63641a2c
commit 7c60c40d6b
377 changed files with 10970 additions and 843 deletions

View File

@@ -0,0 +1,430 @@
using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using Unity.Collections;
using Unity.Jobs;
using Unity.Burst;
using Random = UnityEngine.Random;
namespace Ichni.RhythmGame.ThemeBundles.DepartureToMultiverse
{
public partial class DTMConstellation : EnvironmentObject, IScheduledElement
{
[Header("Core References")]
[Tooltip("拖入用于生成节点的粒子系统")]
public ParticleSystem starParticleSystem;
[Tooltip("拖入用于渲染连线的 MeshFilter")]
public MeshFilter lineMeshFilter;
[SerializeField]
private Camera mainCam;
[Header("Constellation Settings (星座设置)")]
[Tooltip("这个星座包含的星星总数")]
public int maxParticles = 10;
[Tooltip("整个星座允许产生的最大连线数量")]
public int maxLineCount = 12;
// 【修改】:使用 Vector3 代替单轴 Radius控制长方体生成体积
[Tooltip("星座的散布体积 (长宽高)")]
public Vector3 spreadSize = new Vector3(20f, 20f, 20f);
[Tooltip("单颗星星的最大连线数 (建议设置在 2 到 4 之间)")]
public int maxConnectionsPerStar = 3;
[Header("Visual Settings (视觉设置)")]
public float maxConnectionDistance = 20f;
public float activeStarSize = 1f;
public float lineWidth = 0.1f;
[Header("Particle Motion Settings (粒子运动设置)")]
[Tooltip("星系整体的轨道旋转速度 (Velocity over Lifetime -> Orbital)")]
public Vector3 orbitalVelocity = Vector3.one * 0.1f;
[Tooltip("星星自身的旋转角速度 (Rotation over Lifetime -> Angular Velocity)")]
public float angularVelocity = 60f;
// 【新增】:连线 Mesh 的渲染器,用于同步 EmissionColor 到材质
public Renderer lineRenderer;
private ParticleSystem.Particle[] particles;
private Mesh lineMesh;
private bool hasInitializedSpawning = false;
public static DTMConstellation GenerateElement(
string elementName, Guid id, List<string> tags,
bool isFirstGenerated, string themeBundleName, string objectName,
GameElement parentElement, bool isStatic,
int maxParticles, int maxLineCount,
Vector3 spreadSize, int maxConnectionsPerStar,
float maxConnectionDistance, float activeStarSize, float lineWidth,
Vector3 orbitalVelocity, float angularVelocity)
{
DTMConstellation constellation = EnvironmentObject.GenerateElement(
elementName, id, tags, isFirstGenerated,
themeBundleName, objectName, parentElement, isStatic)
.GetComponent<DTMConstellation>();
constellation.maxParticles = maxParticles;
constellation.maxLineCount = maxLineCount;
constellation.spreadSize = spreadSize;
constellation.maxConnectionsPerStar = maxConnectionsPerStar;
constellation.maxConnectionDistance = maxConnectionDistance;
constellation.activeStarSize = activeStarSize;
constellation.lineWidth = lineWidth;
constellation.orbitalVelocity = orbitalVelocity;
constellation.angularVelocity = angularVelocity;
return constellation;
}
public override void FirstSetUpObject(bool isFirstGenerated)
{
// 粒子数组和 Mesh 在此初始化Prefab 实例化后立即运行)
mainCam = GameManager.Instance.cameraManager.gameCamera.cam ?? Camera.main;
particles = new ParticleSystem.Particle[maxParticles];
lineMesh = new Mesh();
lineMesh.name = "Constellation_Lines_Mesh";
lineMesh.MarkDynamic();
if (lineMeshFilter != null) lineMeshFilter.sharedMesh = lineMesh;
var psRenderer = starParticleSystem?.GetComponent<ParticleSystemRenderer>();
if (psRenderer != null) psRenderer.enabled = true;
if (starParticleSystem != null)
{
var emission = starParticleSystem.emission;
emission.enabled = false;
}
// 实例化连线材质,确保 EmissionColor 修改不污染共享资源
if (lineRenderer != null)
lineRenderer.InitializeShader();
}
public override void AfterInitialize()
{
base.AfterInitialize();
ApplyColorSubmodule();
CoreServices.UpdateScheduler.Register(UpdatePhase.Effect, this);
}
public override void OnDelete()
{
base.OnDelete();
CoreServices.UpdateScheduler.Unregister(UpdatePhase.Effect, this);
}
public override void Refresh()
{
base.Refresh();
ApplyColorSubmodule();
}
public override void OnDirtyRefresh(Dictionary<string, bool> flags)
{
ApplyColorSubmodule();
}
/// <summary>将 colorSubmodule 的当前颜色同步到粒子和线条材质</summary>
private void ApplyColorSubmodule()
{
if (colorSubmodule == null) return;
// BaseColor → 粒子 startColor重新发射时使用或逐帧设置已存粒子
if (particles != null)
{
int aliveCount = starParticleSystem != null
? starParticleSystem.GetParticles(particles)
: 0;
Color32 c32 = colorSubmodule.currentBaseColor;
for (int i = 0; i < aliveCount; i++)
particles[i].startColor = c32;
if (aliveCount > 0 && starParticleSystem != null)
starParticleSystem.SetParticles(particles, aliveCount);
}
// EmissionColor → 连线材质
if (lineRenderer != null && lineRenderer.material != null)
{
starParticleSystem.GetComponent<ParticleSystemRenderer>().material.SetColor("_EmissionColor", colorSubmodule.GetCurrentEmissionColor());
lineRenderer.material.SetColor("_EmissionColor", colorSubmodule.GetCurrentEmissionColor());
Debug.Log($"Applied EmissionColor {colorSubmodule.GetCurrentEmissionColor()} to line material of {objectName}");
}
}
void Update()
{
// 保留以供 Unity Editor 中 [Button] 的调用;运行时由调度器驱动
}
#region [IScheduledElement] Scheduler Interface
public void ScheduledUpdate(UpdatePhase phase, float songTime)
{
// 一次性初始化(等价于原 Update() 的首次执行)
if (!hasInitializedSpawning)
{
GenerateSingleConstellation();
hasInitializedSpawning = true;
}
// 每帧 Burst Job 连线 Mesh 重建(原 LateUpdate 逻辑)
BuildConstellationMesh();
}
public bool IsScheduledActive => isActiveAndEnabled;
#endregion
[Button("Refresh Constellation")]
public void GenerateSingleConstellation()
{
starParticleSystem.Stop();
starParticleSystem.Clear();
// --- 【新增】:通过代码接管并设置 Velocity over Lifetime 模块 ---
// 注意:如果轨道旋转不为零,则强制开启该模块
var vol = starParticleSystem.velocityOverLifetime;
if (orbitalVelocity != Vector3.zero)
{
vol.enabled = true;
vol.orbitalX = new ParticleSystem.MinMaxCurve(orbitalVelocity.x);
vol.orbitalY = new ParticleSystem.MinMaxCurve(orbitalVelocity.y);
vol.orbitalZ = new ParticleSystem.MinMaxCurve(orbitalVelocity.z);
}
else
{
vol.enabled = false;
}
// --- 【新增】:通过代码接管并设置 Rotation over Lifetime 模块 ---
var rol = starParticleSystem.rotationOverLifetime;
if (angularVelocity != 0f)
{
rol.enabled = true;
// 对于普通的 Billboard 粒子Z 轴旋转就是屏幕空间上的二维自转
rol.z = new ParticleSystem.MinMaxCurve(angularVelocity * Mathf.Deg2Rad); // 转换为弧度
}
else
{
rol.enabled = false;
}
ParticleSystem.EmitParams emitParams = new ParticleSystem.EmitParams();
for (int i = 0; i < maxParticles; i++)
{
float x = Random.Range(-spreadSize.x * 0.5f, spreadSize.x * 0.5f);
float y = Random.Range(-spreadSize.y * 0.5f, spreadSize.y * 0.5f);
float z = Random.Range(-spreadSize.z * 0.5f, spreadSize.z * 0.5f);
emitParams.position = new Vector3(x, y, z);
// startColor 使用 colorSubmodule 的当前 BaseColor单色
emitParams.startColor = colorSubmodule != null
? (Color32)colorSubmodule.currentBaseColor
: new Color32(0, 255, 255, 255);
// 为了让自身旋转可见,可以在生成时赋予一个随机的初始旋转角度
emitParams.rotation3D = new Vector3(0, 0, Random.Range(0f, 360f));
starParticleSystem.Emit(emitParams, 1);
}
starParticleSystem.Play();
}
void LateUpdate()
{
// 已迁移到调度器 Phase 7 (Effect) 的 ScheduledUpdate → BuildConstellationMesh()
}
private void BuildConstellationMesh()
{
if (starParticleSystem == null || lineMeshFilter == null) return;
int aliveCount = starParticleSystem.GetParticles(particles);
if (aliveCount < 2)
{
lineMesh.Clear();
return;
}
int processCount = Mathf.Min(aliveCount, maxParticles);
NativeArray<Vector3> positions = new NativeArray<Vector3>(processCount, Allocator.TempJob);
NativeArray<Color32> starColors = new NativeArray<Color32>(processCount, Allocator.TempJob);
NativeArray<int> connectionCounts = new NativeArray<int>(processCount, Allocator.TempJob);
NativeArray<bool> adjacencyMatrix = new NativeArray<bool>(processCount * processCount, Allocator.TempJob);
for (int i = 0; i < processCount; i++)
{
positions[i] = particles[i].position;
starColors[i] = particles[i].GetCurrentColor(starParticleSystem);
}
NativeList<Vector3> lineVertices = new NativeList<Vector3>(maxLineCount * 4, Allocator.TempJob);
NativeList<Color32> lineColors = new NativeList<Color32>(maxLineCount * 4, Allocator.TempJob);
NativeList<int> lineIndices = new NativeList<int>(maxLineCount * 6, Allocator.TempJob);
ConstellationJob job = new ConstellationJob
{
positions = positions,
colors = starColors,
sqrMaxDistance = maxConnectionDistance * maxConnectionDistance,
lineWidth = lineWidth,
maxLineCount = maxLineCount,
maxConnectionsPerStar = maxConnectionsPerStar,
cameraForward = mainCam.transform.forward,
lineVertices = lineVertices,
lineColors = lineColors,
lineIndices = lineIndices,
connectionCounts = connectionCounts,
adjacencyMatrix = adjacencyMatrix
};
JobHandle handle = job.Schedule();
handle.Complete();
for (int i = 0; i < processCount; i++)
{
particles[i].startSize = connectionCounts[i] > 0 ? activeStarSize : 0f;
}
starParticleSystem.SetParticles(particles, aliveCount);
lineMesh.Clear();
lineMesh.SetVertices(lineVertices.AsArray());
lineMesh.SetColors(lineColors.AsArray());
lineMesh.SetIndices(lineIndices.AsArray(), MeshTopology.Triangles, 0);
lineMesh.RecalculateBounds();
positions.Dispose();
starColors.Dispose();
lineVertices.Dispose();
lineColors.Dispose();
lineIndices.Dispose();
connectionCounts.Dispose();
adjacencyMatrix.Dispose();
}
}
[BurstCompile(CompileSynchronously = true)]
public struct ConstellationJob : IJob
{
public NativeArray<Vector3> positions;
public NativeArray<Color32> colors;
public float sqrMaxDistance;
public float lineWidth;
public int maxLineCount;
public int maxConnectionsPerStar;
public Vector3 cameraForward;
public NativeList<Vector3> lineVertices;
public NativeList<Color32> lineColors;
public NativeList<int> lineIndices;
public NativeArray<int> connectionCounts;
public NativeArray<bool> adjacencyMatrix;
public void Execute()
{
int count = positions.Length;
int currentLineCount = 0;
int vertexOffset = 0;
for (int i = 0; i < count; i++)
{
if (currentLineCount >= maxLineCount) break;
if (connectionCounts[i] >= maxConnectionsPerStar) continue;
for (int j = i + 1; j < count; j++)
{
if (currentLineCount >= maxLineCount) break;
if (connectionCounts[j] >= maxConnectionsPerStar) continue;
if (connectionCounts[i] == 0 || connectionCounts[j] == 0)
{
float distSq = (positions[i] - positions[j]).sqrMagnitude;
if (distSq < sqrMaxDistance)
{
AddLine(i, j, ref vertexOffset, count);
currentLineCount++;
if (connectionCounts[i] >= maxConnectionsPerStar)
{
break;
}
}
}
}
}
for (int i = 0; i < count; i++)
{
if (currentLineCount >= maxLineCount) break;
if (connectionCounts[i] >= maxConnectionsPerStar) continue;
for (int j = i + 1; j < count; j++)
{
if (currentLineCount >= maxLineCount) break;
if (connectionCounts[j] >= maxConnectionsPerStar) continue;
if (adjacencyMatrix[i * count + j]) continue;
float distSq = (positions[i] - positions[j]).sqrMagnitude;
if (distSq < sqrMaxDistance)
{
AddLine(i, j, ref vertexOffset, count);
currentLineCount++;
if (connectionCounts[i] >= maxConnectionsPerStar)
{
break;
}
}
}
}
}
private void AddLine(int i, int j, ref int vertexOffset, int count)
{
Vector3 posA = positions[i];
Vector3 posB = positions[j];
Vector3 direction = posB - posA;
float dirLength = direction.magnitude;
if (dirLength < 0.0001f) return;
direction /= dirLength;
Vector3 perpDir = Vector3.Cross(direction, cameraForward);
if (perpDir.sqrMagnitude < 0.00001f) return;
Vector3 perp = perpDir.normalized * (lineWidth * 0.5f);
lineVertices.Add(posA + perp);
lineVertices.Add(posA - perp);
lineVertices.Add(posB - perp);
lineVertices.Add(posB + perp);
lineColors.Add(colors[i]);
lineColors.Add(colors[i]);
lineColors.Add(colors[j]);
lineColors.Add(colors[j]);
lineIndices.Add(vertexOffset + 0);
lineIndices.Add(vertexOffset + 1);
lineIndices.Add(vertexOffset + 2);
lineIndices.Add(vertexOffset + 0);
lineIndices.Add(vertexOffset + 2);
lineIndices.Add(vertexOffset + 3);
vertexOffset += 4;
connectionCounts[i]++;
connectionCounts[j]++;
adjacencyMatrix[i * count + j] = true;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8ba3055fc57abcb41811796e6a0ed3aa

View File

@@ -7,7 +7,7 @@ using UnityEngine;
namespace Ichni.RhythmGame.ThemeBundles.DepartureToMultiverse
{
public partial class DTMTrail : EnvironmentObject, IHaveTrail, IHaveInteraction
public partial class DTMTrail : EnvironmentObject, IHaveTrail, IHaveInteraction, IScheduledElement
{
#region [] Exposed Fields
public GameObject headPoint, headCircle, sparks;
@@ -76,14 +76,14 @@ namespace Ichni.RhythmGame.ThemeBundles.DepartureToMultiverse
{
trailRenderer = trailBody.GetComponent<TrailRenderer>();
trailRenderer.emitting = false;
if (visibleTimeLength.animations.Count == 0) trailRenderer.time = 5f;
// 初始化默认值(兼容旧存档中未序列化字段)
if (widthCurve == null || widthCurve.keys.Length == 0) widthCurve = DefaultWidthCurve();
if (trailAlphaGradient == null) trailAlphaGradient = DefaultTrailGradient();
trailRenderer.widthCurve = widthCurve;
// 收集所有使用 BlendUnlit 的 Renderer
renderers.Clear();
CollectBlendUnlitRenderer(headPoint);
@@ -96,20 +96,36 @@ namespace Ichni.RhythmGame.ThemeBundles.DepartureToMultiverse
{
//rend.InitializeShader();
}
sparks.gameObject.SetActive(false);
headPoint.transform.localScale = Vector3.zero;
headCircle.transform.localScale = Vector3.zero;
}
public override void AfterInitialize()
{
base.AfterInitialize();
CoreServices.UpdateScheduler.Register(UpdatePhase.Effect, this);
}
public override void OnDelete()
{
base.OnDelete();
CoreServices.UpdateScheduler.Unregister(UpdatePhase.Effect, this);
}
public override void WhenStart()
{
base.WhenStart();
trailRenderer.emitting = true;
trailRenderer.Clear();
headPoint.gameObject.SetActive(false);
headCircle.gameObject.SetActive(false);
enableTimes.UpdateFlexibleBool(0);
if (!enableTimes.value)
{
headPoint.gameObject.SetActive(false);
headCircle.gameObject.SetActive(false);
}
}
/// <summary>
@@ -218,9 +234,9 @@ namespace Ichni.RhythmGame.ThemeBundles.DepartureToMultiverse
#endregion
#region [] Event Animation Logic
private void Update()
#region [IScheduledElement] Scheduler Interface
public void ScheduledUpdate(UpdatePhase phase, float songTime)
{
float songTime = CoreServices.TimeProvider.SongTime;
enableTimes.UpdateFlexibleBool(songTime);
if (enableTimes.value && !isHeadEnabled)
{
@@ -245,6 +261,9 @@ namespace Ichni.RhythmGame.ThemeBundles.DepartureToMultiverse
// headRotateSpeed 控制逻辑留待后续实现
}
}
public bool IsScheduledActive => isActiveAndEnabled;
#endregion
private Sequence enableHeadSequence;
private Sequence disableHeadSequence;