| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using Oni.Imaging; |
| 4 |
|
| 5 |
namespace Oni.Akira |
| 6 |
{ |
| 7 |
internal class PolygonMesh |
| 8 |
{ |
| 9 |
private readonly MaterialLibrary materialLibrary; |
| 10 |
private readonly List<Vector3> points = new List<Vector3>(); |
| 11 |
private readonly List<Vector3> normals = new List<Vector3>(); |
| 12 |
private readonly List<Vector2> texCoords = new List<Vector2>(); |
| 13 |
private readonly List<Polygon> polygons = new List<Polygon>(); |
| 14 |
private readonly List<Polygon> doors = new List<Polygon>(); |
| 15 |
private readonly List<Room> rooms = new List<Room>(); |
| 16 |
private readonly List<Polygon> ghosts = new List<Polygon>(); |
| 17 |
private readonly List<Polygon> floors = new List<Polygon>(); |
| 18 |
private bool hasDebugInfo; |
| 19 |
|
| 20 |
public PolygonMesh(MaterialLibrary materialLibrary) |
| 21 |
{ |
| 22 |
this.materialLibrary = materialLibrary; |
| 23 |
} |
| 24 |
|
| 25 |
public MaterialLibrary Materials => materialLibrary; |
| 26 |
|
| 27 |
public List<Vector3> Points => points; |
| 28 |
public List<Vector2> TexCoords => texCoords; |
| 29 |
public List<Vector3> Normals => normals; |
| 30 |
public List<Polygon> Polygons => polygons; |
| 31 |
public List<Polygon> Doors => doors; |
| 32 |
public List<Room> Rooms => rooms; |
| 33 |
public List<Polygon> Floors => floors; |
| 34 |
public List<Polygon> Ghosts => ghosts; |
| 35 |
|
| 36 |
public bool HasDebugInfo |
| 37 |
{ |
| 38 |
get { return hasDebugInfo; } |
| 39 |
set { hasDebugInfo = value; } |
| 40 |
} |
| 41 |
|
| 42 |
public BoundingBox GetBoundingBox() => BoundingBox.CreateFromPoints(points); |
| 43 |
|
| 44 |
public void DoLighting() |
| 45 |
{ |
| 46 |
var ambientColor = new Vector3(0.6f, 0.6f, 0.6f); |
| 47 |
|
| 48 |
var lightDir = new[] { |
| 49 |
new Vector3(-0.526f, -0.573f, -0.627f), |
| 50 |
new Vector3(0.719f, 0.342f, 0.604f), |
| 51 |
new Vector3(0.454f, 0.766f, 0.454f) |
| 52 |
}; |
| 53 |
|
| 54 |
var lightColor = new[] { |
| 55 |
new Vector3(1.0f, 1.0f, 1.0f), |
| 56 |
new Vector3(1.0f, 1.0f, 1.0f), |
| 57 |
new Vector3(1.0f, 1.0f, 1.0f) |
| 58 |
}; |
| 59 |
|
| 60 |
//if (importLights) |
| 61 |
//{ |
| 62 |
// ambientColor = ambient; |
| 63 |
//} |
| 64 |
|
| 65 |
foreach (var polygon in polygons) |
| 66 |
{ |
| 67 |
if (polygon.Colors != null) |
| 68 |
continue; |
| 69 |
|
| 70 |
var colors = new Color[polygon.VertexCount]; |
| 71 |
|
| 72 |
if (polygon.NormalIndices != null) |
| 73 |
{ |
| 74 |
for (int i = 0; i < colors.Length; i++) |
| 75 |
{ |
| 76 |
var color = ambientColor; |
| 77 |
|
| 78 |
for (int j = 0; j < lightDir.Length; j++) |
| 79 |
{ |
| 80 |
float dot = Vector3.Dot(lightDir[j], normals[polygon.NormalIndices[i]]); |
| 81 |
color += lightColor[j] * dot; |
| 82 |
} |
| 83 |
|
| 84 |
colors[i] = new Color(Vector3.Clamp(color, Vector3.Zero, Vector3.One)); |
| 85 |
} |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
var color = ambientColor; |
| 90 |
|
| 91 |
for (int j = 0; j < lightDir.Length; j++) |
| 92 |
{ |
| 93 |
float dot = Vector3.Dot(lightDir[j], polygon.Plane.Normal); |
| 94 |
color += lightColor[j] * dot; |
| 95 |
} |
| 96 |
|
| 97 |
for (int i = 0; i < colors.Length; i++) |
| 98 |
colors[i] = new Color(Vector3.Clamp(color, Vector3.Zero, Vector3.One)); |
| 99 |
} |
| 100 |
|
| 101 |
polygon.Colors = colors; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |