Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ben Humphreys · May 03, 2013 at 06:33 AM · shadergraphicsimage

Screen-space Shader Debug

I'm trying to write a fragment shader that works in screen-space, to produce an even gradient across the screen. I've made some progress thanks to this Unity Answer, but for some reason my shader's gradient doesn't stretch the whole width of the screen, only some of the middle. Here's an image of the effect. alt text

Here is the shader. It could be shorter but I've tried to keep it verbose for clarity for myself.

 Shader "Debug/AllInput" { 
    SubShader { 
       Pass { 
          CGPROGRAM 
  
          #include "UnityCG.cginc"
 
          #pragma vertex vert  
          #pragma fragment frag 
  
          struct vertexInput {
             float4 vertex : POSITION;
          };
          struct vertexOutput {
             float4 pos : SV_POSITION;
             float4 vertPos : TEXCOORD5; 
          };
  
          vertexOutput vert(vertexInput input) {
             vertexOutput output;
 
             output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
 
             // We're now in clipspace, ranging from -1,-1,-1, to 1,1,1
             float4 clipSpace =  mul(UNITY_MATRIX_MVP, input.vertex);
 
             // Convert from -1~1 to 0~1
             clipSpace.xy = 0.5*(clipSpace.xy+1.0);
 
             // Convert from 0~1 to 0~1280 (etc.)
             // Skip this, 0~1 more useful for debug rendering
             //clipSpace.xy *= _ScreenParams.xy;
 
             output.vertPos = clipSpace;
             return output;
          }
  
          float4 frag(vertexOutput input) : COLOR {
             return float4(input.vertPos.x, input.vertPos.x, input.vertPos.x, 1.0); 
          }
  
          ENDCG  
       }
    }
 }

Could someone tell me why this is?

Comment
Add comment · Show 1
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 whydoidoit · May 03, 2013 at 06:45 AM 1
Share

I'm guessing the output of the transform isn't what you are expecting. Try just plotting intput.vertPos.x, input.vertPos.x/1000 etc - see what you are actually getting. No pic BTW!

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Ben Humphreys · May 03, 2013 at 06:49 AM

I went back to the original post, and I think I fixed it. I was forgetting to divide by the width of the screen, to convert from Clip Space which is square, to the actual perspective. It's mentioned as "perspective divide" in the CG article from the previous Answer

So the calculations now look like:

     float4 clipSpace =  mul(UNITY_MATRIX_MVP, input.vertex);
     clipSpace.xy /= clipSpace.w;
     clipSpace.xy = 0.5*(clipSpace.xy+1.0);


Thanks again everyone :)

Comment
Add comment · Show 3 · 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 psantoki · Nov 08, 2013 at 01:09 AM 0
Share

Optimized for mad:

clipSpace.xy = clipSpace.xy * 0.5f + 0.5f;

(Always multiply then add, in that order)

avatar image Selmar psantoki · Dec 01, 2015 at 01:58 PM 0
Share

just curious, why would that be an optimization? is there any difference in execution speed?

avatar image Tutanhomon Selmar · Feb 21, 2017 at 01:15 PM 0
Share

Because multiplication is adding same value multiple times. Sometimes it can be just bitwise offset, and modern compilers optimise such operations. That's why @psantoki called it "for mad" - just because it takes place, but not so much helpful )

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

16 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

Related Questions

Show only specific part of UI.Image 0 Answers

cast shadow - export lightmap from 3ds max to unity 2 Answers

Culling part of a sprite in Sprite Manager 2 1 Answer

How to remove URP from the project 0 Answers

The Best Way To Make Stylised Grass in Unity? 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