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

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

shader "custom/gaosimohu"

{

properties

{

_maintex ("texture", 2d) = "white" {}

_blursize("blur size",float)=1.0

}

subshader

{

ztest always

cull off

zwrite off

cginclude//这个可以使其他pass块都可以使用,而不用在两个pass里都写,减少了写的次数

sampler2d _maintex;

half4 _maintex_texelsize;

float _blursize;

struct v2f{

float4 pos:sv_position;

half2 uv[5]:texcoord0;

};

fixed4 fragblur(v2f i):sv_target{

float weight[3]={

0.4026,

0.2442,

0.0545

};

fixed3 sum = tex2d(_maintex,i.uv[0]).rgb * weight[0];

for(int it = 1; it < 3; it++){

sum += tex2d(_maintex,i.uv[it]).rgb * weight[it];

sum += tex2d(_maintex,i.uv[2 * it]).rgb * weight[it];

}

return fixed4(sum,1.0);

}

endcg

pass{

name "gaussian_blur_vertical"//这个pass的唯一名字,可以在其他地方调用,usepass + 名字

cgprogram

#pragma vertex vertlurvertical

#pragma fragment fragblur

#include "unitycg.cginc"

v2f vertlurvertical(appdata_img v){

v2f o;

o.pos = unityobjecttoclippos(v.vertex);

half2 uv = v.texcoord;

o.uv[0]=uv;

o.uv[1]=uv + float2(0.0,_maintex_texelsize.y * 1.0) * _blursize;

o.uv[2]=uv - float2(0.0,_maintex_texelsize.y * 1.0) * _blursize;

o.uv[3]=uv + float2(0.0,_maintex_texelsize.y * 2.0) * _blursize;

o.uv[4]=uv - float2(0.0,_maintex_texelsize.y * 2.0) * _blursize;

return o;

}

endcg

}

pass{

name "gaussian_blur_horizontal"

cgprogram

#pragma vertex vertlurhorizontal

#pragma fragment fragblur

#include "unitycg.cginc"

v2f vertlurhorizontal(appdata_img v){

v2f o;

o.pos = unityobjecttoclippos(v.vertex);

half2 uv = v.texcoord;

o.uv[0]=uv;

o.uv[1]=uv + float2(0.0,_maintex_texelsize.x * 1.0) * _blursize;

o.uv[2]=uv - float2(0.0,_maintex_texelsize.x * 1.0) * _blursize;

o.uv[3]=uv + float2(0.0,_maintex_texelsize.x * 2.0) * _blursize;

o.uv[4]=uv - float2(0.0,_maintex_texelsize.x * 2.0) * _blursize;

return o;

}

endcg

}

}

}

调节_blursize即可看到效果

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

原文链接:https://blog.csdn.net/yiwei151/article/details/78720896

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

展开阅读全文

4 评论

留下您的评论.