• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's the best way to read a texture in camera space in a vertex shader? 0 Answers

Multi-shader Stencil Buffer not working as expected,Multi-shader Stencil not working as expected 0 Answers

Shader issue with a 170 upper angle 0 Answers

Is there a way to replicate this effect on Unity? 1 Answer

Shader Working in Shader Graph But Not In Scene 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges