• 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 roberto_sc · Apr 05, 2014 at 09:10 AM · shadershaderlab

Pixel color replacement shader.

How can I write a shader to test the value of a pixel and if is a certain color then replace it with the color of the pixel of another camera in the same screen position?

I imagine there are 2 approaches:

1 - don't draw the pixel, leaving the camera's clear flags to "depth only", making the engine replace the non-drawn pixels with the pixels of the other camera with a lower depth;

2 - replace the pixels with pixels of another texture.

I tried the second approach, adapting a shader I found, resulting in this:

 Shader "ColorReplacement" {

     Properties {

         _MainTex ("Greyscale (R) Alpha (A)", 2D) = "white" {}

         _ColorRamp ("Colour Palette", 2D) = "gray" {}

     }
     SubShader {
         ZTest LEqual
         ZWrite On
         Pass {
             Name "ColorReplacement"
             CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #pragma fragmentoption ARB_precision_hint_fastest
                 #include "UnityCG.cginc"

                 struct v2f
                 {
                     float4  pos : SV_POSITION;
                     float2  uv : TEXCOORD0;
                 }; 

                 v2f vert (appdata_tan v)
                 {
                     v2f o;
                     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                     o.uv = v.texcoord.xy;
                     return o;
                 }
                 
                 sampler2D _MainTex;
                 sampler2D _ColorRamp;

                 float4 frag(v2f i) : COLOR
                 {
                 // SURFACE COLOUR
                     float greyscale = tex2D(_MainTex, i.uv).r;
                 // RESULT
                     float4 result;
                     result = tex2D(_MainTex, i.uv);
                     if(result.a > 0.5)
                         result.rgb = tex2D(_MainTex, i.uv).rgb;
                     else
                         result.rgb = tex2D(_ColorRamp, i.uv).rgb;
                     return result;
                 }
             ENDCG
         }
     }
     Fallback off
  }


It works but, replacing to whatever is in the _ColorRamp texture when the pixel alpha is greater than 0.5. But the problem is that it considers the rotation and scale, as if the replacement texture was applied to the GameObject, which I don't want.

To exemplify, some figures. Here is a square smile figure using a regular unlit shader (no transparency). The part in white has transparency, the checkered background is drawn by a second camera with a lower depth:

alt text

This is what I get using the above shader on the left, and the desired result on the right:

alt text

The desired result was achieve using a regular unlit transparent shader. However, I cannot use this shader because I don't want transparency, that is, if there's something behind the smiley game object, it shouldn't be visible.

screen shot 2014-04-05 at 9.21.51 pm.png (134.4 kB)
screen shot 2014-04-05 at 10.06.24 pm.png (212.9 kB)
Comment
Add comment · Show 2
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 Kiloblargh · Apr 05, 2014 at 09:44 AM 0
Share

I don't understand your last two sentences at all. I see no reason why using alpha channel transparency doesn't solve your problem.

avatar image roberto_sc · Apr 05, 2014 at 09:56 AM 0
Share

I don't want any object behind the smiley game object to be drawn. The alpha pixels in the smiley texture are supposed to be replaced with the contents of a second camera with a lower depth, not with the stuff that is behind the smiley game object.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by xyiphk · Dec 24, 2016 at 11:17 AM

@roberto_sc I have a same problem and I found the solution which is "Depth Mask"!

http://wiki.unity3d.com/index.php?title=DepthMask http://blog.sina.com.cn/s/blog_4ef78af501010ruy.html

I use this one. It works.

 Shader "Depth Mask Complex"
 {
     SubShader
     {
         Tags {"Queue" = "Background"}
         Blend SrcAlpha OneMinusSrcAlpha
         Lighting Off
         ZWrite On
         ZTest Always
         Pass
         {
             Color(0,0,0,0)
         }
     }
 }
Comment
Add comment · 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
0

Answer by Zer0_Ph34r · Aug 17, 2017 at 03:41 PM

If all you're looking for is to remove the white space from around or inside an image, edit the image in a simple editor, erase all the white that you do not want, and save the image as a .png. Unity will take care of the rest.

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

How to move vertices up/down randomly in shader. 1 Answer

Can anyone help me with reflective shader with fall off property? 0 Answers

How do you hide a shader property? 2 Answers

Logical BlendOp not working 0 Answers

Shader - What is float3.xy? 1 Answer


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