@@ -46,6 +46,11 @@ public class NGraphics : IMeshFactory
46
46
/// </summary>
47
47
public event Action meshModifier ;
48
48
49
+ /// <summary>
50
+ /// 销毁触发
51
+ /// </summary>
52
+ public Action disposeCallback ;
53
+
49
54
NTexture _texture ;
50
55
string _shader ;
51
56
Material _material ;
@@ -66,6 +71,7 @@ public class VertexMatrix
66
71
public Vector3 cameraPos ;
67
72
public Matrix4x4 matrix ;
68
73
}
74
+
69
75
VertexMatrix _vertexMatrix ;
70
76
71
77
bool hasAlphaBackup ;
@@ -141,6 +147,7 @@ public IMeshFactory meshFactory
141
147
_meshFactory = new T ( ) ;
142
148
_meshDirty = true ;
143
149
}
150
+
144
151
return ( T ) _meshFactory ;
145
152
}
146
153
@@ -184,13 +191,21 @@ public NTexture texture
184
191
if ( _texture != value )
185
192
{
186
193
if ( value != null )
194
+ {
187
195
value . AddRef ( ) ;
196
+ }
197
+
188
198
if ( _texture != null )
199
+ {
189
200
_texture . ReleaseRef ( ) ;
201
+ }
190
202
191
203
_texture = value ;
192
204
if ( _customMatarial != 0 && _material != null )
205
+ {
193
206
_material . mainTexture = _texture != null ? _texture . nativeTexture : null ;
207
+ }
208
+
194
209
_meshDirty = true ;
195
210
UpdateManager ( ) ;
196
211
}
@@ -219,9 +234,13 @@ public void SetShaderAndTexture(string shader, NTexture texture)
219
234
{
220
235
_shader = shader ;
221
236
if ( _texture != texture )
237
+ {
222
238
this . texture = texture ;
239
+ }
223
240
else
241
+ {
224
242
UpdateManager ( ) ;
243
+ }
225
244
}
226
245
227
246
/// <summary>
@@ -232,24 +251,33 @@ public Material material
232
251
get
233
252
{
234
253
if ( _customMatarial == 0 && _material == null && _manager != null )
254
+ {
235
255
_material = _manager . GetMaterial ( _materialFlags , blendMode , 0 ) ;
256
+ }
257
+
236
258
return _material ;
237
259
}
238
260
set
239
261
{
240
262
if ( ( _customMatarial & 128 ) != 0 && _material != null )
263
+ {
241
264
Object . DestroyImmediate ( _material ) ;
265
+ }
242
266
243
267
_material = value ;
244
268
if ( _material != null )
245
269
{
246
270
_customMatarial = 1 ;
247
271
if ( _material . HasProperty ( ShaderConfig . ID_Stencil ) || _material . HasProperty ( ShaderConfig . ID_ClipBox ) )
272
+ {
248
273
_customMatarial |= 2 ;
274
+ }
249
275
250
276
meshRenderer . sharedMaterial = _material ;
251
277
if ( _texture != null )
278
+ {
252
279
_material . mainTexture = _texture . nativeTexture ;
280
+ }
253
281
}
254
282
else
255
283
{
@@ -320,9 +348,14 @@ public void ToggleKeyword(string keyword, bool enabled)
320
348
void UpdateManager ( )
321
349
{
322
350
if ( _texture != null )
351
+ {
323
352
_manager = _texture . GetMaterialManager ( _shader ) ;
353
+ }
324
354
else
355
+ {
325
356
_manager = null ;
357
+ }
358
+
326
359
UpdateMaterialFlags ( ) ;
327
360
}
328
361
@@ -331,12 +364,18 @@ void UpdateMaterialFlags()
331
364
if ( _customMatarial != 0 )
332
365
{
333
366
if ( material != null )
367
+ {
334
368
material . shaderKeywords = _shaderKeywords ;
369
+ }
335
370
}
336
371
else if ( _shaderKeywords != null && _manager != null )
372
+ {
337
373
_materialFlags = _manager . GetFlagsByKeywords ( _shaderKeywords ) ;
374
+ }
338
375
else
376
+ {
339
377
_materialFlags = 0 ;
378
+ }
340
379
}
341
380
342
381
/// <summary>
@@ -504,16 +543,25 @@ public void Dispose()
504
543
if ( mesh != null )
505
544
{
506
545
if ( Application . isPlaying )
546
+ {
507
547
Object . Destroy ( mesh ) ;
548
+ }
508
549
else
550
+ {
509
551
Object . DestroyImmediate ( mesh ) ;
552
+ }
553
+
510
554
mesh = null ;
511
555
}
556
+
512
557
if ( ( _customMatarial & 128 ) != 0 && _material != null )
558
+ {
513
559
Object . DestroyImmediate ( _material ) ;
560
+ }
514
561
515
562
if ( _texture != null )
516
563
{
564
+ disposeCallback ? . Invoke ( ) ;
517
565
_texture . ReleaseRef ( ) ;
518
566
_texture = null ;
519
567
}
@@ -524,6 +572,7 @@ public void Dispose()
524
572
meshFilter = null ;
525
573
_stencilEraser = null ;
526
574
meshModifier = null ;
575
+ disposeCallback = null ;
527
576
}
528
577
529
578
/// <summary>
@@ -542,7 +591,9 @@ public void Update(UpdateContext context, float alpha, bool grayed)
542
591
UpdateMeshNow ( ) ;
543
592
}
544
593
else if ( _alpha != alpha )
594
+ {
545
595
ChangeAlpha ( alpha ) ;
596
+ }
546
597
547
598
if ( _propertyBlock != null && _blockUpdated )
548
599
{
@@ -553,7 +604,9 @@ public void Update(UpdateContext context, float alpha, bool grayed)
553
604
if ( _customMatarial != 0 )
554
605
{
555
606
if ( ( _customMatarial & 2 ) != 0 && _material != null )
607
+ {
556
608
context . ApplyClippingProperties ( _material , false ) ;
609
+ }
557
610
}
558
611
else
559
612
{
@@ -573,38 +626,57 @@ public void Update(UpdateContext context, float alpha, bool grayed)
573
626
if ( context . clipped )
574
627
{
575
628
if ( context . stencilReferenceValue > 0 )
629
+ {
576
630
matFlags |= ( int ) MaterialFlags . StencilTest ;
631
+ }
632
+
577
633
if ( context . rectMaskDepth > 0 )
578
634
{
579
635
if ( context . clipInfo . soft )
636
+ {
580
637
matFlags |= ( int ) MaterialFlags . SoftClipped ;
638
+ }
581
639
else
640
+ {
582
641
matFlags |= ( int ) MaterialFlags . Clipped ;
642
+ }
583
643
}
584
644
585
645
_material = _manager . GetMaterial ( matFlags , blendMode , context . clipInfo . clipId ) ;
586
646
if ( _manager . firstMaterialInFrame )
647
+ {
587
648
context . ApplyClippingProperties ( _material , true ) ;
649
+ }
588
650
}
589
651
else
652
+ {
590
653
_material = _manager . GetMaterial ( matFlags , blendMode , 0 ) ;
654
+ }
591
655
}
592
656
}
593
657
else
658
+ {
594
659
_material = null ;
660
+ }
595
661
596
662
if ( ! Material . ReferenceEquals ( _material , meshRenderer . sharedMaterial ) )
663
+ {
597
664
meshRenderer . sharedMaterial = _material ;
665
+ }
598
666
}
599
667
600
668
if ( _maskFlag != 0 )
601
669
{
602
670
if ( _maskFlag == 1 )
671
+ {
603
672
_maskFlag = 2 ;
673
+ }
604
674
else
605
675
{
606
676
if ( _stencilEraser != null )
677
+ {
607
678
_stencilEraser . enabled = false ;
679
+ }
608
680
609
681
_maskFlag = 0 ;
610
682
}
@@ -622,7 +694,9 @@ internal void _PreUpdateMask(UpdateContext context, uint maskId)
622
694
_stencilEraser . meshFilter . mesh = mesh ;
623
695
}
624
696
else
697
+ {
625
698
_stencilEraser . enabled = true ;
699
+ }
626
700
}
627
701
628
702
_maskFlag = 1 ;
@@ -632,7 +706,9 @@ internal void _PreUpdateMask(UpdateContext context, uint maskId)
632
706
//这里使用maskId而不是clipInfo.clipId,是因为遮罩有两个用途,一个是写入遮罩,一个是擦除,两个不能用同一个材质
633
707
Material mat = _manager . GetMaterial ( ( int ) MaterialFlags . AlphaMask | _materialFlags , BlendMode . Normal , maskId ) ;
634
708
if ( ! Material . ReferenceEquals ( mat , _stencilEraser . meshRenderer . sharedMaterial ) )
709
+ {
635
710
_stencilEraser . meshRenderer . sharedMaterial = mat ;
711
+ }
636
712
637
713
context . ApplyAlphaMaskProperties ( mat , true ) ;
638
714
}
@@ -649,18 +725,26 @@ void UpdateMeshNow()
649
725
mesh . Clear ( ) ;
650
726
651
727
if ( meshModifier != null )
728
+ {
652
729
meshModifier ( ) ;
730
+ }
653
731
}
732
+
654
733
return ;
655
734
}
656
735
657
736
VertexBuffer vb = VertexBuffer . Begin ( ) ;
658
737
vb . contentRect = _contentRect ;
659
738
vb . uvRect = _texture . uvRect ;
660
739
if ( _texture != null )
740
+ {
661
741
vb . textureSize = new Vector2 ( _texture . width , _texture . height ) ;
742
+ }
662
743
else
744
+ {
663
745
vb . textureSize = new Vector2 ( 0 , 0 ) ;
746
+ }
747
+
664
748
if ( _flip != FlipType . None )
665
749
{
666
750
if ( _flip == FlipType . Horizontal || _flip == FlipType . Both )
@@ -669,13 +753,15 @@ void UpdateMeshNow()
669
753
vb . uvRect . xMin = vb . uvRect . xMax ;
670
754
vb . uvRect . xMax = tmp ;
671
755
}
756
+
672
757
if ( _flip == FlipType . Vertical || _flip == FlipType . Both )
673
758
{
674
759
float tmp = vb . uvRect . yMin ;
675
760
vb . uvRect . yMin = vb . uvRect . yMax ;
676
761
vb . uvRect . yMax = tmp ;
677
762
}
678
763
}
764
+
679
765
vb . vertexColor = _color ;
680
766
_meshFactory . OnPopulateMesh ( vb ) ;
681
767
@@ -687,8 +773,11 @@ void UpdateMeshNow()
687
773
mesh . Clear ( ) ;
688
774
689
775
if ( meshModifier != null )
776
+ {
690
777
meshModifier ( ) ;
778
+ }
691
779
}
780
+
692
781
vb . End ( ) ;
693
782
return ;
694
783
}
@@ -760,13 +849,20 @@ void UpdateMeshNow()
760
849
#if UNITY_5_2 || UNITY_5_3_OR_NEWER
761
850
mesh . SetVertices ( vb . vertices ) ;
762
851
if ( vb . _isArbitraryQuad )
852
+ {
763
853
mesh . SetUVs ( 0 , vb . FixUVForArbitraryQuad ( ) ) ;
854
+ }
764
855
else
856
+ {
765
857
mesh . SetUVs ( 0 , vb . uvs ) ;
858
+ }
859
+
766
860
mesh . SetColors ( vb . colors ) ;
767
861
mesh . SetTriangles ( vb . triangles , 0 ) ;
768
862
if ( vb . uvs2 . Count == vb . uvs . Count )
863
+ {
769
864
mesh . SetUVs ( 1 , vb . uvs2 ) ;
865
+ }
770
866
771
867
#if ! UNITY_5_6_OR_NEWER
772
868
_colors = null ;
@@ -797,7 +893,9 @@ void UpdateMeshNow()
797
893
vb . End ( ) ;
798
894
799
895
if ( meshModifier != null )
896
+ {
800
897
meshModifier ( ) ;
898
+ }
801
899
}
802
900
803
901
public void OnPopulateMesh ( VertexBuffer vb )
@@ -809,7 +907,7 @@ public void OnPopulateMesh(VertexBuffer vb)
809
907
vb . _isArbitraryQuad = _vertexMatrix != null ;
810
908
}
811
909
812
- class StencilEraser
910
+ sealed class StencilEraser
813
911
{
814
912
public GameObject gameObject ;
815
913
public MeshFilter meshFilter ;
@@ -839,4 +937,4 @@ public bool enabled
839
937
}
840
938
}
841
939
}
842
- }
940
+ }
0 commit comments