• 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
1
Question by PepinH · Nov 14, 2015 at 12:01 AM · 2dshadercolor

Y-inverted graphics in Scene vs Game views

I'm writing a simple shader that takes Red, Blue, and Yellow primary colors and mixes them with each other like paint (so blue and yellow make green, for instance). Everything looks fine in the Scene view, but pixels containing secondary colors are y-inverted in the game view. Any ideas what the issue could be?

alt text

Here's the shader:

 Shader "Custom/RBY" {
     Properties {
         _MainTex("Base (RGB)", 2D) = "white" {}
         
         _Red       ("Red",    Color)    = (1,   0,     0,   1) // RGBA for Red
         _Blue      ("Blue",   Color)    = (0,   0,     1,   1) // RGBA for Blue
         _Yellow    ("Yellow", Color)    = (1,   1,     0,   1) // RGBA for Yellow
         _Purple    ("Purple", Color)    = (0.5, 0,     0.5, 1) // RGBA when Red and Blue combine
         _Orange    ("Orange", Color)    = (1,   0.647, 0,   1) // RGBA when Red and Yellow combine
         _Green     ("Green",  Color)    = (0,   1,     0,   1) // RGBA when Blue and Yellow combine
         _AllColors ("AllColors", Color) = (0,   0,     0,   1) // RGBA when Red, Blue, and Yellow combine
     }
 
     SubShader {
         Tags{ "Queue" = "Transparent" }
 
         GrabPass { }
 
         Pass {
             CGPROGRAM
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             half4 _Red;
             half4 _Blue;
             half4 _Yellow;
             half4 _Purple;
             half4 _Orange;
             half4 _Green;
             half4 _AllColors;
 
             uniform sampler2D _MainTex;
             sampler2D _GrabTexture;
 
             struct v2f {
                 half4 pos : POSITION;
                 half2 uv : TEXCOORD0;
                 float2 screenPos:TEXCOORD1;
             };
 
             bool areColorsEqual(half4 colorA, half4 colorB, bool isAlphaSensitive) {
                 if (colorA.r != colorB.r || colorA.g != colorB.g || colorA.b != colorB.b || (isAlphaSensitive && colorA.a != colorB.a)) {
                     return false;
                 } else {
                     return true;
                 }
             }
 
             v2f vert(appdata_img v) {
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.screenPos = ComputeScreenPos(o.pos);
                 o.uv = v.texcoord;
 
                 return o;
             }
 
             half4 frag(v2f i) : COLOR{
                 half4 sourceColor = tex2D(_MainTex, i.uv);
                 half4 destinationColor = tex2D(_GrabTexture, half2(i.screenPos.x, i.screenPos.y));
 
                 if (areColorsEqual(sourceColor, _Red, false) && sourceColor.a > 0) {
                     if (areColorsEqual(destinationColor, _Blue, false)) {
                         destinationColor = _Purple;
                     } else if (areColorsEqual(destinationColor, _Yellow, false)) {
                         destinationColor = _Orange;
                     } else if (areColorsEqual(destinationColor, _Green, false)) {
                         destinationColor = _AllColors;
                     } else {
                         destinationColor = sourceColor;
                     }
                 } else if (areColorsEqual(sourceColor, _Blue, false) && sourceColor.a > 0) {
                     if (areColorsEqual(destinationColor, _Red, false)) {
                         destinationColor = _Purple;
                     } else if (areColorsEqual(destinationColor, _Yellow, false)) {
                         destinationColor = _Green;
                     } else if (areColorsEqual(destinationColor, _Orange, false)) {
                         destinationColor = _AllColors;
                     } else {
                         destinationColor = sourceColor;
                     }
                 } else if (areColorsEqual(sourceColor, _Yellow, false) && sourceColor.a > 0) {
                     if (areColorsEqual(destinationColor, _Red, false)) {
                         destinationColor = _Orange;
                     } else if (areColorsEqual(destinationColor, _Blue, false)) {
                         destinationColor = _Green;
                     } else if (areColorsEqual(destinationColor, _Purple, false)) {
                         destinationColor = _AllColors;
                     } else {
                         destinationColor = sourceColor;
                     }
                 } else {
                     discard;
                 }
                 return destinationColor;
             }
             ENDCG
         }
     }
 
     Fallback off
 }


scene-vs-game.png (246.5 kB)
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

48 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

Related Questions

Shader invert sprite colors 2D 1 Answer

How to lerp through colors for a sprite? 1 Answer

Antichamber-like shader for lights and outline? 0 Answers

Increase thickness of outlines 0 Answers

How create a material silhouette? 0 Answers

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