Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
2
Question by EnergGames · Jun 20, 2016 at 10:18 AM · materialspritesscrollviewmaskingcustom-shader

Scroll view not working when children have material

Hi guys,

I'm struggling a little with this problem: I have a scroll view which is working perfectly that contains different sprites. Since i needed these sprites to be circle-shaped, i added a custom shader found in this post. Then, I tried applying a materal with the aforementioned shader on the images, and it works flawlessly, but then the viewport doesn't mask the images anymore!

I tried with other default materials, and every time a material is setted on the children of a scrollview, than it just stop masking them.

Do you know why this happens or a way to handle this? Since I really need those images to be circular... (i could also edit the texture on runtime to do this, but the material way is preferable and more flexible; if really there's no other way, I'll do it)

P.S.: I do not have the textures on my pc, since are retrieved from Facebook API (they're the users pictures)

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
3
Best Answer

Answer by EnergGames · Jun 27, 2016 at 12:39 PM

Honestly, I still haven't fully understanded how scroll view works, but I ended up creating two shaders, one for the texture and one for the scroll view. Using the stencil buffer, I managed to draw only the pixels that were within the scroll view with my custom shader (material with custom shader) setted.

Looking up into the documentation, it's said that the scrollview works setting the stencil buffer to 1, but even checking the 1 on the stencil buffer, without a specific materials set on the scrollview, it doesn't actually detect nothing and just draw.

For anyone who's interested, here it is how my two shaders look like:

This check the stencil buffer and draw only if it finds a 1 in the buffer (and mask the texture using the alpha of a different one)

 Shader "Custom/StencilEqualOne" {
 
     Properties{
         
         _Stencil("Stencil ID", Float) = 0
         _MainTex("Texture", 2D) = "white" {}
         _AlphaMask("Mask texture", 2D) = "white" {}
     
     }
     
     SubShader{
 
         Tags{
             "Queue" = "Transparent" 
             "RenderType" = "Transparent"
         }
 
         LOD 200
         Lighting Off
         ZWrite Off
         ZTest Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
 
             Stencil {
                 Ref 1
                 Comp equal
                 Pass keep
             }
         
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             struct Input {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 half2 texcoord : TEXCOORD0;
             };
         
             struct v2f {
                 float4 vertex : SV_POSITION;
                 fixed4 color : COLOR;
                 half2 texcoord : TEXCOORD0;
                 float4 worldPosition : TEXCOORD1;
             };
 
             sampler2D _MainTex;
             sampler2D _AlphaMask;
 
             v2f vert(Input IN) {
                 v2f o;
                 o.worldPosition = IN.vertex;
                 o.vertex = mul(UNITY_MATRIX_MVP, o.worldPosition);
                 o.texcoord = IN.texcoord;
                 o.color = IN.color;
                 return o;
             }
         
             fixed4 frag(v2f IN) : SV_Target {
 
                 fixed4 col = tex2D(_MainTex, IN.texcoord);
                 col.a *= tex2D(_AlphaMask, IN.texcoord).a;
                 clip (col.a - 0.001);
                 return col;
             }
 
             ENDCG
         }
     }
     Fallback "Diffuse"
 }

This one set the stencil buffer to 1 (applied to the scrollview) and draw a texture as background

 Shader "Custom/StencilMaskOne" {
 
     Properties{
     
         _MainTex("Texture", 2D) = "white" {}
     
     }
     
     SubShader{
         
         Tags{
             "Queue" = "Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
         
         //ColorMask 0
         ZWrite off
 
         Lighting Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         Stencil {
             Ref 1
             Comp always
             Pass Replace
         }
 
         Pass {
 
             Cull Off
             ZWrite On
             ZTest Less
             
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             struct appdata {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 half2 texcoord : TEXCOORD0;
             };
 
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 fixed4 color : COLOR;
                 half2 texcoord : TEXCOORD0;
                 float4 worldPosition : TEXCOORD1;
             };
 
             v2f vert (appdata v) {
 
                 v2f o;
                 o.worldPosition = v.vertex;
                 o.pos = mul(UNITY_MATRIX_MVP, o.worldPosition);
                 o.texcoord = v.texcoord;
                 o.color = v.color;
                 return o;
             }
 
             sampler2D _MainTex;
 
             half4 frag(v2f i) : COLOR {
                 half4 col = tex2D(_MainTex, i.texcoord) * i.color;
                 return col;
             }
             ENDCG
         }
     }
 }

I'm a newcomer when it comes to shader programming, so there could be some errors within my shaders, but in my case they're working flawlessly

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 blueknee · Sep 14, 2016 at 08:36 AM 0
Share

Thank you EnergGames, I ran into the same problem and you saved me!

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

63 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

Related Questions

How to make a double sided sprite that has two different images on each side? 0 Answers

Fading groups of sprites cheaply? 0 Answers

Sprite mask and tiled sprite 1 Answer

Image showing out of Mask when applying shader 0 Answers

Using Sprite As Mask For Shader 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