forked from alvani/Unity-glTF-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGlTF_BufferView.cs
125 lines (110 loc) · 3.11 KB
/
GlTF_BufferView.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
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.IO;
using System;
public class GlTF_BufferView : GlTF_Writer {
public enum TARGET
{
ARRAY=34962,
ELEMENT=34963
}
public int bufferIndex = 0;// ": "duck",
public long byteLength;//": 25272,
public long byteOffset;//": 0,
public long byteStride;
public int target= -1;
// public string target = "ARRAY_BUFFER";
public int currentOffset = 0;
public MemoryStream memoryStream = new MemoryStream();
public bool bin = false;
public GlTF_BufferView (string n, int s) { name = n; byteStride = s; }
public GlTF_BufferView (string n, int s, int t) { name = n; byteStride = s; target = t; }
public void Populate (int[] vs, bool flippedTriangle)
{
if (flippedTriangle)
{
for (int i = 0; i < vs.Length; i+=3)
{
ushort u = (ushort)vs[i];
memoryStream.Write (BitConverter.GetBytes(u), 0, BitConverter.GetBytes(u).Length);
currentOffset += 2;
u = (ushort)vs[i+2];
memoryStream.Write (BitConverter.GetBytes(u), 0, BitConverter.GetBytes(u).Length);
currentOffset += 2;
u = (ushort)vs[i+1];
memoryStream.Write (BitConverter.GetBytes(u), 0, BitConverter.GetBytes(u).Length);
currentOffset += 2;
}
}
else
{
for (int i = 0; i < vs.Length; i++)
{
ushort u = (ushort)vs[i];
memoryStream.Write (BitConverter.GetBytes(u), 0, BitConverter.GetBytes(u).Length);
currentOffset += 2;
}
}
byteLength = currentOffset;
}
public void PopulateShort(ushort vs)
{
ushort u = (ushort)vs;
memoryStream.Write(BitConverter.GetBytes(u), 0, BitConverter.GetBytes(u).Length);
currentOffset += 2;
byteLength += 2 ;
}
public void Populate (float[] vs)
{
for (int i = 0; i < vs.Length; i++)
{
// memoryStream.Write (vs[i]);
// memoryStream.Write ((byte[])vs, 0, vs.Length * sizeof(int));
float f = vs[i];
memoryStream.Write (BitConverter.GetBytes(f), 0, BitConverter.GetBytes(f).Length);
currentOffset += 4;
}
byteLength = currentOffset;
}
public void Populate(uint v)
{
memoryStream.Write(BitConverter.GetBytes(v), 0, BitConverter.GetBytes(v).Length);
currentOffset += 4;
byteLength = currentOffset;
}
public void Populate (float v)
{
memoryStream.Write (BitConverter.GetBytes(v), 0, BitConverter.GetBytes(v).Length);
currentOffset += 4;
byteLength = currentOffset;
}
public override void Write ()
{
/*
"bufferView_4642": {
"buffer": "vc.bin",
"byteLength": 630080,
"byteOffset": 0,
"target": "ARRAY_BUFFER"
},
*/
Indent(); jsonWriter.Write ("{\n");
IndentIn();
//var binName = binary ? "binary_glTF" : Path.GetFileNameWithoutExtension(GlTF_Writer.binFileName);
Indent(); jsonWriter.Write ("\"buffer\": " + bufferIndex +",\n");
Indent(); jsonWriter.Write ("\"byteLength\": " + byteLength + ",\n");
if ((int)target != (int)-1)
{
Indent(); jsonWriter.Write("\"target\": " + target + ",\n");
}
if (byteStride >= 4)
{
Indent(); jsonWriter.Write("\"byteStride\": " + byteStride + ",\n");
}
Indent(); jsonWriter.Write ("\"byteOffset\": " + byteOffset + "\n");
IndentOut();
Indent(); jsonWriter.Write ("}");
}
}
#endif