您的位置:首页 > 移动开发 > Unity3D

【Unity5.x Shaders】最基本的Surface Shader-Diffuse shader以及Surface中的三种输出结构

2016-10-17 22:33 288 查看
某些物体可能具有均匀的颜色和光滑的表面,但光滑程度不足以照射反射光。

这些哑光材料最好用Diffuse Shader。 在现实世界中,不存在纯diffuse materials

Diffuse shader经常出现在游戏中,下面就是一个简单的Diffuse shader代码

Shader "Custom/DiffuseShader" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0

struct Input {
float2 uv_MainTex;
};

fixed4 _Color;

void surf (Input IN, inout SurfaceOutputStandard o)
{
o.Albedo = _Color.rgb;
}
ENDCG
}
FallBack "Diffuse"
}




这是一个从Standard Shader改装的的Shader,它将使用基于物理的渲染去模仿灯光在模型上的行为。

如果你想实现一个非真实感(non-photorealistic)的外观,可以改变第一个预处理指令使用Lambert光照模型。

#pragma surface surf Lambert


如果使用了这个模型,surf函数中的输出结构SurfaceOutputStandard需要改成SurfaceOutput

Surface输出结构

Shader允许你通过Surface的输出结构将材质的渲染属性传递给光照模型。

不同的Surface光照模型需要不同的Surface输出结构

在Unity5中,有三种主要的输出结构:

SurfaceOutput

一般的光照模型例如Lambert光照模型

struct SurfaceOutput {
fixed3 Albedo;   //对光源的反射率
fixed3 Normal;   //对应切线空间法线方向
fixed3 Emission; //自发光
half Specular;   //高光反射中的指数部分的系数。
fixed Gloss;     //高光反射中的强度系数
half Alpha;      //透明通道


SurfaceOutputStandard

标准光照模型 使用此结构输出

struct SurfaceOutputStandard {
fixed3 Albedo;  //材质的基础颜色(无论是漫反射还是高光)
fixed3 Normal;
half3 Emission; //类型被声明为half3,注意在SurfaceOutput中被定义为fixed3
half Alpha;
half Occlusion; //遮挡(默认为1)
half Smoothness;//光滑程度(0=粗糙, 1=光滑)
half Metallic;  //0=非金属, 1=金属


SurfaceOutputStandardSpecular

标准镜面光照模型 使用此结构输出

struct SurfaceOutputStandardSpecular
{
fixed3 Albedo;
fixed3 Specular;    // 镜面反射颜色
fixed3 Normal;
half3 Emission;
half Smoothness;
half Occlusion;
fixed Alpha;
};


注意:这里的Specular完全不同于SurfaceOutput中的Specular

它允许指定一个颜色而不是一个单值。

正确使用Surface Shader,是使Surface输出结构输出正确的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity Shader