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 ChiuanWei · Jan 29, 2012 at 10:58 AM · shaderdistortion

Why this Distortion Shader Different in Unity3.4.

alt text

The Shader: // Upgrade NOTE: replaced 'glstate.matrix.modelview[0]' with 'UNITY_MATRIX_MV' // Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'

 Shader "HeatDistortion" {
 Properties {
     _NoiseTex ("Noise Texture (RG)", 2D) = "white" {}
     strength("strength", Range(0.1, 0.3)) = 0.2
     transparency("transparency", Range(0.01, 0.1)) = 0.05
 }
 
 Category {
     Tags { "Queue" = "Transparent+10" }
     SubShader {
         GrabPass {
             Name "BASE"
             Tags { "LightMode" = "Always" }
         }
        
         Pass {
             Name "BASE"
             Tags { "LightMode" = "Always" }
             Lighting Off
             Cull Off
             ZWrite On
             ZTest LEqual
             Blend SrcAlpha OneMinusSrcAlpha
             AlphaTest Greater 0
          
          
 CGPROGRAM
 // Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members distortion)
 #pragma exclude_renderers xbox360
 #pragma vertex vert
 #pragma fragment frag
 #pragma fragmentoption ARB_precision_hint_fastest
 #pragma fragmentoption ARB_fog_exp2
 #include "UnityCG.cginc"
 
 sampler2D _GrabTexture : register(s0);
 float4 _NoiseTex_ST;
 sampler2D _NoiseTex;
 float strength;
 float transparency;
 
 struct data {
     float4 vertex : POSITION;
     float3 normal : NORMAL;
     float4 texcoord : TEXCOORD0;
 };
 
 struct v2f {
     float4 position : POSITION;
     float4 screenPos : TEXCOORD0;
     float2 uvmain : TEXCOORD2;
     float distortion;
 };
 
 v2f vert(data i){
     v2f o;
     o.position = mul(UNITY_MATRIX_MVP, i.vertex);      // compute transformed vertex position
     o.uvmain = TRANSFORM_TEX(i.texcoord, _NoiseTex);   // compute the texcoords of the noise
     //float viewAngle = dot(normalize(mul((float3x3)glstate.matrix.invtrans.modelview[0], i.normal)),
     //                     float3(0,0,1));
     float viewAngle = dot(normalize(ObjSpaceViewDir(i.vertex)),
                          i.normal);
     o.distortion = viewAngle * viewAngle;    // square viewAngle to make the effect fall off stronger
     float depth = -mul( UNITY_MATRIX_MV, i.vertex ).z;    // compute vertex depth
     o.distortion /= 1+depth;        // scale effect with vertex depth
     o.distortion *= strength;    // multiply with user controlled strength
     o.screenPos = o.position;   // pass the position to the pixel shader
     return o;
 }
 
 half4 frag( v2f i ) : COLOR
 {   
     // compute the texture coordinates
     float2 screenPos = i.screenPos.xy / i.screenPos.w;   // screenpos ranges from -1 to 1
     screenPos.x = (screenPos.x + 1) * 0.5;   // I need 0 to 1
     screenPos.y = (screenPos.y + 1) * 0.5;   // I need 0 to 1
 
     // check if anti aliasing is used
     if(_ProjectionParams.x < 0)
         screenPos.y = 1 - screenPos.y;
    
     // get two offset values by looking up the noise texture shifted in different directions
     half4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz);
     half4 offsetColor2 = tex2D(_NoiseTex, i.uvmain - _Time.yx);
    
     // use the r values from the noise texture lookups and combine them for x offset
     // use the g values from the noise texture lookups and combine them for y offset
     // use minus one to shift the texture back to the center
     // scale with distortion amount
     screenPos.x += ((offsetColor1.r + offsetColor2.r) - 1) * i.distortion;
     screenPos.y += ((offsetColor1.g + offsetColor2.g) - 1) * i.distortion;
     
     half4 col = tex2D( _GrabTexture, screenPos );
     col.a = i.distortion/transparency;
     return col;
 }
  
 ENDCG
         }
     }
 }
 
 }

I USE plane in front of Camera. But Everything Looks Revers!!!! this Origin Shader Is Not AutoUpdate in Unity2.6. so it works...How to fix it in unity3d 3.4..

