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 rocket350 · Jul 23, 2016 at 07:30 PM · lightingshaderlabsurface shaderrefractionupside-down

Refraction Surface Shader Upside down

Hello everyone,

I have a surface refraction shader which works great when my camera is set to deferred lighting. Weirdly enough though, as soon as I switch to forward rendering, the shader refracts the things behind it upside down. I don't know much about shaders, but I'm pretty sure it has something to do with the "Render Texture Coordinates" section in this post.

Here is the shader:

 Shader "Effects/Distort" {
     Properties{
         _Refraction("Refraction", Range(0.00, 100.0)) = 1.0
         _DistortTex("Base (RGB)", 2D) = "white" {}
         _ObjectVisibility("Visibility", Range(0.00, 1.00)) = 0.05
     }
 
         SubShader{
 
         Tags{ "RenderType" = "Transparent" "Queue" = "Overlay" }
         LOD 100
 
         GrabPass
     {
 
     }
 
     CGPROGRAM
 #pragma surface surf NoLighting
 #pragma vertex vert
 
         fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten) {
         fixed4 c;
         c.rgb = s.Albedo;
         c.a = s.Alpha;
         return c;
     }
 
     sampler2D _GrabTexture : register(s0);
     sampler2D _DistortTex : register(s2);
     float _Refraction;
 
     float _ObjectVisibility;
 
     float4 _GrabTexture_TexelSize;
 
     struct Input {
         float2 uv_DistortTex;
         float3 color;
         float3 worldRefl;
         float4 screenPos;
         INTERNAL_DATA
     };
     struct appdata_t {
         float4 vertex : POSITION;
     };
 
     
     void vert(inout appdata_full v, out Input o) {
         UNITY_INITIALIZE_OUTPUT(Input, o);
         o.color = v.color;
     }
 
     void surf(Input IN, inout SurfaceOutput o) {
         float3 distort = tex2D(_DistortTex, IN.uv_DistortTex) * float3(IN.color.r,IN.color.g,IN.color.b);
         float2 offset = distort * _Refraction * _GrabTexture_TexelSize.xy;
         IN.screenPos.xy = offset * IN.screenPos.z + IN.screenPos.xy;
         float4 refrColor = tex2Dproj(_GrabTexture, IN.screenPos);
         o.Alpha = refrColor.a;
         o.Emission = refrColor.rgb * (1+_ObjectVisibility);
     }
     ENDCG
     }
 }


The previously linked post talks about inserting the following snippet somewhere:

 #if UNITY_UV_STARTS_AT_TOP
 if (_MainTex_TexelSize.y < 0)
         uv.y = 1-uv.y;
 #endif

I probably have to change _MainTex_TexelSize to _GrabTexture_TexelSize, but that's pretty muc all I know. Where do I need to insert this snippet to get it working?

Thanks for your help in advance!

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
1

Answer by lalaphoon · Aug 02, 2018 at 07:30 PM

alt text

If you are using iOS, then that method won't work (I guess) So here is a solution documented on the link that using ComputeGrabScreenPos function.

In the vertex shader vert, you must get a pos_refraction by defining it in Input structure and doing this:

 o.vertex = UnityObjectToClipPos(v.vertex);
 o.pos_refraction = ComputeGrabScreenPos(o.vertex);

Then in the surf function, you can grab your texture by doing this:

 fixed3 refractColor = tex2Dproj(_GrabTexture, IN.pos_refraction);

The reason why it's better to have an extra variable is that if you have a depth for your shader, its position info shouldn't be gotten from ComputeGrabScreenPos, instead, it should be from ComputeScreenPos. It's tricky if you don't know this, otherwise, it's like when you got your refraction right, you depth wouldn't work. If you keep the depth, your refraction is still upside down. Overall, It costed couple hours to actually get what I expect. Hopefully, this is also working for you.


screen-shot-2018-08-02-at-31854-pm.png (26.4 kB)
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

60 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

Related Questions

Switching shader Lighting On/Off from script 1 Answer

Lighting breaks when zooming in 1 Answer

how to make a single object lit differently by 2 lights source of same kind 0 Answers

How to use world-space normals in surface shader 0 Answers

How to edit the new particle surface shader? / Make it so particles receive light from all angles 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