diff --git a/scripts/build-geometry-unity-package.ps1 b/scripts/build-geometry-unity-package.ps1 index 8c1b8b1f..a16370eb 100644 --- a/scripts/build-geometry-unity-package.ps1 +++ b/scripts/build-geometry-unity-package.ps1 @@ -21,6 +21,7 @@ $candidateFeed = if ([System.IO.Path]::IsPathRooted($CultMathCandidateFeed)) { Join-Path $repoRoot $CultMathCandidateFeed } $pluginRoot = Join-Path $outputRoot "Runtime\Plugins" +$templatePluginRoot = Join-Path $templateRoot "Runtime\Plugins" if (-not (Test-Path -LiteralPath (Join-Path $candidateFeed "CultMath.0.1.0-geometry-migration.2.nupkg"))) { throw "CultMath migration candidate is missing from $candidateFeed. Pack CultMath before staging Geometry." @@ -43,33 +44,59 @@ if ($LASTEXITCODE -ne 0) { throw "GameCult.Geometry publish failed with exit code $LASTEXITCODE" } -New-Item -ItemType Directory -Force -Path $pluginRoot | Out-Null -Copy-Item -LiteralPath (Join-Path $templateRoot "package.json") -Destination $outputRoot -Copy-Item -LiteralPath (Join-Path $templateRoot "README.md") -Destination $outputRoot -Copy-Item -LiteralPath (Join-Path $templateRoot "Runtime\GameCult.Geometry.asmdef") -Destination (Join-Path $outputRoot "Runtime") -Copy-Item -LiteralPath (Join-Path $templateRoot "Runtime\GameCult.Geometry.Unity.asmdef") -Destination (Join-Path $outputRoot "Runtime") -Copy-Item -LiteralPath (Join-Path $templateRoot "Runtime\PlanetaryPatchMeshAdapter.cs") -Destination (Join-Path $outputRoot "Runtime") -Copy-Item -LiteralPath (Join-Path $templateRoot "Runtime\PlanetaryPatchMeshAdapter.cs.meta") -Destination (Join-Path $outputRoot "Runtime") -Copy-Item -LiteralPath (Join-Path $templateRoot "Runtime\PlanetaryPageUpload.cs") -Destination (Join-Path $outputRoot "Runtime") -Copy-Item -LiteralPath (Join-Path $templateRoot "Runtime\PlanetaryPageUpload.cs.meta") -Destination (Join-Path $outputRoot "Runtime") -Copy-Item -LiteralPath (Join-Path $templateRoot "Samples~") -Destination $outputRoot -Recurse -$shaderOutputRoot = Join-Path $outputRoot "Shaders" -New-Item -ItemType Directory -Force -Path $shaderOutputRoot | Out-Null -Copy-Item -Path (Join-Path $repoRoot "src\GameCult.Geometry\Shaders\*.hlsl") -Destination $shaderOutputRoot -Copy-Item -Path (Join-Path $repoRoot "src\GameCult.Geometry\Shaders\*.hlsl.meta") -Destination $shaderOutputRoot -ErrorAction SilentlyContinue -Copy-Item -LiteralPath (Join-Path $repoRoot "src\GameCult.Geometry\LICENSES.md") -Destination $outputRoot -Copy-Item -LiteralPath (Join-Path $repoRoot "src\GameCult.Geometry\THIRD-PARTY-NOTICES.md") -Destination $outputRoot - $geometryAssembly = Join-Path $publishRoot "GameCult.Geometry.dll" if (-not (Test-Path -LiteralPath $geometryAssembly)) { throw "Geometry Unity package is missing GameCult.Geometry.dll" } -Copy-Item -LiteralPath $geometryAssembly -Destination $pluginRoot $geometrySymbols = [System.IO.Path]::ChangeExtension($geometryAssembly, ".pdb") -if (Test-Path -LiteralPath $geometrySymbols) { - Copy-Item -LiteralPath $geometrySymbols -Destination $pluginRoot + +$requiredTemplateFiles = @( + "LICENSES.md", + "THIRD-PARTY-NOTICES.md", + "Runtime\Plugins\GameCult.Geometry.dll", + "Runtime\Plugins\GameCult.Geometry.dll.meta", + "Runtime\Plugins\GameCult.Geometry.pdb", + "Runtime\Plugins\GameCult.Geometry.pdb.meta", + "Shaders\AdvancedErosionFilter.hlsl", + "Shaders\GameCult.Geometry.hlsl", + "Shaders\Planetary.hlsl", + "Shaders\PlanetaryRadialRefinement.hlsl", + "Shaders\SphericalErosion.hlsl" +) +foreach ($relativePath in $requiredTemplateFiles) { + if (-not (Test-Path -LiteralPath (Join-Path $templateRoot $relativePath))) { + throw "Tracked Geometry Unity package is incomplete: missing $relativePath" + } } +$unityAssetFiles = @(Get-ChildItem -LiteralPath $templateRoot -Recurse -File | Where-Object { + $_.Extension -in @(".asmdef", ".cs", ".dll", ".pdb", ".shader", ".hlsl") +}) +$missingAssetMetas = @($unityAssetFiles | Where-Object { -not (Test-Path -LiteralPath ($_.FullName + ".meta")) }) +$missingDirectoryMetas = @(Get-ChildItem -LiteralPath $templateRoot -Recurse -Directory | Where-Object { + -not (Test-Path -LiteralPath ($_.FullName + ".meta")) +}) +if ($missingAssetMetas.Count -ne 0 -or $missingDirectoryMetas.Count -ne 0) { + $missing = @($missingAssetMetas.FullName) + @($missingDirectoryMetas.FullName) + throw "Tracked Geometry Unity package has assets without stable .meta files: $($missing -join ', ')" +} + +$trackedAssembly = Join-Path $templatePluginRoot "GameCult.Geometry.dll" +if ((Get-FileHash -Algorithm SHA256 -LiteralPath $trackedAssembly).Hash -ne + (Get-FileHash -Algorithm SHA256 -LiteralPath $geometryAssembly).Hash) { + throw "Tracked GameCult.Geometry.dll does not match the current Release publish output. Sync the tracked Git-UPM package before tagging." +} +if (Test-Path -LiteralPath $geometrySymbols) { + $trackedSymbols = Join-Path $templatePluginRoot "GameCult.Geometry.pdb" + if ((Get-FileHash -Algorithm SHA256 -LiteralPath $trackedSymbols).Hash -ne + (Get-FileHash -Algorithm SHA256 -LiteralPath $geometrySymbols).Hash) { + throw "Tracked GameCult.Geometry.pdb does not match the current Release publish output. Sync the tracked Git-UPM package before tagging." + } +} + +New-Item -ItemType Directory -Force -Path $outputRoot | Out-Null +Copy-Item -Path (Join-Path $templateRoot "*") -Destination $outputRoot -Recurse -Force + $manifest = Get-Content -LiteralPath (Join-Path $outputRoot "package.json") -Raw | ConvertFrom-Json $stagedAssemblies = @(Get-ChildItem -LiteralPath $pluginRoot -Filter "*.dll") if ($stagedAssemblies.Count -ne 1 -or $stagedAssemblies[0].Name -ne "GameCult.Geometry.dll") { diff --git a/unity/org.gamecult.geometry/LICENSES.md b/unity/org.gamecult.geometry/LICENSES.md new file mode 100644 index 00000000..67455b87 --- /dev/null +++ b/unity/org.gamecult.geometry/LICENSES.md @@ -0,0 +1,13 @@ +# GameCult.Geometry licenses + +GameCult.Geometry is a mixed-license package. License follows file ownership; +the package as a whole is not relicensed under one blanket expression. + +- CultLib-authored integration, persistence, schema, and packaging files are + licensed under the MIT License published by the CultLib repository. +- Files under `Planetary/` and `Shaders/` transferred from CultMath remain + licensed under the Mozilla Public License 2.0. Their source notices and + provenance are preserved in `THIRD-PARTY-NOTICES.md`. + +Downstream redistributors must preserve the applicable notices for the files +they redistribute. diff --git a/unity/org.gamecult.geometry/Runtime.meta b/unity/org.gamecult.geometry/Runtime.meta new file mode 100644 index 00000000..0f218627 --- /dev/null +++ b/unity/org.gamecult.geometry/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d888f08710df48a780e393bc7b6ce1f5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Runtime/GameCult.Geometry.Unity.asmdef.meta b/unity/org.gamecult.geometry/Runtime/GameCult.Geometry.Unity.asmdef.meta new file mode 100644 index 00000000..deaa0678 --- /dev/null +++ b/unity/org.gamecult.geometry/Runtime/GameCult.Geometry.Unity.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 5a5dc5d9ba8d4aea93b53061d6376f7f +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Runtime/GameCult.Geometry.asmdef.meta b/unity/org.gamecult.geometry/Runtime/GameCult.Geometry.asmdef.meta new file mode 100644 index 00000000..ab0835ee --- /dev/null +++ b/unity/org.gamecult.geometry/Runtime/GameCult.Geometry.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a4dee1a0c3f04709994b8cc1f974da7a +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Runtime/Plugins.meta b/unity/org.gamecult.geometry/Runtime/Plugins.meta new file mode 100644 index 00000000..98acd60d --- /dev/null +++ b/unity/org.gamecult.geometry/Runtime/Plugins.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7f8484deafdf444abe3b1dedb9d09d16 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.dll b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.dll new file mode 100644 index 00000000..5664e0ed Binary files /dev/null and b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.dll differ diff --git a/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.dll.meta b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.dll.meta new file mode 100644 index 00000000..4253270b --- /dev/null +++ b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.dll.meta @@ -0,0 +1,21 @@ +fileFormatVersion: 2 +guid: 798aad4558fb450cb48da955d4a9b973 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.pdb b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.pdb new file mode 100644 index 00000000..c1f682e9 Binary files /dev/null and b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.pdb differ diff --git a/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.pdb.meta b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.pdb.meta new file mode 100644 index 00000000..4b609773 --- /dev/null +++ b/unity/org.gamecult.geometry/Runtime/Plugins/GameCult.Geometry.pdb.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a7d52cf0bec7478fbce9866155e4ce17 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~.meta b/unity/org.gamecult.geometry/Samples~.meta new file mode 100644 index 00000000..a5100a64 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e8f73941932842338866bc1e6a8a97cc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer.meta new file mode 100644 index 00000000..88718b45 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a67e7a0f4e73446fb92f992c2a2f03a2 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor.meta new file mode 100644 index 00000000..48746b57 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d94beefdeae3412c9e0b2609a440e64a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor/GameCult.Geometry.PlanetaryViewer.Editor.asmdef.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor/GameCult.Geometry.PlanetaryViewer.Editor.asmdef.meta new file mode 100644 index 00000000..6934a1cc --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor/GameCult.Geometry.PlanetaryViewer.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 39e5e41ae5914e60ad8f7dfab7334fdb +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor/GameCultGeometryPlanetaryViewerMenu.cs.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor/GameCultGeometryPlanetaryViewerMenu.cs.meta new file mode 100644 index 00000000..8e952599 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Editor/GameCultGeometryPlanetaryViewerMenu.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cbba35a250d4e2aadc125df07f85a54 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime.meta new file mode 100644 index 00000000..112754b8 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b7259ed205ed4795b5281effe598092e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCult.Geometry.PlanetaryViewer.asmdef.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCult.Geometry.PlanetaryViewer.asmdef.meta new file mode 100644 index 00000000..917abca3 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCult.Geometry.PlanetaryViewer.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 53e45dc58e30456db0c0b5b2252b5508 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCultGeometryPlanetaryViewer.cs.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCultGeometryPlanetaryViewer.cs.meta new file mode 100644 index 00000000..fbd1b4b1 --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCultGeometryPlanetaryViewer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 131c938f36b74c7389ca4df8a43e3bc2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCultGeometryPlanetaryViewer.shader.meta b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCultGeometryPlanetaryViewer.shader.meta new file mode 100644 index 00000000..ea0d8c6f --- /dev/null +++ b/unity/org.gamecult.geometry/Samples~/PlanetaryViewer/Runtime/GameCultGeometryPlanetaryViewer.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 615f5397a76c495aa9b028fa7751a3b8 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Shaders.meta b/unity/org.gamecult.geometry/Shaders.meta new file mode 100644 index 00000000..3f517308 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1695895ad6a5467f8136d33a1f160133 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Shaders/AdvancedErosionFilter.hlsl b/unity/org.gamecult.geometry/Shaders/AdvancedErosionFilter.hlsl new file mode 100644 index 00000000..219afc86 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/AdvancedErosionFilter.hlsl @@ -0,0 +1,147 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. +// Mathematical procedure adapted from Rune Skovbo Johansen's Advanced Terrain +// Erosion Filter (2025): https://www.shadertoy.com/view/wXcfWn + +#ifndef GAMECULT_GEOMETRY_ADVANCED_EROSION_FILTER_HLSL +#define GAMECULT_GEOMETRY_ADVANCED_EROSION_FILTER_HLSL + +struct GameCultGeometryAdvancedErosionParameters +{ + float scale; float strength; float gully_weight; float detail; + float4 rounding; float4 onset; float2 assumed_slope; + float cell_scale; float normalization; int octaves; float lacunarity; float gain; +}; + +struct GameCultGeometryAdvancedErosionResult +{ + float3 delta; float magnitude; float ridge_map; float fade_target; +}; + +struct GameCultGeometryErosionBandSelection +{ + int active_octaves; float final_octave_weight; float finest_included_wavelength; float unresolved_height_bound; +}; + +GameCultGeometryErosionBandSelection gamecult_geometry_select_erosion_bands(float base_wavelength, float sample_spacing, int maximum_octaves, float lacunarity, float base_amplitude, float gain, float transition_ratio) +{ + base_wavelength=max(base_wavelength,1.0e-6); sample_spacing=max(sample_spacing,0.0); lacunarity=max(lacunarity,1.01); transition_ratio=max(transition_ratio,1.01); maximum_octaves=min(max(maximum_octaves,0),16); + float nyquist=sample_spacing*2.0, wavelength=base_wavelength, final_weight=0.0, finest=0.0; int active=0; + [loop] for(int octave=0; octave=active) omitted+=amplitude; else if(index==active-1) omitted+=amplitude*(1.0-final_weight); amplitude*=abs(gain); + } + GameCultGeometryErosionBandSelection result; result.active_octaves=active; result.final_octave_weight=active>0?final_weight:0.0; result.finest_included_wavelength=finest; result.unresolved_height_bound=omitted; return result; +} + +float gamecult_geometry_erosion_hash_unit(int2 cell, uint salt) +{ + uint value = (uint)cell.x * 0x8da6b343u ^ (uint)cell.y * 0xd8163841u ^ salt; + value ^= value >> 16; value *= 0x7feb352du; value ^= value >> 15; value *= 0x846ca68bu; value ^= value >> 16; + return (value & 0x00ffffffu) * (1.0 / 16777216.0); +} +float2 gamecult_geometry_erosion_hash2(float2 input) +{ + int2 cell = (int2)input; + return float2(gamecult_geometry_erosion_hash_unit(cell, 0x68bc21ebu), gamecult_geometry_erosion_hash_unit(cell, 0x02e5be93u)) * 2.0 - 1.0; +} + +float4 gamecult_geometry_phacelle_noise(float2 position, float2 normal_direction, float frequency, float offset_cycles, float normalization) +{ + float2 side_direction = float2(-normal_direction.y, normal_direction.x) * frequency * 6.28318530718; + float offset = offset_cycles * 6.28318530718; + float2 cell = floor(position); + float2 local = frac(position); + float2 phase = 0.0; + float weight_sum = 0.0; + [unroll] for (int x = -1; x <= 2; x++) + [unroll] for (int y = -1; y <= 2; y++) + { + float2 grid_offset = float2(x, y); + float2 grid_point = cell + grid_offset; + float2 random_offset = gamecult_geometry_erosion_hash2(grid_point) * 0.5; + float2 delta = local - grid_offset - random_offset; + float weight = max(0.0, exp(-dot(delta, delta) * 2.0) - 0.01111); + weight_sum += weight; + float wave = dot(delta, side_direction) + offset; + phase += float2(cos(wave), sin(wave)) * weight; + } + float2 interpolated = phase / max(weight_sum, 1.0e-20); + float magnitude = max(1.0 - saturate(normalization), length(interpolated)); + return float4(interpolated / max(magnitude, 1.0e-20), side_direction); +} + +float gamecult_geometry_erosion_pow_inverse(float value, float power) { return 1.0 - pow(1.0 - saturate(value), power); } +float gamecult_geometry_erosion_ease_out(float value) { float inverse = 1.0 - saturate(value); return 1.0 - inverse * inverse; } +float gamecult_geometry_erosion_smooth_start(float value, float smoothing) +{ + return smoothing <= 1.0e-10 || value >= smoothing ? value - 0.5 * smoothing : 0.5 * value * value / smoothing; +} +float2 gamecult_geometry_erosion_safe_normalize(float2 value) +{ + float value_length = length(value); + return abs(value_length) > 1.0e-10 ? value / value_length : value; +} + +GameCultGeometryAdvancedErosionResult gamecult_geometry_advanced_erosion_filter_banded(float2 position, float3 base_height_and_slope, float fade_target, GameCultGeometryAdvancedErosionParameters p, GameCultGeometryErosionBandSelection band) +{ + float strength = p.strength * p.scale; + fade_target = clamp(fade_target, -1.0, 1.0); + float3 input_value = base_height_and_slope; + float3 height_and_slope = input_value; + float frequency = 1.0 / max(p.scale * p.cell_scale, 1.0e-10); + float2 slope = height_and_slope.yz; + float slope_length = max(length(slope), 1.0e-10); + float magnitude = 0.0; + float rounding_multiplier = 1.0; + float input_rounding = lerp(p.rounding.y, p.rounding.x, saturate(fade_target + 0.5)) * p.rounding.z; + float combined_mask = gamecult_geometry_erosion_ease_out(gamecult_geometry_erosion_smooth_start(slope_length * p.onset.x, input_rounding * p.onset.x)); + float ridge_mask = gamecult_geometry_erosion_ease_out(slope_length * p.onset.z); + float ridge_fade = fade_target; + float2 gully_slope = lerp(slope, slope / slope_length * p.assumed_slope.x, p.assumed_slope.y); + + int octave_count=min(min(max(p.octaves,0),8),max(band.active_octaves,0)); + [loop] for (int octave = 0; octave < octave_count; octave++) + { + float4 phacelle = gamecult_geometry_phacelle_noise(position * frequency, gamecult_geometry_erosion_safe_normalize(gully_slope), p.cell_scale, 0.25, p.normalization); + float2 derivative_direction = phacelle.zw * -frequency; + float sloping = abs(phacelle.y); + gully_slope += sign(phacelle.y) * derivative_direction * strength * p.gully_weight; + float3 gullies = float3(phacelle.x, phacelle.y * derivative_direction.x, phacelle.y * derivative_direction.y); + float3 faded = lerp(float3(fade_target, 0.0, 0.0), gullies * p.gully_weight, combined_mask); + float octave_weight=octave==octave_count-1?saturate(band.final_octave_weight):1.0; + height_and_slope += faded * strength * octave_weight; + magnitude += strength * octave_weight; + fade_target = faded.x; + float octave_rounding = lerp(p.rounding.y, p.rounding.x, saturate(phacelle.x + 0.5)) * rounding_multiplier; + float new_mask = gamecult_geometry_erosion_ease_out(gamecult_geometry_erosion_smooth_start(sloping * p.onset.y, octave_rounding * p.onset.y)); + combined_mask = gamecult_geometry_erosion_pow_inverse(combined_mask, p.detail) * new_mask; + ridge_fade = lerp(ridge_fade, lerp(ridge_fade, gullies.x, ridge_mask), octave_weight); + ridge_mask *= lerp(1.0, gamecult_geometry_erosion_ease_out(sloping * p.onset.w), octave_weight); + strength *= p.gain; + frequency *= p.lacunarity; + rounding_multiplier *= p.rounding.w; + } + + GameCultGeometryAdvancedErosionResult result; + result.delta = height_and_slope - input_value; + result.magnitude = magnitude; + result.ridge_map = ridge_fade * (1.0 - ridge_mask); + result.fade_target = fade_target; + return result; +} + +GameCultGeometryAdvancedErosionResult gamecult_geometry_advanced_erosion_filter(float2 position, float3 base_height_and_slope, float fade_target, GameCultGeometryAdvancedErosionParameters p) +{ + GameCultGeometryErosionBandSelection band; band.active_octaves=p.octaves; band.final_octave_weight=1.0; band.finest_included_wavelength=0.0; band.unresolved_height_bound=0.0; + return gamecult_geometry_advanced_erosion_filter_banded(position, base_height_and_slope, fade_target, p, band); +} + +#endif diff --git a/unity/org.gamecult.geometry/Shaders/AdvancedErosionFilter.hlsl.meta b/unity/org.gamecult.geometry/Shaders/AdvancedErosionFilter.hlsl.meta new file mode 100644 index 00000000..015c51c2 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/AdvancedErosionFilter.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3252ac02e8853ef4b9d1fbc5204b5c6b +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl b/unity/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl new file mode 100644 index 00000000..00623b75 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl @@ -0,0 +1,14 @@ +#ifndef GAMECULT_GEOMETRY_HLSL +#define GAMECULT_GEOMETRY_HLSL + +#if defined(GAMECULT_GEOMETRY_UNITY_PACKAGE) +#include "Packages/org.gamecult.cultmath/shaders/CultMath.hlsl" +#else +#include "CultMath/CultMath.hlsl" +#endif +#include "AdvancedErosionFilter.hlsl" +#include "Planetary.hlsl" +#include "SphericalErosion.hlsl" +#include "PlanetaryRadialRefinement.hlsl" + +#endif diff --git a/unity/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl.meta b/unity/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl.meta new file mode 100644 index 00000000..d685ccce --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b253fa50ddd34bf3b69fb323fe6e00e8 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Shaders/Planetary.hlsl b/unity/org.gamecult.geometry/Shaders/Planetary.hlsl new file mode 100644 index 00000000..8e16bfff --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/Planetary.hlsl @@ -0,0 +1,392 @@ +#ifndef GAMECULT_GEOMETRY_PLANETARY_HLSL +#define GAMECULT_GEOMETRY_PLANETARY_HLSL + +static const float GAMECULT_GEOMETRY_PLANETARY_QUARTER_PI = 0.78539816339744830962; + +float3 gamecult_geometry_planetary_face_direction(int face, float2 coordinate) +{ + float2 tangent = tan(coordinate * GAMECULT_GEOMETRY_PLANETARY_QUARTER_PI); + float3 cube = face == 0 ? float3(1.0, tangent.y, -tangent.x) + : face == 1 ? float3(-1.0, tangent.y, tangent.x) + : face == 2 ? float3(tangent.x, 1.0, -tangent.y) + : face == 3 ? float3(tangent.x, -1.0, tangent.y) + : face == 4 ? float3(tangent.x, tangent.y, 1.0) + : float3(-tangent.x, tangent.y, -1.0); + return cultmath_normalize(cube); +} + +float2 gamecult_geometry_planetary_face_coordinate(float3 direction, out int face) +{ + float3 d = cultmath_normalize(direction); + float3 a = abs(d); + float2 cube_uv; + if (a.x >= a.y && a.x >= a.z) + { + if (d.x >= 0.0) { face = 0; cube_uv = float2(-d.z, d.y) / a.x; } + else { face = 1; cube_uv = float2(d.z, d.y) / a.x; } + } + else if (a.y >= a.z) + { + if (d.y >= 0.0) { face = 2; cube_uv = float2(d.x, -d.z) / a.y; } + else { face = 3; cube_uv = float2(d.x, d.z) / a.y; } + } + else + { + if (d.z >= 0.0) { face = 4; cube_uv = d.xy / a.z; } + else { face = 5; cube_uv = float2(-d.x, d.y) / a.z; } + } + return atan(cube_uv) / GAMECULT_GEOMETRY_PLANETARY_QUARTER_PI; +} + +bool gamecult_geometry_planetary_page_local(float3 direction, float4 tile_address, float active, out float2 local) +{ + int face; + float2 uv = gamecult_geometry_planetary_face_coordinate(direction, face); + local = 0.0; + if (active < 0.5 || abs((float)face - tile_address.x) > 0.25) return false; + float axis_tiles = exp2(tile_address.y); + float2 scaled = (uv * 0.5 + 0.5) * axis_tiles; + float2 canonical = floor(min(scaled, axis_tiles - 1.0e-5)); + if (any(abs(canonical - tile_address.zw) > 0.25)) return false; + local = scaled - tile_address.zw; + return all(local >= 0.0) && all(local <= 1.0); +} + +float3 gamecult_geometry_planetary_surface_normal(float3 direction, float3 world_distance_tangent_gradient) +{ + direction = cultmath_normalize(direction); + float3 tangent_gradient = world_distance_tangent_gradient - direction * dot(world_distance_tangent_gradient, direction); + return cultmath_normalize(direction - tangent_gradient); +} + +float2 gamecult_geometry_planetary_equirectangular_forward(float3 direction) +{ + direction = cultmath_normalize(direction); + return float2(atan2(direction.y, direction.x) / CULTMATH_PI, asin(clamp(direction.z, -1.0, 1.0)) / CULTMATH_HALF_PI); +} + +float3 gamecult_geometry_planetary_equirectangular_inverse(float2 coordinate) +{ + float longitude = coordinate.x * CULTMATH_PI; + float latitude = coordinate.y * CULTMATH_HALF_PI; + float cos_latitude = cos(latitude); + return cultmath_normalize(float3(cos_latitude * cos(longitude), cos_latitude * sin(longitude), sin(latitude))); +} + +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_A1 = 1.340264; +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_A2 = -0.081106; +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_A3 = 0.000893; +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_A4 = 0.003796; +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_M = 0.8660254037844386; +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_X_MAX = 2.706629; +static const float GAMECULT_GEOMETRY_EQUAL_EARTH_Y_MAX = 1.3173628; + +float gamecult_geometry_equal_earth_y(float theta) +{ + float theta2 = theta * theta; + float theta6 = theta2 * theta2 * theta2; + return theta * (GAMECULT_GEOMETRY_EQUAL_EARTH_A1 + GAMECULT_GEOMETRY_EQUAL_EARTH_A2 * theta2 + theta6 * (GAMECULT_GEOMETRY_EQUAL_EARTH_A3 + GAMECULT_GEOMETRY_EQUAL_EARTH_A4 * theta2)); +} + +float2 gamecult_geometry_planetary_equal_earth_forward(float3 direction) +{ + direction = cultmath_normalize(direction); + float longitude = atan2(direction.y, direction.x); + float latitude = asin(clamp(direction.z, -1.0, 1.0)); + float theta = asin(GAMECULT_GEOMETRY_EQUAL_EARTH_M * sin(latitude)); + float theta2 = theta * theta; + float theta6 = theta2 * theta2 * theta2; + float denominator = GAMECULT_GEOMETRY_EQUAL_EARTH_M * (GAMECULT_GEOMETRY_EQUAL_EARTH_A1 + 3.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A2 * theta2 + theta6 * (7.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A3 + 9.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A4 * theta2)); + return float2(longitude * cos(theta) / denominator / GAMECULT_GEOMETRY_EQUAL_EARTH_X_MAX, gamecult_geometry_equal_earth_y(theta) / GAMECULT_GEOMETRY_EQUAL_EARTH_Y_MAX); +} + +float3 gamecult_geometry_planetary_equal_earth_inverse(float2 coordinate) +{ + float target_y = coordinate.y * GAMECULT_GEOMETRY_EQUAL_EARTH_Y_MAX; + float theta = target_y / GAMECULT_GEOMETRY_EQUAL_EARTH_A1; + [unroll] for (int iteration = 0; iteration < 8; iteration++) + { + float theta2 = theta * theta; + float theta6 = theta2 * theta2 * theta2; + float derivative = GAMECULT_GEOMETRY_EQUAL_EARTH_A1 + 3.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A2 * theta2 + theta6 * (7.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A3 + 9.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A4 * theta2); + theta -= (gamecult_geometry_equal_earth_y(theta) - target_y) / derivative; + } + float latitude = asin(clamp(sin(theta) / GAMECULT_GEOMETRY_EQUAL_EARTH_M, -1.0, 1.0)); + float theta2 = theta * theta; + float theta6 = theta2 * theta2 * theta2; + float denominator = GAMECULT_GEOMETRY_EQUAL_EARTH_M * (GAMECULT_GEOMETRY_EQUAL_EARTH_A1 + 3.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A2 * theta2 + theta6 * (7.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A3 + 9.0 * GAMECULT_GEOMETRY_EQUAL_EARTH_A4 * theta2)); + float longitude = coordinate.x * GAMECULT_GEOMETRY_EQUAL_EARTH_X_MAX * denominator / cos(theta); + float cos_latitude = cos(latitude); + return cultmath_normalize(float3(cos_latitude * cos(longitude), cos_latitude * sin(longitude), sin(latitude))); +} + +float gamecult_geometry_planetary_wrap_longitude(float longitude) +{ + longitude = fmod(longitude, CULTMATH_TAU); + if (longitude > CULTMATH_PI) longitude -= CULTMATH_TAU; + if (longitude < -CULTMATH_PI) longitude += CULTMATH_TAU; + return longitude; +} + +float3 gamecult_geometry_planetary_lon_lat_direction(float longitude, float latitude) +{ + float cos_latitude = cos(latitude); + return cultmath_normalize(float3(cos_latitude * cos(longitude), cos_latitude * sin(longitude), sin(latitude))); +} + +float gamecult_geometry_planetary_asinh(float value) { return log(value + sqrt(value * value + 1.0)); } +float gamecult_geometry_planetary_sinh(float value) { return 0.5 * (exp(value) - exp(-value)); } + +bool gamecult_geometry_planetary_azimuthal_forward(float3 direction, float2 center_lon_lat, int kind, out float2 coordinate) +{ + direction = cultmath_normalize(direction); + float longitude = gamecult_geometry_planetary_wrap_longitude(atan2(direction.y, direction.x) - center_lon_lat.x); + float latitude = asin(clamp(direction.z, -1.0, 1.0)); + float sin0 = sin(center_lon_lat.y), cos0 = cos(center_lon_lat.y); + float sin_latitude = sin(latitude), cos_latitude = cos(latitude), cos_longitude = cos(longitude); + float cos_c = clamp(sin0 * sin_latitude + cos0 * cos_latitude * cos_longitude, -1.0, 1.0); + float2 raw = float2(cos_latitude * sin(longitude), cos0 * sin_latitude - sin0 * cos_latitude * cos_longitude); + float factor = 1.0; + if (kind == 3) + { + if (cos_c < 0.0) { coordinate = 0.0; return false; } + } + else if (kind == 4) + { + float c = acos(cos_c); factor = (c < 1.0e-6 ? 1.0 : c / sin(c)) / CULTMATH_PI; + } + else if (kind == 5) factor = sqrt(2.0 / max(1.0 + cos_c, 1.0e-20)) * 0.5; + else + { + if (cos_c <= 0.0) { coordinate = 0.0; return false; } + factor = 1.0 / cos_c; + } + coordinate = raw * factor; + return true; +} + +bool gamecult_geometry_planetary_azimuthal_inverse(float2 coordinate, float2 center_lon_lat, int kind, out float3 direction) +{ + float rho = length(coordinate); + float c; + if (kind == 3) { if (rho > 1.0) { direction = 0.0; return false; } c = asin(clamp(rho, 0.0, 1.0)); } + else if (kind == 4) { if (rho > 1.0) { direction = 0.0; return false; } c = rho * CULTMATH_PI; } + else if (kind == 5) { if (rho > 1.0) { direction = 0.0; return false; } c = 2.0 * asin(clamp(rho, 0.0, 1.0)); } + else c = atan(rho); + if (rho < 1.0e-6) { direction = gamecult_geometry_planetary_lon_lat_direction(center_lon_lat.x, center_lon_lat.y); return true; } + float sin_c = sin(c), cos_c = cos(c), sin0 = sin(center_lon_lat.y), cos0 = cos(center_lon_lat.y); + float latitude = asin(clamp(cos_c * sin0 + coordinate.y * sin_c * cos0 / rho, -1.0, 1.0)); + float delta_longitude = atan2(coordinate.x * sin_c, rho * cos0 * cos_c - coordinate.y * sin0 * sin_c); + direction = gamecult_geometry_planetary_lon_lat_direction(gamecult_geometry_planetary_wrap_longitude(delta_longitude + center_lon_lat.x), latitude); + return true; +} + +bool gamecult_geometry_planetary_cube_atlas_forward(float3 direction, out float2 coordinate) +{ + int face; + float2 face_coordinate = gamecult_geometry_planetary_face_coordinate(direction, face); + int column = face % 3; + int row = face / 3; + coordinate = float2(-1.0 + (column + (face_coordinate.x + 1.0) * 0.5) * (2.0 / 3.0), 1.0 - (row + (face_coordinate.y + 1.0) * 0.5)); + return true; +} + +bool gamecult_geometry_planetary_cube_atlas_inverse(float2 coordinate, out float3 direction) +{ + if (any(coordinate < -1.0) || any(coordinate > 1.0)) { direction = 0.0; return false; } + int column = min((int)((coordinate.x + 1.0) * 1.5), 2); + int row = min((int)(1.0 - coordinate.y), 1); + float u = ((coordinate.x + 1.0) * 1.5 - column) * 2.0 - 1.0; + float v = ((1.0 - coordinate.y) - row) * 2.0 - 1.0; + direction = gamecult_geometry_planetary_face_direction(row * 3 + column, float2(u, v)); + return true; +} + +bool gamecult_geometry_planetary_projection_forward(float3 direction, int kind, float3 center_lon_lat_scale, out float2 coordinate) +{ + float scale = max(center_lon_lat_scale.z, 1.0e-20); + if (kind == 6) + { + bool valid = gamecult_geometry_planetary_cube_atlas_forward(direction, coordinate); + coordinate /= scale; return valid; + } + if (kind >= 3) + { + bool valid = gamecult_geometry_planetary_azimuthal_forward(direction, center_lon_lat_scale.xy, kind, coordinate); + coordinate /= scale; return valid; + } + direction = cultmath_normalize(direction); + float longitude = gamecult_geometry_planetary_wrap_longitude(atan2(direction.y, direction.x) - center_lon_lat_scale.x); + float latitude = asin(clamp(direction.z, -1.0, 1.0)); + if (kind == 0) coordinate = float2(longitude / CULTMATH_PI, latitude / CULTMATH_HALF_PI); + else if (kind == 1) + { + const float maximum_latitude = 1.4844222297453324; + if (abs(latitude) > maximum_latitude) { coordinate = 0.0; return false; } + coordinate = float2(longitude / CULTMATH_PI, gamecult_geometry_planetary_asinh(tan(latitude)) / CULTMATH_PI); + } + else + { + float3 centered = gamecult_geometry_planetary_lon_lat_direction(longitude, latitude); + coordinate = gamecult_geometry_planetary_equal_earth_forward(centered); + } + coordinate /= scale; + return true; +} + +bool gamecult_geometry_planetary_projection_inverse(float2 coordinate, int kind, float3 center_lon_lat_scale, out float3 direction) +{ + coordinate *= max(center_lon_lat_scale.z, 1.0e-20); + if (kind == 6) return gamecult_geometry_planetary_cube_atlas_inverse(coordinate, direction); + if (kind >= 3) return gamecult_geometry_planetary_azimuthal_inverse(coordinate, center_lon_lat_scale.xy, kind, direction); + if (any(abs(coordinate) > 1.0)) { direction = 0.0; return false; } + float longitude, latitude; + if (kind == 0) { longitude = coordinate.x * CULTMATH_PI; latitude = coordinate.y * CULTMATH_HALF_PI; } + else if (kind == 1) { longitude = coordinate.x * CULTMATH_PI; latitude = atan(gamecult_geometry_planetary_sinh(coordinate.y * CULTMATH_PI)); } + else + { + float3 centered = gamecult_geometry_planetary_equal_earth_inverse(coordinate); + longitude = atan2(centered.y, centered.x); latitude = asin(clamp(centered.z, -1.0, 1.0)); + } + direction = gamecult_geometry_planetary_lon_lat_direction(gamecult_geometry_planetary_wrap_longitude(longitude + center_lon_lat_scale.x), latitude); + return true; +} + +struct GameCultGeometryPlanetaryFieldDefinition +{ + float radius; + int seed; + GameCultGeometryAdvancedErosionParameters erosion; +}; + +struct GameCultGeometryPlanetaryBaseFieldSample +{ + float radial_displacement; + float3 radial_gradient; + float field_value; + float3 field_gradient; + float fade_target; +}; + +struct GameCultGeometryPlanetarySurfaceSample +{ + float radial_displacement; + float3 tangent_gradient; + float3 surface_normal; + float slope; + float ridge; + float gully; + float finest_resolved_wavelength; + float unresolved_height_bound; +}; + +struct GameCultGeometryPlanetaryPageSample +{ + float4 height_gradient; + float2 masks; +}; + +GameCultGeometryPlanetaryPageSample gamecult_geometry_planetary_page_lerp( + GameCultGeometryPlanetaryPageSample a, + GameCultGeometryPlanetaryPageSample b, + GameCultGeometryPlanetaryPageSample c, + GameCultGeometryPlanetaryPageSample d, + float2 fraction) +{ + GameCultGeometryPlanetaryPageSample result; + result.height_gradient = lerp(lerp(a.height_gradient, b.height_gradient, fraction.x), lerp(c.height_gradient, d.height_gradient, fraction.x), fraction.y); + result.masks = lerp(lerp(a.masks, b.masks, fraction.x), lerp(c.masks, d.masks, fraction.x), fraction.y); + return result; +} + +GameCultGeometryPlanetaryPageSample gamecult_geometry_planetary_residual_sample( + GameCultGeometryPlanetarySurfaceSample child, + GameCultGeometryPlanetarySurfaceSample parent, + bool has_parent) +{ + GameCultGeometryPlanetaryPageSample result; + result.height_gradient = float4(child.radial_displacement, child.tangent_gradient); + if (has_parent) result.height_gradient -= float4(parent.radial_displacement, parent.tangent_gradient); + result.masks = float2(child.ridge, child.gully); + return result; +} + +float4 gamecult_geometry_planetary_summary_empty() +{ + return float4(3.402823466e+38, -3.402823466e+38, 0.0, 0.0); +} + +float4 gamecult_geometry_planetary_summary_accumulate(float4 summary, float4 height_gradient, float unresolved_height) +{ + summary.x = min(summary.x, height_gradient.x); + summary.y = max(summary.y, height_gradient.x); + summary.z = max(summary.z, length(height_gradient.yzw)); + summary.w = max(summary.w, max(unresolved_height, 0.0)); + return summary; +} + +float gamecult_geometry_planetary_pow4(float value) { float square = value * value; return square * square; } + +GameCultGeometryPlanetarySurfaceSample gamecult_geometry_planetary_field_sample( + GameCultGeometryPlanetaryFieldDefinition definition, + float3 unit_direction, + GameCultGeometryPlanetaryBaseFieldSample base_sample, + float footprint_meters) +{ + float3 direction = cultmath_normalize(unit_direction); + float3 radial_gradient = base_sample.radial_gradient - direction * dot(base_sample.radial_gradient, direction); + float3 field_gradient = base_sample.field_gradient - direction * dot(base_sample.field_gradient, direction); + GameCultGeometryAdvancedErosionParameters p = definition.erosion; + GameCultGeometryErosionBandSelection band = gamecult_geometry_select_erosion_bands( + p.scale * p.cell_scale, footprint_meters, p.octaves, p.lacunarity, + p.strength * p.scale, p.gain, 2.0); + float3 powers = float3( + gamecult_geometry_planetary_pow4(abs(direction.x)), + gamecult_geometry_planetary_pow4(abs(direction.y)), + gamecult_geometry_planetary_pow4(abs(direction.z))); + float power_sum = max(powers.x + powers.y + powers.z, 1.0e-6); + float3 weights = powers / power_sum; + float3 world = direction * definition.radius; + float seed = (float)definition.seed; + GameCultGeometryAdvancedErosionResult xy = gamecult_geometry_advanced_erosion_filter_banded( + world.xy + float2(713.0 + seed * 0.754877666, -291.0 + seed * 0.569840296), + float3(base_sample.field_value, field_gradient.x, field_gradient.y), base_sample.fade_target, p, band); + GameCultGeometryAdvancedErosionResult yz = gamecult_geometry_advanced_erosion_filter_banded( + world.yz + float2(-431.0 + seed * 0.438289027, 887.0 + seed * 0.328438163), + float3(base_sample.field_value, field_gradient.y, field_gradient.z), base_sample.fade_target, p, band); + GameCultGeometryAdvancedErosionResult zx = gamecult_geometry_advanced_erosion_filter_banded( + world.zx + float2(197.0 + seed * 0.219783071, 557.0 + seed * 0.145898034), + float3(base_sample.field_value, field_gradient.z, field_gradient.x), base_sample.fade_target, p, band); + float erosion_height = xy.delta.x * weights.z + yz.delta.x * weights.x + zx.delta.x * weights.y; + float3 erosion_gradient = float3( + xy.delta.y * weights.z + zx.delta.z * weights.y, + xy.delta.z * weights.z + yz.delta.y * weights.x, + yz.delta.z * weights.x + zx.delta.y * weights.y); + float3 power_gradient_x = float3(4.0 * direction.x * direction.x * direction.x / definition.radius, 0.0, 0.0); + float3 power_gradient_y = float3(0.0, 4.0 * direction.y * direction.y * direction.y / definition.radius, 0.0); + float3 power_gradient_z = float3(0.0, 0.0, 4.0 * direction.z * direction.z * direction.z / definition.radius); + power_gradient_x -= direction * dot(power_gradient_x, direction); + power_gradient_y -= direction * dot(power_gradient_y, direction); + power_gradient_z -= direction * dot(power_gradient_z, direction); + float3 power_sum_gradient = power_gradient_x + power_gradient_y + power_gradient_z; + float inverse_power_sum_squared = 1.0 / (power_sum * power_sum); + float3 weight_gradient_x = (power_gradient_x * power_sum - power_sum_gradient * powers.x) * inverse_power_sum_squared; + float3 weight_gradient_y = (power_gradient_y * power_sum - power_sum_gradient * powers.y) * inverse_power_sum_squared; + float3 weight_gradient_z = (power_gradient_z * power_sum - power_sum_gradient * powers.z) * inverse_power_sum_squared; + erosion_gradient += yz.delta.x * weight_gradient_x + zx.delta.x * weight_gradient_y + xy.delta.x * weight_gradient_z; + float3 gradient = radial_gradient + erosion_gradient; + gradient -= direction * dot(gradient, direction); + float ridge_evidence = xy.ridge_map * weights.z + yz.ridge_map * weights.x + zx.ridge_map * weights.y; + GameCultGeometryPlanetarySurfaceSample result; + result.radial_displacement = base_sample.radial_displacement + erosion_height; + result.tangent_gradient = gradient; + result.surface_normal = gamecult_geometry_planetary_surface_normal(direction, gradient); + result.slope = length(gradient); + result.ridge = saturate(ridge_evidence * 0.5 + 0.5); + result.gully = saturate(0.5 - ridge_evidence * 0.5); + result.finest_resolved_wavelength = band.finest_included_wavelength; + result.unresolved_height_bound = band.unresolved_height_bound; + return result; +} + +#endif diff --git a/unity/org.gamecult.geometry/Shaders/Planetary.hlsl.meta b/unity/org.gamecult.geometry/Shaders/Planetary.hlsl.meta new file mode 100644 index 00000000..009c2989 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/Planetary.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: bb055ad5328888e479e90ae3fe62c3fe +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Shaders/PlanetaryRadialRefinement.hlsl b/unity/org.gamecult.geometry/Shaders/PlanetaryRadialRefinement.hlsl new file mode 100644 index 00000000..20f3e2e5 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/PlanetaryRadialRefinement.hlsl @@ -0,0 +1,27 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +#ifndef GAMECULT_GEOMETRY_PLANETARY_RADIAL_REFINEMENT_HLSL +#define GAMECULT_GEOMETRY_PLANETARY_RADIAL_REFINEMENT_HLSL + +float3 gamecult_geometry_planetary_radial_refinement_step( + float3 position, + float3 ray_direction, + float3 center, + float target_radius, + float minimum_derivative, + float maximum_correction) +{ + float3 local=position-center; + float radius=length(local); + float3 radial=local/max(radius,0.0001); + float error=radius-target_radius; + float derivative=dot(ray_direction,radial); + float safe_minimum=max(minimum_derivative,0.0001); + float safe_derivative=abs(derivative)>safe_minimum?derivative:(derivative<0?-safe_minimum:safe_minimum); + float correction=clamp(-error/safe_derivative,-abs(maximum_correction),abs(maximum_correction)); + return position+ray_direction*correction; +} + +#endif diff --git a/unity/org.gamecult.geometry/Shaders/PlanetaryRadialRefinement.hlsl.meta b/unity/org.gamecult.geometry/Shaders/PlanetaryRadialRefinement.hlsl.meta new file mode 100644 index 00000000..28d629b1 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/PlanetaryRadialRefinement.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 356389371b854e12b9d419f7ee310d81 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/Shaders/SphericalErosion.hlsl b/unity/org.gamecult.geometry/Shaders/SphericalErosion.hlsl new file mode 100644 index 00000000..107ba24b --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/SphericalErosion.hlsl @@ -0,0 +1,80 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +#ifndef GAMECULT_GEOMETRY_SPHERICAL_EROSION_HLSL +#define GAMECULT_GEOMETRY_SPHERICAL_EROSION_HLSL + +struct GameCultGeometrySphericalErosionParameters +{ + float frequency; float amplitude; float lacunarity; float gain; + float slope_strength; float detail; float seed; int octaves; +}; + +struct GameCultGeometrySphericalErosionSample +{ + float height_offset; float ridge; float gully; float3 gradient; +}; + +float gamecult_geometry_spherical_cell_wave(float3 position, float3 flow, float3 cell, float seed) +{ + float3 pivot = cell + float3(cultmath_hash(cell + seed + 11.7), cultmath_hash(cell.yzx + seed + 37.1), cultmath_hash(cell.zyx + seed + 73.9)); + return dot(position - pivot, flow) * 6.28318530718 + cultmath_hash(cell + seed + 101.3) * 6.28318530718; +} + +float gamecult_geometry_spherical_directional_wave(float3 position, float3 flow, float seed) +{ + float3 cell = floor(position); + float3 local = cultmath_frac(position); + float3 weights = local * local * (3.0 - 2.0 * local); + float z0 = cultmath_lerp( + cultmath_lerp(gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(0,0,0), seed), gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(1,0,0), seed), weights.x), + cultmath_lerp(gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(0,1,0), seed), gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(1,1,0), seed), weights.x), weights.y); + float z1 = cultmath_lerp( + cultmath_lerp(gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(0,0,1), seed), gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(1,0,1), seed), weights.x), + cultmath_lerp(gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(0,1,1), seed), gamecult_geometry_spherical_cell_wave(position, flow, cell + float3(1,1,1), seed), weights.x), weights.y); + return cultmath_lerp(z0, z1, weights.z); +} + +float3 gamecult_geometry_spherical_stable_tangent(float3 position) +{ + float3 axis = abs(position.z) < 0.8 ? float3(0,0,1) : float3(0,1,0); + return cultmath_normalize(cross(axis, position)); +} + +GameCultGeometrySphericalErosionSample gamecult_geometry_spherical_erosion(float3 unit_position, float base_height, float3 base_gradient, GameCultGeometrySphericalErosionParameters p) +{ + float3 position = cultmath_normalize(unit_position); + float3 gradient = base_gradient - position * dot(base_gradient, position); + float slope = length(gradient); + float3 flow = slope > 1.0e-6 ? -gradient / slope : gamecult_geometry_spherical_stable_tangent(position); + float frequency = max(p.frequency, 0.001); + float amplitude = p.amplitude; + float combi_mask = 1.0 - pow(1.0 - cultmath_saturate(slope * p.slope_strength), 2.0); + float fade_target = cultmath_saturate(base_height * 0.5 + 0.5) * 2.0 - 1.0; + GameCultGeometrySphericalErosionSample result = (GameCultGeometrySphericalErosionSample)0; + [loop] for (int octave = 0; octave < min(max(p.octaves, 0), 8); octave++) + { + float phase = gamecult_geometry_spherical_directional_wave(position * frequency, flow, p.seed + octave * 19.19); + float wave = cos(phase); + float wave_slope = sin(phase); + float faded = cultmath_lerp(fade_target, wave, combi_mask); + result.height_offset += faded * amplitude; + result.ridge = max(result.ridge, cultmath_saturate(faded)); + result.gully = max(result.gully, cultmath_saturate(-faded)); + float3 across = cultmath_normalize(cross(position, flow)); + gradient += across * (sign(wave_slope) * amplitude * frequency * combi_mask); + gradient -= position * dot(gradient, position); + slope = length(gradient); + flow = slope > 1.0e-6 ? -gradient / slope : gamecult_geometry_spherical_stable_tangent(position); + float new_mask = 1.0 - pow(1.0 - cultmath_saturate(abs(wave_slope) * 1.35), 2.0); + combi_mask = (1.0 - pow(1.0 - cultmath_saturate(combi_mask), max(p.detail, 0.001))) * new_mask; + fade_target = faded; + frequency *= max(p.lacunarity, 1.01); + amplitude *= cultmath_saturate(p.gain); + } + result.gradient = gradient; + return result; +} + +#endif diff --git a/unity/org.gamecult.geometry/Shaders/SphericalErosion.hlsl.meta b/unity/org.gamecult.geometry/Shaders/SphericalErosion.hlsl.meta new file mode 100644 index 00000000..7b7ddc79 --- /dev/null +++ b/unity/org.gamecult.geometry/Shaders/SphericalErosion.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 43d458fc5b6b441a941e37001df919e3 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/org.gamecult.geometry/THIRD-PARTY-NOTICES.md b/unity/org.gamecult.geometry/THIRD-PARTY-NOTICES.md new file mode 100644 index 00000000..3388b601 --- /dev/null +++ b/unity/org.gamecult.geometry/THIRD-PARTY-NOTICES.md @@ -0,0 +1,24 @@ +# GameCult.Geometry source provenance + +## CultMath planetary geometry + +The C# files under `Planetary/` and the HLSL files under `Shaders/` are licensed +under the Mozilla Public License 2.0 and retain MPL-2.0 notices. CultMath owns +the numeric primitives used by these geometry systems; GameCult.Geometry owns +their spatial meaning. + +## Advanced Terrain Erosion Filter + +`Planetary/AdvancedErosionFilter.cs` and `Shaders/AdvancedErosionFilter.hlsl` +adapt the mathematical procedure from +Rune Skovbo Johansen's **Advanced Terrain Erosion Filter**. + +- Copyright 2025 Rune Skovbo Johansen. +- Upstream: +- Explanation: +- License: Mozilla Public License 2.0, + +The implementation includes C#/HLSL type lowering, bounded octave counts, +finite zero-rounding behavior, and a shared integer avalanche hash for CPU/GPU +cell-selection parity. The technique builds on earlier work credited upstream +to Fewes and Clay John; the hash form follows public shader math by Inigo Quilez.