Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 TheSorm · Jul 09, 2016 at 10:23 PM · shaderbatchingshader writingpass

Shader Passes

Hey i write a little 2D outline shader, but my problem is that i use multiple Passes, so dynamic batching do not work and thats bad for performance.

This is one Pass, i just draw the vertexes like 4 times, x+spread, x-spread,y+spread, y-spread. Than in the last Pass i paint the sprite in his normal color in the middle and i have a nice outline. Is ther a way to combine my passes ? If yes how can i do that?

Pass 1-4 loks like this (this is the case: vertex.x + spread):

 Pass{
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #pragma multi_compile DUMMY PIXELSNAP_ON
                 #include "UnityCG.cginc"
 
                     struct appdata_t
                     {
                         float4 vertex   : POSITION;
                         float4 color    : COLOR;
                         float2 texcoord : TEXCOORD0;
                     };
 
                     struct v2f
                     {
                         float4 vertex   : SV_POSITION;
                         fixed4 color : COLOR;
                         half2 texcoord  : TEXCOORD0;
                     };
 
                     fixed4 _Color;
                     float _OutLineSpread;
 
                     v2f vert(appdata_t IN)
                     {
                         v2f OUT;
                         float4 vertex = float4(IN.vertex.x , IN.vertex.y , IN.vertex.z, 1);
                         vertex += float4(vertex.x + _OutLineSpread, vertex.y, vertex.z, 1);
 
                         OUT.vertex = mul(UNITY_MATRIX_MVP, vertex);
                          
 
                         OUT.texcoord = IN.texcoord;
                         OUT.color = IN.color * _Color;
                         return OUT;
                     }
 
                     sampler2D _MainTex;
 
                     fixed4 frag(v2f IN) : SV_Target
                     {
                         fixed4 c = tex2D(_MainTex, IN.texcoord);
                     _Color *= c.a;
                     return _Color;
                     }
                     ENDCG
                  }

And the last Pass loks like this:

 Pass{
                     CGPROGRAM
                     #pragma vertex vert
                     #pragma fragment frag
                     #pragma multi_compile DUMMY PIXELSNAP_ON
                     #include "UnityCG.cginc"
 
                     struct appdata_t
                     {
                         float4 vertex   : POSITION;
                         float4 color    : COLOR;
                         float2 texcoord : TEXCOORD0;
                     };
 
                     struct v2f
                     {
                         float4 vertex   : SV_POSITION;
                         fixed4 color : COLOR;
                         half2 texcoord  : TEXCOORD0;
                     };
 
                     fixed4 _Color;
                     float _OutLineSpreadX;
                     float _OutLineSpreadY;
 
                     v2f vert(appdata_t IN)
                     {
                         v2f OUT;
                         OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
                         OUT.texcoord = IN.texcoord ;
                         OUT.color = IN.color;
 
                         return OUT;
                     }
 
                     sampler2D _MainTex;
 
                     fixed4 frag(v2f IN) : SV_Target
                     {
                         fixed4 c = tex2D(_MainTex, IN.texcoord);
                         c.rgb *= c.a;
                         return c;
                     }
                         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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jul 10, 2016 at 09:05 AM

No, it's not possible to do in a single pass, unless you were to dynamically bake the outline into the sprite texture on the CPU, but that's almost certain to be slower.

Comment
Add comment · Show 1 · Share
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
avatar image TheSorm · Jul 10, 2016 at 10:46 AM 0
Share

Hm idk because I render up to thousands of this objects with this shade and end up with more than 4000 batches ... is there a way to bake the outline at the begin of the game in the texture and use it after that ?

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.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

Why are my GPU instanced objects all black? 2 Answers

Use contents of RWStructuredBuffer written by shader in another shader 0 Answers

Unity Shadergraph Alpha threshold problem 0 Answers

Where do I put this shader code? Getting weird error? 1 Answer

Shader Graph texture edge detection 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges