Unity shader实现*放大缩小效果
程序员文章站
2023-11-17 14:39:28
本文实例为大家分享了unity shader实现*放大缩小效果的具体代码,供大家参考,具体内容如下
代码:
以下实现的shader代码:
shader...
本文实例为大家分享了unity shader实现*放大缩小效果的具体代码,供大家参考,具体内容如下
代码:
以下实现的shader代码:
shader "hidden/wave" { properties { _maintex ("texture", 2d) = "white" {} _wavewidth("wave width",float) = 0.5 _centerx("centerx",float)=0.5 _centery("centery",float)=0.5 } subshader { // no culling or depth cull off zwrite off ztest always pass { cgprogram #pragma vertex vert #pragma fragment frag #include "unitycg.cginc" struct appdata { float4 vertex : position; float2 uv : texcoord0; }; struct v2f { float2 uv : texcoord0; float4 vertex : sv_position; }; float _wavewidth; float _centerx; float _centery; v2f vert (appdata v) { v2f o; o.vertex = unityobjecttoclippos(v.vertex); o.uv = v.uv; return o; } sampler2d _maintex; fixed4 frag (v2f i) : sv_target { float2 center=float2(_centerx,_centery); float2 distance= center - i.uv; float x=center.x+ center.x*(-distance.x/center.x) *(1-_wavewidth); float y=center.y+ center.y*(-distance.y/center.y) *(1-_wavewidth); float2 uv = float2(x,y); return tex2d(_maintex, uv); } endcg } } }
主要的内容还是在frag中。
下面是挂在摄像机上的脚本:
using system.collections; using system.collections.generic; using unityengine; public class wavecreame : monobehaviour { public shader waveshader = null; [range(0.0f,1f)] public float wavewidth = 0.3f; private material m_wavematerial = null; private float m_centerx = 0.5f; private float m_ctentery = 0.5f; // use this for initialization void start () { m_wavematerial = new material(waveshader); } // update is called once per frame void update () { vector3 pos = input.mouseposition; m_centerx = pos.x / screen.width; m_ctentery = pos.y / screen.height; if (input.getmousebutton(0)) { wavewidth += time.deltatime * 0.5f; } if (input.getmousebutton(1)) { wavewidth -= time.deltatime * 0.5f; } } private void onrenderimage(rendertexture source, rendertexture destination) { if (waveshader == null || m_wavematerial == null) return; m_wavematerial.setfloat("_wavewidth", wavewidth); m_wavematerial.setfloat("_centerx", m_centerx); m_wavematerial.setfloat("_centery", m_ctentery); graphics.blit(source, destination, m_wavematerial); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。