总结:总算解决了NGUI中Shader不能实时更改的问题,原来NGUI中的Texture组件提供了OnRender代码示例如下
/***************************************
Editor: Tason
Version: v1.0
Last Edit Date: 2018-XX-XX
Tel: [email protected]
Function Doc:
***************************************/
using UnityEngine;
public class Test : MonoBehaviour
{
//控制的NGUI组件
public UITexture m_texture;
//需要替换的图片
public Texture m_tex;
//增亮的叠加颜色
public Color m_backGroundColor;
//条纹色
public Color m_noiseColor;
//使用bool量控制 不然性能消耗大
public bool m_change = false;
public void Awake()
{
m_texture.onRender += UpdateMaterial;
}
//调用方法
public void OnButtonDown()
{
m_change = true;
}
private void UpdateMaterial(Material _m)
{
if (m_change)
{
//m_change = !m_change;
_m.SetTexture("__MainTex", m_tex);
_m.SetColor("_AddColor", m_backGroundColor);
_m.SetColor("Color0", m_noiseColor);
}
}
}
下面总结一下项目中用到的shader部分
资源包链接(鬼知道什么时候失效):链接:https://pan.baidu.com/s/1OU8pQ5myeuXptInuxlTl7A 密码:dt8h
ShaderForge算法图:
参数简要:
Shader代码:
// Shader created with Shader Forge v1.26
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.26;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,lico:1,lgpr:1,limd:0,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:3,bdst:7,dpts:2,wrdp:False,dith:0,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:True,qofs:0,qpre:3,rntp:2,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False;n:type:ShaderForge.SFN_Final,id:6456,x:33044,y:33111,varname:node_6456,prsc:2|custl-6788-OUT,alpha-3885-A;n:type:ShaderForge.SFN_Tex2d,id:3885,x:32431,y:33336,ptovrint:False,ptlb:MainTex,ptin:_MainTex,varname:node_3885,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Distance,id:2099,x:31037,y:33156,varname:node_2099,prsc:2|A-4069-OUT,B-8416-OUT;n:type:ShaderForge.SFN_Vector1,id:8416,x:30856,y:33354,varname:node_8416,prsc:2,v1:0.5;n:type:ShaderForge.SFN_OneMinus,id:2559,x:31237,y:33156,varname:node_2559,prsc:2|IN-2099-OUT;n:type:ShaderForge.SFN_Add,id:1289,x:31459,y:33156,varname:node_1289,prsc:2|A-2559-OUT,B-9541-OUT;n:type:ShaderForge.SFN_Slider,id:9541,x:31080,y:33386,ptovrint:False,ptlb:Sub,ptin:_Sub,varname:node_9541,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:-1,cur:-0.154,max:0;n:type:ShaderForge.SFN_Power,id:7763,x:31875,y:33136,varname:node_7763,prsc:2|VAL-5442-OUT,EXP-2491-OUT;n:type:ShaderForge.SFN_Slider,id:2491,x:31492,y:33391,ptovrint:False,ptlb:PowerValue,ptin:_PowerValue,varname:node_2491,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:5.793525,max:10;n:type:ShaderForge.SFN_Clamp01,id:5442,x:31649,y:33156,varname:node_5442,prsc:2|IN-1289-OUT;n:type:ShaderForge.SFN_Add,id:6788,x:32674,y:33205,varname:node_6788,prsc:2|A-3594-OUT,B-3885-RGB;n:type:ShaderForge.SFN_Rotator,id:1622,x:30507,y:33169,varname:node_1622,prsc:2|UVIN-2342-OUT,ANG-150-OUT;n:type:ShaderForge.SFN_ComponentMask,id:4069,x:30734,y:33146,varname:node_4069,prsc:2,cc1:0,cc2:-1,cc3:-1,cc4:-1|IN-1622-UVOUT;n:type:ShaderForge.SFN_Slider,id:150,x:30159,y:33377,ptovrint:False,ptlb:Angle,ptin:_Angle,varname:node_150,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0.4102564,max:1;n:type:ShaderForge.SFN_Add,id:2342,x:30238,y:33154,varname:node_2342,prsc:2|A-5168-OUT,B-5842-UVOUT;n:type:ShaderForge.SFN_Color,id:4851,x:32055,y:32879,ptovrint:False,ptlb:MainColor,ptin:_MainColor,varname:node_4851,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Multiply,id:3594,x:32441,y:33115,varname:node_3594,prsc:2|A-4851-RGB,B-7763-OUT,C-7314-OUT;n:type:ShaderForge.SFN_Time,id:7538,x:31571,y:33735,varname:node_7538,prsc:2;n:type:ShaderForge.SFN_Slider,id:7820,x:31477,y:33631,ptovrint:False,ptlb:Speed,ptin:_Speed,varname:node_7820,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:0.5,max:2;n:type:ShaderForge.SFN_Multiply,id:8576,x:31865,y:33704,varname:node_8576,prsc:2|A-7820-OUT,B-7538-T;n:type:ShaderForge.SFN_Frac,id:6961,x:32065,y:33704,varname:node_6961,prsc:2|IN-8576-OUT;n:type:ShaderForge.SFN_Subtract,id:5168,x:29948,y:32892,varname:node_5168,prsc:2|A-9989-OUT,B-2123-OUT;n:type:ShaderForge.SFN_Vector1,id:2123,x:29777,y:33056,varname:node_2123,prsc:2,v1:0.5;n:type:ShaderForge.SFN_Multiply,id:9989,x:29771,y:32753,varname:node_9989,prsc:2|A-3231-OUT,B-7454-OUT;n:type:ShaderForge.SFN_TexCoord,id:5842,x:29899,y:33158,varname:node_5842,prsc:2,uv:0;n:type:ShaderForge.SFN_Set,id:6403,x:32251,y:33704,varname:Timer,prsc:2|IN-6961-OUT;n:type:ShaderForge.SFN_Get,id:7454,x:29538,y:32823,varname:node_7454,prsc:2|IN-6403-OUT;n:type:ShaderForge.SFN_ValueProperty,id:7314,x:32145,y:33194,ptovrint:False,ptlb:AddPower,ptin:_AddPower,varname:node_7314,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:2;n:type:ShaderForge.SFN_ValueProperty,id:3231,x:29538,y:32689,ptovrint:False,ptlb:Pinlv,ptin:_Pinlv,varname:node_3231,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:3;proporder:3885-9541-2491-150-4851-7820-7314-3231;pass:END;sub:END;*/
Shader "Custom/JW_ShaoGuang1" {
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_Sub ("Sub", Range(-1, 0)) = -0.154
_PowerValue ("PowerValue", Range(0, 10)) = 5.793525
_Angle ("Angle", Range(0, 1)) = 0.4102564
_MainColor ("MainColor", Color) = (1,1,1,1)
_Speed ("Speed", Range(0, 2)) = 0.5
_AddPower ("AddPower", Float ) = 2
_Pinlv ("Pinlv", Float ) = 3
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
LOD 200
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
#pragma target 3.0
uniform float4 _TimeEditor;
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform float _Sub;
uniform float _PowerValue;
uniform float _Angle;
uniform float4 _MainColor;
uniform float _Speed;
uniform float _AddPower;
uniform float _Pinlv;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
float4 frag(VertexOutput i) : COLOR {
////// Lighting:
float node_1622_ang = _Angle;
float node_1622_spd = 1.0;
float node_1622_cos = cos(node_1622_spd*node_1622_ang);
float node_1622_sin = sin(node_1622_spd*node_1622_ang);
float2 node_1622_piv = float2(0.5,0.5);
float4 node_7538 = _Time + _TimeEditor;
float Timer = frac((_Speed*node_7538.g));
float2 node_1622 = (mul((((_Pinlv*Timer)-0.5)+i.uv0)-node_1622_piv,float2x2( node_1622_cos, -node_1622_sin, node_1622_sin, node_1622_cos))+node_1622_piv);
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
float3 finalColor = ((_MainColor.rgb*pow(saturate(((1.0 - distance(node_1622.r,0.5))+_Sub)),_PowerValue)*_AddPower)+_MainTex_var.rgb);
fixed4 finalRGBA = fixed4(finalColor,_MainTex_var.a);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
案例2:
Shader图:
Shader代码:
不给!(和上面差不多的)
资源链接:
链接:https://pan.baidu.com/s/1aL9rrLI4GL7g0NchOEIhMw 密码:8af2
效果图:
Shader图:
Shader代码:
// Shader created with Shader Forge v1.26
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.26;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,lico:1,lgpr:1,limd:0,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:3,bdst:7,dpts:2,wrdp:False,dith:0,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:True,qofs:0,qpre:3,rntp:2,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False;n:type:ShaderForge.SFN_Final,id:7659,x:32935,y:32815,varname:node_7659,prsc:2|custl-8121-OUT,alpha-3255-A;n:type:ShaderForge.SFN_Tex2d,id:163,x:32068,y:32843,ptovrint:False,ptlb:_MainTex,ptin:__MainTex,varname:node_163,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:c6facb50442a7c04f9b5ba8095afcb59,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Tex2d,id:1611,x:32129,y:33105,ptovrint:False,ptlb:MaskTex,ptin:_MaskTex,varname:node_1611,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:8c8f3f69aa942e44baa4fadb4b8fdd31,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Add,id:8121,x:32592,y:32846,varname:node_8121,prsc:2|A-163-RGB,B-6403-OUT,C-4092-OUT,D-5441-OUT;n:type:ShaderForge.SFN_Color,id:9959,x:32129,y:33323,ptovrint:False,ptlb:AddColor,ptin:_AddColor,varname:node_9959,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:0,c2:0,c3:0,c4:1;n:type:ShaderForge.SFN_Multiply,id:6403,x:32391,y:33162,varname:node_6403,prsc:2|A-1611-A,B-9959-RGB;n:type:ShaderForge.SFN_Tex2d,id:6092,x:32058,y:33579,ptovrint:False,ptlb:Ripple0,ptin:_Ripple0,varname:node_6092,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:02452105858c44340b87d2502a825cd3,ntxv:0,isnm:False|UVIN-2818-OUT;n:type:ShaderForge.SFN_Multiply,id:4092,x:32445,y:33548,varname:node_4092,prsc:2|A-6092-R,B-6593-RGB,C-1611-A;n:type:ShaderForge.SFN_Color,id:6593,x:32148,y:33784,ptovrint:False,ptlb:Color0,ptin:_Color0,varname:node_6593,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:0.8381984,c2:0.2762219,c3:0.9632353,c4:1;n:type:ShaderForge.SFN_Time,id:8528,x:30838,y:33298,varname:node_8528,prsc:2;n:type:ShaderForge.SFN_Slider,id:7763,x:30733,y:33491,ptovrint:False,ptlb:Speed,ptin:_Speed,varname:node_7763,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:-3,cur:3,max:3;n:type:ShaderForge.SFN_Multiply,id:824,x:31071,y:33402,varname:node_824,prsc:2|A-8528-TSL,B-7763-OUT;n:type:ShaderForge.SFN_TexCoord,id:2477,x:30676,y:33585,varname:node_2477,prsc:2,uv:0;n:type:ShaderForge.SFN_Add,id:6891,x:31292,y:33583,varname:node_6891,prsc:2|A-2477-U,B-824-OUT;n:type:ShaderForge.SFN_Append,id:2818,x:31801,y:33578,varname:node_2818,prsc:2|A-6891-OUT,B-2477-V;n:type:ShaderForge.SFN_Tex2d,id:3255,x:32645,y:33031,ptovrint:False,ptlb:Alpha,ptin:_Alpha,varname:node_3255,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:c6facb50442a7c04f9b5ba8095afcb59,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Tex2d,id:9275,x:31808,y:34002,ptovrint:False,ptlb:Ripple1,ptin:_Ripple1,varname:_Ripple1,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:02a272d8b6f1de94d9d5ca824188070c,ntxv:0,isnm:False|UVIN-9013-OUT;n:type:ShaderForge.SFN_Slider,id:2172,x:30604,y:34362,ptovrint:False,ptlb:Speed2,ptin:_Speed2,varname:_Speed_copy,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:-3,cur:-3,max:3;n:type:ShaderForge.SFN_Multiply,id:547,x:30942,y:34176,varname:node_547,prsc:2|A-8388-TSL,B-2172-OUT;n:type:ShaderForge.SFN_TexCoord,id:4734,x:30959,y:33977,varname:node_4734,prsc:2,uv:0;n:type:ShaderForge.SFN_Add,id:4775,x:31141,y:34184,varname:node_4775,prsc:2|A-4734-V,B-547-OUT;n:type:ShaderForge.SFN_Append,id:9013,x:31551,y:34001,varname:node_9013,prsc:2|A-4734-U,B-4775-OUT;n:type:ShaderForge.SFN_Time,id:8388,x:30709,y:34169,varname:node_8388,prsc:2;n:type:ShaderForge.SFN_Multiply,id:5441,x:32372,y:34000,varname:node_5441,prsc:2|A-9275-R,B-6593-RGB,C-1611-A;proporder:163-1611-9959-6092-6593-7763-3255-9275-2172;pass:END;sub:END;*/
Shader "Custom/JW_UV0" {
Properties {
__MainTex ("_MainTex", 2D) = "white" {}
_MaskTex ("MaskTex", 2D) = "white" {}
_AddColor ("AddColor", Color) = (0,0,0,1)
_Ripple0 ("Ripple0", 2D) = "white" {}
_Color0 ("Color0", Color) = (0.8381984,0.2762219,0.9632353,1)
_Speed ("Speed", Range(-3, 3)) = 3
_Alpha ("Alpha", 2D) = "white" {}
_Ripple1 ("Ripple1", 2D) = "white" {}
_Speed2 ("Speed2", Range(-3, 3)) = -3
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
LOD 200
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
#pragma target 3.0
uniform float4 _TimeEditor;
uniform sampler2D __MainTex; uniform float4 __MainTex_ST;
uniform sampler2D _MaskTex; uniform float4 _MaskTex_ST;
uniform float4 _AddColor;
uniform sampler2D _Ripple0; uniform float4 _Ripple0_ST;
uniform float4 _Color0;
uniform float _Speed;
uniform sampler2D _Alpha; uniform float4 _Alpha_ST;
uniform sampler2D _Ripple1; uniform float4 _Ripple1_ST;
uniform float _Speed2;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
float4 frag(VertexOutput i) : COLOR {
////// Lighting:
float4 __MainTex_var = tex2D(__MainTex,TRANSFORM_TEX(i.uv0, __MainTex));
float4 _MaskTex_var = tex2D(_MaskTex,TRANSFORM_TEX(i.uv0, _MaskTex));
float4 node_8528 = _Time + _TimeEditor;
float2 node_2818 = float2((i.uv0.r+(node_8528.r*_Speed)),i.uv0.g);
float4 _Ripple0_var = tex2D(_Ripple0,TRANSFORM_TEX(node_2818, _Ripple0));
float4 node_8388 = _Time + _TimeEditor;
float2 node_9013 = float2(i.uv0.r,(i.uv0.g+(node_8388.r*_Speed2)));
float4 _Ripple1_var = tex2D(_Ripple1,TRANSFORM_TEX(node_9013, _Ripple1));
float3 finalColor = (__MainTex_var.rgb+(_MaskTex_var.a*_AddColor.rgb)+(_Ripple0_var.r*_Color0.rgb*_MaskTex_var.a)+(_Ripple1_var.r*_Color0.rgb*_MaskTex_var.a));
float4 _Alpha_var = tex2D(_Alpha,TRANSFORM_TEX(i.uv0, _Alpha));
fixed4 finalRGBA = fixed4(finalColor,_Alpha_var.a);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
资源链接:
链接:https://pan.baidu.com/s/1tMaRiKHymh4KG3lkCvkPRg 密码:7f64
效果图:
原理图:
资源链接:
链接:https://pan.baidu.com/s/1oqyigNzobb0tElIZu-4HEA 密码:lmlc