-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOBJObjectBuilder.cs
More file actions
200 lines (165 loc) · 5.83 KB
/
Copy pathOBJObjectBuilder.cs
File metadata and controls
200 lines (165 loc) · 5.83 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2019 Dummiesman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
using Dummiesman;
using System.Collections.Generic;
using UnityEngine;
namespace Dummiesman {
public class OBJObjectBuilder {
//
public int PushedFaceCount { get; private set; } = 0;
//stuff passed in by ctor
private OBJLoader _loader;
private string _name;
private Dictionary<ObjLoopHash, int> _globalIndexRemap = new Dictionary<ObjLoopHash, int>();
private Dictionary<string, List<int>> _materialIndices = new Dictionary<string, List<int>>();
private List<int> _currentIndexList;
private string _lastMaterial = null;
//our local vert/normal/uv
private List<Vector3> _vertices = new List<Vector3>();
private List<Vector3> _normals = new List<Vector3>();
private List<Vector2> _uvs = new List<Vector2>();
//this will be set if the model has no normals or missing normal info
private bool recalculateNormals = false;
/// <summary>
/// Loop hasher helper class
/// </summary>
private class ObjLoopHash {
public int vertexIndex;
public int normalIndex;
public int uvIndex;
public override bool Equals(object obj) {
if (!(obj is ObjLoopHash))
return false;
var hash = obj as ObjLoopHash;
return (hash.vertexIndex == vertexIndex) && (hash.uvIndex == uvIndex) && (hash.normalIndex == normalIndex);
}
public override int GetHashCode() {
int hc = 3;
hc = unchecked(hc * 314159 + vertexIndex);
hc = unchecked(hc * 314159 + normalIndex);
hc = unchecked(hc * 314159 + uvIndex);
return hc;
}
}
public GameObject Build() {
var go = new GameObject(_name);
//add meshrenderer
var mr = go.AddComponent<MeshRenderer>();
int submesh = 0;
//locate the material for each submesh
Material[] materialArray = new Material[_materialIndices.Count];
foreach (var kvp in _materialIndices) {
Material material = null;
if (_loader.Materials == null) {
material = OBJLoaderHelper.CreateNullMaterial();
material.name = kvp.Key;
} else {
if (!_loader.Materials.TryGetValue(kvp.Key, out material)) {
material = OBJLoaderHelper.CreateNullMaterial();
material.name = kvp.Key;
_loader.Materials[kvp.Key] = material;
}
}
materialArray[submesh] = material;
submesh++;
}
mr.sharedMaterials = materialArray;
//add meshfilter
var mf = go.AddComponent<MeshFilter>();
submesh = 0;
var msh = new Mesh() {
name = _name,
indexFormat = (_vertices.Count > 65535) ? UnityEngine.Rendering.IndexFormat.UInt32 : UnityEngine.Rendering.IndexFormat.UInt16,
subMeshCount = _materialIndices.Count
};
//set vertex data
msh.SetVertices(_vertices);
msh.SetNormals(_normals);
msh.SetUVs(0, _uvs);
//set faces
foreach (var kvp in _materialIndices) {
msh.SetTriangles(kvp.Value, submesh);
submesh++;
}
//recalculations
if (recalculateNormals)
msh.RecalculateNormals();
msh.RecalculateTangents();
msh.RecalculateBounds();
mf.sharedMesh = msh;
//
return go;
}
public void SetMaterial(string name) {
if (!_materialIndices.TryGetValue(name, out _currentIndexList))
{
_currentIndexList = new List<int>();
_materialIndices[name] = _currentIndexList;
}
}
public void PushFace(string material, List<int> vertexIndices, List<int> normalIndices, List<int> uvIndices) {
//invalid face size?
if (vertexIndices.Count < 3) {
return;
}
//set material
if (material != _lastMaterial) {
SetMaterial(material);
_lastMaterial = material;
}
//remap
int[] indexRemap = new int[vertexIndices.Count];
for (int i = 0; i < vertexIndices.Count; i++) {
int vertexIndex = vertexIndices[i];
int normalIndex = normalIndices[i];
int uvIndex = uvIndices[i];
var hashObj = new ObjLoopHash() {
vertexIndex = vertexIndex,
normalIndex = normalIndex,
uvIndex = uvIndex
};
int remap = -1;
if (!_globalIndexRemap.TryGetValue(hashObj, out remap)) {
//add to dict
_globalIndexRemap.Add(hashObj, _vertices.Count);
remap = _vertices.Count;
//add new verts and what not
_vertices.Add((vertexIndex >= 0 && vertexIndex < _loader.Vertices.Count) ? _loader.Vertices[vertexIndex] : Vector3.zero);
_normals.Add((normalIndex >= 0 && normalIndex < _loader.Normals.Count) ? _loader.Normals[normalIndex] : Vector3.zero);
_uvs.Add((uvIndex >= 0 && uvIndex < _loader.UVs.Count) ? _loader.UVs[uvIndex] : Vector2.zero);
//mark recalc flag
if (normalIndex < 0)
recalculateNormals = true;
}
indexRemap[i] = remap;
}
//add face to our mesh list
if (indexRemap.Length == 3) {
_currentIndexList.AddRange(new int[] { indexRemap[0], indexRemap[1], indexRemap[2] });
} else if (indexRemap.Length == 4) {
_currentIndexList.AddRange(new int[] { indexRemap[0], indexRemap[1], indexRemap[2] });
_currentIndexList.AddRange(new int[] { indexRemap[2], indexRemap[3], indexRemap[0] });
} else if (indexRemap.Length > 4) {
for (int i = indexRemap.Length - 1; i >= 2; i--) {
_currentIndexList.AddRange(new int[] { indexRemap[0], indexRemap[i - 1], indexRemap[i] });
}
}
PushedFaceCount++;
}
public OBJObjectBuilder(string name, OBJLoader loader) {
_name = name;
_loader = loader;
}
}
}