zl程序教程

您现在的位置是:首页 >  其它

当前栏目

11. 鼠标移到物体上高亮显示轮廓

显示 11 鼠标 物体 高亮 轮廓
2023-09-11 14:15:07 时间

1. 新建一个Cube,新建一个Material并拖到Cube上

 

2. 新建一个Shader,重命名为“Outline”,修改Shader代码如下:

Shader "Outline" {
 Properties 
 { 
 _Color ("Main Color", Color) = (.5,.5,0.5,1)
  _OutlineColor ("Outline Color", Color) = (0,1,0,1)
   _Outline ("Outline width", Range (0.0, 0.03)) = .005 
   _MainTex ("Base (RGB)", 2D) = "white" { } 
   _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 
   } 
   SubShader { 
   Tags    { "Queue"="AlphaTest+1" "IgnoreProjector"="True" "RenderType"="TransparentCutout" } 
   LOD 500 
   // outline pass
    Pass { 
    Cull Off
     ZWrite Off 
     AlphaTest Greater [_Cutoff]
      CGINCLUDE
       #include "UnityCG.cginc"
        ENDCG 
        CGPROGRAM
         #pragma vertex vert
          #pragma fragment frag
           struct appdata { 
           float4 vertex : POSITION;
            float3 normal : NORMAL; 
            float2 texcoord : TEXCOORD;
             };
              struct v2f {
               float4 pos : POSITION;
                float4 color : COLOR;
                 float2 texcoord : TEXCOORD; 
                 };
                  uniform float _Outline; 
                  uniform float4 _OutlineColor; 
                  uniform sampler2D _MainTex;
                   v2f vert(appdata v) { 
                   v2f o; 
                   o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                    float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal); 
                    float2 offset = TransformViewToProjection(norm.xy);
                     o.pos.xy += offset * o.pos.z * _Outline;
                      o.color = _OutlineColor;
                       o.texcoord = v.texcoord;
                        return o;
                         }
                          half4 frag(v2f i) :COLOR { 
                          float4 c = i.color; 
                          c.w *= tex2D(_MainTex, i.texcoord).w;
                           return c; 
                           }
                            ENDCG
                             } 
                             // main pass 
                             Pass { 
                             ZWrite On 
                             AlphaTest Greater [_Cutoff]
                              Material { 
                              Diffuse [_Color]
                               Ambient [_Color] 
                               } 
                               Lighting On 
                               SetTexture [_MainTex] { 
                               ConstantColor [_Color] 
                               Combine texture * constant 
                               } 
                               SetTexture [_MainTex] { 
                               Combine previous * primary DOUBLE
                                }
                                 } 
                                 }
                                  Fallback "Transparent/Cutout/Diffuse" 
                                  }

 

3. 新建一个脚本test.cs,并拖到Cube上

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour
{
    private void OnMouseEnter()
    {
        foreach (Material m in renderer.materials)
        {
            m.shader = Shader.Find("Outline");
        }
    }

    private void OnMouseExit()
    {
        foreach (Material m in renderer.materials)
        {
            m.shader = Shader.Find("Diffuse");
        }
    }
}

 

4. 运行后当鼠标移到Cube上可以看到效果如图所示