边缘光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

Shader "CM/RimLight" {

    Properties

    {

        _Color ("Main Color", Color) = (1,1,1,1)

        _MainTex ("Base (RGB)", 2D) = "white" {}

        _IlluminPower("Illumin Power", Range(0, 2)) = 1

        _RimColor ("Rim Color", Color) = (1, 1, 1, 1)

        _RimPower ("Rim Power", Float) = 0.7

    }

    SubShader

    {

        Pass

        {

            Lighting Off

            CGPROGRAM

             

            #pragma vertex vert

            #pragma fragment frag

            #include "UnityCG.cginc"

 

            uniform sampler2D _MainTex;

            uniform fixed4 _Color;

            uniform float4 _MainTex_ST;

            float _IlluminPower;

            uniform fixed4 _RimColor;

            float _RimPower;

             

            struct appdata {

                float4 vertex : POSITION;

                float3 normal : NORMAL;

                float2 texcoord : TEXCOORD0;

            };

 

            struct v2f {

                float4 pos : SV_POSITION;

                float2 uv : TEXCOORD0;

                fixed3 color : COLOR;

            };

 

            v2f vert (appdata_base v)

            {

                v2f o;

                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

 

                float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));

                float dotProduct = 1 - dot(v.normal, viewDir);

                

                o.color = _RimColor * pow(dotProduct, _RimPower);//smoothstep(1 - _RimPower, 1.0, dotProduct);

 

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;

            }

 

 

            fixed4 frag(v2f i) : COLOR

            {

                fixed4 texcol = tex2D(_MainTex, i.uv);

                texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + i.color;

                return texcol;

            }

             

            ENDCG

        }

    }

}

 

  

冰冻效果

 

二、使用cap texture:

 

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

Shader "CM/Rimlight2" {

    Properties

    {

        _Color ("Main Color", Color) = (1,1,1,1)

        _MainTex ("Base (RGB)", 2D) = "white" {}

        _IlluminPower("Illumin Power", Range(0, 2)) = 1

 

        _CapTex("Cap Tex (RGB)", 2D) = "white" {}

        _CapColor("Cap Color", Color) = (1,1,1,1)

        _CapIntensity("Cap Intensity", Range(0,3)) = 0

    }

    SubShader

    {

        Pass

        {

            Lighting Off

            CGPROGRAM

             

            #pragma vertex vert

            #pragma fragment frag

            #include "UnityCG.cginc"

 

            uniform sampler2D _MainTex;

            uniform fixed4 _Color;

            uniform float4 _MainTex_ST;

            float _IlluminPower;

 

             

            float _CapIntensity;

            uniform sampler2D _CapTex;

            fixed4 _CapColor;

                 

             

            struct appdata {

                float4 vertex : POSITION;

                float3 normal : NORMAL;

                float2 texcoord : TEXCOORD0;

            };

 

            struct v2f {

                float4 pos : SV_POSITION;

                float2 uv : TEXCOORD0;

                fixed2 cap : COLOR;

            };

 

            v2f vert (appdata_base v)

            {

                v2f o;

                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

 

                half2 capCoord;

                capCoord.x = dot(UNITY_MATRIX_IT_MV[0].xyz,v.normal);

                capCoord.y = dot(UNITY_MATRIX_IT_MV[1].xyz,v.normal);

                o.cap = capCoord * 0.5 + 0.5;

 

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;

            }

 

 

            fixed4 frag(v2f i) : COLOR

            {

                fixed4 texcol = tex2D(_MainTex, i.uv);

                fixed4 cap = tex2D(_CapTex, i.cap);

                 

                texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + cap.rgb * _CapIntensity * _CapColor;

                return texcol;

            }

             

            ENDCG

        }

    }

}

 

 

 三、在光照射方向上存在边缘光

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

Shader "CM/Rimlight3" {

    Properties

    {

        _Color ("Main Color", Color) = (1,1,1,1)

        _MainTex ("Base (RGB)", 2D) = "white" {}

        _IlluminPower("Illumin Power", Range(0, 2)) = 1

        _RimColor ("Rim Color", Color) = (0, 0.8, 0.8, 1)

        _RimPower ("Rim Power", Float) = 0.7

    }

    SubShader

    {

        Pass

        {

            Lighting Off

            CGPROGRAM

             

            #pragma vertex vert

            #pragma fragment frag

            #include "UnityCG.cginc"

 

            uniform sampler2D _MainTex;

            uniform fixed4 _Color;

            uniform float4 _MainTex_ST;

            float _IlluminPower;

            uniform fixed4 _RimColor;

            float _RimPower;

             

            struct appdata {

                float4 vertex : POSITION;

                float3 normal : NORMAL;

                float2 texcoord : TEXCOORD0;

            };

 

            struct v2f {

                float4 pos : SV_POSITION;

                float2 uv : TEXCOORD0;

                fixed3 color : COLOR;

            };

 

            v2f vert (appdata_base v)

            {

                v2f o;

                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

 

                float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));

                float dotRim = 1 - dot(v.normal, viewDir);

                float3 lightDir = normalize(ObjSpaceLightDir(v.vertex));

                float dotDiff = dot(v.normal, lightDir);

                 

                o.color = _RimColor * pow(dotRim, _RimPower) * dotDiff;

 

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;

            }

 

 

            fixed4 frag(v2f i) : COLOR

            {

                fixed4 texcol = tex2D(_MainTex, i.uv);

                texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + i.color;

                return texcol;

            }

             

            ENDCG

        }

    }

}

  

 四、在光照射方向上产生边缘光  ----------------- 使用matcap 贴图  (shader 依然用上面的rimlight2), 只需要替换matcap贴图 即可:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值