packages feed

waterfall-cad-0.9.0.0: src/Waterfall/Paint.hs

{-# LANGUAGE DerivingVia, DeriveGeneric #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE RankNTypes #-}
{-|
Module: Waterfall.Paint

`Paint` can be applied to change the appearance of the surface of a `Waterfall.Solid.Solid` (with `Waterfall.Solid.paintSolid`).

`Paint` values can be constructed from the @paintWith*@ functions and can be combined monoidally (with `<>`), e.g.

@
redShinyPaint =
    paintWithColour (Colour 1 0 0)
    <> paintWithRoughness 0.01
    <> paintWithMetallic 1
    <> paintWithDoubleSidedness SingleSided
@

Not all `Paint` properties are supported by every file format:

* glTF uses the full set
* OBJ uses the colour, opacity, and roughness (as part of the specular component)
* STEP only uses the colour
* SVG uses colour and opacity
* STL ignores the `Paint` properties completely
-}
module Waterfall.Paint 
( Colour (..)
, Paint
, DoubleSidedness (..)
, defaultPaint
, paintIsDefined
, mixPaints
, mixPaintsWithColours
-- * Constructors
, paintWithColour
, paintWithOpacity
, paintWithMetallic
, paintWithRoughness
, paintWithEmissiveColour
, paintWithName
, paintWithDoubleSidedness
-- * Optics
, paintColour
, paintOpacity
, paintMetallic
, paintRoughness
, paintEmissiveColour
, paintName
, paintDoubleSidedness
) where
import Data.Monoid (Last (..), Endo (..))
import GHC.Generics (Generically (..), Generic)
import Data.Maybe (catMaybes, mapMaybe, isNothing, fromMaybe)
import Data.List (intersperse, intercalate)
import Control.Lens (Lens', lens, (^.), (.~), (&))
import Waterfall.Colour (Colour (..), mixColours)
import qualified Waterfall.Colour as Colour
import Data.List.NonEmpty (NonEmpty, toList)
import qualified Data.List.NonEmpty as NE
import Control.DeepSeq (NFData (rnf), rwhnf)

-- | Whether to display a surface as Double or Single Sided
--
-- In most formats and viewers this will be implemented using backface culling 
data DoubleSidedness = SingleSided | DoubleSided 
    deriving (Eq, Ord, Show) 

instance NFData DoubleSidedness where rnf = rwhnf

-- | Can be applied to change the appearance of the surface of a `Waterfall.Solid.Solid`
-- 
-- `Paint` values can be constructed from the @paintWith*@ functions
-- and can be combined monoidally (with `<>`).
data Paint = Paint 
    { _paintColour :: Last Colour
    , _paintOpacity :: Last Double
    , _paintMetallic :: Last Double
    , _paintRoughness :: Last Double
    , _paintEmissiveColour :: Last Colour
    , _paintName :: Last String
    , _paintDoubleSidedness :: Last DoubleSidedness
    } deriving (Eq, Ord, Generic)
    deriving 
        ( Semigroup -- ^ `(<>)` takes the rightmost defined value of each property.
        , Monoid
        ) via (Generically Paint)

instance NFData Paint

instance Show Paint where
  showsPrec :: Int -> Paint -> ShowS
  showsPrec d p = 
    let term name field = 
            let fv v = 
                    showString name
                     . showChar ' '
                     . showsPrec 11 v 
            in fv <$> getLast (field p)
        terms = catMaybes 
            [ term "paintWithColour" _paintColour
            , term "paintWithOpacity" _paintOpacity
            , term "paintWithMetallic" _paintMetallic
            , term "paintWithRoughness" _paintRoughness
            , term "paintWithEmissiveColour" _paintEmissiveColour
            , term "paintWithName" _paintName
            , term "paintWithDoubleSidedness" _paintDoubleSidedness
            ]
    in case terms of
        [] -> showString "mempty"
        _ -> showParen (d > 6) . appEndo
            . foldMap Endo
            . intersperse (showString " <> ") 
            $ terms

-- | Fallback values used when a paint is unset
--
-- @
-- defaultPaint /= mempty
-- @
defaultPaint :: Paint
defaultPaint = paintWithColour (Colour.white)
    <> paintWithOpacity 1 
    <> paintWithDoubleSidedness DoubleSided
    <> paintWithMetallic 0
    <> paintWithRoughness 0.5

-- | Test whether a paint is storing any values
paintIsDefined :: Paint -> Bool
paintIsDefined = (/= mempty)


average :: [Double] -> Maybe Double
average [] = Nothing
average s = Just (sum s / realToFrac (length s))

mixValue :: Lens' Paint (Maybe Double) -> NonEmpty Paint -> Paint
mixValue l paints = 
    let value = paints
            & toList
            & mapMaybe (^. l)
            & average
    in mempty & l .~ value

    
mixColoursWith
    :: (NonEmpty Colour -> Colour) 
    -> Lens' Paint (Maybe Colour) 
    -> NonEmpty Paint
    -> Paint
mixColoursWith f l paints = 
    let definedColours = 
            NE.nonEmpty . mapMaybe (^. l) . toList $ paints 
    in mempty & l .~ (f <$> definedColours)

mixNames :: NonEmpty Paint -> Paint
mixNames paints = 
    if all (isNothing . (^. paintName)) paints
        then mempty
        else 
            let name = 
                    intercalate " plus " 
                        $ toList 
                        (fromMaybe "unnamed-paint" . (^. paintName)
                            <$> paints
                        )
            in paintWithName name

mixDoubleSidedness :: NonEmpty Paint -> Paint
mixDoubleSidedness paints = 
    mempty 
        & paintDoubleSidedness .~ 
            (maximum . fmap (^. paintDoubleSidedness) $ paints)

-- | A version of `mixPaints` that exposes how to average `Colour`s
mixPaintsWithColours 
    :: (NonEmpty Colour -> Colour)
    -> NonEmpty Paint 
    -> Paint
mixPaintsWithColours colourFn paints = 
        mixColoursWith colourFn paintColour paints
        <> mixColoursWith colourFn paintEmissiveColour paints
        <> mixValue paintOpacity paints
        <> mixValue paintMetallic paints
        <> mixValue paintRoughness paints
        <> mixNames paints
        <> mixDoubleSidedness paints

-- | Compute an average from a list of `Paint`s 
-- 
-- Undefined values in the `Paint`s do not contribute to the average (although they do affect the name, if any of the `Paint`s have a name set).
mixPaints :: NonEmpty Paint -> Paint
mixPaints = mixPaintsWithColours mixColours

-- | The paint colour
paintColour :: Lens' Paint (Maybe Colour)
paintColour = 
    lens
        (getLast . _paintColour) 
        (\p c -> p { _paintColour = Last c })

-- | Create a paint with a given colour
paintWithColour :: Colour -> Paint
paintWithColour c = mempty { _paintColour = Last (Just c) }

-- | The paint opacity
paintOpacity :: Lens' Paint (Maybe Double)
paintOpacity = 
    lens
        (getLast . _paintOpacity) 
        (\p c -> p { _paintOpacity = Last c })

-- | Create a paint with a given opacity
--
-- Opacity is in the range [0, 1]
paintWithOpacity :: Double -> Paint
paintWithOpacity o = mempty { _paintOpacity = Last (Just o) }

-- | The paint "Metalness" value, in a [PBR](https://en.wikipedia.org/wiki/Physically_based_rendering) model.
paintMetallic :: Lens' Paint (Maybe Double)
paintMetallic = 
    lens
        (getLast . _paintMetallic) 
        (\p c -> p { _paintMetallic = Last c })

-- | Create a paint with a given "Metalness"
--
-- In the range [0, 1], 1 is fully metal.
paintWithMetallic :: Double -> Paint
paintWithMetallic m = mempty { _paintMetallic = Last (Just m) }

-- | The paint "Roughness" value, in a [PBR](https://en.wikipedia.org/wiki/Physically_based_rendering) model.
paintRoughness :: Lens' Paint (Maybe Double)
paintRoughness = 
    lens
        (getLast . _paintRoughness) 
        (\p c -> p { _paintRoughness = Last c })

-- | Create a paint with a given roughness
-- 
-- In the range [0, 1], 0 is perfectly smooth, 1 fully diffuse
paintWithRoughness :: Double -> Paint
paintWithRoughness r = mempty { _paintRoughness= Last (Just r) }

-- | The paint "Emissive" factor, in a [PBR](https://en.wikipedia.org/wiki/Physically_based_rendering) model.
--
-- EmissiveColour is converted from sRGB to linear RGB when written.
paintEmissiveColour :: Lens' Paint (Maybe Colour)
paintEmissiveColour = 
    lens
        (getLast . _paintEmissiveColour) 
        (\p c -> p { _paintEmissiveColour = Last c })

-- | Create a paint with a given emissive factor
paintWithEmissiveColour :: Colour -> Paint
paintWithEmissiveColour c = mempty { _paintEmissiveColour = Last (Just c) }

-- | The paint name
paintName :: Lens' Paint (Maybe String)
paintName = 
    lens
        (getLast . _paintName) 
        (\p c -> p { _paintName = Last c })

-- | Create a paint with a given name
paintWithName :: String -> Paint
paintWithName n = mempty { _paintName = Last (Just n) }

-- | Whether to display a surface as Double or Single Sided
paintDoubleSidedness :: Lens' Paint (Maybe DoubleSidedness)
paintDoubleSidedness = 
    lens
        (getLast . _paintDoubleSidedness) 
        (\p c -> p { _paintDoubleSidedness = Last c })

-- | Create a paint with a given `DoubleSidedness`
paintWithDoubleSidedness :: DoubleSidedness -> Paint
paintWithDoubleSidedness ds = mempty { _paintDoubleSidedness = Last (Just ds) }