- Home /
Question by
shruikan · Sep 26, 2017 at 02:14 PM ·
shadershadersshader programmingvertex shader
Mobile performance of splat map shader with distance blending
Hi, currently I'm working on shader that uses rgba channels of splatmap to distribute textures. Also I've added distance blending to avoid ugly tiling. Now I'm faceing very low performance on low end mobile devices. At first shader was made with shader forge and was rewritten to vertex shader. My question is how to optimize its code?
Shader "Unlit/GroundMaskShader UNLITLOW"
{
Properties
{
_SplatMap ("Splat Map", 2D) = "white" {}
_BasicGroundTextureBLACK ("Basic Ground Texture (BLACK)", 2D) = "white" {}
_BasicTextureFarUV ("Basic Texture Far UV", Float ) = .5
_FirstGroundTextureR ("First Ground Texture (R)", 2D) = "white" {}
_FirstTextureFarUV ("First Texture Far UV", Float ) = .5
_SecondGroundTextureG ("Second Ground Texture (G)", 2D) = "white" {}
_SecondTextureFarUV ("Second Texture Far UV", Float ) = .5
_ThirdGroundTextureB ("Third Ground Texture (B)", 2D) = "white" {}
_ThirdTextureFarUV ("Third Texture Far UV", Float ) = .5
_FourthGroundTextureA ("Fourth Ground Texture (A)", 2D) = "white" {}
_FourthTextureFarUV ("Fourth Texture Far UV", Float ) = .5
_UVTransitionDistance ("UV Transition Distance", Float ) = 100
_UVTransitionSharpness ("UV Transition Sharpness", Float ) = 1
_Brightness ("Brightness", Float ) = 1.5
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
half2 texcoord0 : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 uv0 : TEXCOORD0;
half2 uv1 : TEXCOORD1;
half2 uv2 : TEXCOORD2;
half2 uv3 : TEXCOORD3;
half2 uv4 : TEXCOORD4;
half2 uv5 : TEXCOORD5;
float4 posWorld : TEXCOORD6;
half2 uv1_m : TEXCOORD7;
half2 uv2_m : TEXCOORD8;
half2 uv3_m : TEXCOORD9;
half2 uv4_m : TEXCOORD10;
half2 uv5_m : TEXCOORD11;
//float dist;
};
sampler2D _SplatMap; half4 _SplatMap_ST;
sampler2D _BasicGroundTextureBLACK; half4 _BasicGroundTextureBLACK_ST;
sampler2D _ThirdGroundTextureB; half4 _ThirdGroundTextureB_ST;
sampler2D _FirstGroundTextureR; half4 _FirstGroundTextureR_ST;
sampler2D _SecondGroundTextureG; half4 _SecondGroundTextureG_ST;
sampler2D _FourthGroundTextureA; half4 _FourthGroundTextureA_ST;
half _UVTransitionDistance;
half _UVTransitionSharpness;
half _BasicTextureFarUV;
half _SecondTextureFarUV;
half _ThirdTextureFarUV;
half _FourthTextureFarUV;
half _FirstTextureFarUV;
half _Brightness;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.uv0 = TRANSFORM_TEX(v.texcoord0, _SplatMap);
o.uv1 = TRANSFORM_TEX(v.texcoord0, _BasicGroundTextureBLACK);
o.uv2 = TRANSFORM_TEX(v.texcoord0, _FirstGroundTextureR);
o.uv3 = TRANSFORM_TEX(v.texcoord0, _SecondGroundTextureG);
o.uv4 = TRANSFORM_TEX(v.texcoord0, _ThirdGroundTextureB);
o.uv5 = TRANSFORM_TEX(v.texcoord0, _FourthGroundTextureA);
o.uv1_m = o.uv1 * _BasicTextureFarUV;
o.uv2_m = o.uv2 * _FirstTextureFarUV;
o.uv3_m = o.uv3 * _SecondTextureFarUV;
o.uv4_m = o.uv4 * _ThirdTextureFarUV;
o.uv5_m = o.uv5 * _FourthTextureFarUV;
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 splatmap = tex2D(_SplatMap, i.uv0);
fixed node_271 = saturate(pow((distance(i.posWorld,_WorldSpaceCameraPos)/_UVTransitionDistance),_UVTransitionSharpness));
fixed3 node_6448 = tex2D(_BasicGroundTextureBLACK, i.uv1);
fixed3 node_6722 = tex2D(_BasicGroundTextureBLACK, i.uv1_m);
fixed3 node_85 = lerp(node_6448,node_6722,node_271);
fixed3 node_4541 = tex2D(_FirstGroundTextureR, i.uv2);
fixed3 node_7125 = tex2D(_FirstGroundTextureR, i.uv2_m);
fixed3 node_982 = lerp(node_4541,node_7125,node_271);
fixed3 node_3714 = tex2D(_SecondGroundTextureG, i.uv3);
fixed3 node_2807 = tex2D(_SecondGroundTextureG, i.uv3_m);
fixed3 node_5465 = lerp(node_3714,node_2807,node_271);
fixed3 node_4204 = tex2D(_ThirdGroundTextureB, i.uv4);
fixed3 node_5812 = tex2D(_ThirdGroundTextureB, i.uv4_m);
fixed3 node_8952 = lerp(node_4204,node_5812,node_271);
fixed3 node_9400 = tex2D(_FourthGroundTextureA, i.uv5);
fixed3 node_6127 = tex2D(_FourthGroundTextureA, i.uv5_m);
fixed3 node_8866 = lerp(node_9400,node_6127,node_271);
fixed3 node_8901 = lerp((lerp( lerp( lerp( node_85, node_982, splatmap.r ), node_5465, splatmap.g ), node_8952, splatmap.b )), node_8866, (1.0 - splatmap.a));
return fixed4(node_8901 * _Brightness,1);
}
ENDCG
}
}
}
Comment