yum/3ner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum/3ner

yumRewrite Fold editorffbf55b

master
27.0 KiB619 linesraw
1// Fixed fields are intentional: Unity 2022.3 cannot animate custom nested classes or arrays.
2// Keep slot names stable; saved AnimationClips bind directly to these fields.
3using UnityEngine;
4
5namespace Yum.Fold
6{
7    [ExecuteAlways, DisallowMultipleComponent]
8    [AddComponentMenu("")]
9    public sealed class FoldMaterialAnimator : MonoBehaviour
10    {
11        [HideInInspector] public FoldStack owner;
12        [HideInInspector] public Renderer targetRenderer;
13        [HideInInspector] public Material material;
14        [HideInInspector] public int materialIndex;
15        public bool slot0_enabled;
16        [UnityEngine.Animations.DiscreteEvaluation] public int slot0_opcode;
17        public float slot0_float0;
18        public float slot0_float1;
19        public float slot0_float2;
20        public float slot0_float3;
21        public Vector4 slot0_vector0;
22        public Vector4 slot0_vector1;
23        public Vector4 slot0_vector2;
24        public Vector4 slot0_vector3;
25        public bool slot1_enabled;
26        [UnityEngine.Animations.DiscreteEvaluation] public int slot1_opcode;
27        public float slot1_float0;
28        public float slot1_float1;
29        public float slot1_float2;
30        public float slot1_float3;
31        public Vector4 slot1_vector0;
32        public Vector4 slot1_vector1;
33        public Vector4 slot1_vector2;
34        public Vector4 slot1_vector3;
35        public bool slot2_enabled;
36        [UnityEngine.Animations.DiscreteEvaluation] public int slot2_opcode;
37        public float slot2_float0;
38        public float slot2_float1;
39        public float slot2_float2;
40        public float slot2_float3;
41        public Vector4 slot2_vector0;
42        public Vector4 slot2_vector1;
43        public Vector4 slot2_vector2;
44        public Vector4 slot2_vector3;
45        public bool slot3_enabled;
46        [UnityEngine.Animations.DiscreteEvaluation] public int slot3_opcode;
47        public float slot3_float0;
48        public float slot3_float1;
49        public float slot3_float2;
50        public float slot3_float3;
51        public Vector4 slot3_vector0;
52        public Vector4 slot3_vector1;
53        public Vector4 slot3_vector2;
54        public Vector4 slot3_vector3;
55        public bool slot4_enabled;
56        [UnityEngine.Animations.DiscreteEvaluation] public int slot4_opcode;
57        public float slot4_float0;
58        public float slot4_float1;
59        public float slot4_float2;
60        public float slot4_float3;
61        public Vector4 slot4_vector0;
62        public Vector4 slot4_vector1;
63        public Vector4 slot4_vector2;
64        public Vector4 slot4_vector3;
65        public bool slot5_enabled;
66        [UnityEngine.Animations.DiscreteEvaluation] public int slot5_opcode;
67        public float slot5_float0;
68        public float slot5_float1;
69        public float slot5_float2;
70        public float slot5_float3;
71        public Vector4 slot5_vector0;
72        public Vector4 slot5_vector1;
73        public Vector4 slot5_vector2;
74        public Vector4 slot5_vector3;
75        public bool slot6_enabled;
76        [UnityEngine.Animations.DiscreteEvaluation] public int slot6_opcode;
77        public float slot6_float0;
78        public float slot6_float1;
79        public float slot6_float2;
80        public float slot6_float3;
81        public Vector4 slot6_vector0;
82        public Vector4 slot6_vector1;
83        public Vector4 slot6_vector2;
84        public Vector4 slot6_vector3;
85        public bool slot7_enabled;
86        [UnityEngine.Animations.DiscreteEvaluation] public int slot7_opcode;
87        public float slot7_float0;
88        public float slot7_float1;
89        public float slot7_float2;
90        public float slot7_float3;
91        public Vector4 slot7_vector0;
92        public Vector4 slot7_vector1;
93        public Vector4 slot7_vector2;
94        public Vector4 slot7_vector3;
95        public bool slot8_enabled;
96        [UnityEngine.Animations.DiscreteEvaluation] public int slot8_opcode;
97        public float slot8_float0;
98        public float slot8_float1;
99        public float slot8_float2;
100        public float slot8_float3;
101        public Vector4 slot8_vector0;
102        public Vector4 slot8_vector1;
103        public Vector4 slot8_vector2;
104        public Vector4 slot8_vector3;
105        public bool slot9_enabled;
106        [UnityEngine.Animations.DiscreteEvaluation] public int slot9_opcode;
107        public float slot9_float0;
108        public float slot9_float1;
109        public float slot9_float2;
110        public float slot9_float3;
111        public Vector4 slot9_vector0;
112        public Vector4 slot9_vector1;
113        public Vector4 slot9_vector2;
114        public Vector4 slot9_vector3;
115        public bool slot10_enabled;
116        [UnityEngine.Animations.DiscreteEvaluation] public int slot10_opcode;
117        public float slot10_float0;
118        public float slot10_float1;
119        public float slot10_float2;
120        public float slot10_float3;
121        public Vector4 slot10_vector0;
122        public Vector4 slot10_vector1;
123        public Vector4 slot10_vector2;
124        public Vector4 slot10_vector3;
125        public bool slot11_enabled;
126        [UnityEngine.Animations.DiscreteEvaluation] public int slot11_opcode;
127        public float slot11_float0;
128        public float slot11_float1;
129        public float slot11_float2;
130        public float slot11_float3;
131        public Vector4 slot11_vector0;
132        public Vector4 slot11_vector1;
133        public Vector4 slot11_vector2;
134        public Vector4 slot11_vector3;
135        public bool slot12_enabled;
136        [UnityEngine.Animations.DiscreteEvaluation] public int slot12_opcode;
137        public float slot12_float0;
138        public float slot12_float1;
139        public float slot12_float2;
140        public float slot12_float3;
141        public Vector4 slot12_vector0;
142        public Vector4 slot12_vector1;
143        public Vector4 slot12_vector2;
144        public Vector4 slot12_vector3;
145        public bool slot13_enabled;
146        [UnityEngine.Animations.DiscreteEvaluation] public int slot13_opcode;
147        public float slot13_float0;
148        public float slot13_float1;
149        public float slot13_float2;
150        public float slot13_float3;
151        public Vector4 slot13_vector0;
152        public Vector4 slot13_vector1;
153        public Vector4 slot13_vector2;
154        public Vector4 slot13_vector3;
155        public bool slot14_enabled;
156        [UnityEngine.Animations.DiscreteEvaluation] public int slot14_opcode;
157        public float slot14_float0;
158        public float slot14_float1;
159        public float slot14_float2;
160        public float slot14_float3;
161        public Vector4 slot14_vector0;
162        public Vector4 slot14_vector1;
163        public Vector4 slot14_vector2;
164        public Vector4 slot14_vector3;
165        public bool slot15_enabled;
166        [UnityEngine.Animations.DiscreteEvaluation] public int slot15_opcode;
167        public float slot15_float0;
168        public float slot15_float1;
169        public float slot15_float2;
170        public float slot15_float3;
171        public Vector4 slot15_vector0;
172        public Vector4 slot15_vector1;
173        public Vector4 slot15_vector2;
174        public Vector4 slot15_vector3;
175
176        public SlotAccess slots => new SlotAccess(this);
177        public readonly struct SlotAccess
178        {
179            readonly FoldMaterialAnimator driver;
180            public SlotAccess(FoldMaterialAnimator driver) { this.driver = driver; }
181            public int Length => 16;
182            public FoldOperator this[int index] { get => driver.Read(index, new FoldOperator()); set => driver.Write(index, value); }
183        }
184        public FoldOperator Read(int index, FoldOperator result)
185        {
186            switch (index)
187            {
188                case 0:
189                    result.enabled = slot0_enabled;
190                    result.opcode = slot0_opcode;
191                    result.float0 = slot0_float0;
192                    result.float1 = slot0_float1;
193                    result.float2 = slot0_float2;
194                    result.float3 = slot0_float3;
195                    result.vector0 = slot0_vector0;
196                    result.vector1 = slot0_vector1;
197                    result.vector2 = slot0_vector2;
198                    result.vector3 = slot0_vector3;
199                    break;
200                case 1:
201                    result.enabled = slot1_enabled;
202                    result.opcode = slot1_opcode;
203                    result.float0 = slot1_float0;
204                    result.float1 = slot1_float1;
205                    result.float2 = slot1_float2;
206                    result.float3 = slot1_float3;
207                    result.vector0 = slot1_vector0;
208                    result.vector1 = slot1_vector1;
209                    result.vector2 = slot1_vector2;
210                    result.vector3 = slot1_vector3;
211                    break;
212                case 2:
213                    result.enabled = slot2_enabled;
214                    result.opcode = slot2_opcode;
215                    result.float0 = slot2_float0;
216                    result.float1 = slot2_float1;
217                    result.float2 = slot2_float2;
218                    result.float3 = slot2_float3;
219                    result.vector0 = slot2_vector0;
220                    result.vector1 = slot2_vector1;
221                    result.vector2 = slot2_vector2;
222                    result.vector3 = slot2_vector3;
223                    break;
224                case 3:
225                    result.enabled = slot3_enabled;
226                    result.opcode = slot3_opcode;
227                    result.float0 = slot3_float0;
228                    result.float1 = slot3_float1;
229                    result.float2 = slot3_float2;
230                    result.float3 = slot3_float3;
231                    result.vector0 = slot3_vector0;
232                    result.vector1 = slot3_vector1;
233                    result.vector2 = slot3_vector2;
234                    result.vector3 = slot3_vector3;
235                    break;
236                case 4:
237                    result.enabled = slot4_enabled;
238                    result.opcode = slot4_opcode;
239                    result.float0 = slot4_float0;
240                    result.float1 = slot4_float1;
241                    result.float2 = slot4_float2;
242                    result.float3 = slot4_float3;
243                    result.vector0 = slot4_vector0;
244                    result.vector1 = slot4_vector1;
245                    result.vector2 = slot4_vector2;
246                    result.vector3 = slot4_vector3;
247                    break;
248                case 5:
249                    result.enabled = slot5_enabled;
250                    result.opcode = slot5_opcode;
251                    result.float0 = slot5_float0;
252                    result.float1 = slot5_float1;
253                    result.float2 = slot5_float2;
254                    result.float3 = slot5_float3;
255                    result.vector0 = slot5_vector0;
256                    result.vector1 = slot5_vector1;
257                    result.vector2 = slot5_vector2;
258                    result.vector3 = slot5_vector3;
259                    break;
260                case 6:
261                    result.enabled = slot6_enabled;
262                    result.opcode = slot6_opcode;
263                    result.float0 = slot6_float0;
264                    result.float1 = slot6_float1;
265                    result.float2 = slot6_float2;
266                    result.float3 = slot6_float3;
267                    result.vector0 = slot6_vector0;
268                    result.vector1 = slot6_vector1;
269                    result.vector2 = slot6_vector2;
270                    result.vector3 = slot6_vector3;
271                    break;
272                case 7:
273                    result.enabled = slot7_enabled;
274                    result.opcode = slot7_opcode;
275                    result.float0 = slot7_float0;
276                    result.float1 = slot7_float1;
277                    result.float2 = slot7_float2;
278                    result.float3 = slot7_float3;
279                    result.vector0 = slot7_vector0;
280                    result.vector1 = slot7_vector1;
281                    result.vector2 = slot7_vector2;
282                    result.vector3 = slot7_vector3;
283                    break;
284                case 8:
285                    result.enabled = slot8_enabled;
286                    result.opcode = slot8_opcode;
287                    result.float0 = slot8_float0;
288                    result.float1 = slot8_float1;
289                    result.float2 = slot8_float2;
290                    result.float3 = slot8_float3;
291                    result.vector0 = slot8_vector0;
292                    result.vector1 = slot8_vector1;
293                    result.vector2 = slot8_vector2;
294                    result.vector3 = slot8_vector3;
295                    break;
296                case 9:
297                    result.enabled = slot9_enabled;
298                    result.opcode = slot9_opcode;
299                    result.float0 = slot9_float0;
300                    result.float1 = slot9_float1;
301                    result.float2 = slot9_float2;
302                    result.float3 = slot9_float3;
303                    result.vector0 = slot9_vector0;
304                    result.vector1 = slot9_vector1;
305                    result.vector2 = slot9_vector2;
306                    result.vector3 = slot9_vector3;
307                    break;
308                case 10:
309                    result.enabled = slot10_enabled;
310                    result.opcode = slot10_opcode;
311                    result.float0 = slot10_float0;
312                    result.float1 = slot10_float1;
313                    result.float2 = slot10_float2;
314                    result.float3 = slot10_float3;
315                    result.vector0 = slot10_vector0;
316                    result.vector1 = slot10_vector1;
317                    result.vector2 = slot10_vector2;
318                    result.vector3 = slot10_vector3;
319                    break;
320                case 11:
321                    result.enabled = slot11_enabled;
322                    result.opcode = slot11_opcode;
323                    result.float0 = slot11_float0;
324                    result.float1 = slot11_float1;
325                    result.float2 = slot11_float2;
326                    result.float3 = slot11_float3;
327                    result.vector0 = slot11_vector0;
328                    result.vector1 = slot11_vector1;
329                    result.vector2 = slot11_vector2;
330                    result.vector3 = slot11_vector3;
331                    break;
332                case 12:
333                    result.enabled = slot12_enabled;
334                    result.opcode = slot12_opcode;
335                    result.float0 = slot12_float0;
336                    result.float1 = slot12_float1;
337                    result.float2 = slot12_float2;
338                    result.float3 = slot12_float3;
339                    result.vector0 = slot12_vector0;
340                    result.vector1 = slot12_vector1;
341                    result.vector2 = slot12_vector2;
342                    result.vector3 = slot12_vector3;
343                    break;
344                case 13:
345                    result.enabled = slot13_enabled;
346                    result.opcode = slot13_opcode;
347                    result.float0 = slot13_float0;
348                    result.float1 = slot13_float1;
349                    result.float2 = slot13_float2;
350                    result.float3 = slot13_float3;
351                    result.vector0 = slot13_vector0;
352                    result.vector1 = slot13_vector1;
353                    result.vector2 = slot13_vector2;
354                    result.vector3 = slot13_vector3;
355                    break;
356                case 14:
357                    result.enabled = slot14_enabled;
358                    result.opcode = slot14_opcode;
359                    result.float0 = slot14_float0;
360                    result.float1 = slot14_float1;
361                    result.float2 = slot14_float2;
362                    result.float3 = slot14_float3;
363                    result.vector0 = slot14_vector0;
364                    result.vector1 = slot14_vector1;
365                    result.vector2 = slot14_vector2;
366                    result.vector3 = slot14_vector3;
367                    break;
368                case 15:
369                    result.enabled = slot15_enabled;
370                    result.opcode = slot15_opcode;
371                    result.float0 = slot15_float0;
372                    result.float1 = slot15_float1;
373                    result.float2 = slot15_float2;
374                    result.float3 = slot15_float3;
375                    result.vector0 = slot15_vector0;
376                    result.vector1 = slot15_vector1;
377                    result.vector2 = slot15_vector2;
378                    result.vector3 = slot15_vector3;
379                    break;
380                default: throw new System.IndexOutOfRangeException();
381            }
382            return result;
383        }
384        public void Write(int index, FoldOperator value)
385        {
386            switch (index)
387            {
388                case 0:
389                    slot0_enabled = value.enabled;
390                    slot0_opcode = value.opcode;
391                    slot0_float0 = value.float0;
392                    slot0_float1 = value.float1;
393                    slot0_float2 = value.float2;
394                    slot0_float3 = value.float3;
395                    slot0_vector0 = value.vector0;
396                    slot0_vector1 = value.vector1;
397                    slot0_vector2 = value.vector2;
398                    slot0_vector3 = value.vector3;
399                    break;
400                case 1:
401                    slot1_enabled = value.enabled;
402                    slot1_opcode = value.opcode;
403                    slot1_float0 = value.float0;
404                    slot1_float1 = value.float1;
405                    slot1_float2 = value.float2;
406                    slot1_float3 = value.float3;
407                    slot1_vector0 = value.vector0;
408                    slot1_vector1 = value.vector1;
409                    slot1_vector2 = value.vector2;
410                    slot1_vector3 = value.vector3;
411                    break;
412                case 2:
413                    slot2_enabled = value.enabled;
414                    slot2_opcode = value.opcode;
415                    slot2_float0 = value.float0;
416                    slot2_float1 = value.float1;
417                    slot2_float2 = value.float2;
418                    slot2_float3 = value.float3;
419                    slot2_vector0 = value.vector0;
420                    slot2_vector1 = value.vector1;
421                    slot2_vector2 = value.vector2;
422                    slot2_vector3 = value.vector3;
423                    break;
424                case 3:
425                    slot3_enabled = value.enabled;
426                    slot3_opcode = value.opcode;
427                    slot3_float0 = value.float0;
428                    slot3_float1 = value.float1;
429                    slot3_float2 = value.float2;
430                    slot3_float3 = value.float3;
431                    slot3_vector0 = value.vector0;
432                    slot3_vector1 = value.vector1;
433                    slot3_vector2 = value.vector2;
434                    slot3_vector3 = value.vector3;
435                    break;
436                case 4:
437                    slot4_enabled = value.enabled;
438                    slot4_opcode = value.opcode;
439                    slot4_float0 = value.float0;
440                    slot4_float1 = value.float1;
441                    slot4_float2 = value.float2;
442                    slot4_float3 = value.float3;
443                    slot4_vector0 = value.vector0;
444                    slot4_vector1 = value.vector1;
445                    slot4_vector2 = value.vector2;
446                    slot4_vector3 = value.vector3;
447                    break;
448                case 5:
449                    slot5_enabled = value.enabled;
450                    slot5_opcode = value.opcode;
451                    slot5_float0 = value.float0;
452                    slot5_float1 = value.float1;
453                    slot5_float2 = value.float2;
454                    slot5_float3 = value.float3;
455                    slot5_vector0 = value.vector0;
456                    slot5_vector1 = value.vector1;
457                    slot5_vector2 = value.vector2;
458                    slot5_vector3 = value.vector3;
459                    break;
460                case 6:
461                    slot6_enabled = value.enabled;
462                    slot6_opcode = value.opcode;
463                    slot6_float0 = value.float0;
464                    slot6_float1 = value.float1;
465                    slot6_float2 = value.float2;
466                    slot6_float3 = value.float3;
467                    slot6_vector0 = value.vector0;
468                    slot6_vector1 = value.vector1;
469                    slot6_vector2 = value.vector2;
470                    slot6_vector3 = value.vector3;
471                    break;
472                case 7:
473                    slot7_enabled = value.enabled;
474                    slot7_opcode = value.opcode;
475                    slot7_float0 = value.float0;
476                    slot7_float1 = value.float1;
477                    slot7_float2 = value.float2;
478                    slot7_float3 = value.float3;
479                    slot7_vector0 = value.vector0;
480                    slot7_vector1 = value.vector1;
481                    slot7_vector2 = value.vector2;
482                    slot7_vector3 = value.vector3;
483                    break;
484                case 8:
485                    slot8_enabled = value.enabled;
486                    slot8_opcode = value.opcode;
487                    slot8_float0 = value.float0;
488                    slot8_float1 = value.float1;
489                    slot8_float2 = value.float2;
490                    slot8_float3 = value.float3;
491                    slot8_vector0 = value.vector0;
492                    slot8_vector1 = value.vector1;
493                    slot8_vector2 = value.vector2;
494                    slot8_vector3 = value.vector3;
495                    break;
496                case 9:
497                    slot9_enabled = value.enabled;
498                    slot9_opcode = value.opcode;
499                    slot9_float0 = value.float0;
500                    slot9_float1 = value.float1;
501                    slot9_float2 = value.float2;
502                    slot9_float3 = value.float3;
503                    slot9_vector0 = value.vector0;
504                    slot9_vector1 = value.vector1;
505                    slot9_vector2 = value.vector2;
506                    slot9_vector3 = value.vector3;
507                    break;
508                case 10:
509                    slot10_enabled = value.enabled;
510                    slot10_opcode = value.opcode;
511                    slot10_float0 = value.float0;
512                    slot10_float1 = value.float1;
513                    slot10_float2 = value.float2;
514                    slot10_float3 = value.float3;
515                    slot10_vector0 = value.vector0;
516                    slot10_vector1 = value.vector1;
517                    slot10_vector2 = value.vector2;
518                    slot10_vector3 = value.vector3;
519                    break;
520                case 11:
521                    slot11_enabled = value.enabled;
522                    slot11_opcode = value.opcode;
523                    slot11_float0 = value.float0;
524                    slot11_float1 = value.float1;
525                    slot11_float2 = value.float2;
526                    slot11_float3 = value.float3;
527                    slot11_vector0 = value.vector0;
528                    slot11_vector1 = value.vector1;
529                    slot11_vector2 = value.vector2;
530                    slot11_vector3 = value.vector3;
531                    break;
532                case 12:
533                    slot12_enabled = value.enabled;
534                    slot12_opcode = value.opcode;
535                    slot12_float0 = value.float0;
536                    slot12_float1 = value.float1;
537                    slot12_float2 = value.float2;
538                    slot12_float3 = value.float3;
539                    slot12_vector0 = value.vector0;
540                    slot12_vector1 = value.vector1;
541                    slot12_vector2 = value.vector2;
542                    slot12_vector3 = value.vector3;
543                    break;
544                case 13:
545                    slot13_enabled = value.enabled;
546                    slot13_opcode = value.opcode;
547                    slot13_float0 = value.float0;
548                    slot13_float1 = value.float1;
549                    slot13_float2 = value.float2;
550                    slot13_float3 = value.float3;
551                    slot13_vector0 = value.vector0;
552                    slot13_vector1 = value.vector1;
553                    slot13_vector2 = value.vector2;
554                    slot13_vector3 = value.vector3;
555                    break;
556                case 14:
557                    slot14_enabled = value.enabled;
558                    slot14_opcode = value.opcode;
559                    slot14_float0 = value.float0;
560                    slot14_float1 = value.float1;
561                    slot14_float2 = value.float2;
562                    slot14_float3 = value.float3;
563                    slot14_vector0 = value.vector0;
564                    slot14_vector1 = value.vector1;
565                    slot14_vector2 = value.vector2;
566                    slot14_vector3 = value.vector3;
567                    break;
568                case 15:
569                    slot15_enabled = value.enabled;
570                    slot15_opcode = value.opcode;
571                    slot15_float0 = value.float0;
572                    slot15_float1 = value.float1;
573                    slot15_float2 = value.float2;
574                    slot15_float3 = value.float3;
575                    slot15_vector0 = value.vector0;
576                    slot15_vector1 = value.vector1;
577                    slot15_vector2 = value.vector2;
578                    slot15_vector3 = value.vector3;
579                    break;
580                default: throw new System.IndexOutOfRangeException();
581            }
582        }
583        MaterialPropertyBlock original, block;
584        readonly FoldOperator scratch = new FoldOperator();
585        readonly System.Collections.Generic.List<Material> shared = new System.Collections.Generic.List<Material>();
586        void OnEnable() => Apply();
587        void LateUpdate() => Apply();
588        void OnDidApplyAnimationProperties() => Apply();
589        void OnDisable() => Restore();
590        void OnDestroy() => Restore();
591        public void Apply()
592        {
593            if (!isActiveAndEnabled || !owner || !owner.isActiveAndEnabled || !targetRenderer || !material) { Restore(); return; }
594            targetRenderer.GetSharedMaterials(shared);
595            if (materialIndex < 0 || materialIndex >= shared.Count || shared[materialIndex] != material) { Restore(); return; }
596            if (block == null) block = new MaterialPropertyBlock();
597            if (original == null)
598            {
599                original = new MaterialPropertyBlock();
600                targetRenderer.GetPropertyBlock(original, materialIndex);
601            }
602            targetRenderer.GetPropertyBlock(block, materialIndex);
603            if (block.isEmpty) targetRenderer.GetPropertyBlock(block);
604            for (int i = 0; i < 16; i++) Read(i, scratch).Write(block, i);
605            targetRenderer.SetPropertyBlock(block, materialIndex);
606        }
607        public void Restore()
608        {
609            if (original == null) return;
610            if (targetRenderer)
611            {
612                targetRenderer.GetSharedMaterials(shared);
613                if (materialIndex >= 0 && materialIndex < shared.Count)
614                    targetRenderer.SetPropertyBlock(original.isEmpty ? null : original, materialIndex);
615            }
616            original = null;
617        }
618    }
619}