125*Origin Shader:*125

 Shader "HeatDistortion" {
 
 Properties {
 
     _NoiseTex ("Noise Texture (RG)", 2D) = "white" {}
 
     strength("strength", Range(0.1, 0.3)) = 0.2
 
     transparency("transparency", Range(0.01, 0.1)) = 0.05
 
 }
 
  
 
 Category {
 
     Tags { "Queue" = "Transparent+10" }
 
     SubShader {
 
         GrabPass {
 
             Name "BASE"
 
             Tags { "LightMode" = "Always" }
 
         }
 
        
 
         Pass {
 
             Name "BASE"
 
             Tags { "LightMode" = "Always" }
 
             Lighting Off
 
             Cull Off
 
             ZWrite On
 
             ZTest LEqual
 
             Blend SrcAlpha OneMinusSrcAlpha
 
             AlphaTest Greater 0
 
          
 
          
 
 CGPROGRAM
 
 #pragma vertex vert
 
 #pragma fragment frag
 
 #pragma fragmentoption ARB_precision_hint_fastest
 
 #pragma fragmentoption ARB_fog_exp2
 
 #include "UnityCG.cginc"
 
  
 
 sampler2D _GrabTexture : register(s0);
 
 float4 _NoiseTex_ST;
 
 sampler2D _NoiseTex;
 
 float strength;
 
 float transparency;
 
  
 
 struct data {
 
     float4 vertex : POSITION;
 
     float3 normal : NORMAL;
 
     float4 texcoord : TEXCOORD0;
 
 };
 
  
 
 struct v2f {
 
     float4 position : POSITION;
 
     float4 screenPos : TEXCOORD0;
 
     float2 uvmain : TEXCOORD2;
 
     float distortion;
 
 };
 
  
 
 v2f vert(data i){
 
     v2f o;
 
     o.position = mul(glstate.matrix.mvp, i.vertex);      // compute transformed vertex position
 
     o.uvmain = TRANSFORM_TEX(i.texcoord, _NoiseTex);   // compute the texcoords of the noise
 
     float viewAngle = dot(normalize(ObjSpaceViewDir(i.vertex)),
 
                          i.normal);
 
     o.distortion = viewAngle * viewAngle;   // square viewAngle to make the effect fall off stronger
 
     float depth = -mul( glstate.matrix.modelview[0], i.vertex ).z;  // compute vertex depth
 
     o.distortion /= 1+depth;        // scale effect with vertex depth
 
     o.distortion *= strength;   // multiply with user controlled strength
 
     o.screenPos = o.position;   // pass the position to the pixel shader
 
     return o;
 
 }
 
  
 
 half4 frag( v2f i ) : COLOR
 
 {   
 
     // compute the texture coordinates
 
     float2 screenPos = i.screenPos.xy / i.screenPos.w;   // screenpos ranges from -1 to 1
 
     screenPos.x = (screenPos.x + 1) * 0.5;   // I need 0 to 1
 
     screenPos.y = (screenPos.y + 1) * 0.5;   // I need 0 to 1
 
  
 
     // check if anti aliasing is used
 
     if (_ProjectionParams.x < 0)
 
         screenPos.y = 1 - screenPos.y;
 
    
 
     // get two offset values by looking up the noise texture shifted in different directions
 
     half4 offsetColor1 = tex2D(_NoiseTex, i.uvmain + _Time.xz);
 
     half4 offsetColor2 = tex2D(_NoiseTex, i.uvmain - _Time.yx);
 
    
 
     // use the r values from the noise texture lookups and combine them for x offset
 
     // use the g values from the noise texture lookups and combine them for y offset
 
     // use minus one to shift the texture back to the center
 
     // scale with distortion amount
 
     screenPos.x += ((offsetColor1.r + offsetColor2.r) - 1) * i.distortion;
 
     screenPos.y += ((offsetColor1.g + offsetColor2.g) - 1) * i.distortion;
 
    
 
     half4 col = tex2D( _GrabTexture, screenPos );
 
     col.a = i.distortion/transparency;
 
     return col;
 
 }
 
  
 
 ENDCG
 
         }
 
     }
 
 }
 
  
 
 }



Please...Help me...i am not good at Shader...

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 Owen-Reynolds · Jan 29, 2012 at 04:40 PM 0
Share

It draws to a plane -- not a renderTexture? Have you tried rotating the plane around?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by c_v · Jun 18, 2012 at 04:39 PM

Late answer I suppose but... You should check out this article: http://unity3d.com/support/documentation/Components/SL-PlatformDifferences.html

And add this lines to you shader,

 #if SHADER_API_D3D9
   screenPos.y = 1 - screenPos.y;
 #endif

right after

 // check if anti aliasing is used
     if (_ProjectionParams.x < 0)
         screenPos.y = 1 - screenPos.y;
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Glass Refraction Shader 2 Answers

Shader: distort light direction besed on mesh 1 Answer

[Unity 2019] How get distortion effect based on normal map? 0 Answers

How I do a Pixel Distort Shader 0 Answers

Post processing vertex distortion effect 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