@@ -16,8 +16,6 @@ import Ghengin.Core.Type.Compatible
1616import Data.List (foldl' )
1717import Ghengin.Core.Log
1818
19- import GHC.Float
20- import Numeric.Noise hiding (Noise )
2119import qualified Data.List.NonEmpty as NE
2220import qualified Data.Vector as V
2321
@@ -36,6 +34,8 @@ import qualified Math.Linear as FIR
3634
3735import Ghengin.Core.Shader.Data
3836
37+ import Planet.Noise
38+
3939--------------------------------------------------------------------------------
4040-- * Planet
4141--------------------------------------------------------------------------------
@@ -63,69 +63,48 @@ data PlanetShape = PlanetShape
6363 }
6464
6565-- | Make the point on a planet for the given point on a unit sphere
66- pointOnPlanet :: PlanetShape -> Vec3 -> Vec3
66+ --
67+ -- Returns the updated point and the elevation of that point
68+ pointOnPlanet :: PlanetShape -> Vec3 -> (Vec3 , Float )
6769pointOnPlanet PlanetShape {.. } pointOnUnitSphere =
6870 let elevation = evalNoise planetNoise pointOnUnitSphere
69- in pointOnUnitSphere ^* planetRadius ^* (1 + elevation)
71+ finalElevation = planetRadius * (1 + elevation)
72+ in (pointOnUnitSphere ^* finalElevation, finalElevation)
7073
74+ -- | Construct the planet mesh and return the minimum and maximum elevation points on the planet
7175newPlanetMesh :: _ -- more constraints
7276 => CompatibleVertex '[Vec3 , Vec3 ] π
7377 => CompatibleMesh '[Transform ] π
7478 => RenderPipeline π bs
7579 ⊸ Planet
76- -> Core (PlanetMesh , RenderPipeline π bs )
80+ -> Core (( PlanetMesh , RenderPipeline π bs ), Ur MinMax )
7781newPlanetMesh rp Planet {.. } = enterD " newPlanetMesh" $ Linear. do
7882
7983 let UnitSphere us is = newUnitSphere resolution
8084
81- planetVxs = P. map (\ (p :&: n) -> pointOnPlanet planetShape p :&: n) us
82-
83- -- (ps', elevations) = P.unzip $ (`map` vs) \(p :& _) ->
84- -- case nss of
85- -- ns NE.:| nss' ->
86- -- let initialElevation = evalNoise ns p
87- -- mask = if enableMask then initialElevation else 1
88- -- noiseElevation = foldl' (\acc ns' -> acc + (evalNoise ns' p)*mask) initialElevation nss'
89- -- elevation = ra * (1 + noiseElevation)
90- -- in (p ^* (elevation), elevation)
91- --
92- -- ns' = V.toList $ computeNormals (V.fromList (P.map fromIntegral is)) (V.fromList ps')
93- -- vs'' = P.zipWith3 (\a b c -> a :& b :&: c) ps' ns' (map (\(_ :& _ :&: c) -> c) vs)
85+ (planetPs, elevations)
86+ = P. unzip $ P. map (\ (p :&: _) -> pointOnPlanet planetShape p) us
87+ planetNs = V. toList $ computeNormals (V. fromList (P. map fromIntegral is)) (V. fromList planetPs)
88+ planetVs = P. zipWith (:&:) planetPs planetNs
9489
95- -- minmax = MinMax (P.minimum elevations) (P.maximum elevations)
90+ minmax = MinMax (P. minimum elevations) (P. maximum elevations)
9691
97- in (createMeshWithIxs rp (DynamicBinding (Ur mempty ) :## GHNil ) planetVxs is ↑ )
92+ in (, Ur minmax) <$> ( createMeshWithIxs rp (DynamicBinding (Ur mempty ) :## GHNil ) planetVs is ↑ )
9893
9994--------------------------------------------------------------------------------
10095-- * Material
10196--------------------------------------------------------------------------------
10297
98+ type PlanetMaterial = Material '[MinMax , Texture2D (RGBA8 UNorm )]
99+
103100newPlanetMaterial :: forall π p
104- . CompatibleMaterial '[MinMax ,Texture2D ] π
101+ . CompatibleMaterial '[MinMax , Texture2D ( RGBA8 UNorm ) ] π
105102 => MinMax
106- -> Alias Texture2D
103+ -> Alias ( Texture2D ( RGBA8 UNorm ))
107104 ⊸ RenderPipeline π p
108- ⊸ Core (Material '[ MinMax , Texture2D ] , RenderPipeline π p )
105+ ⊸ Core (PlanetMaterial , RenderPipeline π p )
109106newPlanetMaterial mm t pl = ( material @ _ @ π (StaticBinding (Ur mm) :## Texture2DBinding t :## GHNil ) pl ↑ )
110107
111- --------------------------------------------------------------------------------
112- -- * Noise
113- --------------------------------------------------------------------------------
114-
115- data Noise = CoherentNoise
116- { centre :: ! Vec3
117- , roughness :: ! Float
118- , strength :: ! Float
119- }
120-
121- evalNoise :: Noise -> Vec3 -> Float
122- evalNoise CoherentNoise {.. } point = double2Float
123- let seed = 123456
124- WithVec3 px py pz = point ^* roughness ^+^ centre
125- noiseValue = coherentNoise seed (float2Double px, float2Double py, float2Double pz)
126- noiseScaled = (noiseValue + 1 ) * 0.5 {- from [-1 to 1] to [0 to 1] -}
127- in noiseScaled * float2Double strength
128-
129108--------------------------------------------------------------------------------
130109
131110-- non-compositional instance for "Transform", just for demo
0 commit comments