A 2 m pyramid of dark steel, carved with Valheim's runes, hovers 0.5 m above the ground, bobs and turns slowly. A cyan rib runs down each face; it breathes and burns brighter while crystals wait. One still collider keeps building, support and the E key as before. New hammer icon.
260 lines
12 KiB
C#
260 lines
12 KiB
C#
using System.Collections.Generic;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Ljoskjarne's body: a stone pyramid, 2 m to a side and 2 m tall, floating 0.5 m above the
|
|
/// ground. Dark steel carved with Valheim's own runes (the Ashlands rune tile's glyphs, laid out by
|
|
/// scripts/paint_nexus.py), and a cyan seam down the middle of each face.
|
|
///
|
|
/// Only the look moves. The piece keeps one still collider from the ground up, so building on it,
|
|
/// support, hovering and the E key behave as before; the pyramid bobs and turns on each client by
|
|
/// itself, nothing over the network. The seam breathes, and burns brighter while crystals wait.
|
|
/// </summary>
|
|
public static class AlfarNexusLook
|
|
{
|
|
private const float Height = 2f;
|
|
private const float HalfBase = 1f;
|
|
private const float Lift = 0.5f;
|
|
private const float SeamWidth = 0.04f; // half the rib's width at the base
|
|
private const float SeamHeight = 0.025f;
|
|
private const string SourceMaterialPrefab = "piece_cartographytable";
|
|
private const string EmbersSource = "fx_Torch_Carried";
|
|
|
|
private static readonly Color Seam = new Color(0.35f, 0.75f, 1f);
|
|
|
|
/// <summary>Strips the cartography table's body and puts the pyramid in its place. False leaves
|
|
/// the table, with a warning, so the Nexus is never invisible.</summary>
|
|
public static bool Build(GameObject prefab)
|
|
{
|
|
Texture2D face = PieceLook.LoadTexture("AlfarNexus_face.png");
|
|
Renderer tableRenderer = prefab.transform.Find("new/high")?.GetComponent<Renderer>();
|
|
if (face == null || tableRenderer == null || tableRenderer.sharedMaterial == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[Alfar] Could not build the Ljoskjarne's pyramid; it keeps the cartography table's look.");
|
|
return false;
|
|
}
|
|
|
|
Material stone = new Material(tableRenderer.sharedMaterial) { name = "AlfarNexus_stone" };
|
|
stone.color = Color.white;
|
|
stone.SetTexture("_MainTex", face);
|
|
foreach (string slot in new[] { "_BumpMap", "_MetallicGlossMap" })
|
|
if (stone.HasProperty(slot)) stone.SetTexture(slot, null);
|
|
if (stone.HasProperty("_Metallic")) stone.SetFloat("_Metallic", 0.75f);
|
|
if (stone.HasProperty("_Glossiness")) stone.SetFloat("_Glossiness", 0.55f);
|
|
|
|
Material seam = new Material(stone) { name = "AlfarNexus_seam" };
|
|
seam.SetTexture("_MainTex", null);
|
|
seam.color = Color.black;
|
|
seam.EnableKeyword("_EMISSION");
|
|
seam.SetColor("_EmissionColor", Seam);
|
|
|
|
// The table goes whole: its meshes, its damage states, its fragments and its four colliders.
|
|
for (int i = prefab.transform.childCount - 1; i >= 0; i--)
|
|
Object.DestroyImmediate(prefab.transform.GetChild(i).gameObject);
|
|
WearNTear wear = prefab.GetComponent<WearNTear>();
|
|
if (wear != null)
|
|
{
|
|
wear.m_new = null;
|
|
wear.m_worn = null;
|
|
wear.m_broken = null;
|
|
wear.m_fragmentRoots = new GameObject[0];
|
|
}
|
|
|
|
// One still collider, from the ground to the tip, a little inside the faces.
|
|
GameObject solid = new GameObject("collider") { layer = prefab.layer };
|
|
solid.transform.SetParent(prefab.transform, false);
|
|
BoxCollider box = solid.AddComponent<BoxCollider>();
|
|
box.center = new Vector3(0f, (Lift + Height) / 2f, 0f);
|
|
box.size = new Vector3(HalfBase * 1.4f, Lift + Height, HalfBase * 1.4f);
|
|
|
|
Transform floating = new GameObject("float").transform;
|
|
floating.SetParent(prefab.transform, false);
|
|
floating.gameObject.layer = prefab.layer;
|
|
|
|
MeshRenderer seamRenderer = Part(floating, "seam", SeamMesh(), seam).GetComponent<MeshRenderer>();
|
|
Part(floating, "pyramid", PyramidMesh(), stone);
|
|
|
|
NexusLook look = prefab.AddComponent<NexusLook>();
|
|
look.Float = floating;
|
|
look.SeamRenderer = seamRenderer;
|
|
look.Motes = AddMotes(prefab.transform);
|
|
return true;
|
|
}
|
|
|
|
private static GameObject Part(Transform parent, string name, Mesh mesh, Material material)
|
|
{
|
|
GameObject part = new GameObject(name) { layer = parent.gameObject.layer };
|
|
part.transform.SetParent(parent, false);
|
|
part.AddComponent<MeshFilter>().sharedMesh = mesh;
|
|
part.AddComponent<MeshRenderer>().sharedMaterial = material;
|
|
return part;
|
|
}
|
|
|
|
private static Vector3 Apex => new Vector3(0f, Lift + Height, 0f);
|
|
|
|
private static Vector3[] BaseCorners => new[]
|
|
{
|
|
new Vector3(-HalfBase, Lift, -HalfBase), new Vector3(HalfBase, Lift, -HalfBase),
|
|
new Vector3(HalfBase, Lift, HalfBase), new Vector3(-HalfBase, Lift, HalfBase),
|
|
};
|
|
|
|
/// <summary>Four faces, each with its own three corners so the shading stays flat and every face
|
|
/// shows the whole carving (u along the base, v = 1 at the tip), and the underside.</summary>
|
|
private static Mesh PyramidMesh()
|
|
{
|
|
Vector3[] c = BaseCorners;
|
|
var vertices = new Vector3[16];
|
|
var uvs = new Vector2[16];
|
|
var triangles = new int[18];
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
// Wound clockwise seen from outside, which is Unity's front face.
|
|
vertices[i * 3] = c[(i + 1) % 4];
|
|
vertices[i * 3 + 1] = c[i];
|
|
vertices[i * 3 + 2] = Apex;
|
|
uvs[i * 3] = new Vector2(1f, 0f);
|
|
uvs[i * 3 + 1] = new Vector2(0f, 0f);
|
|
uvs[i * 3 + 2] = new Vector2(0.5f, 1f);
|
|
triangles[i * 3] = i * 3;
|
|
triangles[i * 3 + 1] = i * 3 + 1;
|
|
triangles[i * 3 + 2] = i * 3 + 2;
|
|
}
|
|
for (int i = 0; i < 4; i++) { vertices[12 + i] = c[i]; uvs[12 + i] = new Vector2(0.5f, 0.02f); }
|
|
int[] under = { 12, 13, 14, 12, 14, 15 };
|
|
System.Array.Copy(under, 0, triangles, 12, 6);
|
|
|
|
Mesh mesh = new Mesh { name = "AlfarNexus_pyramid", vertices = vertices, uv = uvs, triangles = triangles };
|
|
mesh.RecalculateNormals();
|
|
mesh.RecalculateBounds();
|
|
return mesh;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The seams: a raised rib down the middle of each face, from the middle of its base edge to
|
|
/// the tip, standing on the stone with its own sides. A flat strip a centimetre off the face
|
|
/// lost the depth test to the stone at a glancing angle, and the cyan came and went in patches
|
|
/// as the pyramid turned; a rib has no surface lying that close to the face.
|
|
/// </summary>
|
|
private static Mesh SeamMesh()
|
|
{
|
|
Vector3[] c = BaseCorners;
|
|
var vertices = new List<Vector3>();
|
|
var triangles = new List<int>();
|
|
Vector3 centre = new Vector3(0f, Lift + Height / 3f, 0f);
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
Vector3 a = c[i], b = c[(i + 1) % 4];
|
|
Vector3 middle = (a + b) / 2f;
|
|
Vector3 along = (b - a).normalized;
|
|
Vector3 normal = Vector3.Cross(b - a, Apex - a).normalized;
|
|
if (Vector3.Dot(normal, middle - centre) < 0f) normal = -normal;
|
|
|
|
Vector3 top = normal * SeamHeight, foot = normal * 0.002f;
|
|
Vector3 baseLeft = middle - along * SeamWidth, baseRight = middle + along * SeamWidth;
|
|
Vector3 tipLeft = Apex - along * 0.004f, tipRight = Apex + along * 0.004f;
|
|
|
|
Quad(vertices, triangles, baseLeft + top, baseRight + top, tipRight + top, tipLeft + top, normal);
|
|
Quad(vertices, triangles, baseLeft + foot, baseLeft + top, tipLeft + top, tipLeft + foot, -along);
|
|
Quad(vertices, triangles, baseRight + foot, baseRight + top, tipRight + top, tipRight + foot, along);
|
|
Quad(vertices, triangles, baseLeft + foot, baseRight + foot, baseRight + top, baseLeft + top, middle - Apex);
|
|
}
|
|
|
|
Mesh mesh = new Mesh { name = "AlfarNexus_seam", vertices = vertices.ToArray(), triangles = triangles.ToArray() };
|
|
mesh.RecalculateNormals();
|
|
mesh.RecalculateBounds();
|
|
return mesh;
|
|
}
|
|
|
|
/// <summary>
|
|
/// One quad with its own corners, turned so its front faces <paramref name="outward"/>: Unity's
|
|
/// front face is the side Cross(v1 - v0, v2 - v0) points to.
|
|
/// </summary>
|
|
private static void Quad(List<Vector3> vertices, List<int> triangles, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 outward)
|
|
{
|
|
int first = vertices.Count;
|
|
vertices.Add(p0); vertices.Add(p1); vertices.Add(p2); vertices.Add(p3);
|
|
|
|
bool flip = Vector3.Dot(Vector3.Cross(p1 - p0, p2 - p0), outward) < 0f;
|
|
int[] quad = flip ? new[] { 0, 2, 1, 0, 3, 2 } : new[] { 0, 1, 2, 0, 2, 3 };
|
|
foreach (int k in quad) triangles.Add(first + k);
|
|
}
|
|
|
|
/// <summary>A few cyan motes drifting up from the ground under it: the torch's embers, recoloured,
|
|
/// spread over the pyramid's shadow and slowed down.</summary>
|
|
private static ParticleSystem AddMotes(Transform root)
|
|
{
|
|
GameObject torch = PrefabManager.Cache.GetPrefab<GameObject>(EmbersSource);
|
|
Transform source = torch != null ? torch.transform.Find("Embers") : null;
|
|
if (source == null) return null;
|
|
|
|
GameObject motes = Object.Instantiate(source.gameObject, root, false);
|
|
motes.name = "motes";
|
|
motes.transform.localPosition = new Vector3(0f, 0.05f, 0f);
|
|
motes.transform.localRotation = Quaternion.identity;
|
|
PieceLook.Rehue(motes.transform, Seam * 2f);
|
|
|
|
ParticleSystem particles = motes.GetComponent<ParticleSystem>();
|
|
if (particles != null)
|
|
{
|
|
ParticleSystem.ShapeModule shape = particles.shape;
|
|
shape.enabled = true;
|
|
shape.shapeType = ParticleSystemShapeType.Circle;
|
|
shape.radius = HalfBase;
|
|
shape.rotation = new Vector3(-90f, 0f, 0f);
|
|
ParticleSystem.MainModule main = particles.main;
|
|
main.simulationSpeed = 0.5f;
|
|
}
|
|
return particles;
|
|
}
|
|
|
|
/// <summary>The motion and the seam's light, per client.</summary>
|
|
public class NexusLook : MonoBehaviour
|
|
{
|
|
public Transform Float;
|
|
public Renderer SeamRenderer;
|
|
public ParticleSystem Motes;
|
|
|
|
private const float BobHeight = 0.08f;
|
|
private const float BobSeconds = 4.5f;
|
|
private const float TurnSeconds = 40f;
|
|
private const float BreathSeconds = 3.5f;
|
|
|
|
private AlfarNexusBehaviour m_nexus;
|
|
private Material m_seam;
|
|
private float m_phase;
|
|
private float m_ready; // eases between 0 (no crystals) and 1, so the brightening is not a jump
|
|
|
|
private void Awake()
|
|
{
|
|
m_nexus = GetComponent<AlfarNexusBehaviour>();
|
|
if (SeamRenderer != null) m_seam = SeamRenderer.material; // this one's own copy, to animate
|
|
// Two Nexuses side by side must not bob in step.
|
|
m_phase = (Mathf.Abs(transform.position.x * 0.37f + transform.position.z * 0.71f)) % 10f;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
float t = Time.time + m_phase;
|
|
if (Float != null)
|
|
{
|
|
Float.localPosition = new Vector3(0f, Mathf.Sin(t * 2f * Mathf.PI / BobSeconds) * BobHeight, 0f);
|
|
Float.localRotation = Quaternion.Euler(0f, t * 360f / TurnSeconds, 0f);
|
|
}
|
|
|
|
if (m_seam == null) return;
|
|
bool ready = m_nexus != null && m_nexus.Crystals > 0;
|
|
m_ready = Mathf.MoveTowards(m_ready, ready ? 1f : 0f, Time.deltaTime * 0.5f);
|
|
float breath = 0.75f + 0.25f * Mathf.Sin(t * 2f * Mathf.PI / BreathSeconds);
|
|
m_seam.SetColor("_EmissionColor", Seam * breath * Mathf.Lerp(1f, 2.2f, m_ready));
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (m_seam != null) Destroy(m_seam);
|
|
}
|
|
}
|
|
}
|
|
}
|