UGUI—合成Texture

作者:追风剑情 发布于:2020-10-11 14:53 分类:Unity3d

一、ComposeTexture.shader

  1. Shader "Custom/ComposeShader"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _SecondTex("Texture", 2D) = "white" {}
  7. }
  8. SubShader
  9. {
  10. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  11. LOD 100
  12.  
  13. Pass
  14. {
  15. Blend SrcAlpha OneMinusSrcAlpha
  16.  
  17. CGPROGRAM
  18. #pragma vertex vert
  19. #pragma fragment frag
  20. #include "UnityCG.cginc"
  21.  
  22. struct appdata
  23. {
  24. float4 vertex : POSITION;
  25. float2 uv : TEXCOORD0;
  26. };
  27.  
  28. struct v2f
  29. {
  30. float2 uv : TEXCOORD0;
  31. float4 vertex : SV_POSITION;
  32. };
  33.  
  34. sampler2D _MainTex;
  35. float4 _MainTex_ST;
  36. sampler2D _SecondTex;
  37. float4 _SecondTex_ST;
  38.  
  39. v2f vert(appdata v)
  40. {
  41. v2f o;
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  44. return o;
  45. }
  46.  
  47. fixed4 frag(v2f i) : SV_Target
  48. {
  49. fixed4 col = tex2D(_MainTex, i.uv);
  50. fixed4 col2 = tex2D(_SecondTex, i.uv);
  51. //step(a, x) 如果x<a,返回0;否则,返回1
  52. //col2.a = step(0.95, col2.a);
  53. col.rgb = col.rgba * (1 - col2.a) + col2.rgba * col2.a;
  54. return col;
  55. }
  56. ENDCG
  57. }
  58. }
  59. }

二、ComposeTexture.mat

三、ComposeTexture.cs

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class ComposeTexture : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private Texture2D m_MainTex;
  11. [SerializeField]
  12. private Texture2D m_SecondTex;
  13. [SerializeField]
  14. private Material m_ComposeMat;
  15. [SerializeField]
  16. private RawImage m_RawImage;
  17.  
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. RenderTexture destRT = RenderTexture.GetTemporary(1024, 1024, 0, RenderTextureFormat.ARGB32);
  22. destRT.autoGenerateMips = false;
  23.  
  24. //覆盖目标纹理
  25. //Graphics.ConvertTexture(m_Tex1, 0, destTex, 0);
  26. //判断对Graphics.CopyTexture() API的支持情况
  27. //Debug.LogFormat("SystemInfo.copyTextureSupport: {0}", SystemInfo.copyTextureSupport);
  28.  
  29. //注意:
  30. //1、TextureFormat要一致才能复制,否则会报错
  31. //Graphics.CopyTexture with a region will not copy readable texture data for compressed formats (source texture format 12)
  32. //2、Mip Maps的个数要一致,或者都设为false,才能复制
  33. //Graphics.CopyTexture called with mismatching mip counts (src 11 dst 1)
  34. //3、尺寸要一致,否则报错
  35. //Graphics.CopyTexture called with mismatching sizes (src 2048x1024 dst 1024x1024)
  36. //Graphics.CopyTexture(m_Tex1, 0, destTex, 0);
  37.  
  38. //将m_SecondTex合成到m_MainTex上
  39. m_ComposeMat.SetTexture("_SecondTex", m_SecondTex);
  40. Graphics.Blit(m_MainTex, destRT, m_ComposeMat);
  41. Texture2D destTex = ConvertTexture2D(destRT);
  42. destRT.Release();
  43.  
  44. m_RawImage.texture = destTex;
  45. }
  46.  
  47. //RenderTexture转Texture2D
  48. private Texture2D ConvertTexture2D(RenderTexture renderTexture)
  49. {
  50. int width = renderTexture.width;
  51. int height = renderTexture.height;
  52. Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
  53. RenderTexture.active = renderTexture;
  54. texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  55. texture2D.Apply();
  56. return texture2D;
  57. }
  58. }


四、创建RawImage

1111.png

运行效果

22222.png

示例:下面这个shader可以实现将第二贴图绘制到主贴图的指定位置

  1. //笔刷
  2. Shader "Custom/BrushShader"
  3. {
  4. Properties
  5. {
  6. _MainTex ("Texture", 2D) = "white" {}
  7. _BrushTex("Texture", 2D) = "white" {}
  8. //要绘制的位置(归一化坐标)
  9. _Position("Position", Vector) = (0, 0, 0, 0)
  10. //画笔尺寸/_MainTex尺寸
  11. _Scale("Scale", Vector) = (0, 0, 0, 0)
  12. }
  13. SubShader
  14. {
  15. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  16. LOD 100
  17.  
  18. Pass
  19. {
  20. Blend SrcAlpha OneMinusSrcAlpha
  21.  
  22. CGPROGRAM
  23. #pragma vertex vert
  24. #pragma fragment frag
  25. #include "UnityCG.cginc"
  26.  
  27. struct appdata
  28. {
  29. float4 vertex : POSITION;
  30. float2 uv : TEXCOORD0;
  31. };
  32.  
  33. struct v2f
  34. {
  35. float2 uv : TEXCOORD0;
  36. float4 vertex : SV_POSITION;
  37. };
  38.  
  39. sampler2D _MainTex;
  40. float4 _MainTex_ST;
  41. sampler2D _BrushTex;
  42. float4 _BrushTex_ST;
  43. fixed4 _Position;
  44. fixed4 _Scale;
  45.  
  46. v2f vert (appdata v)
  47. {
  48. v2f o;
  49. o.vertex = UnityObjectToClipPos(v.vertex);
  50. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  51. return o;
  52. }
  53.  
  54. fixed4 frag (v2f i) : SV_Target
  55. {
  56. fixed4 col = tex2D(_MainTex, i.uv);
  57. if (i.uv.x >= _Position.x && i.uv.x <= _Position.z && i.uv.y >= _Position.y && i.uv.y <= _Position.w)
  58. {
  59. fixed2 uv2 = fixed2( (1 - (_Position.x - i.uv.x) * _Scale.x) , (1 - (_Position.y - i.uv.y) * _Scale.y));
  60. fixed4 col2 = tex2D(_BrushTex, uv2);
  61. col.rgba = col2.rgba * col2.a + col.rgba * (1 - col2.a);
  62. }
  63. return col;
  64. }
  65. ENDCG
  66. }
  67. }
  68. }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号