-
Notifications
You must be signed in to change notification settings - Fork 115
/
MapSODemand.cs
executable file
·502 lines (445 loc) · 19.9 KB
/
MapSODemand.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
* Kopernicus Planetary System Modifier
* -------------------------------------------------------------
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* This library is intended to be used as a plugin for Kerbal Space Program
* which is copyright 2011-2017 Squad. Your usage of Kerbal Space Program
* itself is governed by the terms of its EULA, not the license above.
*
* https://kerbalspaceprogram.com
*/
using System;
using UnityEngine;
namespace Kopernicus
{
namespace OnDemand
{
/// <summary>
/// MapSO Replacement to support Texture streaming
/// </summary>
public class MapSODemand : MapSO, ILoadOnDemand
{
// BitPerPixels is always 4
protected new const Int32 _bpp = 4;
// Representation of the map
protected new Texture2D _data { get; set; }
// States
public Boolean IsLoaded { get; set; }
public Boolean AutoLoad { get; set; }
// Path of the Texture
public String Path { get; set; }
// MapDepth
public new MapDepth Depth { get; set; }
/// <summary>
/// Load the Map
/// </summary>
public void Load()
{
// Check if the Map is already loaded
if (IsLoaded)
return;
// Load the Map
Texture2D map = OnDemandStorage.LoadTexture(Path, false, false, false);
// If the map isn't null
if (map != null)
{
CreateMap(Depth, map);
IsLoaded = true;
Events.OnMapSOLoad.Fire(this);
Debug.Log("[OD] ---> Map " + name + " enabling self. Path = " + Path);
return;
}
// Return nothing
Debug.Log("[OD] ERROR: Failed to load map " + name + " at path " + Path);
}
/// <summary>
/// Unload the map
/// </summary>
public void Unload()
{
// We can only destroy the map, if it is loaded
if (!IsLoaded)
return;
// Nuke the map
DestroyImmediate(_data);
// Set flags
IsLoaded = false;
// Event
Events.OnMapSOUnload.Fire(this);
// Log
Debug.Log("[OD] <--- Map " + name + " disabling self. Path = " + Path);
}
/// <summary>
/// Create a map from a Texture2D
/// </summary>
public override void CreateMap(MapDepth depth, Texture2D tex)
{
// If the Texture is null, abort
if (tex == null)
{
Debug.Log("[OD] ERROR: Failed to load map");
return;
}
// Set _data
_data = tex;
// Variables
_width = tex.width;
_height = tex.height;
Depth = depth;
_rowWidth = _width * _bpp;
// We're compiled
_isCompiled = true;
}
public MapSODemand()
: base()
{
// register here or on PQSMod creation?
// for now we'll do it on creation (i.e. elsewhere)
}
// GetPixelByte
public override byte GetPixelByte(Int32 x, Int32 y)
{
// If we aren't loaded....
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelbyte with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return 0;
}
return (byte)(_data.GetPixel(x, y).r * Float2Byte);
}
// GetPixelColor - Double
public override Color GetPixelColor(Double x, Double y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelColD with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return Color.black;
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return Color.Lerp(
Color.Lerp(
GetPixelColor(coords.xFloor, coords.yFloor),
GetPixelColor(coords.xCeiling, coords.yFloor),
coords.u),
Color.Lerp(
GetPixelColor(coords.xFloor, coords.yCeiling),
GetPixelColor(coords.xCeiling, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelColor - Float
public override Color GetPixelColor(Single x, Single y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelColF with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return Color.black;
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return Color.Lerp(
Color.Lerp(
GetPixelColor(coords.xFloor, coords.yFloor),
GetPixelColor(coords.xCeiling, coords.yFloor),
coords.u),
Color.Lerp(
GetPixelColor(coords.xFloor, coords.yCeiling),
GetPixelColor(coords.xCeiling, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelColor - Int
public override Color GetPixelColor(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelColI with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return Color.black;
}
return _data.GetPixel(x, y);
}
// GetPixelColor32 - Double
public override Color GetPixelColor32(Double x, Double y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelCol32D with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return Color.black;
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return Color32.Lerp(
Color32.Lerp(
GetPixelColor32(coords.xFloor, coords.yFloor),
GetPixelColor32(coords.xCeiling, coords.yFloor),
coords.u),
Color32.Lerp(
GetPixelColor32(coords.xFloor, coords.yCeiling),
GetPixelColor32(coords.xCeiling, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelColor32 - Float - Honestly Squad, why are they named GetPixelColor32, but return normal Colors instead of Color32?
public override Color GetPixelColor32(Single x, Single y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelCol32F with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return Color.black;
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return Color32.Lerp(
Color32.Lerp(
GetPixelColor32(coords.xFloor, coords.yFloor),
GetPixelColor32(coords.xCeiling, coords.yFloor),
coords.u),
Color32.Lerp(
GetPixelColor32(coords.xFloor, coords.yCeiling),
GetPixelColor32(coords.xCeiling, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelColor32 - Int
public override Color32 GetPixelColor32(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelCol32I with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return new Color32();
}
return _data.GetPixel(x, y);
}
// GetPixelFloat - Double
public override Single GetPixelFloat(Double x, Double y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelFloatD with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return 0f;
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return Mathf.Lerp(
Mathf.Lerp(
GetPixelFloat(coords.xFloor, coords.yFloor),
GetPixelFloat(coords.xCeiling, coords.yFloor),
coords.u),
Mathf.Lerp(
GetPixelFloat(coords.xFloor, coords.yCeiling),
GetPixelFloat(coords.xCeiling, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelFloat - Float
public override Single GetPixelFloat(Single x, Single y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelFloatF with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return 0f;
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return Mathf.Lerp(
Mathf.Lerp(
GetPixelFloat(coords.xFloor, coords.yFloor),
GetPixelFloat(coords.xCeiling, coords.yFloor),
coords.u),
Mathf.Lerp(
GetPixelFloat(coords.xFloor, coords.yCeiling),
GetPixelFloat(coords.xCeiling, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelFloat - Integer
public override Single GetPixelFloat(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelFloatI with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return 0f;
}
Color pixel = _data.GetPixel(x, y);
Single value = 0f;
if (Depth == MapDepth.Greyscale)
value = pixel.r;
else if (Depth == MapDepth.HeightAlpha)
value = pixel.r + pixel.a;
else if (Depth == MapDepth.RGB)
value = pixel.r + pixel.g + pixel.b;
else if (Depth == MapDepth.RGBA)
value = pixel.r + pixel.g + pixel.b + pixel.a;
// Enhanced support for L8 .dds
if (_data.format == TextureFormat.Alpha8)
value = pixel.a;
return value / (Int32)Depth;
}
// GetPixelHeightAlpha - Double
public override HeightAlpha GetPixelHeightAlpha(Double x, Double y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelHeightAlphaD with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return new HeightAlpha(0f, 0f);
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return HeightAlpha.Lerp(
HeightAlpha.Lerp(
GetPixelHeightAlpha(coords.xFloor, coords.yFloor),
GetPixelHeightAlpha(coords.xCeiling, coords.yFloor),
coords.u),
HeightAlpha.Lerp(
GetPixelHeightAlpha(coords.xFloor, coords.yCeiling),
GetPixelHeightAlpha(coords.xFloor, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelHeightAlpha - Float
public override HeightAlpha GetPixelHeightAlpha(Single x, Single y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelHeightAlphaF with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return new HeightAlpha(0f, 0f);
}
BilinearCoords coords = ConstructBilinearCoords(x, y);
return HeightAlpha.Lerp(
HeightAlpha.Lerp(
GetPixelHeightAlpha(coords.xFloor, coords.yFloor),
GetPixelHeightAlpha(coords.xCeiling, coords.yFloor),
coords.u),
HeightAlpha.Lerp(
GetPixelHeightAlpha(coords.xFloor, coords.yCeiling),
GetPixelHeightAlpha(coords.xFloor, coords.yCeiling),
coords.u),
coords.v);
}
// GetPixelHeightAlpha - Int
public override HeightAlpha GetPixelHeightAlpha(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelHeightAlphaI with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return new HeightAlpha(0f, 0f);
}
Color pixel = _data.GetPixel(x, y);
if (Depth == (MapDepth.HeightAlpha | MapDepth.RGBA))
return new HeightAlpha(pixel.r, pixel.a);
else
return new HeightAlpha(pixel.r, 1f);
}
// GreyByte
public override byte GreyByte(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting GreyByteI with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return 0;
}
return (byte)(Float2Byte * _data.GetPixel(x, y).r);
}
// GreyFloat
public override Single GreyFloat(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting GreyFloat with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return 0f;
}
return _data.GetPixel(x, y).grayscale;
}
// PixelByte
public override byte[] PixelByte(Int32 x, Int32 y)
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: getting pixelByte with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return new byte[_bpp];
}
Color c = _data.GetPixel(x, y);
if (Depth == MapDepth.Greyscale)
return new byte[] { (byte)c.r };
else if (Depth == MapDepth.HeightAlpha)
return new byte[] { (byte)c.r, (byte)c.a };
else if (Depth == MapDepth.RGB)
return new byte[] { (byte)c.r, (byte)c.g, (byte)c.b };
else
return new byte[] { (byte)c.r, (byte)c.g, (byte)c.b, (byte)c.a };
}
// CompileToTexture
public override Texture2D CompileToTexture()
{
if (!IsLoaded)
{
if (OnDemandStorage.onDemandLogOnMissing) Debug.Log("[OD] ERROR: compiling with unloaded map " + name + " of path " + Path + ", autoload = " + AutoLoad);
if (AutoLoad) Load();
else return new Texture2D(_width, _height);
}
Texture2D compiled = Instantiate(_data) as Texture2D;
compiled.Apply(false, true);
return compiled;
}
// ConstructBilinearCoords from Double
protected new BilinearCoords ConstructBilinearCoords(Double x, Double y)
{
// Create the struct
BilinearCoords coords = new BilinearCoords();
// Floor
x = Math.Abs(x - Math.Floor(x));
y = Math.Abs(y - Math.Floor(y));
// X to U
coords.x = x * _width;
coords.xFloor = (Int32)Math.Floor(coords.x);
coords.xCeiling = (Int32)Math.Ceiling(coords.x);
coords.u = (Single)(coords.x - coords.xFloor);
if (coords.xCeiling == _width) coords.xCeiling = 0;
// Y to V
coords.y = y * _height;
coords.yFloor = (Int32)Math.Floor(coords.y);
coords.yCeiling = (Int32)Math.Ceiling(coords.y);
coords.v = (Single)(coords.y - coords.yFloor);
if (coords.yCeiling == _height) coords.yCeiling = 0;
// We're done
return coords;
}
// ConstructBilinearCoords from Single
protected new BilinearCoords ConstructBilinearCoords(Single x, Single y)
{
return ConstructBilinearCoords((Double)x, (Double)y);
}
// BilinearCoords
public struct BilinearCoords
{
public Double x, y;
public Int32 xCeiling, xFloor, yCeiling, yFloor;
public Single u, v;
}
}
}
}