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 /
  • Help Room /
avatar image
0
Question by Instr3x · Mar 12, 2018 at 02:24 PM · textureshadersdistortion

How can I change magnification based on alpha value of texture in shader?

I found this shader on Reddit. The problem is I wanted to make a shader that will use texture to get magnification value, but I have no clue how to solve that. I tried to modify it to magnify based on distance from uv_diff and uv_center and it actually worked, but this is not the effect I wanted to achieve. I want to achieve effect on gif, which I made in another game engine, Construct 2.Thank you in advance.


alt text


 Shader "Custom/Magnification"
 {
     Properties
     {
         _Magnification("Magnification", Float) = 1
     }
 
     SubShader
     {
         Tags{ "Queue" = "Transparent" "PreviewType" = "Plane" }
         LOD 100
 
         GrabPass{ "_GrabTexture" }
 
         Pass
             {
                 ZTest On
                 ZWrite Off
                 Blend One Zero
                 Lighting Off
                 Fog{ Mode Off }
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
             };
 
             struct v2f
             {
                 //our UV coordinate on the GrabTexture
                 float4 uv : TEXCOORD0;
                 //our vertex position after projection
                 float4 vertex : SV_POSITION;
             };
 
             sampler2D _GrabTexture;
             half _Magnification;
 
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 //the UV coordinate of our object's center on the GrabTexture
                 float4 uv_center = ComputeGrabScreenPos(UnityObjectToClipPos(float4(0, 0, 0, 1)));
                 //the vector from uv_center to our UV coordinate on the GrabTexture
                 float4 uv_diff = ComputeGrabScreenPos(o.vertex) - uv_center;
                 //apply magnification
                 uv_diff /= _Magnification;
                 //save result
                 o.uv = uv_center + uv_diff;
                 return o;
             }
 
             fixed4 frag(v2f i) : COLOR
             {
                 return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv));
             }
             ENDCG
         }
     }
 }

shader2.gif (434.9 kB)
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 Nikitty · Jan 24, 2019 at 11:28 PM

I do not programming shaders, I solved this issue.

I hope helped someone.

Just try:

 Shader "Custom/Magnification"
  {
      Properties
      {
          _Magnification("Magnification", Float) = 1
         [HideInInspector] _MainTex ("Masking Texture", 2D) = "white" {}
         _AdditiveColor ("Additive Tint color", Color) = (0, 0, 0, 0)
         _MultiplyColor ("Multiply Tint color", Color) = (1, 1, 1, 1)
      }
  
      SubShader
      {
          Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
          LOD 100
  
          GrabPass{ "_GrabTexture" }
  
          Pass
              {
 
             
             Cull Off
             Lighting Off
             ZWrite Off
             ZTest [unity_GUIZTestMode]
             Blend SrcAlpha OneMinusSrcAlpha
  
              CGPROGRAM
              #pragma vertex vert
              #pragma fragment frag
  
              #include "UnityCG.cginc"
  
              struct appdata
              {
                  float4 vertex : POSITION;
                  float2 texcoord : TEXCOORD0;
              };
  
              struct v2f
              {
                  //our UV coordinate on the GrabTexture
                  float4 uv : TEXCOORD0;
                  float2 uvmain : TEXCOORD1;
                  //our vertex position after projection
                  float4 vertex : POSITION;
              };
  
              sampler2D _GrabTexture;
              half _Magnification;
              float4 _GrabTexture_TexelSize;
              float _Size;
              float4 _AdditiveColor;
              float4 _MultiplyColor;
 
              sampler2D _MainTex;
              float4 _MainTex_ST;
  
              v2f vert(appdata v)
              {
                  v2f o;
                  o.vertex = UnityObjectToClipPos(v.vertex);
                  //the UV coordinate of our object's center on the GrabTexture
                  float4 uv_center = ComputeGrabScreenPos(UnityObjectToClipPos(float4(0, 0, 0, 1)));
                  //the vector from uv_center to our UV coordinate on the GrabTexture
                  float4 uv_diff = ComputeGrabScreenPos(o.vertex) - uv_center;
                  //apply magnification
                  uv_diff /= _Magnification;
                  //save result
                  o.uv = uv_center + uv_diff;
                  o.uvmain = TRANSFORM_TEX(v.texcoord, _MainTex);
                  return o;
              }
  
              fixed4 frag(v2f i) : COLOR
              {
                     half4 sum = half4(0,0,0,0);
 
                     #define GRABPIXEL(weight,kernelx) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uv.x + _GrabTexture_TexelSize.x * kernelx * _Size, i.uv.y, i.uv.z, i.uv.w))) * weight
 
                     sum += GRABPIXEL(0.05, -4.0);
                     sum += GRABPIXEL(0.09, -3.0);
                     sum += GRABPIXEL(0.12, -2.0);
                     sum += GRABPIXEL(0.15, -1.0);
                     sum += GRABPIXEL(0.18,  0.0);
                     sum += GRABPIXEL(0.15, +1.0);
                     sum += GRABPIXEL(0.12, +2.0);
                     sum += GRABPIXEL(0.09, +3.0);
                     sum += GRABPIXEL(0.05, +4.0);
                     
                     half4 result = half4(sum.r * _MultiplyColor.r + _AdditiveColor.r, 
                         sum.g * _MultiplyColor.g + _AdditiveColor.g, 
                         sum.b * _MultiplyColor.b + _AdditiveColor.b, 
                         tex2D(_MainTex, i.uvmain).a);
                     
                     return result;
               }
              
              
              
              ENDCG
          }
      }
  }

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

159 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

Related Questions

Problem with a transparent texture in an object that already has a material 0 Answers

Implement a texture to a object like a decal and try to Rotate/Move/Scale 0 Answers

Can you write to a custom buffer inside a shader for usage in another later on 0 Answers

Alpha transparency has outline 0 Answers

Shader/Lighting Problem! Terrain texture looks awful. Please Help 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