This repository was archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializedObject.cs
More file actions
208 lines (156 loc) · 6.82 KB
/
SerializedObject.cs
File metadata and controls
208 lines (156 loc) · 6.82 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
201
202
203
204
205
206
207
208
namespace VigorXR.Utilities.UNSTABLE
{
using UnityEngine;
using UnityEngine.ProBuilder;
using System.Collections.Generic;
using System.Linq;
public class SerializedObject : MonoBehaviour
{
Vector2[] uv;
Vector3[] verticies;
int[] triangles;
Vector3[] normals;
public Dictionary<int, int> materialIndexes = new Dictionary<int, int>();
private ObjectData GatheredObjectData = new ObjectData();
public SerializedType ObjectSerializationType = SerializedType.PlayerEmptyObject;
public void Serialize()
{
var handler = FindObjectOfType<BuildingUtilityNew>();
GatheredObjectData.ObjectType = ObjectSerializationType;
GatheredObjectData.GameObjectData.position = transform.position;
GatheredObjectData.GameObjectData.rotation = transform.rotation;
GatheredObjectData.GameObjectData.localScale = transform.localScale;
if (ObjectSerializationType == SerializedType.PlayerMeshObject)
{
var mesh = GetComponent<MeshFilter>().sharedMesh;
var renderer = GetComponent<MeshRenderer>();
var pb = GetComponent<ProBuilderMesh>();
GatheredObjectData.ProBuilderMeshData.uv = mesh.uv;
GatheredObjectData.ProBuilderMeshData.vertices = mesh.vertices;
GatheredObjectData.ProBuilderMeshData.triangles = mesh.triangles;
GatheredObjectData.ProBuilderMeshData.normals = mesh.normals;
GatheredObjectData.ProBuilderMeshData._SmaterialIndexes = materialIndexes;
}
}
public void Rebuild(ObjectData data)
{
var handler = FindObjectOfType<BuildingUtilityNew>();
GatheredObjectData = data;
transform.position = GatheredObjectData.GameObjectData.position;
transform.rotation = GatheredObjectData.GameObjectData.rotation;
transform.localScale = GatheredObjectData.GameObjectData.localScale;
if (GatheredObjectData.ObjectType == SerializedType.PlayerMeshObject)
{
var mesh = GetComponent<MeshFilter>();
var renderer = GetComponent<MeshRenderer>();
var pb = GetComponent<ProBuilderMesh>();
mesh.sharedMesh = new Mesh();
triangles = GatheredObjectData.ProBuilderMeshData.triangles;
verticies = GatheredObjectData.ProBuilderMeshData.vertices;
uv = GatheredObjectData.ProBuilderMeshData.uv;
normals = GatheredObjectData.ProBuilderMeshData.normals;
materialIndexes = GatheredObjectData.ProBuilderMeshData._SmaterialIndexes;
mesh.sharedMesh.vertices = verticies;
mesh.sharedMesh.triangles = triangles;
mesh.sharedMesh.uv = uv;
mesh.sharedMesh.normals = normals;
pb.sharedVertices = new List<SharedVertex>();
mesh.sharedMesh.RecalculateNormals();
mesh.sharedMesh.RecalculateBounds();
pb.positions = verticies;
List<Face> faces = new List<Face>();
for (int i = 0; i < triangles.Length / 3; i++)
{
int[] indices =
{
triangles[(i * 3) + 0],
triangles[(i * 3) + 1],
triangles[(i * 3) + 2],
};
var face = new Face(indices);
faces.Add(face);
}
pb.faces = faces;
foreach(var item in pb.faces)
{
try
{
pb.SetMaterial(new Face[] { item }, handler.ObjectMaterials[materialIndexes[pb.faces.IndexOf(item)]]);
}
catch
{
this.SetFaceMaterial(new Face[] { item }, 0);
}
}
pb.ToMesh();
this.GetComponent<ProBuilderMesh>().Refresh();
this.GetComponent<MeshCollider>().sharedMesh = null;
this.GetComponent<MeshCollider>().sharedMesh = mesh.sharedMesh;
var _materials = renderer.materials.ToList();
_materials.RemoveAt(0);
renderer.materials = _materials.ToArray();
}
}
public void SetFaceMaterial(Face[] faces, int materialID)
{
var handler = FindObjectOfType<BuildingUtilityNew>();
if (ObjectSerializationType != SerializedType.PlayerMeshObject)
return;
var pb = this.GetComponent<ProBuilderMesh>();
var material = handler.ObjectMaterials[materialID];
pb.SetMaterial(faces, material);
foreach(var item in faces)
{
var index = pb.faces.IndexOf(item);
try
{
materialIndexes[index] = materialID;
}
catch
{
materialIndexes.Add(index, materialID);
}
}
pb.ToMesh();
pb.Refresh();
}
public ObjectData PrepareForSerialization()
{
Serialize();
return GatheredObjectData;
}
}
[System.Serializable]
public class ObjectData
{
//Holds ALL data for a serialized custom room object.
//The serialization type of the object.
[SerializeField] public SerializedType ObjectType { get; set; } = new SerializedType();
//The mesh data for the object.
[SerializeField] public ProBuilderMeshData ProBuilderMeshData { get; set; } = new ProBuilderMeshData();
//Information about the object's GameObject.
[SerializeField] public GameObjectData GameObjectData { get; set; } = new GameObjectData();
}
[System.Serializable]
public class ProBuilderMeshData
{
[SerializeField] public Vector2[] uv { get; set; }
[SerializeField] public Vector3[] vertices { get; set; }
[SerializeField] public int[] triangles { get; set; }
[SerializeField] public Vector3[] normals { get; set; }
[SerializeField] public Dictionary<int, int> _SmaterialIndexes { get; set; }
}
[System.Serializable]
public class GameObjectData
{
[SerializeField] public Vector3 position { get; set; }
[SerializeField] public Quaternion rotation { get; set; }
[SerializeField] public Vector3 localScale { get; set; }
}
[System.Serializable]
public enum SerializedType
{
PlayerMeshObject,
PlayerEmptyObject
}
}