Files
HCI_GazeLab/Assets/GazeLab/Scripts/Environment/MultilayerParallaxBackground.cs
T

202 lines
7.7 KiB
C#

using UnityEngine;
namespace GazeLab.Environment
{
public sealed class MultilayerParallaxBackground : MonoBehaviour
{
public Camera targetCamera;
[Range(40, 260)] public int starCount = 150;
[Range(4, 18)] public int nebulaCloudCount = 8;
public float slowLayerSpeed = 0.14f;
public float fastLayerSpeed = 0.72f;
private Transform[] starTransforms;
private float[] starSpeeds;
private float[] starBaseScales;
private Transform[] nebulaTransforms;
private float[] nebulaSpeeds;
private static Sprite sharedStarSprite;
private static Sprite sharedNebulaSprite;
private float worldScaleFactor = 1f;
private void Start()
{
if (targetCamera == null) targetCamera = Camera.main;
BuildBackgroundLayers();
}
public void SetWorldScaleFactor(float scaleFactor)
{
worldScaleFactor = Mathf.Clamp(scaleFactor, 0.35f, 1.25f);
if (starTransforms == null) return;
for (int i = 0; i < starTransforms.Length; i++)
{
if (starTransforms[i] != null)
{
starTransforms[i].localScale = Vector3.one * (starBaseScales[i] * Mathf.Lerp(1f, worldScaleFactor, 0.45f));
}
}
}
private void BuildBackgroundLayers()
{
if (targetCamera == null) return;
targetCamera.backgroundColor = new Color(0.025f, 0.035f, 0.075f, 1f);
if (sharedStarSprite == null) sharedStarSprite = CreateProceduralStarSprite();
if (sharedNebulaSprite == null) sharedNebulaSprite = CreateProceduralNebulaSprite();
GetBounds(out float minX, out float maxX, out float minY, out float maxY);
// 1. Deep Nebula Clouds Layer
nebulaTransforms = new Transform[nebulaCloudCount];
nebulaSpeeds = new float[nebulaCloudCount];
Color[] nebulaPalette = new Color[]
{
new Color(0.16f, 0.28f, 0.55f, 0.13f),
new Color(0.32f, 0.14f, 0.48f, 0.11f),
new Color(0.10f, 0.40f, 0.46f, 0.10f)
};
for (int i = 0; i < nebulaCloudCount; i++)
{
GameObject neb = new GameObject($"NebulaLayer_{i}");
neb.transform.SetParent(transform, false);
neb.transform.position = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 8f);
neb.transform.localScale = Vector3.one * Random.Range(3.2f, 6.5f);
var sr = neb.AddComponent<SpriteRenderer>();
sr.sprite = sharedNebulaSprite;
sr.sortingOrder = -90;
sr.color = nebulaPalette[i % nebulaPalette.Length];
nebulaTransforms[i] = neb.transform;
nebulaSpeeds[i] = Random.Range(0.05f, 0.12f);
}
// 2. Multi-Depth Parallax Stars
starTransforms = new Transform[starCount];
starSpeeds = new float[starCount];
starBaseScales = new float[starCount];
for (int i = 0; i < starCount; i++)
{
GameObject st = new GameObject($"Star_{i}");
st.transform.SetParent(transform, false);
st.transform.position = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 5f);
float depth = Random.value;
float scale = Mathf.Lerp(0.18f, 0.85f, depth * depth);
starBaseScales[i] = scale;
st.transform.localScale = Vector3.one * scale;
var sr = st.AddComponent<SpriteRenderer>();
sr.sprite = sharedStarSprite;
sr.sortingOrder = -80 + Mathf.RoundToInt(depth * 10f);
sr.color = new Color(
Mathf.Lerp(0.62f, 1.0f, depth),
Mathf.Lerp(0.76f, 0.96f, depth),
1.0f,
Mathf.Lerp(0.22f, 0.85f, depth));
starTransforms[i] = st.transform;
starSpeeds[i] = Mathf.Lerp(slowLayerSpeed, fastLayerSpeed, depth) * Random.Range(0.8f, 1.25f);
}
}
private void GetBounds(out float minX, out float maxX, out float minY, out float maxY)
{
float halfY = targetCamera != null ? targetCamera.orthographicSize + 0.8f : 6f;
float halfX = targetCamera != null ? halfY * targetCamera.aspect + 0.8f : 10f;
Vector3 c = targetCamera != null ? targetCamera.transform.position : Vector3.zero;
minX = c.x - halfX;
maxX = c.x + halfX;
minY = c.y - halfY;
maxY = c.y + halfY;
}
private void Update()
{
if (targetCamera == null || starTransforms == null) return;
GetBounds(out float minX, out float maxX, out float minY, out float maxY);
float dt = Time.deltaTime;
for (int i = 0; i < nebulaTransforms.Length; i++)
{
Transform t = nebulaTransforms[i];
if (t == null) continue;
Vector3 p = t.position;
p.y -= nebulaSpeeds[i] * dt;
if (p.y < minY - 2.5f)
{
p.y = maxY + 2.5f;
p.x = Random.Range(minX, maxX);
}
t.position = p;
}
for (int i = 0; i < starTransforms.Length; i++)
{
Transform t = starTransforms[i];
if (t == null) continue;
Vector3 p = t.position;
p.y -= starSpeeds[i] * dt;
if (p.y < minY)
{
p.y = maxY;
p.x = Random.Range(minX, maxX);
}
t.position = p;
}
}
private static Sprite CreateProceduralStarSprite()
{
const int size = 16;
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
{
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear
};
Vector2 c = new Vector2((size - 1) * 0.5f, (size - 1) * 0.5f);
float r = size * 0.5f;
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
float d = Vector2.Distance(new Vector2(x, y), c) / r;
float a = Mathf.Clamp01(1f - d);
tex.SetPixel(x, y, new Color(1f, 1f, 1f, a * a));
}
}
tex.Apply();
return Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), 64f);
}
private static Sprite CreateProceduralNebulaSprite()
{
const int size = 64;
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
{
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear
};
Vector2 c = new Vector2((size - 1) * 0.5f, (size - 1) * 0.5f);
float r = size * 0.5f;
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
float d = Vector2.Distance(new Vector2(x, y), c) / r;
float noise = Mathf.PerlinNoise(x * 0.09f + 1.3f, y * 0.09f + 2.7f);
float a = Mathf.Clamp01(1f - d) * noise;
tex.SetPixel(x, y, new Color(1f, 1f, 1f, a * a));
}
}
tex.Apply();
return Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), 32f);
}
}
}