-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureHelper.cs
282 lines (259 loc) · 11.5 KB
/
TextureHelper.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Collections.Generic;
namespace BrightExistence.SimpleTools
{
public enum Rotation
{
zero = 0,
counter90d = 1,
counter180d = 2,
counter270d = 3
}
public enum TexturePart
{
albedo = 0,
emissive = 1,
height = 2,
normal = 3
}
public class SpecificTexture
{
/// <summary>
/// Name of texture excluding prefix. Ex: MyTextureTop NOT MyHandle.MyMod.MyTextureTop
/// </summary>
public string Name { get; protected set; }
/// <summary>
/// Name of texture including prefix Ex: MyHandle.MyMod.MyTextureTop NOT MyTextureTop
/// </summary>
public string NAMESPACE { get; protected set; }
/// <summary>
/// The string by which this texture group be referenced.
/// </summary>
public string ID
{
get
{
if (NAMESPACE == null) return this.Name;
else return NAMESPACE + "." + Name;
}
}
public string AlbedoPath;
public string NormalPath;
public string EmissivePath;
public string HeightPath;
public SpecificTexture(string strName, string strNAMESPACE = null)
{
Name = strName == null ? "NewSpecificTexture" : strName;
NAMESPACE = strNAMESPACE;
try
{
if (!Variables.SpecificTextures.Contains(this)) Variables.SpecificTextures.Add(this);
}
catch (Exception)
{
Pipliz.Log.Write("{0} : WARNING : Specific texture {1} could not be automatically added to auto-load list. Make sure you explicityly added it.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
}
/// <summary>
/// Returns this item as a ItemTypeServer.TextureMapping struct. (Note this will strip name, ID, and namespace properties.)
/// </summary>
/// <returns>ItemTypeServer.TextureMapping struct</returns>
protected ItemTypesServer.TextureMapping asTextureMapping()
{
ItemTypesServer.TextureMapping thisMapping = new ItemTypesServer.TextureMapping(new Pipliz.JSON.JSONNode());
if (this.AlbedoPath != null) thisMapping.AlbedoPath = this.AlbedoPath;
if (this.NormalPath != null) thisMapping.NormalPath = this.NormalPath;
if (this.EmissivePath != null) thisMapping.EmissivePath = this.EmissivePath;
if (this.HeightPath != null) thisMapping.HeightPath = this.HeightPath;
return thisMapping;
}
/// <summary>
/// Registers this texture in the server database. Should be called during the afterSelectedWorld callback method.
/// </summary>
public void registerTexture()
{
Pipliz.Log.Write("{0}: Registering specific texture as {1}", NAMESPACE == null ? "" : NAMESPACE, this.ID);
foreach (string S in new List <string> { this.AlbedoPath, this.EmissivePath, this.HeightPath, this.NormalPath})
{
if (S != null)
{
if (System.IO.File.Exists(S))
{
Pipliz.Log.Write("{0}: Looks good, albedo file exists.", NAMESPACE == null ? "" : NAMESPACE);
}
else
{
Pipliz.Log.WriteError("{0}: ERROR! Registering texture to a file {1} which does not exist!", NAMESPACE == null ? "" : NAMESPACE, S);
}
}
}
ItemTypesServer.SetTextureMapping(this.ID, this.asTextureMapping());
Pipliz.Log.Write("{0}: Specific texture registered: {1}", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
}
/// <summary>
/// Front-end for built-in ItemTypesServer.TextureMapping class to enable auto-registration.
/// </summary>
public class TextureGroup
{
/// <summary>
/// Name of texture, excluding any prefixes. Ex: myTexture NOT myHandle.myMod.myTexture
/// </summary>
public string Name = "MyTexture";
/// <summary>
/// Prefix used to generate ID. Ex: myHandle.myMod
/// </summary>
protected string NAMESPACE;
/// <summary>
/// The non-rotated (default) texture files.
/// </summary>
public SpecificTexture Default;
/// <summary>
/// The texture files rotated 90 degrees counter-clockwise.
/// </summary>
public SpecificTexture counter90d;
/// <summary>
/// The texture files rotated 180 degrees counter-clockwise.
/// </summary>
public SpecificTexture counter270d;
/// <summary>
/// The texture files rotated 270 degrees counter-clockwise.
/// </summary>
public SpecificTexture counter180d;
/// <summary>
/// </summary>
/// <param name="strName">Name of texture, excluding any prefixes. Ex: myTexture NOT myHandle.myMod.myTexture</param>
/// <param name="strNAMESPACE">Prefix used to generate ID. Ex: myHandle.myMod</param>
public TextureGroup(string strName, string strNAMESPACE = null)
{
Name = (strName == null || strName.Length < 1) ? "NewTexture" : strName;
NAMESPACE = strNAMESPACE == null ? "" : strNAMESPACE;
Default = new SpecificTexture(this.Name, this.NAMESPACE);
Pipliz.Log.Write("{0}: Initialized texturegroup {1}, it is not yet registered.", NAMESPACE == null ? "" : NAMESPACE, this.Name);
}
/// <summary>
/// Automates specific texture retrieval, accounting for null entries.
/// </summary>
/// <param name="facing">Orientation of desired texture.</param>
/// <returns>Returns the specified specific texture, or the default texture if the specified texture is null.</returns>
public SpecificTexture getTexture (Rotation facing)
{
switch (facing)
{
case Rotation.counter180d:
return this.counter180d == null ? this.Default : this.counter180d;
case Rotation.zero:
return this.Default;
case Rotation.counter270d:
return this.counter270d == null ? this.Default : this.counter270d;
case Rotation.counter90d:
return this.counter90d == null ? this.Default : this.counter90d;
default:
return this.Default;
}
}
/// <summary>
/// Automates setting specific textures, preventing accidental reference exceptions.
/// </summary>
/// <param name="whenRotated">Orientation of desired texture.</param>
/// <param name="part">Which part of that texture is being set.</param>
/// <param name="path">Path to physical texture file.</param>
public void setRotatedTexture(Rotation whenRotated, TexturePart part, string path)
{
switch (whenRotated)
{
case Rotation.zero:
if (this.Default == null) this.Default = new SpecificTexture(this.Name, this.NAMESPACE);
switch (part)
{
case TexturePart.albedo:
this.Default.AlbedoPath = path;
break;
case TexturePart.emissive:
this.Default.EmissivePath = path;
break;
case TexturePart.height:
this.Default.HeightPath = path;
break;
case TexturePart.normal:
this.Default.NormalPath = path;
break;
default:
break;
}
break;
case Rotation.counter90d:
if (this.counter90d == null) this.counter90d = new SpecificTexture(this.Name+"z+", this.NAMESPACE);
switch (part)
{
case TexturePart.albedo:
this.counter90d.AlbedoPath = path;
break;
case TexturePart.emissive:
this.counter90d.EmissivePath = path;
break;
case TexturePart.height:
this.counter90d.HeightPath = path;
break;
case TexturePart.normal:
this.counter90d.NormalPath = path;
break;
default:
break;
}
break;
case Rotation.counter180d:
if (this.counter180d == null) this.counter180d = new SpecificTexture(this.Name + "x-", this.NAMESPACE);
switch (part)
{
case TexturePart.albedo:
this.counter180d.AlbedoPath = path;
break;
case TexturePart.emissive:
this.counter180d.EmissivePath = path;
break;
case TexturePart.height:
this.counter180d.HeightPath = path;
break;
case TexturePart.normal:
this.counter180d.NormalPath = path;
break;
default:
break;
}
break;
case Rotation.counter270d:
if (this.counter270d == null) this.counter270d = new SpecificTexture(this.Name + "z-", this.NAMESPACE);
switch (part)
{
case TexturePart.albedo:
this.counter270d.AlbedoPath = path;
break;
case TexturePart.emissive:
this.counter270d.EmissivePath = path;
break;
case TexturePart.height:
this.counter270d.HeightPath = path;
break;
case TexturePart.normal:
this.counter270d.NormalPath = path;
break;
default:
break;
}
break;
}
}
/// <summary>
/// Registers this texture in the server database. Should be called during the afterSelectedWorld callback method.
/// </summary>
public void registerGroup()
{
Pipliz.Log.Write("{0}: Registering texture group {1}", NAMESPACE == null ? "" : NAMESPACE, this.Name);
this.Default.registerTexture();
if (this.counter180d != null) counter180d.registerTexture();
if (this.counter90d != null) counter90d.registerTexture();
if (this.counter270d != null) counter270d.registerTexture();
}
}
}