Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gameDude4 · Dec 23, 2018 at 07:58 AM · shadershadersshader programmingpixel

Post-processing shader using 3D pixel coordinates to dictate colour

Dear All,

I am trying to make a post-processor shader which I cannot quite crack.

Essentially I want to make a post processing shader that finds the 3D world coordinate of its associated pixels, based upon these locations I want to calculate the distance from a game object and then "tint" the pixels based upon this distance.

The end result would be a postprocessor shader that will change colour based upon what you are looking at and how close that is to a specific game object.

So far I have been trying a variety of shaders myself and based also upon those found online however am stuck and starting to question if this is even possible.

Has anyone got any ideas or code that could help me on my way?

Thanks in advance for your help!

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
0

Answer by BastianUrbach · Dec 25, 2018 at 12:32 PM

This is how it was done in the old GlobalFog image effect:

 Camera cam = GetComponent<Camera>();
 Transform camtr = cam.transform;
 cam.depthTextureMode = DepthTextureMode.Depth;
 
 Vector3[] frustumCorners = new Vector3[4];
 cam.CalculateFrustumCorners(new Rect(0, 0, 1, 1), cam.farClipPlane, cam.stereoActiveEye, frustumCorners);
 var bottomLeft = camtr.TransformVector(frustumCorners[0]);
 var topLeft = camtr.TransformVector(frustumCorners[1]);
 var topRight = camtr.TransformVector(frustumCorners[2]);
 var bottomRight = camtr.TransformVector(frustumCorners[3]);
 
 Matrix4x4 frustumCornersArray = Matrix4x4.identity;
 frustumCornersArray.SetRow(0, bottomLeft);
 frustumCornersArray.SetRow(1, bottomRight);
 frustumCornersArray.SetRow(2, topLeft);
 frustumCornersArray.SetRow(3, topRight);
 material.SetMatrix("_FrustumCornersWS", frustumCornersArray);


 #pragma vertex vert
 #pragma fragment frag
 
 #include "UnityCG.cginc"
 
 struct appdata
 {
     float4 vertex : POSITION;
     float2 uv : TEXCOORD0;
 };
 
 struct v2f
 {
     float2 uv : TEXCOORD0;
     float2 uv_depth : TEXCOORD1;
     float4 interpolatedRay : TEXCOORD2;
     float4 vertex : SV_POSITION;
 };
 
 float4x4 _FrustumCornersWS;
 float4 _MainTex_TexelSize;
 
 v2f vert (appdata v)
 {
     v2f o;
     o.vertex = UnityObjectToClipPos(v.vertex);
     o.uv = v.uv.xy;
     o.uv_depth = v.uv.xy;
     
     #if UNITY_UV_STARTS_AT_TOP
     if (_MainTex_TexelSize.y < 0)
         o.uv.y = 1-o.uv.y;
     #endif
 
     int frustumIndex = v.uv.x + (2 * o.uv.y);
     o.interpolatedRay = _FrustumCornersWS[frustumIndex];
     o.interpolatedRay.w = frustumIndex;
 
     return o;
 }
 
 sampler2D _MainTex;
 sampler2D _CameraDepthTexture;
 
 fixed4 frag (v2f i) : SV_Target
 {
     float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(i.uv_depth));
     float dpth = Linear01Depth(rawDepth);
     float3 wsDir = dpth * i.interpolatedRay.xyz;
     float3 wsPos = _WorldSpaceCameraPos + wsDir;
 
     return float4(frac(wsPos.xyz), 1);
 }
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

161 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 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 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

Post-processing shader using 3D pixel coordinates to dictate colour,Postprocessor shader based upon pixel 3D location and distance from object,Genuinely challenging post-processing shader... 1 Answer

Displace and show normals in fragment shader 1 Answer

Custom UI Shader Stencil Problem [willing to pay for fix] 1 Answer

How to set shader vertex normals to face up 1 Answer

EnableKeyword("_EMISSION") not working 2019.3.13f1 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