-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAccessor.cs
143 lines (126 loc) · 3.94 KB
/
Accessor.cs
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
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Diagnostics;
namespace RevitGltfExporter
{
public class Accessor
{
[JsonIgnore]
public string id;
private int _meshId;
public ComponentType componentType;
public ElementType type;
public object min;
public object max;
public int bufferView => Gltf.Instance.indexOfBufferView(bufferViewId);
public long byteOffset = 0;
public long count => _data.Length / ((int)type * countBytes(componentType));
private byte[] _data;
[JsonIgnore]
public string bufferViewId => String.Format("{0}-{1}-{2}", _meshId, type, componentType);
public Accessor(int meshId, AccessorType accessorType, object data, ComponentType componentType, ElementType type, object max = null, object min = null)
{
this._meshId = meshId;
this.componentType = componentType;
this.type = type;
this.max = max;
this.min = min;
this._data = toBinary(data);
SHA256 sha = SHA256.Create();
id = accessorType.ToString() + type.ToString() + componentType.ToString() + Convert.ToBase64String( sha.ComputeHash(_data));
}
private int countBytes(ComponentType componentType)
{
switch(componentType)
{
case ComponentType.UNSIGNED_BYTE:
case ComponentType.BYTE:
return 1;
case ComponentType.UNSIGNED_SHORT:
case ComponentType.SHORT:
return 2;
default: return 4;
}
}
private byte[] toBinary(object data)
{
switch(componentType)
{
case ComponentType.UNSIGNED_INT:
return ((int[])data).SelectMany(item => BitConverter.GetBytes(item)).ToArray();
case ComponentType.FLOAT:
return ((float[])data).SelectMany(item => BitConverter.GetBytes(item)).ToArray();
case ComponentType.UNSIGNED_SHORT:
return ((int[])data).SelectMany(item => BitConverter.GetBytes((ushort)item)).ToArray();
case ComponentType.UNSIGNED_BYTE:
return ((int[])data).Select(item => (byte)item).ToArray();
default: return null;
}
}
[JsonIgnore]
public int byteLength
{
get
{
return (int)Math.Ceiling(_data.Length / 4.0) * 4;
}
}
[JsonIgnore]
public long offset
{
get
{
BufferView view = Gltf.Instance.getBufferView(bufferView);
return view.byteOffset + byteOffset;
}
}
[JsonIgnore]
public byte[] bytes => _data;
public void saveBinary()
{
BufferView view = Gltf.Instance.getBufferView(bufferView);
byteOffset = view.byteLength;
view.byteLength += byteLength;
//ms.Position = 4;
//view.Add(ms);
//ms = null;
//ms.Dispose();
//ms.Close();
}
}
public enum ComponentType
{
BYTE = 5120,
UNSIGNED_BYTE = 5121,
SHORT = 5122,
UNSIGNED_SHORT = 5123,
UNSIGNED_INT = 5125,
FLOAT = 5126
}
[JsonConverter(typeof(StringEnumConverter))]
public enum ElementType
{
SCALAR = 1,
VEC2 = 2,
VEC3 = 3,
VEC4 = 4,
MAT2 = 4,
MAT3 = 9,
MAT4 = 16
}
public enum AccessorType
{
INDICES,
POINTS,
NORMALS,
UVS
}
}