- Home /
need help making this mobile shader more efficient
hi all, i made this shader in shaderforge since i'm not to savvy yet on shader code. my desired effect is that the alpha channel of one texture can be applied as a mask to another texture on the same material, and can be panned to create a "spotlight" type effect on the shader.
i built this in shader forge, but unfortunately with everything else i've got going, just adding this shader on two quads in the scene drops the game by 10 fps.
i'm sure this can be done better, but i just don't have the experience yet.
shader should ideally receive global fog and ambient light.
thanks in advance
// Shader created with Shader Forge Beta 0.36
// Shader Forge (c) Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:0.36;sub:START;pass:START;ps:flbk:,lico:1,lgpr:1,nrmq:1,limd:1,uamb:True,mssp:True,lmpd:False,lprd:False,enco:False,frtr:True,vitr:True,dbil:True,rmgx:True,rpth:0,hqsc:False,hqlp:False,tesm:0,blpr:0,bsrc:0,bdst:0,culm:0,dpts:2,wrdp:True,ufog:True,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,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,ofsf:0,ofsu:0,f2p0:False;n:type:ShaderForge.SFN_Final,id:1,x:32719,y:32712|diff-671-OUT;n:type:ShaderForge.SFN_Blend,id:671,x:33118,y:32722,blmd:1,clmp:True|SRC-673-RGB,DST-675-OUT;n:type:ShaderForge.SFN_Tex2d,id:673,x:33393,y:32572,ptlb:MainTex,ptin:_MainTex,tex:f2649719d690348568eaf59822801e1f,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Multiply,id:675,x:33182,y:32997|A-677-A,B-679-RGB;n:type:ShaderForge.SFN_Tex2d,id:677,x:33410,y:32821,ptlb:Overlay,ptin:_Overlay,tex:,ntxv:0,isnm:False|UVIN-683-UVOUT;n:type:ShaderForge.SFN_Color,id:679,x:33410,y:33025,ptlb:Color,ptin:_Color,glob:False,c1:1,c2:1,c3:1,c4:1;n:type:ShaderForge.SFN_Panner,id:683,x:33689,y:32739,spu:0,spv:1|UVIN-685-UVOUT,DIST-687-OUT;n:type:ShaderForge.SFN_TexCoord,id:685,x:34001,y:32660,uv:0;n:type:ShaderForge.SFN_Slider,id:687,x:33968,y:32929,ptlb:Horizontal,ptin:_Horizontal,min:0,cur:1,max:1;proporder:673-679-687-677;pass:END;sub:END;*/
Shader "FX/scrollingMask" {
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_Horizontal ("Horizontal", Range(0, 1)) = 1
_Overlay ("Overlay", 2D) = "white" {}
}
SubShader {
Tags {
"RenderType"="Opaque"
}
Pass {
Name "ForwardBase"
Tags {
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _LightColor0;
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _Overlay; uniform float4 _Overlay_ST;
uniform float4 _Color;
uniform float _Horizontal;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
LIGHTING_COORDS(3,4)
};
VertexOutput vert (VertexInput v) {
VertexOutput o;
o.uv0 = v.texcoord0;
o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
/////// Normals:
float3 normalDirection = i.normalDir;
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i)*2;
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = dot( normalDirection, lightDirection );
float3 diffuse = max( 0.0, NdotL) * attenColor + UNITY_LIGHTMODEL_AMBIENT.rgb*2;
float3 finalColor = 0;
float3 diffuseLight = diffuse;
float2 node_800 = i.uv0;
float2 node_683 = (i.uv0.rg+_Horizontal*float2(0,1));
finalColor += diffuseLight * saturate((tex2D(_MainTex,TRANSFORM_TEX(node_800.rg, _MainTex)).rgb*(tex2D(_Overlay,TRANSFORM_TEX(node_683, _Overlay)).a*_Color.rgb)));
/// Final Color:
return fixed4(finalColor,1);
}
ENDCG
}
Pass {
Name "ForwardAdd"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
Fog { Color (0,0,0,0) }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd_fullshadows
#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
#pragma target 3.0
uniform float4 _LightColor0;
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _Overlay; uniform float4 _Overlay_ST;
uniform float4 _Color;
uniform float _Horizontal;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
LIGHTING_COORDS(3,4)
};
VertexOutput vert (VertexInput v) {
VertexOutput o;
o.uv0 = v.texcoord0;
o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
fixed4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
/////// Normals:
float3 normalDirection = i.normalDir;
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i)*2;
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = dot( normalDirection, lightDirection );
float3 diffuse = max( 0.0, NdotL) * attenColor;
float3 finalColor = 0;
float3 diffuseLight = diffuse;
float2 node_801 = i.uv0;
float2 node_683 = (i.uv0.rg+_Horizontal*float2(0,1));
finalColor += diffuseLight * saturate((tex2D(_MainTex,TRANSFORM_TEX(node_801.rg, _MainTex)).rgb*(tex2D(_Overlay,TRANSFORM_TEX(node_683, _Overlay)).a*_Color.rgb)));
/// Final Color:
return fixed4(finalColor * 1,0);
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
Your answer
Follow this Question
Related Questions
Black Material if Use Mip level?!! 0 Answers
Transparent material on iPhone Android 3 Answers
Shader performance question. 0 Answers
My shader works on desktop, but black screen on Android. 0 Answers