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 Julien-Lynge · Jun 06, 2013 at 09:18 PM · camerashaderrendertextureorthographic

Shader: get size of a pixel for orthographic camera

I've been working on a shader to create object outlines, and I can't seem to figure out what the width/height of a single pixel is in projection space, i.e. after you've multiplied everything by MVP. What I would like to do is move each vertex out along a normal by some number of pixels.

So let's say I have a simple 1x1 square quad. At each corner I have a normal, pointing out from the center of the quad. All the points and the normals are in 2D space relative to the camera - they quad is facing the camera straight on. I then take each normal and get it into projection space:

 float3 norm = mul((float3x3)UNITY_MATRIX_MVP, v.normal);
 float2 offset = norm.xy;

Then, I want to take that offset direction and multiply it by, say, 2 pixels. So something like this, though this code isn't working:

 output.pos.xy += (offset * _NumberOfPixels) / _ScreenParams.y ;

(note: output.pos is the vector pos after it's converted to projection space)

A bit more information: this camera's orthographic size changes over time. I'm rendering the output of the ortho camera to a RenderTexture, which has a known size in pixels.

I'm happy to write code to keep track of the camera size with a script, and pass it to a material variable, if someone can explain to me how to use them to determine pixel size in projection space.

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 Julien-Lynge · Jun 06, 2013 at 09:50 PM 0
Share

As a side note, is there any way to get a multiplier based on the size of a pixel that will work for both orthographic and perspective cameras? I would like to be able to write shaders that can move verts around a set number of pixels regardless of the camera rendering them.

2 Replies

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

Answer by goal2112 · Jul 09, 2013 at 05:02 PM

I think that there has to be a global value for a pixel size, but otherwise you can always pass it in to your shader.

 pixel.width = 1.0f / Screen.width;
 pixel.height = 1.0f / Screen.height;

simple enough.

EDIT: Just found this: http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html

 float4 _ScreenParams : 
 x is current render target width in pixels 
 y is current render target height in pixels 
 z is 1.0 + 1.0/width 
 w is 1.0 + 1.0/height
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 nicloay · Oct 14, 2018 at 06:29 AM

use unity_OrthoParams.xy

You can download shader sources from unity download pages Then at UnityShaderVariables.cginc find this

     // x = orthographic camera's width
     // y = orthographic camera's height
     // z = unused
     // w = 1.0 if camera is ortho, 0.0 if perspective
     float4 unity_OrthoParams;

And it's also available at this page https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

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.

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

Unity Grabpass Feature 1 Answer

Easy way to render a texture, pixel perfectly, with a shader, and get the result? 0 Answers

Use camera depth texture to find distance from camera 0 Answers

,Can camera output negative values to render texture? 0 Answers

Check if mouse is over GUI element using Rendertexture? 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