【Reading Notes】CP2-Surface Shaders and Texture Mapping

Introduce

在这一章节,我们将探索Surface Sshader.将会从非常简单的材质开始,最后完成一个全息投影?和地形纹理融合(Blending)。还可以使用纹理动画,混合,以及其他任何我们想要的属性。本章知识点:
+ Diffuse shading 漫反射
+ Using packed arrays 使用数组
+ Adding texture to a shader 为shader添加纹理
+ Scrolling textures by modify uv values 纹理动画
+ Normal mapping 法相贴图
+ Creating a transparent material 创建透明材质
+ Creating a Holographic shader 创建全息的shader?
+ Packing and blending 纹理包装和混合
+ Creating a circle around you terrain

总的来说,在Surface Shader中有两步至关重要。第一,你必须指定材质的Physical(这个怎么翻译才好?)属性,例如:diffuse color(漫反射颜色)、smoothness(光滑系数)、transparency(通明度)。这些属性在unity的Surface function中被初始化,并被保存到surface output结构体中。第二步,这个surface out 将传递到lighting model中进行光照计算。这个特殊的函数将取得场景中的光照信息,结合surface output这些参数计算最后的片元的颜色。光照函数决定了当光照射到物体上的表现。下面是个流程图:
img

## Diffuse shading
在开始使用纹理映射之前,明白漫反射材质如何工作是很重要的。统一的颜色,光滑的表面但不足以反射光线(形成高光)。这样的材质最能表示Diffuse Shader了。在真实世界中纯漫反射的材质是不存在的;漫反射Shader在游戏中有着非常高的性能但同时伴随这比较差的美术表现。

一个漫反射Shader:

Shader "cookbook/PureDiffuse" {
    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 Lambert fullforwardshadows

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

        //sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 c = _Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

### Unity5 中的surfaceOutput sturct

SurfaceOutput
{
    fixed3 Albedo; //diffuse color 
    fixed3 Normal; //法线
    fixed3 Emiision; //自发光颜色
    fixed Alpha; //透明度
    half Specular; //高光强度
    fixed Gloss; //光泽度
}

SurfaceOuutputStandard
{
    fixed3 Albedo; //基本颜色(diffuse or specular)
    fixed3 Normal; //法线
    half3 Emission; //自发光颜色
    fixed Alpha; //透明度
    half Occlusion; //吸收??默认值是1
    half Smoothness; //光滑度,(0 = rough, 1 = smooth)
    half Metallic; //金属质感(0 = non-metal, 1 = metal)
}

SurfaceOutputStandardSpecular
{
    fixed3 Albedo; //基本颜色(diffuse color)
    fixed3 Normal; //法线
    half3 Emission;
    fixed Alpha;
    half Occlusion;
    half Smoothness;
    fixed3 Specular;// 高光颜色,和SurfaceOutput有着很大的不同,可以指定高光的颜色
}

是否可以正确的使用Surface Shader是一个关于能不能正确的初始化Surface output结构的问题。

Using Packed arrays

不是很负责的说,shader中的代码在屏幕上的每一个像素都至少执行一次。这是为什么GPU对于并行运算做了很大的优化。在CG的标准函数变量的设计也遵循这样的哲学(高并行)。明白这一点对于如何写出高性能的sh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值