首页 > 其他 > 详细

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

时间:2014-04-09 20:13:20      阅读:674      评论:0      收藏:0      [点我收藏+]

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

 

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

bubuko.com,布布扣
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" 
                                  }
bubuko.com,布布扣

 

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

bubuko.com,布布扣
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");
        }
    }
}
bubuko.com,布布扣

 

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

bubuko.com,布布扣

 

11. 鼠标移到物体上高亮显示轮廓,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/code1992/p/3653826.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!