summaryrefslogtreecommitdiff
path: root/testdata/ShadowMapping.wip.lc
diff options
context:
space:
mode:
Diffstat (limited to 'testdata/ShadowMapping.wip.lc')
-rw-r--r--testdata/ShadowMapping.wip.lc220
1 files changed, 220 insertions, 0 deletions
diff --git a/testdata/ShadowMapping.wip.lc b/testdata/ShadowMapping.wip.lc
new file mode 100644
index 00000000..e9a5f11a
--- /dev/null
+++ b/testdata/ShadowMapping.wip.lc
@@ -0,0 +1,220 @@
1shadowMapSize = 512
2
3triangleCtx = TriangleCtx CullNone PolygonFill NoOffset LastVertex
4
5gaussFilter7 :: [(Float, Float)]
6gaussFilter7 =
7 [ (-3.0, 0.015625)
8 , (-2.0, 0.09375)
9 , (-1.0, 0.234375)
10 , (0.0, 0.3125)
11 , (1.0, 0.234375)
12 , (2.0, 0.09375)
13 , (3.0, 0.015625)
14 ]
15
16gaussFilter9 :: [(Float, Float)]
17gaussFilter9 =
18 [ (-4.0, 0.05)
19 , (-3.0, 0.09)
20 , (-2.0, 0.12)
21 , (-1.0, 0.15)
22 , (0.0, 0.16)
23 , (1.0, 0.15)
24 , (2.0, 0.12)
25 , (3.0, 0.09)
26 , (4.0, 0.05)
27 ]
28
29blurCoefficients :: [(Float, Float)]
30blurCoefficients = gaussFilter9
31
32blur :: [(Float, Float)] -> Image 1 ('Color (Vec 4 Float)) -> FrameBuffer 1 '[ 'Color (Vec 4 Float) ]
33blur coefficients img = filter1D dirH (PrjImage (filter1D dirV img))
34 where
35 dirH v = V2 (v / shadowMapSize) 0.0
36 dirV v = V2 0.0 (v / shadowMapSize)
37
38-- todo -- filter1D :: (Float -> Vec 2 Float) -> Image 1 ('Color (Vec 4 Float)) -> FrameBuffer 1 '[ 'Color (Vec 4 Float) ]
39 filter1D dir img = accumulate accCtx frag
40 (rasterizePrimitives triangleCtx ((NoPerspective)) prims) clearBuf
41 where
42 accCtx = ((ColorOp NoBlending (V4 True True True True)))
43 clearBuf = FrameBuffer ((ColorImage @1 (V4 0 0 0 0.0)))
44-- vert :: Vec 2 Float -> VertexOut (Vec 2 Float)
45 vert ((uv)) = (pos, uv')
46 where
47 uv' = uv *! 0.5 +! 0.5
48 pos = V4 uv%x uv%y 1.0 1.0
49
50 prims = mapPrimitives vert (fetch "postSlot" ((Attribute "position")) :: PrimitiveStream Triangle ((Vec 2 Float)))
51
52-- frag :: FragmentShader (Vec 2 Float -> Color (Vec 4 Float))
53 frag ((uv)) = ((sample))
54 where
55 tex = Texture2D (V2 shadowMapSize shadowMapSize) img
56 smp = Sampler LinearFilter ClampToEdge tex
57 sample = foldr1 (\a b -> a + b) [ texture2D smp (uv + dir ofs) *! coeff
58 | (ofs, coeff) <- coefficients
59 ]
60
61moments :: FrameBuffer 1 '[Depth Float, Color (Vec 4 Float)]
62moments = accumulate accCtx frag (rasterizePrimitives triangleCtx ((Smooth)) prims) clearBuf
63 where
64 accCtx = (DepthOp Less True, ColorOp NoBlending (V4 True True True True))
65 clearBuf = FrameBuffer (DepthImage @1 1000, ColorImage @1 (V4 0.0 0.0 0.0 1.0{-todo: use 1-}))
66
67 lightMatrix = Uniform "lightMatrix" :: Mat 4 4 Float
68 modelMatrix = Uniform "modelMatrix" :: Mat 4 4 Float
69
70-- vert :: ((Vec 3 Float)) -> ((Float
71 vert ((pos)) = (lightPos, depth)
72 where
73 lightPos = lightMatrix *. modelMatrix *. v3FToV4F pos
74 depth = lightPos%z
75
76 prims = mapPrimitives vert (fetch "geometrySlot" ((Attribute "position" :: Vec 3 Float)))
77
78-- frag :: Float -> (Depth Float,Color (Vec 4 Float))
79 frag ((depth)) = (depth, V4 moment1 moment2 0 1)
80 where
81 dx = dFdx depth
82 dy = dFdy depth
83 moment1 = depth
84 moment2 = depth *! depth +! 0.25 *! (dx *! dx +! dy *! dy)
85{-
86depth :: FrameBuffer 1 (Depth Float, Color (Vec 4 Float))
87depth = accumulate accCtx PassAll frag (Rasterize triangleCtx prims) clearBuf
88 where
89 accCtx = accumulationContext (DepthOp Less True, ColorOp NoBlending (V4 True True True True))
90 clearBuf = FrameBuffer (DepthImage @1 1000, ColorImage @1 (V4 0.0 0.0 0.0 1.0))
91
92 lightMatrix = Uniform "lightMatrix" :: Mat 4 4 Float
93 modelMatrix = Uniform "modelMatrix" :: Mat 4 4 Float
94
95 vert :: Vec 3 Float -> VertexOut Float
96 vert pos = VertexOut lightPos 1 () (Smooth depth)
97 where
98 lightPos = lightMatrix *. modelMatrix *. v3FToV4F pos
99 depth = lightPos%z
100
101 prims = Transform vert (fetch "geometrySlot" Triangle (Attribute "position" :: Vec 3 Float))
102
103 frag :: FragmentShader (Float -> (Depth Float, Color (Vec 4 Float)))
104 frag = FragmentShaderRastDepth $ \depth -> (V4 depth 0 0 1)
105
106vsm :: FrameBuffer 1 (Depth Float, Color (Vec 4 Float))
107vsm = accumulate accCtx PassAll frag (Rasterize triangleCtx prims) clearBuf
108 where
109 accCtx = accumulationContext (DepthOp Less True, ColorOp NoBlending (V4 True True True True))
110 cameraMatrix = Uniform "cameraMatrix" :: Mat 4 4 Float
111 lightMatrix = Uniform "lightMatrix" :: Mat 4 4 Float
112 modelMatrix = Uniform "modelMatrix" :: Mat 4 4 Float
113 lightPosition = Uniform "lightPosition" :: Vec 3 Float
114
115 shadowMap = Texture2D (V2 shadowMapSize shadowMapSize) (PrjImageColor moments)
116 shadowMapBlur = Texture2D (V2 shadowMapSize shadowMapSize) (PrjImage blurredMoments)
117 where
118 blurredMoments = blur blurCoefficients (PrjImageColor moments)
119 sampler = Sampler LinearFilter ClampToEdge shadowMapBlur
120
121 frag :: FragmentShader ((Vec 3 Float, Vec 4 Float, Vec 3 Float) -> (Depth Float, Color (Vec 4 Float)))
122 frag = FragmentShaderRastDepth $ \(worldPos, lightPos, worldNormal) -> (luminance)
123 where
124 clampUV x = clampS x 0.0 1.0
125 scaleUV x = x *! 0.5 +! 0.5
126 lightU = lightPos%x
127 lightV = lightPos%y
128 lightDepth = lightPos%z
129 lightW = lightPos%w
130 uv = clampUV (scaleUV ((V2 lightU lightV) /! lightW))
131
132 val = texture2D sampler uv
133 moment1 = val%x
134 moment2 = val%y
135 variance = max 0.002 (moment2 -! moment1 *! moment1)
136 distance = max 0.0 (lightDepth -! moment1)
137 lightProbMax = variance /! (variance +! distance *! distance)
138
139 lambert = max 0.0 (dot worldNormal (normalize (lightPosition - worldPos)))
140
141 uv' = uv -! 0.5
142 spotShape = 1.0 -! length uv *! 4.0
143 intensity = max 0 (spotShape *! lambert)
144
145 val2 = scaleUV (round (uv' *! 10.0)) *! intensity
146 spotR = val2%x
147 spotG = val2%y
148
149 luminance = (V4 spotR spotG intensity 1.0) *! pow lightProbMax 2.0
150
151
152 clearBuf = FrameBuffer ( DepthImage @1 1000
153 , ColorImage @1 (V4 0.1 0.2 0.6 1))
154
155 vert :: (Vec 3 Float, Vec 3 Float) -> VertexOut (Vec 3 Float, Vec 4 Float, Vec 3 Float)
156 vert (localPos, localNormal) = VertexOut viewPos 1 () (Smooth worldPos%xyz, ((Smooth)) lightPos, ((Smooth)) worldNormal)
157 where
158 worldPos = modelMatrix *. v3FToV4F localPos
159 viewPos = cameraMatrix *. worldPos
160 lightPos = lightMatrix *. worldPos
161 worldNormal = normalize ((modelMatrix *. v3FToV4F localNormal)%xyz)
162
163 prims = Transform vert (fetch "geometrySlot" Triangle (Attribute "position" :: Vec 3 Float, Attribute "normal" :: Vec 3 Float))
164
165
166sm :: FrameBuffer 1 (Depth Float, Color (Vec 4 Float))
167sm = accumulate accCtx PassAll frag (Rasterize triangleCtx prims) clearBuf
168 where
169 accCtx = accumulationContext (DepthOp Less True, ColorOp NoBlending (V4 True True True True))
170 clearBuf = FrameBuffer (DepthImage @1 1000, ColorImage @1 (V4 0.1 0.2 0.6 1))
171 cameraMatrix = Uniform "cameraMatrix" :: Mat 4 4 Float
172 lightMatrix = Uniform "lightMatrix" :: Mat 4 4 Float
173 modelMatrix = Uniform "modelMatrix" :: Mat 4 4 Float
174 lightPosition = Uniform "lightPosition" :: Vec 3 Float
175
176 vert :: (Vec 3 Float, Vec 3 Float) -> VertexOut (Vec 3 Float, Vec 4 Float, Vec 3 Float)
177 vert (localPos, localNormal) = VertexOut viewPos 1 () (Smooth (worldPos%xyz), ((Smooth)) lightPos, ((Smooth)) worldNormal)
178 where
179 worldPos = modelMatrix *. v3FToV4F localPos
180 viewPos = cameraMatrix *. worldPos
181 lightPos = lightMatrix *. worldPos
182 worldNormal = normalize ((modelMatrix *. v3FToV4F localNormal)%xyz)
183
184 prims = Transform vert (fetch "geometrySlot" Triangle (Attribute "position" :: Vec 3 Float, Attribute "normal" :: Vec 3 Float))
185
186 --shadowMap :: Exp Obj (Texture Tex2D SingleTex (Regular Float) Red)
187 shadowMap = Texture2D (V2 shadowMapSize shadowMapSize) (PrjImageColor depth)
188 sampler = Sampler PointFilter ClampToEdge shadowMap
189
190 frag :: FragmentShader ((Vec 3 Float, Vec 4 Float, Vec 3 Float) -> (Depth Float, Color (Vec 4 Float)))
191 frag = FragmentShaderRastDepth $ \(worldPos, lightPos, worldNormal) -> (luminance)
192 where
193 lightU = lightPos%x
194 lightV = lightPos%y
195 lightDepth = lightPos%z
196 lightW = lightPos%w
197 clampUV x = clampS x 0 1
198 scaleUV x = x *! 0.5 +! 0.5
199 uv = clampUV (scaleUV ((V2 lightU lightV) /! lightW))
200
201 surfaceDistance = (texture2D sampler uv)%x
202 lightPortion = if (lightDepth <= surfaceDistance +! 0.01) then 1 else 0
203
204 lambert = max 0 (dot worldNormal (normalize (lightPosition - worldPos)))
205
206 --intensity = lambert @* lightPortion
207 --luminance = pack' (V4 intensity intensity intensity (floatF 1))
208
209 uv' = uv -! 0.5
210 spotShape = 1 -! length uv' *! 4
211 intensity = max 0 (spotShape *! lambert)
212
213 val = scaleUV (round (uv' *! 10)) *! intensity
214 spotR = val%x
215 spotG = val%y
216
217 luminance = (V4 spotR spotG intensity 1) *! lightPortion
218
219main = ScreenOut sm
220-}