在Shader中,我们除了可以设定各种光线处理外,还可以增加纹理贴图。

使用 settexture 命令可以为着色器指定纹理。

示例代码:

Shader "Sbin/ff2" {
// 贴图采样
properties {
// 变量名("描述名",类型)=值
_Color("主体", color)=(,,,)
_Ambient("环境光", color)=(0.3,0.3,0.3,0.3)
_Specular("高光", color)=(,,,) // 变量名("描述名",range(区域最小值,区域最大值)=默认值
_Shininess("高光强度",range(,))=
_Emission("自发光", color)=(,,,) _Constant("透明通道", color)=(,,,0.3) _MainTex("纹理", 2d)=""
_SecondTex("第二张纹理",2d)="" } SubShader {
Tags { "Queue" = "Transparent" } pass { Blend SrcAlpha OneMinusSrcAlpha material {
diffuse[_Color]
ambient[_Ambient]
specular[_Specular]
shininess[_Shininess]
emission[_Emission]
}
lighting on // 启用光照
separatespecular on // 镜面高光 // 纹理属性
settexture[_MainTex] {
// 合并 当前纹理 * 前面所有材质和关照的颜色
// primary 代表顶点光照后的颜色
// double 颜色*2
// quad 颜色*4
combine texture * primary double
} // 第二张纹理
settexture[_SecondTex] {
// 用当前采用到的纹理与之前所有采样到的结果进行混合 //combine texture * previous double // , 号后面的参数,它只是取了纹理alpha通道, 前面所有的颜色alpha值失效
constantcolor[_Constant]
combine texture * previous double, texture * constant
}
}
}
// FallBack "Diffuse"
}

效果图:

默认渲染顺序图:

指令说明

settexture 应用纹理

combine 纹理混合时使用的计算方式  

Shader "Examples/2 Alpha Blended Textures" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}
}
SubShader {
Pass {
// Apply base texture
SetTexture [_MainTex] {
combine texture
}
// Blend in the alpha texture using the lerp operator
SetTexture [_BlendTex] {
combine texture lerp (texture) previous
}
}
}
}

constantColor 透明通道

Blend 进行阿尔法最后的混合,制作透明的游戏对象  

Tags 控制渲染顺序

【官方文档中的一些说明】

Blend operations (混合操作)

可以使用的混合方式:

   
Add Add source and destination together.  源和目标叠加在一起
Sub Subtract destination from source.  从源上减去
RevSub Subtract source from destination. 
Min Use the smaller of source and destination.
Max Use the larger of source and destination.
LogicalClear Logical operation: Clear (0) DX11.1 only.
LogicalSet Logical operation: Set (1) DX11.1 only.
LogicalCopy Logical operation: Copy (s) DX11.1 only.
LogicalCopyInverted Logical operation: Copy inverted (!s) DX11.1 only.
LogicalNoop Logical operation: Noop (d) DX11.1 only.
LogicalInvert Logical operation: Invert (!d) DX11.1 only.
LogicalAnd Logical operation: And (s & d) DX11.1 only.
LogicalNand Logical operation: Nand !(s & d) DX11.1 only.
LogicalOr Logical operation: Or (s | d) DX11.1 only.
LogicalNor Logical operation: Nor !(s | d) DX11.1 only.
LogicalXor Logical operation: Xor (s ^ d) DX11.1 only.
LogicalEquiv Logical operation: Equivalence !(s ^ d) DX11.1 only.
LogicalAndReverse Logical operation: Reverse And (s & !d) DX11.1 only.
LogicalAndInverted Logical operation: Inverted And (!s & d) DX11.1 only.
LogicalOrReverse Logical operation: Reverse Or (s | !d) DX11.1 only.
LogicalOrInverted Logical operation: Inverted Or (!s | d) DX11.1 only.

Blend factors (混合因子)

All following properties are valid for both SrcFactor & DstFactor in the Blend command.Source refers to the calculated color, Destination is the color already on the screen. The blend factors are ignored if BlendOp is using logical operations.

所有指令都是使用 SrcFactor & DstFactor 的方式进行混合。 源是指要计算的颜色, 目的地是指当前已经要显示在屏幕上的颜色。 如果忽略了逻辑运算符则使用 BlendOp 方式。

   
One The value of one - use this to let either the source or the destination color come through fully.
Zero The value zero - use this to remove either the source or the destination values.
SrcColor The value of this stage is multiplied by the source color value.
SrcAlpha The value of this stage is multiplied by the source alpha value.
DstColor The value of this stage is multiplied by frame buffer source color value.
DstAlpha The value of this stage is multiplied by frame buffer source alpha value.
OneMinusSrcColor The value of this stage is multiplied by (1 - source color).
OneMinusSrcAlpha The value of this stage is multiplied by (1 - source alpha).
OneMinusDstColor The value of this stage is multiplied by (1 - destination color).
OneMinusDstAlpha The value of this stage is multiplied by (1 - destination alpha).

Details (详情)

下面是最常用的混合类型:

Blend SrcAlpha OneMinusSrcAlpha // 透明通道混合
Blend One One // Additive 叠加
Blend OneMinusDstColor One // Soft Additive 柔性叠加
Blend DstColor Zero // Multiplicative 相乘
Blend DstColor SrcColor // 2x Multiplicative
2倍乘法

