-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSpriteProvider.cs
More file actions
178 lines (149 loc) · 5.93 KB
/
SpriteProvider.cs
File metadata and controls
178 lines (149 loc) · 5.93 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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace OpenTibiaUnity.Core.Appearances
{
public enum SpriteLoadingStatus
{
Loading,
Failed,
Completed,
}
public class SpriteTypeImpl
{
public string File;
public ushort SpriteType;
public uint FirstSpriteId;
public uint LastSpriteId;
public uint AtlasId;
public Texture2D Texture;
}
public class CachedSprite
{
public uint id { get; set; }
public Texture2D texture { get; set; }
public Vector4 uv { get; set; }
public Vector2 size { get; set; }
public MaterialPropertyBlock materialProperyBlock { get; set; }
public void GenerateMaterialProps(MaterialPropertyBlock props) {
props.SetTexture("_MainTex", texture);
props.SetVector("_MainTex_UV", uv);
}
public void GenerateChannelsMaterialProps(MaterialPropertyBlock props) {
props.SetTexture("_ChannelsTex", texture);
props.SetVector("_ChannelsTex_UV", uv);
}
}
public sealed class SpritesProvider {
List<SpriteTypeImpl> _spriteSheet;
List<CachedSprite> _cachedSprites = new List<CachedSprite>();
private static Vector2 GetSpriteSize(uint spriteType) {
var width = spriteType / 100;
var height = spriteType % 100;
return new Vector2(width, height);
}
public IEnumerator Parse(System.IO.Stream stream) {
using (var reader = new System.IO.BinaryReader(stream)) {
uint total = reader.ReadUInt32();
_spriteSheet = new List<SpriteTypeImpl>((int)total);
for (uint i = 0; i < total; i++) {
var atlasId = reader.ReadUInt32();
var spriteType = reader.ReadUInt16();
var firstSpriteId = reader.ReadUInt32();
var lastSpriteId = reader.ReadUInt32();
var texture = new Texture2D(1, 1);
var size = reader.ReadUInt32();
var data = new byte[size];
reader.Read(data, 0, (int)size);
texture.LoadImage(data);
var spriteImpl = new SpriteTypeImpl {
AtlasId = atlasId,
FirstSpriteId = firstSpriteId,
LastSpriteId = lastSpriteId,
SpriteType = spriteType,
Texture = texture,
};
_spriteSheet.Add(spriteImpl);
// load a batch of 5 textures at once
if (i % 5 == 0)
yield return null;
}
}
}
public void Dispose() {
_spriteSheet.Clear();
_cachedSprites.Clear();
}
private SpriteLoadingStatus GetSpriteInfo(uint spriteId, out Vector4 uv, out Vector2 size, out Texture2D texture) {
SpriteTypeImpl match = _spriteSheet.Find(m => spriteId >= m.FirstSpriteId && spriteId <= m.LastSpriteId);
var loadingStatus = SpriteLoadingStatus.Failed;
if (match != null) {
loadingStatus = SpriteLoadingStatus.Completed;
texture = match.Texture;
} else {
texture = null;
}
if (texture == null) {
uv = Vector4.zero;
size = Vector2.zero;
return loadingStatus;
}
size = GetSpriteSize(match.SpriteType) * Constants.FieldSize;
uint realId = spriteId - match.FirstSpriteId;
int texPerRow = (int)(texture.width / size.x);
float x = (realId % texPerRow) * size.x;
float y = texture.width - (realId / texPerRow * size.y) - size.y;
uv = new Vector4(size.x / texture.width, size.y / texture.height, x / texture.width, y / texture.height);
return loadingStatus;
}
public SpriteLoadingStatus GetSprite(uint spriteId, out CachedSprite cachedSprite) {
cachedSprite = FindCachedSprite(spriteId);
if (cachedSprite != null)
return SpriteLoadingStatus.Completed;
var loadingStatus = GetSpriteInfo(spriteId, out Vector4 uv, out Vector2 size, out Texture2D texture);
if (loadingStatus != SpriteLoadingStatus.Completed)
return loadingStatus;
cachedSprite = new CachedSprite {
id = spriteId,
uv = uv,
size = size,
texture = texture,
materialProperyBlock = new MaterialPropertyBlock(),
};
cachedSprite.GenerateMaterialProps(cachedSprite.materialProperyBlock);
InsertCachedSprite(cachedSprite);
return SpriteLoadingStatus.Completed;
}
private CachedSprite FindCachedSprite(uint spriteId) {
int l = 0, r = _cachedSprites.Count - 1;
while (l <= r) {
int i = l + (r - l) / 2;
var other = _cachedSprites[i];
if (other.id > spriteId)
l = i + 1;
else if (other.id < spriteId)
r = i - 1;
else
return other;
}
return null;
}
private void InsertCachedSprite(CachedSprite sprite) {
int l = 0, r = _cachedSprites.Count - 1;
while (l <= r) {
int i = l + (r - l) / 2;
var other = _cachedSprites[i];
if (other.id < sprite.id)
l = i + 1;
else if (other.id > sprite.id)
r = i - 1;
else
return;
}
_cachedSprites.Insert(l, sprite);
}
}
}