-
Notifications
You must be signed in to change notification settings - Fork 13
/
Program.fs
272 lines (214 loc) · 9.76 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
open Aardvark.Base
open FSharp.Data.Adaptive
open Aardvark.SceneGraph
open Aardvark.Application
open Aardvark.Application.Slim
open FSharp.Data.Adaptive.Operators
open Aardvark.Rendering
open Aardvark.Rendering.Vulkan
open Aardvark.Rendering.Text
module Shader =
open FShade
type Vertex = { [<Position>] p : V4d; [<WorldPosition>] wp : V4d; [<Color>] c : V4d; [<Normal>] n : V3d; [<Semantic("LightDir")>] ldir : V3d }
let divide (v : Triangle<Vertex>) =
triangle {
let ccDiv = v.P0.c
let nDiv = v.P0.n
let cpDiv = (v.P0.wp + v.P1.wp + v.P2.wp) / 3.0
//let cc = (v.P0.c + v.P1.c + v.P2.c) / 3.0
// 0 1 c c 1 2 c 2 0
yield { v.P0 with n = nDiv }
yield { v.P1 with n = nDiv }
yield { p = uniform.ViewProjTrafo * cpDiv; wp = cpDiv; c = V4d.IIII; n = nDiv; ldir = V3d.Zero}
yield { v.P2 with n = nDiv }
yield { v.P0 with n = nDiv }
}
let shrink (v : Triangle<Vertex>) =
triangle {
let cp = (v.P0.wp + v.P1.wp + v.P2.wp) / 3.0
let r = 0.9
let a = V4d(cp.XYZ * (1.0 - r) + v.P0.wp.XYZ * r, v.P0.wp.W)
let b = V4d(cp.XYZ * (1.0 - r) + v.P1.wp.XYZ * r, v.P1.wp.W)
let c = V4d(cp.XYZ * (1.0 - r) + v.P2.wp.XYZ * r, v.P2.wp.W)
yield { v.P0 with p = uniform.ViewProjTrafo * a; wp = a }
yield { v.P1 with p = uniform.ViewProjTrafo * b; wp = b }
yield { v.P2 with p = uniform.ViewProjTrafo * c; wp = c }
}
let nop (v : Triangle<Vertex>) =
triangle {
yield v.P0
yield v.P1
yield v.P2
}
let invert (v : Triangle<Vertex>) =
triangle {
let p01 = (v.P0.wp + v.P1.wp) / 2.0
let p12 = (v.P1.wp + v.P2.wp) / 2.0
let p20 = (v.P2.wp + v.P0.wp) / 2.0
yield { v.P0 with p = uniform.ViewProjTrafo * p01; wp = p01 }
yield { v.P1 with p = uniform.ViewProjTrafo * p12; wp = p12 }
yield { v.P2 with p = uniform.ViewProjTrafo * p20; wp = p20 }
}
let tricolor (v : Triangle<Vertex>) =
triangle {
yield { v.P0 with c = V4d.OIII }
yield { v.P1 with c = V4d.IOII }
yield { v.P2 with c = V4d.IIOI }
}
let tritri (v : Triangle<Vertex>) =
triangle {
let p01 = (v.P0.wp + v.P1.wp) / 2.0
let p12 = (v.P1.wp + v.P2.wp) / 2.0
let p20 = (v.P2.wp + v.P0.wp) / 2.0
yield v.P0
yield { v.P0 with p = uniform.ViewProjTrafo * p01; wp = p01 }
yield { v.P2 with p = uniform.ViewProjTrafo * p20; wp = p20 }
restartStrip()
yield { v.P0 with p = uniform.ViewProjTrafo * p01; wp = p01 }
yield { v.P1 with p = uniform.ViewProjTrafo * p12; wp = p12 }
yield { v.P2 with p = uniform.ViewProjTrafo * p20; wp = p20 }
restartStrip()
yield { v.P2 with p = uniform.ViewProjTrafo * p20; wp = p20 }
yield { v.P1 with p = uniform.ViewProjTrafo * p12; wp = p12 }
yield v.P2
restartStrip()
yield { v.P0 with p = uniform.ViewProjTrafo * p01; wp = p01 }
yield v.P1
yield { v.P1 with p = uniform.ViewProjTrafo * p12; wp = p12 }
restartStrip()
let p01 = p01 + V4d.OOIO
let p12 = p12 + V4d.OOIO
let p20 = p20 + V4d.OOIO
yield { v.P0 with p = uniform.ViewProjTrafo * p01; wp = p01 }
yield { v.P1 with p = uniform.ViewProjTrafo * p12; wp = p12 }
yield { v.P2 with p = uniform.ViewProjTrafo * p20; wp = p20 }
restartStrip()
}
[<ReflectedDefinition>]
let computeNormal (p0 : V4d) (p1 : V4d) (p2 : V4d) =
Vec.cross (p1.XYZ - p0.XYZ) (p2.XYZ - p0.XYZ) |> Vec.normalize
let extrude (v : Triangle<Vertex>) =
triangle {
let p0Ext = v.P0.wp.XYZ
let p1Ext = v.P1.wp.XYZ
let p2Ext = v.P2.wp.XYZ
let cExt = (p0Ext + p1Ext + p2Ext) / 3.0
let nExt = Vec.cross (p1Ext - p0Ext) (p2Ext - p1Ext)
let lnExt = Vec.length nExt
let areaExt = 0.5 * lnExt
let nExt = nExt / lnExt
let phExt = cExt + nExt * areaExt
let w0Ext = V4d(p0Ext, 1.0)
let w1Ext = V4d(p1Ext, 1.0)
let w2Ext = V4d(p2Ext, 1.0)
let whExt = V4d(phExt, 1.0)
yield { wp = w0Ext; p = uniform.ViewProjTrafo * w0Ext; n = nExt; c = v.P0.c; ldir = V3d.Zero }
yield { wp = w1Ext; p = uniform.ViewProjTrafo * w1Ext; n = nExt; c = v.P1.c; ldir = V3d.Zero }
yield { wp = w2Ext; p = uniform.ViewProjTrafo * w2Ext; n = nExt; c = v.P2.c; ldir = V3d.Zero }
restartStrip()
let nExt = computeNormal w0Ext w1Ext whExt
yield { wp = w0Ext; p = uniform.ViewProjTrafo * w0Ext; n = nExt; c = v.P0.c; ldir = V3d.Zero }
yield { wp = w1Ext; p = uniform.ViewProjTrafo * w1Ext; n = nExt; c = v.P1.c; ldir = V3d.Zero }
yield { wp = whExt; p = uniform.ViewProjTrafo * whExt; n = nExt; c = V4d.IOII; ldir = V3d.Zero }
restartStrip()
let nExt = computeNormal w1Ext w2Ext whExt
yield { wp = w1Ext; p = uniform.ViewProjTrafo * w1Ext; n = nExt; c = v.P1.c; ldir = V3d.Zero }
yield { wp = w2Ext; p = uniform.ViewProjTrafo * w2Ext; n = nExt; c = v.P2.c; ldir = V3d.Zero }
yield { wp = whExt; p = uniform.ViewProjTrafo * whExt; n = nExt; c = V4d.IOII; ldir = V3d.Zero }
restartStrip()
let nExt = computeNormal w2Ext w0Ext whExt
yield { wp = w2Ext; p = uniform.ViewProjTrafo * w2Ext; n = nExt; c = v.P2.c; ldir = V3d.Zero }
yield { wp = w0Ext; p = uniform.ViewProjTrafo * w0Ext; n = nExt; c = v.P0.c; ldir = V3d.Zero }
yield { wp = whExt; p = uniform.ViewProjTrafo * whExt; n = nExt; c = V4d.IOII; ldir = V3d.Zero }
restartStrip()
}
let withLightDir (v : Vertex) =
vertex {
return { v with ldir = (uniform.ViewTrafo * (V4d(50.0, 60.0, 70.0, 1.0) - v.wp)).XYZ |> Vec.normalize; n = (uniform.ViewTrafo * V4d(v.n, 0.0)).XYZ |> Vec.normalize }
}
[<EntryPoint>]
let main argv =
Aardvark.Init()
let available =
[
"divide", toEffect Shader.divide
"shrink", toEffect Shader.shrink
"extrude", toEffect Shader.extrude
// "invert", toEffect Shader.invert
// "tricolor", toEffect Shader.tricolor
]
let rec allInsertions (v : 'a) (l : list<'a>) =
match l with
| [] -> [[v]]
| h :: t ->
List.concat [
[v :: h :: t]
allInsertions v t |> List.map (fun r -> h :: r)
]
let rec all (l : list<'a>) =
match l with
| [] -> [[]]
| h :: t ->
let t = all t
List.append
(t |> List.collect (fun l -> allInsertions h l))
(t)
let combinations =
// [available] // for less
all available // for more
|> List.sortBy List.length
|> List.map (fun l ->
let name =
match l |> List.map fst with
| [] -> "empty"
| list -> list |> String.concat ", " |> sprintf "[ %s ]"
let effect = l |> List.map snd |> FShade.Effect.compose
name, effect
)
|> List.toArray
let w = 1 + (ceil (sqrt (float combinations.Length)) |> int)
let h = ceil (float combinations.Length / float w) |> int
let sg =
Sg.ofList [
for j in 0 .. h - 1 do
for i in 0 .. w - 1 do
let id = i + w * j
if id < combinations.Length then
let (name, effect) = combinations.[id]
let label =
Sg.text DefaultFonts.Hack.Regular C4b.White (AVal.constant name)
|> Sg.transform (Trafo3d.FromBasis(V3d.IOO, V3d.OOI, V3d.OIO, V3d.Zero) * Trafo3d.Scale(0.1))
|> Sg.translate 0.0 0.0 1.0
let inner =
Sg.ofList [
Sg.sphere' 3 C4b.Red 0.5
|> Sg.translate 0.5 0.5 0.5
|> Sg.uniform "Color" (AVal.constant V4d.IOOI)
Sg.box' C4b.Blue Box3d.Unit
|> Sg.uniform "Color" (AVal.constant V4d.IIOI)
|> Sg.translate 1.6 0.0 0.0
]
|> Sg.scale 0.5
|> Sg.shader {
do! DefaultSurfaces.trafo
do! effect
do! Shader.withLightDir
do! DefaultSurfaces.stableHeadlight
}
yield
Sg.ofList [
inner
label
]
|> Sg.translate (2.0 * float i) 0.0 (1.5 * float j)
]
|> Sg.scale 0.5
|> Sg.transform (Trafo3d.FromBasis(-V3d.IOO,V3d.OIO, V3d.OOI, V3d.OOO))
show {
backend Backend.GL
display Display.Mono
debug false
samples 8
scene sg
}
0