Unity shader实现自由放大缩小效果

本文实例为大家分享了unity shader实现自由放大缩小效果的具体代码,供大家参考,具体内容如下

代码:

以下实现的shader代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

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中。

下面是挂在摄像机上的脚本:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/clzmin/article/details/73696551

本文链接:https://my.lmcjl.com/post/8880.html

展开阅读全文

4 评论

留下您的评论.