1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
|
4 |
namespace Oni.Dae |
5 |
{ |
6 |
internal class Source : Entity |
7 |
{ |
8 |
private float[] floatData; |
9 |
private string[] nameData; |
10 |
private int stride; |
11 |
private int count; |
12 |
|
13 |
public Source(IEnumerable<float> data, int stride) |
14 |
{ |
15 |
this.floatData = data.ToArray(); |
16 |
this.stride = stride; |
17 |
this.count = floatData.Length / stride; |
18 |
} |
19 |
|
20 |
public Source(float[] data, int stride) |
21 |
{ |
22 |
this.floatData = (float[])data.Clone(); |
23 |
this.stride = stride; |
24 |
this.count = data.Length / stride; |
25 |
} |
26 |
|
27 |
public Source(string[] data, int stride) |
28 |
{ |
29 |
this.nameData = (string[])data.Clone(); |
30 |
this.stride = stride; |
31 |
this.count = data.Length / stride; |
32 |
} |
33 |
|
34 |
public Source(IList<Vector2> data) |
35 |
{ |
36 |
int count = data.Count; |
37 |
float[] array = new float[count * 2]; |
38 |
|
39 |
for (int i = 0; i < count; i++) |
40 |
{ |
41 |
array[i * 2 + 0] = data[i].X; |
42 |
array[i * 2 + 1] = 1.0f - data[i].Y; |
43 |
} |
44 |
|
45 |
this.floatData = array; |
46 |
this.count = count; |
47 |
this.stride = 2; |
48 |
} |
49 |
|
50 |
public Source(IList<Vector3> data) |
51 |
{ |
52 |
int count = data.Count; |
53 |
float[] array = new float[count * 3]; |
54 |
|
55 |
for (int i = 0; i < count; i++) |
56 |
{ |
57 |
array[i * 3 + 0] = data[i].X; |
58 |
array[i * 3 + 1] = data[i].Y; |
59 |
array[i * 3 + 2] = data[i].Z; |
60 |
} |
61 |
|
62 |
this.floatData = array; |
63 |
this.count = count; |
64 |
this.stride = 3; |
65 |
} |
66 |
|
67 |
public string[] NameData => nameData; |
68 |
public float[] FloatData => floatData; |
69 |
public int Count => count; |
70 |
|
71 |
public int Stride |
72 |
{ |
73 |
get |
74 |
{ |
75 |
return stride; |
76 |
} |
77 |
set |
78 |
{ |
79 |
stride = value; |
80 |
|
81 |
if (floatData != null) |
82 |
count = floatData.Length / stride; |
83 |
else |
84 |
count = nameData.Length / stride; |
85 |
} |
86 |
} |
87 |
|
88 |
public static Vector2 ReaderVector2(Source source, int index) |
89 |
{ |
90 |
var data = source.floatData; |
91 |
var i = index * source.stride; |
92 |
|
93 |
return new Vector2(data[i + 0], data[i + 1]); |
94 |
} |
95 |
|
96 |
public static Vector2 ReadTexCoord(Source source, int index) |
97 |
{ |
98 |
var data = source.floatData; |
99 |
var i = index * source.stride; |
100 |
|
101 |
return new Vector2(data[i + 0], 1.0f - data[i + 1]); |
102 |
} |
103 |
|
104 |
public static Vector3 ReadVector3(Source source, int index) |
105 |
{ |
106 |
var data = source.floatData; |
107 |
var i = index * source.stride; |
108 |
|
109 |
return new Vector3(data[i + 0], data[i + 1], data[i + 2]); |
110 |
} |
111 |
|
112 |
public static Vector4 ReadVector4(Source source, int index) |
113 |
{ |
114 |
var data = source.floatData; |
115 |
var i = index * source.stride; |
116 |
|
117 |
return new Vector4(data[i + 0], data[i + 1], data[i + 2], data[i + 3]); |
118 |
} |
119 |
|
120 |
public static Imaging.Color ReadColor(Source source, int index) |
121 |
{ |
122 |
return new Imaging.Color(ReadVector4(source, index)); |
123 |
} |
124 |
} |
125 |
} |