Example (示例)

 下面是一个小例子,可以在屏幕上添加一个纹理的纹理:
Shader "Simple Additive" {
Properties {
_MainTex ("Texture to blend", 2D) = "black" {}
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Blend One One
SetTexture [_MainTex] { combine texture }
}
}
}

Tags 语法

    Tags { "TagName1" = "Value1" "TagName2" = "Value2" }

Subshader 语法

Subshader { [Tags] [CommonState] Passdef [Passdef ...] }
 

[Unity] Shader(着色器)之纹理贴图的更多相关文章

  1. Unity Shader着色器优化

    https://mp.weixin.qq.com/s?__biz=MzU5MjQ1NTEwOA==&mid=2247493518&idx=1&sn=c51b92e9300bcf ...

  2. [Unity] Shader(着色器)输入输出和语义

    在Unity5.x后, 已经支持了基于物理的光照模型,也就是常说的次时代引擎所必须具备的功能. 如果在Properties使用2D,CG里要用sampler2D,代表使用的是2维纹理 如果在Prope ...

  3. [Unity] Shader(着色器)之固定管线

    在Unity中,固定管线Shader的性能是最好的. 什么是固定管线呢? 固定渲染管线 —— 这是标准的几何&光照(T&L)管线,功能是固定的,它控制着世界.视.投影变换及固定光照控制 ...

  4. Unity3D学习笔记(三十四):Shader着色器(1)

    一.GPU:图形处理器,Graphics Processing Unit 显卡的处理器就是图形处理器.与CPU类似.   GPU和CPU的区别? 1.CPU主要是为了串行指令设计,GPU则是为了大规模 ...

  5. Unity 5着色器系统代码介绍(上)

    http://forum.china.unity3d.com/thread-25724-1-10.html Unity 5着色器系统代码介绍(上) Unity在着色器开发方面提供了很大的灵活性.有些工 ...

  6. Unity 几何着色器

    Unity 几何着色器 shaderGeometry Shader几何着色器 Unity 几何着色器 如果学习不能带来价值,那将毫无意义 简介     在顶点和片段着色器之间有一个可选的着色器,叫做几 ...

  7. Unity3D学习笔记(三十五):Shader着色器(2)- 顶点片元着色器

    Alpha测试 AlphaTest Great:大于 AlphaTest Less:小于 AlphaTest Equal:等于 AlphaTest GEqual:大于等于 AlphaTest LEqu ...

  8. Unity 5着色器系统代码介绍(下)

    http://forum.china.unity3d.com/thread-25738-1-10.html 上一篇对着色器系统的工作原理做了介绍,现在我们将继续深入,将目光聚焦在标准着色器的光照函数. ...

  9. Unity 光照着色器

    光照着色器需要考虑光照的分类,一般分为漫反射和镜面反射. 漫反射计算基本光照: float brightness=dot(normal,lightDir)    将法线和光的入射方向进行点积运算,求出 ...

随机推荐

  1. [已解决]从微软合作伙伴资源和MSDN下载系统和软件Microsoft download Manager无效

    有个itellyou,更新了所有MSDN的软件包.如果自己有微软的注册账户,还是从微软官网下载比较好.而且对自己账户里的系统和itellyou里的做了对比.发现SHA1码不相同,估计官方分配的序列号也 ...

  2. js 输出二维数组的最大值

    function num(arr){ max=a[0][0]; for (var i = 0; i < a.length; i++) { for (var j = 0; j< a[i].l ...

  3. sqlserver如何创建镜像图文教程(转)

    由于工作中需要做SQL的镜像异地备份,以前都没有研究过,百度了一个文章记录下,方便以后查询 转载地址:http://jingyan.baidu.com/article/d5c4b52b20843fda ...

  4. stm32 hid 键盘描述

    /* USB Standard Device Descriptor */ const uint8_t Joystick_DeviceDescriptor[JOYSTICK_SIZ_DEVICE_DES ...

  5. shell中export理解误区

    一直以来,以为shell脚本中经过export后的变量会影响到执行这个shell的终端中的环境变量.环境变量这个概念不是shell所独有的,而是linux里面进程所拥有的,shell解释器运行起来就是 ...

  6. HTML总结

    几个知识点: HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML框架结构: <!DOCTYPE html> <html> < ...

  7. NOIP模拟赛20161016R1

    1.传教士(bishop) 问题描述: panzhili王国的疆土恰好是一个矩形,为了管理方便,国王jjs将整个疆土划分成N*M块大小相同的区域.由于jjs希望他的子民也能信教爱教(”打拳”神教),所 ...

  8. Tech Websites

    1,3D Game Engine Programming 2, 3, 4, 5,

  9. 基于jquery的bootstrap在线文本编辑器插件Summernote

    Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...

  10. echo print() print_r() var_dump()的区别

    常用调试方法 echo()可以一次输出多个值,多个值之间用逗号分隔.echo是语言结构(language construct),而并不是真正的函数,因此不能作为表达式的一部分使用. print()函数 ...