Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
MeshPass.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEngine;
5using UnityEngine.Rendering;
6
7public class MeshPass : ScriptableObject
8{
9 public const int TokenLiquid = 10000;
10
11 public const int TokenLowWall = 1000000;
12
13 public const int TokenLowWallDefault = 3000000;
14
16
18
20
21 public Material mat;
22
23 public Mesh mesh;
24
26
27 public Sprite sprite;
28
29 public bool setTile;
30
31 public bool setColor;
32
33 public bool setMatColor;
34
35 public bool setExtra;
36
37 public int renderQueue;
38
40
41 public FilterMode filter;
42
43 public int lastCount;
44
45 public int lastBatchCount;
46
47 [NonSerialized]
48 public bool haveSubPass;
49
50 [NonSerialized]
51 public bool haveShadowPass;
52
53 [NonSerialized]
54 public bool haveSnowPass;
55
56 [NonSerialized]
57 public int idx;
58
59 [NonSerialized]
60 public int batchIdx;
61
62 [NonSerialized]
63 public int batchSize = 1023;
64
65 [NonSerialized]
66 public List<MeshBatch> batches = new List<MeshBatch>();
67
68 [NonSerialized]
69 private bool initialized;
70
71 [NonSerialized]
72 private bool resize;
73
74 [NonSerialized]
75 public Texture preserveTex;
76
78
79 private Vector3 tempV = Vector3.zero;
80
81 private Vector2 tempE = Vector2.zero;
82
83 [NonSerialized]
84 private bool first = true;
85
86 public float meshX
87 {
88 get
89 {
90 return pmesh.size.x;
91 }
92 set
93 {
94 pmesh.size.x = value;
95 }
96 }
97
98 public void Init()
99 {
100 batchSize = ((batches.Count > 0) ? batches[0].size : 1023);
101 if (!initialized || mesh == null)
102 {
103 if ((bool)subPass)
104 {
105 subPass.Init();
106 }
107 if ((bool)snowPass)
108 {
109 snowPass.Init();
110 }
111 if ((bool)shadowPass)
112 {
114 }
115 batches.Add(new MeshBatch(this));
116 _Refresh();
117 initialized = true;
118 }
119 idx = (batchIdx = 0);
120 }
121
122 public void Add(Point point, float tile = 0f, float color = 0f)
123 {
124 Vector3 vector = point.Position();
126 _batch.matrices[idx].m03 = vector.x;
127 _batch.matrices[idx].m13 = vector.y;
128 _batch.matrices[idx].m23 = vector.z;
129 _batch.tiles[idx] = tile;
130 if (setColor)
131 {
132 _batch.colors[idx] = color;
133 }
134 idx++;
135 if (idx == batchSize)
136 {
137 NextBatch();
138 }
139 }
140
141 public void Add(ref Vector3 v, float tile = 0f, float color = 0f)
142 {
144 _batch.matrices[idx].m03 = v.x;
145 _batch.matrices[idx].m13 = v.y;
146 _batch.matrices[idx].m23 = v.z;
147 _batch.tiles[idx] = tile;
148 if (setColor)
149 {
150 _batch.colors[idx] = color;
151 }
152 idx++;
153 if (idx == batchSize)
154 {
155 NextBatch();
156 }
157 }
158
159 public void Add(float x, float y, float z, float tile = 0f, float color = 0f)
160 {
162 _batch.matrices[idx].m03 = x;
163 _batch.matrices[idx].m13 = y;
164 _batch.matrices[idx].m23 = z;
165 _batch.tiles[idx] = tile;
166 if (setColor)
167 {
168 _batch.colors[idx] = color;
169 }
170 idx++;
171 if (idx == batchSize)
172 {
173 NextBatch();
174 }
175 }
176
177 public void AddWithScale(float x, float y, float z, int tile, float scale)
178 {
180 Matrix4x4[] matrices = _batch.matrices;
181 float num = (0f - scale) / 2f + 0.5f;
182 matrices[idx].m03 = x + num;
183 matrices[idx].m13 = y + num;
184 matrices[idx].m23 = z;
185 matrices[idx].m00 = scale;
186 matrices[idx].m11 = scale;
187 _batch.tiles[idx] = tile;
188 idx++;
189 if (idx == batchSize)
190 {
191 NextBatch();
192 }
193 }
194
195 public void AddShadow(MeshPassParam p, ref Vector3 fix)
196 {
198 _batch.matrices[idx].SetTRS(Vector3.zero, Quaternion.Euler(0f, 0f, 0f), Vector3.one);
199 _batch.matrices[idx].m03 = p.x + fix.x;
200 _batch.matrices[idx].m13 = p.y + fix.y;
201 _batch.matrices[idx].m23 = p.z + fix.z;
202 if (setTile)
203 {
204 _batch.tiles[idx] = p.tile;
205 }
206 idx++;
207 if (idx == batchSize)
208 {
209 NextBatch();
210 }
211 }
212
213 public void AddShadow(float x, float y, float z, ShadowData.Item s, SourcePref pref, int dir = 0, bool snow = false)
214 {
215 int angle = s.angle;
216 float num = (float)s.scaleX * 0.01f;
217 float num2 = (float)s.scaleY * 0.01f;
218 bool flag = dir % 2 == 1;
219 int num3 = (flag ? 1 : (-1));
221 Matrix4x4[] matrices = _batch.matrices;
222 matrices[idx].m03 = x - 0.01f * (float)((dir < 2) ? (flag ? pref.shadowRX : pref.shadowX) : (flag ? pref.shadowBRX : pref.shadowBX)) + ((0.7f * (0f - num) + 0.7f) * (float)num3 + (float)s.x * 0.01f) * (float)num3;
223 matrices[idx].m13 = y - 0.01f * (float)((dir < 2) ? (flag ? pref.shadowRY : pref.shadowY) : (flag ? pref.shadowBRY : pref.shadowBY)) + 0.6f * (0f - num2) + 0.6f + (float)s.y * 0.01f;
224 matrices[idx].m23 = z;
225 matrices[idx].m00 = num;
226 matrices[idx].m11 = num2;
227 if (angle == 0)
228 {
229 matrices[idx].m00 = num;
230 matrices[idx].m01 = 0f;
231 matrices[idx].m10 = 0f;
232 matrices[idx].m11 = num2;
233 }
234 else
235 {
236 tempV.z = -angle;
237 Quaternion quaternion = Quaternion.Euler(0f, 0f, -angle);
238 tempE.x = 1f - quaternion.z * quaternion.z * 2f;
239 tempE.y = (0f - quaternion.w) * quaternion.z * 2f;
240 matrices[idx].m00 = tempE.x * num;
241 matrices[idx].m01 = tempE.y * num2;
242 matrices[idx].m10 = (0f - tempE.y) * num;
243 matrices[idx].m11 = tempE.x * num2;
244 }
245 _batch.tiles[idx] = (s.tile + (snow ? 1000 : 0)) * num3;
246 idx++;
247 if (idx == batchSize)
248 {
249 NextBatch();
250 }
251 }
252
253 public void AddWithRotation(float x, float y, float z, float sx, float sy, int tile, float angle, bool flip)
254 {
255 float num = 1f;
256 float num2 = 1f;
257 int num3 = ((!flip) ? 1 : (-1));
259 Matrix4x4[] matrices = _batch.matrices;
260 matrices[idx].m03 = x + ((0.7f * (0f - num) + 0.7f) * (float)num3 + sx * 0.01f) * (float)num3;
261 matrices[idx].m13 = y + 0.6f * (0f - num2) + 0.6f + sy * 0.01f;
262 matrices[idx].m23 = z;
263 matrices[idx].m00 = num;
264 matrices[idx].m11 = num2;
265 tempV.z = 0f - angle;
266 Quaternion quaternion = Quaternion.Euler(0f, 0f, 0f - angle);
267 tempE.x = 1f - quaternion.z * quaternion.z * 2f;
268 tempE.y = (0f - quaternion.w) * quaternion.z * 2f;
269 matrices[idx].m00 = tempE.x * num;
270 matrices[idx].m01 = tempE.y * num2;
271 matrices[idx].m10 = (0f - tempE.y) * num;
272 matrices[idx].m11 = tempE.x * num2;
273 _batch.tiles[idx] = tile * num3;
274 idx++;
275 if (idx == batchSize)
276 {
277 NextBatch();
278 }
279 }
280
281 public void Add(MeshPassParam p)
282 {
284 _batch.matrices[idx].m03 = p.x;
285 _batch.matrices[idx].m13 = p.y;
286 _batch.matrices[idx].m23 = p.z;
287 if (setTile)
288 {
289 _batch.tiles[idx] = p.tile;
290 }
291 if (setColor)
292 {
293 _batch.colors[idx] = p.color;
294 }
295 if (setMatColor)
296 {
298 }
299 idx++;
300 if (idx == batchSize)
301 {
302 NextBatch();
303 }
304 }
305
306 public void Add(float x, float y, float z, float tile, float color, float matColor = 0f)
307 {
309 _batch.matrices[idx].m03 = x;
310 _batch.matrices[idx].m13 = y;
311 _batch.matrices[idx].m23 = z;
312 _batch.tiles[idx] = tile;
313 if (setColor)
314 {
315 _batch.colors[idx] = color;
316 }
317 if (setMatColor)
318 {
319 _batch.matColors[idx] = matColor;
320 }
321 idx++;
322 if (idx == batchSize)
323 {
324 NextBatch();
325 }
326 }
327
328 public void Add(MeshPassParam p, float tile, float color, float matColor = 0f)
329 {
331 _batch.matrices[idx].m03 = p.x;
332 _batch.matrices[idx].m13 = p.y;
333 _batch.matrices[idx].m23 = p.z;
334 _batch.tiles[idx] = tile;
335 if (setColor)
336 {
337 _batch.colors[idx] = color;
338 }
339 if (setMatColor)
340 {
341 _batch.matColors[idx] = matColor;
342 }
343 idx++;
344 if (idx == batchSize)
345 {
346 NextBatch();
347 }
348 }
349
350 public void Draw()
351 {
352 if (haveSnowPass)
353 {
354 snowPass.Draw();
355 }
356 if (haveSubPass)
357 {
358 subPass.Draw();
359 }
360 if (haveShadowPass)
361 {
363 }
364 if (idx == 0 && batchIdx == 0)
365 {
366 return;
367 }
368 for (int i = 0; i < batchIdx + 1; i++)
369 {
370 int num = ((i == batchIdx) ? idx : batches[i].size);
371 if (num == 0)
372 {
373 break;
374 }
375 if (setTile)
376 {
377 batches[i].mpb.SetFloatArray("_Tiles", batches[i].tiles);
378 }
379 if (setColor)
380 {
381 batches[i].mpb.SetFloatArray("_Color", batches[i].colors);
382 }
383 if (setMatColor)
384 {
385 batches[i].mpb.SetFloatArray("_MatColor", batches[i].matColors);
386 }
387 if (renderQueue != 0)
388 {
389 if (batches[i].mat == null)
390 {
391 batches[i].mat = new Material(mat);
392 }
393 batches[i].mat.renderQueue = renderQueue + i;
394 Graphics.DrawMeshInstanced(mesh, 0, batches[i].mat, batches[i].matrices, num, batches[i].mpb, ShadowCastingMode.Off, receiveShadows: false, 0, Camera.main);
395 }
396 else
397 {
398 Graphics.DrawMeshInstanced(mesh, 0, mat, batches[i].matrices, num, batches[i].mpb, ShadowCastingMode.Off, receiveShadows: false, 0, Camera.main);
399 }
400 }
403 idx = (batchIdx = 0);
404 if (resize)
405 {
406 Debug.Log("#pass Resize Pass:" + base.name);
407 resize = false;
408 batches[0] = new MeshBatch(this);
409 }
410 }
411
412 public void DrawEmpty()
413 {
414 idx = (batchIdx = 0);
415 }
416
417 public void NextBatch()
418 {
419 idx = 0;
420 batchIdx++;
421 if (batchIdx >= batches.Count)
422 {
423 if (batchSize != 1023)
424 {
425 batchSize = 1023;
426 resize = true;
427 }
428 batches.Add(new MeshBatch(this));
429 }
430 }
431
432 private Mesh SpriteToMesh(Sprite sprite)
433 {
434 Mesh obj = new Mesh();
435 obj.SetVertices(Array.ConvertAll(sprite.vertices, (Converter<Vector2, Vector3>)((Vector2 c) => c)).ToList());
436 obj.uv = sprite.uv;
437 obj.SetTriangles(Array.ConvertAll(sprite.triangles, (Converter<ushort, int>)((ushort c) => c)), 0);
438 return obj;
439 }
440
441 public override string ToString()
442 {
443 return base.name;
444 }
445
446 public void Refresh()
447 {
448 if (Application.isPlaying)
449 {
450 UnityEngine.Object.DestroyImmediate(mesh);
451 MeshPass[] passes = Core.Instance.scene.passes;
452 foreach (MeshPass obj in passes)
453 {
454 obj.initialized = false;
455 obj.batches.Clear();
456 obj.Init();
457 }
458 }
459 }
460
461 public void OnValidate()
462 {
463 Refresh();
464 }
465
466 public void _Refresh()
467 {
471 if (!mesh)
472 {
473 if ((bool)pmesh)
474 {
475 mesh = pmesh.GetMesh();
476 }
477 else if ((bool)sprite)
478 {
480 }
481 }
482 if (haveSnowPass)
483 {
484 snowPass.mesh = mesh;
485 }
486 if (haveSubPass)
487 {
488 subPass.mesh = mesh;
489 }
490 if (haveShadowPass)
491 {
492 shadowPass.mesh = mesh;
493 }
494 }
495}
if(item3.idFile==idFirstFile &&item3.id==idFirstTopic)
Definition: UIBook.cs:627
Definition: Core.cs:14
static new Core Instance
Definition: Core.cs:15
Scene scene
Definition: Core.cs:65
float[] matColors
Definition: MeshBatch.cs:11
Matrix4x4[] matrices
Definition: MeshBatch.cs:5
float[] tiles
Definition: MeshBatch.cs:7
float[] colors
Definition: MeshBatch.cs:9
const int TokenLiquid
Definition: MeshPass.cs:9
bool setExtra
Definition: MeshPass.cs:35
void Refresh()
Definition: MeshPass.cs:446
MeshPass subPass
Definition: MeshPass.cs:15
void Add(ref Vector3 v, float tile=0f, float color=0f)
Definition: MeshPass.cs:141
int batchIdx
Definition: MeshPass.cs:60
float meshX
Definition: MeshPass.cs:87
bool setTile
Definition: MeshPass.cs:29
bool setMatColor
Definition: MeshPass.cs:33
void Add(MeshPassParam p)
Definition: MeshPass.cs:281
void Draw()
Definition: MeshPass.cs:350
void DrawEmpty()
Definition: MeshPass.cs:412
void Add(MeshPassParam p, float tile, float color, float matColor=0f)
Definition: MeshPass.cs:328
Sprite sprite
Definition: MeshPass.cs:27
int lastCount
Definition: MeshPass.cs:43
void AddWithRotation(float x, float y, float z, float sx, float sy, int tile, float angle, bool flip)
Definition: MeshPass.cs:253
ProceduralMesh pmesh
Definition: MeshPass.cs:25
void AddWithScale(float x, float y, float z, int tile, float scale)
Definition: MeshPass.cs:177
Vector3 tempV
Definition: MeshPass.cs:79
MeshPass snowPass
Definition: MeshPass.cs:17
void Add(float x, float y, float z, float tile, float color, float matColor=0f)
Definition: MeshPass.cs:306
Vector2 tempE
Definition: MeshPass.cs:81
override string ToString()
Definition: MeshPass.cs:441
Texture preserveTex
Definition: MeshPass.cs:75
void Add(float x, float y, float z, float tile=0f, float color=0f)
Definition: MeshPass.cs:159
const int TokenLowWallDefault
Definition: MeshPass.cs:13
int lastBatchCount
Definition: MeshPass.cs:45
bool first
Definition: MeshPass.cs:84
FilterMode filter
Definition: MeshPass.cs:41
void Add(Point point, float tile=0f, float color=0f)
Definition: MeshPass.cs:122
bool haveSubPass
Definition: MeshPass.cs:48
void AddShadow(float x, float y, float z, ShadowData.Item s, SourcePref pref, int dir=0, bool snow=false)
Definition: MeshPass.cs:213
void NextBatch()
Definition: MeshPass.cs:417
Material mat
Definition: MeshPass.cs:21
void Init()
Definition: MeshPass.cs:98
int batchSize
Definition: MeshPass.cs:63
bool initialized
Definition: MeshPass.cs:69
const int TokenLowWall
Definition: MeshPass.cs:11
bool setColor
Definition: MeshPass.cs:31
bool haveShadowPass
Definition: MeshPass.cs:51
void OnValidate()
Definition: MeshPass.cs:461
Mesh SpriteToMesh(Sprite sprite)
Definition: MeshPass.cs:432
int renderQueue
Definition: MeshPass.cs:37
void AddShadow(MeshPassParam p, ref Vector3 fix)
Definition: MeshPass.cs:195
MeshPass shadowPass
Definition: MeshPass.cs:19
Mesh mesh
Definition: MeshPass.cs:23
MeshBatch _batch
Definition: MeshPass.cs:77
MeshPassParam _p
Definition: MeshPass.cs:39
bool haveSnowPass
Definition: MeshPass.cs:54
bool resize
Definition: MeshPass.cs:72
List< MeshBatch > batches
Definition: MeshPass.cs:66
int idx
Definition: MeshPass.cs:57
void _Refresh()
Definition: MeshPass.cs:466
Definition: Point.cs:9
ref Vector3 Position(int height)
Definition: Point.cs:524
MeshPass[] passes
Definition: Scene.cs:115
int shadowBRY
Definition: SourcePref.cs:251
int shadowBRX
Definition: SourcePref.cs:239
int shadowBY
Definition: SourcePref.cs:227
int shadowBX
Definition: SourcePref.cs:215