unity-game-engine - 如何在unity3d中制作具有自定义属性的轮廓着色器

标签 unity-game-engine shader outline

我正在制作一个像更衣室这样的统一项目。基本功能是改变游戏对象(例如鞋子)的结构。所以我想在玩家选择gameobj时实现轮廓效果,我发现将源着色器修改为轮廓着色器效果很好。但是源着色器有法线贴图和光照贴图属性,轮廓着色器只有:

Shader "Outlined/Silhouetted Diffuse" {
    Properties {
        _Color ("Main Color", Color) = (.0,.0,.0,0)
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _Outline ("Outline width", Range (0.0, 0.03)) = .01
        _MainTex ("Base (RGB)", 2D) = "white" { }
    }

因此,当着色器更改时,光照贴图和法线贴图属性会同时丢失。我不知道如何将这两个属性添加到轮廓着色器:

Shader "Outlined/Silhouetted Diffuse" {
    Properties {
        _Color ("Main Color", Color) = (.0,.0,.0,0)
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _Outline ("Outline width", Range (0.0, 0.03)) = .01
        _MainTex ("Base (RGB)", 2D) = "white" { }

      //**********add these two properties ***********
        _BumpMap ("Normalmap", 2D) = "bump" {} 
        _LightMap ("Lightmap (RGB)", 2D) = "black" {} 
}

最佳答案

轮廓着色器:Source

    Shader "Outlined/Diffuse" {
    Properties {
        _Color ("Main Color", Color) = (.5,.5,.5,1)
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _Outline ("Outline width", Range (.002, 0.03)) = .005
        _MainTex ("Base (RGB)", 2D) = "white" { }
    }

CGINCLUDE
#include "UnityCG.cginc"

struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
};

struct v2f {
    float4 pos : POSITION;
    float4 color : COLOR;
};

uniform float _Outline;
uniform float4 _OutlineColor;

v2f vert(appdata v) {
    // just make a copy of incoming vertex data but scaled according to normal direction
    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;
    return o;
}
ENDCG

    SubShader {
        //Tags {"Queue" = "Geometry+100" }
CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
fixed4 _Color;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG

        // note that a vertex shader is specified here but its using the one above
        Pass {
            Name "OUTLINE"
            Tags { "LightMode" = "Always" }
            Cull Front
            ZWrite On
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha
            //Offset 50,50

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            half4 frag(v2f i) :COLOR { return i.color; }
            ENDCG
        }
    }

    SubShader {
CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
fixed4 _Color;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG

        Pass {
            Name "OUTLINE"
            Tags { "LightMode" = "Always" }
            Cull Front
            ZWrite On
            ColorMask RGB
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma exclude_renderers gles xbox360 ps3
            ENDCG
            SetTexture [_MainTex] { combine primary }
        }
    }

    Fallback "Diffuse"
}

关于unity-game-engine - 如何在unity3d中制作具有自定义属性的轮廓着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19487162/

相关文章:

jquery - IE 中的 div 不显示 CSS 大纲

unity-game-engine - 如何制作 GUI.Toggle 字体大小和颜色更大并为黑色,如屏幕截图中的示例?

shader - DirectX 11 什么是着色器资源 View

c# - 由于某种原因我的手榴弹没有初始化

unity-game-engine - 将 GLSL Shadertoy 着色器移植到 Unity 时出现问题

c - Opengl 统一变量不起作用?

css - 有没有办法(或插件)让 Vim 为 CSS 生成代码大纲?

outline - 给定非凸多边形中的大量顶点,我如何找到边?

创建一个生成 Unity3D 应用程序的程序

android - build.gradle 和 Manifest 文件丢失了吗?