RacesOfValheim/JotunnModStub/BoneShip/BoneShipSail.cs
2026-10-06 11:36:18 +02:00

178 lines
6.9 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The bone ships' sails: black, and torn.
///
/// Every vanilla sail is a Custom/Vegetation material - an alpha-cutout shader (it has a _Cutoff of
/// 0.5) - so a hole is simply a texel whose alpha is under that. Nothing is imported: the texture is
/// drawn here in code, one per source material (sail_white on the Karve, sail and sail_brown on the
/// Longship, ashlandshipsail_mat on the Ashlands ship), and goes onto a COPY of the material so the
/// vanilla ships keep theirs.
///
/// The holes come from value noise, not a drawing, because the sail's UV layout was not read. Two
/// scales: broad tears, and small shreds, with the top and bottom edges frayed. Whether the shader
/// actually clips on that alpha is the one thing only the game can confirm.
/// </summary>
public static class BoneShipSail
{
private const int Size = 256;
// The big tears: a share of the sail, roughly. Higher threshold = fewer, smaller holes.
private const float TearThreshold = 0.62f;
private const float ShredThreshold = 0.70f;
public static void Apply(GameObject prefab, string shipName)
{
Dictionary<Material, Material> copies = new Dictionary<Material, Material>();
int seed = shipName.GetHashCode();
foreach (Renderer renderer in prefab.GetComponentsInChildren<Renderer>(true))
{
if (renderer == null || renderer is ParticleSystemRenderer || renderer is LineRenderer) continue;
Material[] shared = renderer.sharedMaterials;
bool changed = false;
for (int i = 0; i < shared.Length; i++)
{
Material source = shared[i];
if (!IsSail(source)) continue;
Material copy;
if (!copies.TryGetValue(source, out copy))
{
copy = new Material(source) { name = source.name + "_bone" };
copy.SetTexture("_MainTex", Draw(seed + source.name.GetHashCode()));
copy.SetColor("_Color", Color.white);
copies[source] = copy;
}
shared[i] = copy;
changed = true;
}
if (changed) renderer.sharedMaterials = shared;
}
if (copies.Count == 0)
{
Jotunn.Logger.LogWarning($"[BoneShip] {shipName}: no sail material found, the sail is left as it is.");
}
}
private static bool IsSail(Material material)
{
if (material == null || material.shader == null) return false;
if (!material.HasProperty("_MainTex")) return false;
string name = material.name.ToLowerInvariant();
return name.Contains("sail");
}
// ---------------------------------------------------------------- the texture
private static Texture2D Draw(int seed)
{
Texture2D texture = new Texture2D(Size, Size, TextureFormat.RGBA32, true)
{
name = "boneship_sail",
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear,
};
Color32[] pixels = new Color32[Size * Size];
for (int y = 0; y < Size; y++)
{
float v = (y + 0.5f) / Size;
for (int x = 0; x < Size; x++)
{
float u = (x + 0.5f) / Size;
pixels[y * Size + x] = Pixel(u, v, seed);
}
}
texture.SetPixels32(pixels);
texture.Apply(true, false);
return texture;
}
private static Color32 Pixel(float u, float v, int seed)
{
// Cloth: near black, mottled, with a faint warp of vertical streaks.
float mottle = Fbm(u * 7f, v * 7f, seed + 3);
float streak = 0.5f + 0.5f * Mathf.Sin(u * 90f + Fbm(u * 3f, v * 3f, seed + 5) * 6f);
float shade = Mathf.Lerp(0.04f, 0.17f, mottle) * Mathf.Lerp(0.85f, 1.05f, streak);
Color cloth = new Color(shade * 1.00f, shade * 0.97f, shade * 0.93f, 1f);
// Holes. Each term is an alpha with 0.5 at its own edge; the lowest wins.
float tear = Fbm(u * 4f, v * 4f, seed + 11);
float shred = Fbm(u * 13f, v * 13f, seed + 29);
float alpha = 1f;
alpha = Mathf.Min(alpha, 0.5f + (TearThreshold - tear) * 14f);
alpha = Mathf.Min(alpha, 0.5f + (ShredThreshold - shred) * 14f);
// Fringe: the top and bottom edges are eaten away in ragged teeth, the sides a little.
float edge = Mathf.Min(v, 1f - v);
float teeth = ValueNoise(u * 22f, v * 3f, seed + 41);
alpha = Mathf.Min(alpha, 0.5f + (teeth * 0.16f - edge) * 28f);
float side = Mathf.Min(u, 1f - u);
float sideTeeth = ValueNoise(u * 3f, v * 20f, seed + 53);
alpha = Mathf.Min(alpha, 0.5f + (sideTeeth * 0.07f - side) * 28f);
// Scorched round the holes: darker where the tear is nearly through.
float burn = Mathf.Clamp01((tear - (TearThreshold - 0.10f)) * 8f);
cloth = Color.Lerp(cloth, new Color(0.015f, 0.015f, 0.015f, 1f), burn * 0.8f);
cloth.a = Mathf.Clamp01(alpha);
return cloth;
}
// ---------------------------------------------------------------- noise
private static float Hash(int x, int y, int seed)
{
unchecked
{
int h = x * 374761393 + y * 668265263 + seed * 1442695041;
h = (h ^ (h >> 13)) * 1274126177;
return ((h ^ (h >> 16)) & 0xffff) / 65535f;
}
}
private static float ValueNoise(float x, float y, int seed)
{
int xi = Mathf.FloorToInt(x);
int yi = Mathf.FloorToInt(y);
float tx = x - xi;
float ty = y - yi;
tx = tx * tx * (3f - 2f * tx);
ty = ty * ty * (3f - 2f * ty);
float a = Hash(xi, yi, seed);
float b = Hash(xi + 1, yi, seed);
float c = Hash(xi, yi + 1, seed);
float d = Hash(xi + 1, yi + 1, seed);
return Mathf.Lerp(Mathf.Lerp(a, b, tx), Mathf.Lerp(c, d, tx), ty);
}
private static float Fbm(float x, float y, int seed)
{
float value = 0f;
float amplitude = 0.5f;
for (int octave = 0; octave < 4; octave++)
{
value += amplitude * ValueNoise(x, y, seed + octave * 17);
x *= 2.03f;
y *= 2.03f;
amplitude *= 0.5f;
}
return value / 0.9375f; // 0.5 + 0.25 + 0.125 + 0.0625, so the sum stays in 0..1
}
}
}