Complete portable planetary presentation contracts
This commit is contained in:
parent
42fef7cdd7
commit
102403efac
74 changed files with 1166 additions and 31 deletions
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"name": "CultMath.PlanetaryViewer.Editor",
|
||||
"rootNamespace": "CultMath.Unity.Samples.Editor",
|
||||
"references": [
|
||||
"CultMath.PlanetaryViewer"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CultMath.Unity.Samples.Editor;
|
||||
|
||||
public static class CultMathPlanetaryViewerMenu
|
||||
{
|
||||
[MenuItem("GameObject/CultMath/Planetary Field Viewer", false, 10)]
|
||||
public static void CreateViewer()
|
||||
{
|
||||
var root = new GameObject("CultMath Planetary Field Viewer");
|
||||
Undo.RegisterCreatedObjectUndo(root, "Create CultMath Planetary Viewer");
|
||||
root.AddComponent<CultMathPlanetaryViewer>();
|
||||
Selection.activeGameObject = root;
|
||||
|
||||
if (Camera.main != null) return;
|
||||
var cameraObject = new GameObject("Planetary Viewer Camera");
|
||||
Undo.RegisterCreatedObjectUndo(cameraObject, "Create Planetary Viewer Camera");
|
||||
cameraObject.tag = "MainCamera";
|
||||
cameraObject.transform.position = new Vector3(0, 0, -24);
|
||||
cameraObject.transform.LookAt(root.transform);
|
||||
var camera = cameraObject.AddComponent<Camera>();
|
||||
camera.nearClipPlane = 0.01f;
|
||||
camera.farClipPlane = 1000;
|
||||
}
|
||||
}
|
||||
16
Samples~/PlanetaryViewer/README.md
Normal file
16
Samples~/PlanetaryViewer/README.md
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# Planetary Field Viewer
|
||||
|
||||
Import this sample through Unity's Package Manager, then choose
|
||||
`GameObject > CultMath > Planetary Field Viewer`.
|
||||
|
||||
The host project must contain `Assets/csc.rsp` with
|
||||
`-langversion:latest`, as documented in the CultMath package README.
|
||||
|
||||
The generated six-face mesh uses `PlanetaryPatch`. Its shader calls
|
||||
`cultmath_planetary_field_sample` from the packaged CultMath HLSL, while the
|
||||
component exposes the matching CPU `Sample` method. Seed, radius, physical
|
||||
sample footprint, erosion settings, and base-field equations are shared between
|
||||
the two bodies.
|
||||
|
||||
This is a renderer integration fixture, not a gameplay owner. It deliberately
|
||||
contains no Aetheria state, biomes, resources, or simulation decisions.
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "CultMath.PlanetaryViewer",
|
||||
"rootNamespace": "CultMath.Unity.Samples",
|
||||
"references": [
|
||||
"CultMath",
|
||||
"CultMath.Unity"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
139
Samples~/PlanetaryViewer/Runtime/CultMathPlanetaryViewer.cs
Normal file
139
Samples~/PlanetaryViewer/Runtime/CultMathPlanetaryViewer.cs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace CultMath.Unity.Samples;
|
||||
|
||||
[ExecuteAlways]
|
||||
public sealed class CultMathPlanetaryViewer : MonoBehaviour
|
||||
{
|
||||
[Min(0.001f)] public float Radius = 8.4f;
|
||||
[Range(1, 256)] public int CellsPerFace = 64;
|
||||
public int Seed = 42;
|
||||
[Min(0.0001f)] public float SampleFootprint = 0.01f;
|
||||
public Shader? PlanetShader;
|
||||
public Vector3 LightDirection = new(-0.4f, 0.7f, -0.6f);
|
||||
public Color LowColor = new(0.055f, 0.16f, 0.09f, 1);
|
||||
public Color HighColor = new(0.62f, 0.48f, 0.28f, 1);
|
||||
public Color RidgeColor = new(0.88f, 0.83f, 0.7f, 1);
|
||||
|
||||
private readonly List<Mesh> meshes = new();
|
||||
private Material? material;
|
||||
private int builtCells;
|
||||
|
||||
public PlanetaryFieldDefinition Definition => PlanetaryFieldDefinition.Create(
|
||||
1,
|
||||
Radius,
|
||||
Seed,
|
||||
ViewerErosion(Radius));
|
||||
|
||||
public PlanetarySurfaceSample Sample(float3 direction)
|
||||
{
|
||||
var source = new ViewerBaseField(Radius);
|
||||
var normalized = math.normalize(direction);
|
||||
return PlanetaryField.Sample(Definition, normalized, source.Sample(normalized), PlanetaryQueryScale.AtFootprint(SampleFootprint));
|
||||
}
|
||||
|
||||
private void OnEnable() => Rebuild();
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
Radius = Mathf.Max(Radius, 0.001f);
|
||||
CellsPerFace = Mathf.Clamp(CellsPerFace, 1, 256);
|
||||
SampleFootprint = Mathf.Max(SampleFootprint, 0.0001f);
|
||||
if (isActiveAndEnabled) Rebuild();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (builtCells != CellsPerFace || material == null) Rebuild();
|
||||
ApplyMaterial();
|
||||
}
|
||||
|
||||
private void Rebuild()
|
||||
{
|
||||
ClearGenerated();
|
||||
PlanetShader ??= Shader.Find("CultMath/Planetary Field Viewer");
|
||||
if (PlanetShader == null) return;
|
||||
material = new Material(PlanetShader) { name = "CultMath Planetary Viewer Material", hideFlags = HideFlags.HideAndDontSave };
|
||||
foreach (var face in (PlanetaryCubeFace[])Enum.GetValues(typeof(PlanetaryCubeFace)))
|
||||
{
|
||||
var mesh = PlanetaryPatchMeshAdapter.CreateFaceMesh(face, CellsPerFace);
|
||||
mesh.hideFlags = HideFlags.HideAndDontSave;
|
||||
meshes.Add(mesh);
|
||||
var child = new GameObject(face.ToString()) { hideFlags = HideFlags.DontSave };
|
||||
child.transform.SetParent(transform, false);
|
||||
child.AddComponent<MeshFilter>().sharedMesh = mesh;
|
||||
child.AddComponent<MeshRenderer>().sharedMaterial = material;
|
||||
}
|
||||
builtCells = CellsPerFace;
|
||||
ApplyMaterial();
|
||||
}
|
||||
|
||||
private void ApplyMaterial()
|
||||
{
|
||||
if (material == null) return;
|
||||
var p = ViewerErosion(Radius);
|
||||
material.SetFloat("_Radius", Radius);
|
||||
material.SetFloat("_SampleFootprint", SampleFootprint);
|
||||
material.SetInt("_Seed", Seed);
|
||||
material.SetFloat("_ErosionScale", p.Scale);
|
||||
material.SetFloat("_ErosionStrength", p.Strength);
|
||||
material.SetFloat("_GullyWeight", p.GullyWeight);
|
||||
material.SetFloat("_Detail", p.Detail);
|
||||
material.SetVector("_Rounding", new(p.Rounding.x, p.Rounding.y, p.Rounding.z, p.Rounding.w));
|
||||
material.SetVector("_Onset", new(p.Onset.x, p.Onset.y, p.Onset.z, p.Onset.w));
|
||||
material.SetVector("_AssumedSlope", new(p.AssumedSlope.x, p.AssumedSlope.y, 0, 0));
|
||||
material.SetFloat("_CellScale", p.CellScale);
|
||||
material.SetFloat("_Normalization", p.Normalization);
|
||||
material.SetInt("_Octaves", p.Octaves);
|
||||
material.SetFloat("_Lacunarity", p.Lacunarity);
|
||||
material.SetFloat("_Gain", p.Gain);
|
||||
material.SetVector("_LightDirection", LightDirection.normalized);
|
||||
material.SetColor("_LowColor", LowColor);
|
||||
material.SetColor("_HighColor", HighColor);
|
||||
material.SetColor("_RidgeColor", RidgeColor);
|
||||
}
|
||||
|
||||
private void OnDisable() => ClearGenerated();
|
||||
private void OnDestroy() => ClearGenerated();
|
||||
|
||||
private void ClearGenerated()
|
||||
{
|
||||
for (var i = transform.childCount - 1; i >= 0; i--) DestroyGenerated(transform.GetChild(i).gameObject);
|
||||
foreach (var mesh in meshes) DestroyGenerated(mesh);
|
||||
meshes.Clear();
|
||||
if (material != null) DestroyGenerated(material);
|
||||
material = null;
|
||||
builtCells = 0;
|
||||
}
|
||||
|
||||
private static void DestroyGenerated(UnityEngine.Object value)
|
||||
{
|
||||
if (Application.isPlaying) Destroy(value);
|
||||
else DestroyImmediate(value);
|
||||
}
|
||||
|
||||
private static AdvancedErosionParameters ViewerErosion(float radius) => new(
|
||||
radius * 0.075f, 0.12f, 0.58f, 1.45f,
|
||||
new float4(0.1f, 0.015f, 0.1f, 2),
|
||||
new float4(1.25f, 1.25f, 2.8f, 1.5f),
|
||||
new float2(0.7f, 0.85f), 0.7f, 0.5f, 7, 2, 0.5f);
|
||||
|
||||
private readonly struct ViewerBaseField : IPlanetaryBaseField
|
||||
{
|
||||
private readonly float radius;
|
||||
public ViewerBaseField(float radius) => this.radius = radius;
|
||||
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var value = direction.x * 0.6f + direction.y * direction.z * 0.3f;
|
||||
var angularGradient = new float3(0.6f, direction.z * 0.3f, direction.y * 0.3f);
|
||||
angularGradient -= direction * math.dot(angularGradient, direction);
|
||||
var fieldGradient = angularGradient / radius;
|
||||
var radialDisplacement = value * radius * 0.035f;
|
||||
var radialGradient = angularGradient * 0.035f;
|
||||
return new(radialDisplacement, radialGradient, value, fieldGradient, math.clamp(value, -1, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
Shader "CultMath/Planetary Field Viewer"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_LowColor ("Low Color", Color) = (0.055, 0.16, 0.09, 1)
|
||||
_HighColor ("High Color", Color) = (0.62, 0.48, 0.28, 1)
|
||||
_RidgeColor ("Ridge Color", Color) = (0.88, 0.83, 0.7, 1)
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
|
||||
Pass
|
||||
{
|
||||
Cull Back
|
||||
ZWrite On
|
||||
HLSLPROGRAM
|
||||
#pragma target 5.0
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
#include "UnityCG.cginc"
|
||||
#include "Packages/org.gamecult.cultmath/shaders/CultMath.hlsl"
|
||||
|
||||
float _Radius;
|
||||
float _SampleFootprint;
|
||||
int _Seed;
|
||||
float _ErosionScale, _ErosionStrength, _GullyWeight, _Detail;
|
||||
float4 _Rounding, _Onset, _AssumedSlope;
|
||||
float _CellScale, _Normalization, _Lacunarity, _Gain;
|
||||
int _Octaves;
|
||||
float3 _LightDirection;
|
||||
float4 _LowColor, _HighColor, _RidgeColor;
|
||||
|
||||
struct Attributes { float4 vertex : POSITION; };
|
||||
struct Varyings
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float3 worldNormal : TEXCOORD0;
|
||||
float height : TEXCOORD1;
|
||||
float ridge : TEXCOORD2;
|
||||
};
|
||||
|
||||
CultMathPlanetarySurfaceSample viewer_sample(float3 direction)
|
||||
{
|
||||
direction = normalize(direction);
|
||||
float value = direction.x * 0.6 + direction.y * direction.z * 0.3;
|
||||
float3 angular_gradient = float3(0.6, direction.z * 0.3, direction.y * 0.3);
|
||||
angular_gradient -= direction * dot(angular_gradient, direction);
|
||||
CultMathPlanetaryBaseFieldSample base_sample;
|
||||
base_sample.radial_displacement = value * _Radius * 0.035;
|
||||
base_sample.radial_gradient = angular_gradient * 0.035;
|
||||
base_sample.field_value = value;
|
||||
base_sample.field_gradient = angular_gradient / _Radius;
|
||||
base_sample.fade_target = clamp(value, -1.0, 1.0);
|
||||
CultMathAdvancedErosionParameters erosion;
|
||||
erosion.scale = _ErosionScale; erosion.strength = _ErosionStrength;
|
||||
erosion.gully_weight = _GullyWeight; erosion.detail = _Detail;
|
||||
erosion.rounding = _Rounding; erosion.onset = _Onset;
|
||||
erosion.assumed_slope = _AssumedSlope.xy; erosion.cell_scale = _CellScale;
|
||||
erosion.normalization = _Normalization; erosion.octaves = _Octaves;
|
||||
erosion.lacunarity = _Lacunarity; erosion.gain = _Gain;
|
||||
CultMathPlanetaryFieldDefinition definition;
|
||||
definition.radius = _Radius; definition.seed = _Seed; definition.erosion = erosion;
|
||||
return cultmath_planetary_field_sample(definition, direction, base_sample, _SampleFootprint);
|
||||
}
|
||||
|
||||
Varyings vert(Attributes input)
|
||||
{
|
||||
float3 direction = normalize(input.vertex.xyz);
|
||||
CultMathPlanetarySurfaceSample sample = viewer_sample(direction);
|
||||
float3 local = direction * (_Radius + sample.radial_displacement);
|
||||
Varyings output;
|
||||
output.position = UnityObjectToClipPos(float4(local, 1));
|
||||
output.worldNormal = UnityObjectToWorldNormal(sample.surface_normal);
|
||||
output.height = sample.radial_displacement / max(_Radius * 0.1, 1.0e-5);
|
||||
output.ridge = sample.ridge;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 frag(Varyings input) : SV_Target
|
||||
{
|
||||
float height = saturate(input.height * 0.5 + 0.5);
|
||||
float3 albedo = lerp(_LowColor.rgb, _HighColor.rgb, height);
|
||||
albedo = lerp(albedo, _RidgeColor.rgb, saturate((input.ridge - 0.55) * 2.5));
|
||||
float diffuse = saturate(dot(normalize(input.worldNormal), normalize(-_LightDirection))) * 0.82 + 0.08;
|
||||
return float4(albedo * diffuse, 1);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue