-
Notifications
You must be signed in to change notification settings - Fork 0
/
Effect.fs
104 lines (87 loc) · 3.83 KB
/
Effect.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
namespace Brains
open System
open System.Collections.Generic
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
type DirectionalLight =
{
mutable DiffuseColor : Vector3;
mutable SpecularColor : Vector3;
mutable Direction : Vector3;
mutable Enabled : bool;
}
with
static member create(l : BasicDirectionalLight) =
{
DiffuseColor = l.DiffuseColor;
SpecularColor = l.SpecularColor;
Direction = l.Direction;
Enabled = l.Enabled;
}
member l.applyTo(b : BasicDirectionalLight) =
b.DiffuseColor <- l.DiffuseColor
b.SpecularColor <- l.SpecularColor
b.Direction <- l.Direction
b.Enabled <- l.Enabled
type EffectState =
{
mutable DiffuseColor : Vector3;
mutable DirectionalLight0 : DirectionalLight;
mutable DirectionalLight1 : DirectionalLight;
mutable DirectionalLight2 : DirectionalLight;
}
with
static member create(b : BasicEffect) =
{
DiffuseColor = b.DiffuseColor;
DirectionalLight0 = DirectionalLight.create(b.DirectionalLight0);
DirectionalLight1 = DirectionalLight.create(b.DirectionalLight1);
DirectionalLight2 = DirectionalLight.create(b.DirectionalLight2);
}
member e.applyTo(b : BasicEffect) =
b.DiffuseColor <- e.DiffuseColor
e.DirectionalLight0.applyTo(b.DirectionalLight0)
e.DirectionalLight1.applyTo(b.DirectionalLight1)
e.DirectionalLight2.applyTo(b.DirectionalLight2)
static member Default =
{
DiffuseColor = Vector3(1.0f, 1.0f, 1.0f);
DirectionalLight0 = {DiffuseColor = Vector3(0.0f, 1.0f, 1.0f); SpecularColor = Vector3(1.0f); Direction = Vector3(-0.5f, -0.5f, -0.5f); Enabled = true};
DirectionalLight1 = {DiffuseColor = Vector3(0.0f, 0.5f, 0.5f); SpecularColor = Vector3(1.0f); Direction = Vector3(0.5f, -0.5f, 0.5f); Enabled = true};
DirectionalLight2 = {DiffuseColor = Vector3(); SpecularColor = Vector3(); Direction = Vector3(); Enabled = false};
}
type ModelState = {
mutable Model : Model;
mutable Position : Vector3;
mutable Scale : Vector3;
mutable Rotation : float32;
mutable EffectState : EffectState;
}
with
static member view(position, rotation) =
Matrix.CreateLookAt(
position + new Vector3(0.0f, 0.0f, 80.0f),
position,
Vector3(sin(rotation), cos(rotation), 0.0f))
static member projection =
Matrix.CreatePerspectiveFieldOfView(
MathHelper.Pi / 4.0f, 3.0f / 4.0f,
0.01f,
1000.0f)
member this.draw(color : Color) =
for mesh in this.Model.Meshes do
for effect in mesh.Effects do
let e = effect :?> BasicEffect
e.World <- Matrix.Identity
e.View <- Matrix.CreateScale(this.Scale * 0.25f) * Matrix.CreateRotationZ(this.Rotation) * ModelState.view(this.Position, 0.0f)
e.Projection <- ModelState.projection
e.LightingEnabled <- true
this.EffectState.applyTo(e)
e.DirectionalLight0.DiffuseColor <- color.ToVector3()
e.DirectionalLight1.DiffuseColor <- color.ToVector3()
// since we rotate the camera we need to rotate the lights back so they appear stationary
let rot = Matrix.CreateRotationZ(-this.Rotation)
e.DirectionalLight0.Direction <- Vector3.Transform(e.DirectionalLight0.Direction, rot)
e.DirectionalLight1.Direction <- Vector3.Transform(e.DirectionalLight1.Direction, rot)
mesh.Draw();