2、shader基本语法、变量类型、shader的三种形式、subshader、fallback、Pass LOD、tags...

新建一个shader,名为MyShader1内容如下:

1、_MainTex 为变量名

2、“Base (RGB)”表示在unity编辑面板中显示的名字,可以定义为中文

3、2D 表示变量的类型

4、white 表示初始值(默认值),默认为白色

变量名建议下划线开头

我们也来定义几个变量试试:

_My2D ("Base (RGB)", 2D) = "white" {}
_myfloat("浮点数",float)=0.3
_myRange("数值范围",Range(1,10))=3
_MyVector("向量",Vector)=(1,1,1,1)
_MyColor("颜色",Color)=(22,55,66,1)
_MyRect("矩形",Rect)="red" {}

Shader数值类型:

大家尽可能使用占内存小的fixed类型,优化性能,但是某些显卡,特别是手机上对fixed的支持不太好,所以要根据具体的情况来使用!

 

 

shader的三种形式,也就是shader的三种写法

Unity中所有渲染都使用着色器完成

unity的shaderlab对cg的程序进行了一个封装,封装的目的是让我们编写脚本的人能够更快的上手,更简单的去编写,用更简洁的代码去实现一个功能,

这里分为三种写法,如下:

1、顶点和片段着色器

2、表面着色器

3、固定功能着色器

 

固定功能着色器

此着色器写法是非常简单的,但是功能少,Unity官方是不建议大家用这个写法的

优势是能够用在比较陈旧古老的硬件上,在其他着色器的功能不支持之后,才去考虑用固定着色器。

顶点和片段着色器

功能最强大、最自由的形态,但是要编写的代码自然就会增多

特征是在Pass里出现CGPROGRAM和ENDCG块

Pass:通道    Name为通道名称

float2:相当于一个数组,包含两个float值,

float3:包含三个float值,同理float4......

pos为变量名

POSITION为语义,通俗来讲,因为cg里面有很多东西,比如一个颜色color,它是rgba,所以他是float4类型的数值,表示四个数,还有比如说向量,一个坐标(x,y,z),另外再加上一个方向向量,那么它也是一个float4类型的数值,float4不仅可以表示为颜色color,还可以表示为向量,以及UV等,那么为了表明我们声明的这个float4类型的变量是干嘛的,是用来表示颜色的还是向量的?为了说明它的作用,所以采用了语义来描述

显卡会首先调用顶点函数,通过计算得到一个v2f类型的值,这个值再交给片段函数,片段函数返回的是一个语义为Color的half4类型的值

appdata_base是来自于我们引入的包UnityCG.cginc里面定义的!UnityCG.cginc路径大概在Unity\Editor\Data\CGIncludes\下

表面着色器

Unity推荐使用的一种写法,当我们在unity里面create一个shader的时候,unity默认给我们创建的就是表面着色器

因为表面着色器自身会编译为多个通道,所以我们不能为它添加Pass通道,手动添加就会报错!

 

 

subshader、fallback、Pass LOD

一个subshader里面至少要包含一个通道Pass(表面着色器除外,因为它自身会编译为多个通道)

由于显卡兼容性存在偏差,如果存在多个subshader,默认情况下,是先执行第一个subshader,如果可以执行的话就执行它

如果第一个sunshader由于兼容性或者其他的问题不可以执行的话,那么会按顺序执行下面的subshader,如果一直到最后一个subshader也不能执行,

那么就使用Fallback指定的shader(diffuse)来执行

Pass的意义在于多次渲染,如果你有一个pass,那么着色器只会被调用一次,

如果你有多个Pass的话,那么就相当于执行多次subshader了,这就叫双通道或者多通道

在编写shader的时候,Pass要尽量的少,每多一个pass,那么dc就会多一次!

但是在实际运用的有些情况当中,是有可能要用到多个pass的,等具体情况具体分析

如果LOD设置为250的话,那么小于等于250的值对应的效果会被显示,而大于250的效果可能就不显示了!

 

 

tags

渲染序列Queue:

在场景里面,摄像头看到的物体是有先后关系的,那么它的渲染也是有个顺序的,它是有一个值可以设置的,值越小越先绘制,越大越后绘制(显示在最前面)

可以理解为权重越大,离摄像机越近!

这里定义了一些默认值,例如Background为1000,Geometry为2000等

自己可以定义任意值,但是表达式必须要包含某个默认值,例如"Queue"="1200"直接赋值为一个数字是错误的,会报错的!

只能这样:"Queue"="Geometry+5"

 

我们来做一个例子:场景中放一个cube盒子,和一个sphere球,把球放在盒子的后面,我们通过操作两者的shader来让盒子后面的球显示在最前面,

根据上面对渲染序列的介绍,那么正确的做法应该就是把球的Queue的值设的比盒子的大就可以了,可是并没有达到预期的效果,如下,盒子还是在最前面显示:

这里就涉及到了一个知识点了:深度缓存,我们只要关闭盒子的深度缓存就可以了(球的深度缓存关不关不受影响)!

默认绘制优先级是以深度缓存进行处理的,

深度缓存可以理解为,摆放物体距摄像机的远近,距离摄像机近的物体越后渲染,权重越大,显示越靠前,反之,显示越靠后!

Zwrite off:关闭深度缓存, 在渲染时,每个模型都有一个深度的,现在有可能球的深度是1,盒子是2,深度越大,绘制在越前面,

所以造成这种情况,只要关闭深度缓存,就会应用我们指定的渲染序列了!

Tags { Queue="Geometry+5" RenderType=Opaque }
LOD 200
Zwrite off

效果图:

如果有这样一个需求:即使被其他物体遮住,也要显示在这些物体的最前面,那么除了调整自己的渲染序列Queue值
大于那些在它前面的物体的Queue值,还要把遮住它的那些物体的深度缓存给关闭就可以了!
如果你不把那么遮挡你的物体的深度缓存给关闭了,那么即使你的渲染序列大于他们,也是没有用的!
关闭深度缓存的目的就是把被自己挡住的并且渲染序列大于自己的那些物体给去除遮挡

 

 

转载于:https://www.cnblogs.com/MrZivChu/p/shader2.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决cocos2d-x中文显示问题 /* Copyright (C) 1999-2003, 2005-2006, 2008-2011 Free Software Foundation, Inc. This file is part of the GNU LIBICONV Library. The GNU LIBICONV Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU LIBICONV Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU LIBICONV Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* When installed, this file is called "iconv.h". */ #ifndef _LIBICONV_H #define _LIBICONV_H #define _LIBICONV_VERSION 0x010E /* version number: (major<<8) + minor */ extern @DLL_VARIABLE@ int _libiconv_version; /* Likewise */ /* We would like to #include any system header file which could define iconv_t, 1. in order to eliminate the risk that the user gets compilation errors because some other system header file includes /usr/include/iconv.h which defines iconv_t or declares iconv after this file, 2. when compiling for LIBICONV_PLUG, we need the proper iconv_t type in order to produce binary compatible code. But gcc's #include_next is not portable. Thus, once libiconv's iconv.h has been installed in /usr/local/include, there is no way any more to include the original /usr/include/iconv.h. We simply have to get away without it. Ad 1. The risk that a system header file does #include "iconv.h" or #include_next "iconv.h" is small. They all do #include <iconv.h>. Ad 2. The iconv_t type is a pointer type in all cases I have seen. (It has to be a scalar type because (iconv_t)(-1) is a possible return value from iconv_open().) */ /* Define iconv_t ourselves. */ #undef iconv_t #define iconv_t libiconv_t typedef void* iconv_t; /* Get size_t declaration. Get wchar_t declaration if it exists. */ #include <stddef.h> /* Get errno declaration and values. */ #include <errno.h> /* Some systems, like SunOS 4, don't have EILSEQ. Some systems, like BSD/OS, have EILSEQ in a different header. On these systems, define EILSEQ ourselves. */ #ifndef EILSEQ #define EILSEQ @EILSEQ@ #endif #ifdef __cplusplus extern "C" { #endif /* Allocates descriptor for code conversion from encoding ‘fromcode’ to encoding ‘tocode’. */ #ifndef LIBICONV_PLUG #define iconv_open libiconv_open #endif extern iconv_t iconv_open (const char* tocode, const char* fromcode); /* Converts, using conversion descriptor ‘cd’, at most ‘*inbytesleft’ bytes starting at ‘*inbuf’, writing at most ‘*outbytesleft’ bytes starting at ‘*outbuf’. Decrements ‘*inbytesleft’ and increments ‘*inbuf’ by the same amount. Decrements ‘*outbytesleft’ and increments ‘*outbuf’ by the same amount. */ #ifndef LIBICONV_PLUG #define iconv libiconv #endif extern size_t iconv (iconv_t cd, @ICONV_CONST@ char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); /* Frees resources allocated for conversion descriptor ‘cd’. */ #ifndef LIBICONV_PLUG #define iconv_close libiconv_close #endif extern int iconv_close (iconv_t cd); #ifdef __cplusplus } #endif #ifndef LIBICONV_PLUG /* Nonstandard extensions. */ #if @USE_MBSTATE_T@ #if @BROKEN_WCHAR_H@ /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before <wchar.h>. BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included before <wchar.h>. */ #include <stddef.h> #include <stdio.h> #include <time.h> #endif #include <wchar.h> #endif #ifdef __cplusplus extern "C" { #endif /* A type that holds all memory needed by a conversion descriptor. A pointer to such an object can be used as an iconv_t. */ typedef struct { void* dummy1[28]; #if @USE_MBSTATE_T@ mbstate_t dummy2; #endif } iconv_allocation_t; /* Allocates descriptor for code conversion from encoding ‘fromcode’ to encoding ‘tocode’ into preallocated memory. Returns an error indicator (0 or -1 with errno set). */ #define iconv_open_into libiconv_open_into extern int iconv_open_into (const char* tocode, const char* fromcode, iconv_allocation_t* resultp); /* Control of attributes. */ #define iconvctl libiconvctl extern int iconvctl (iconv_t cd, int request, void* argument); /* Hook performed after every successful conversion of a Unicode character. */ typedef void (*iconv_unicode_char_hook) (unsigned int uc, void* data); /* Hook performed after every successful conversion of a wide character. */ typedef void (*iconv_wide_char_hook) (wchar_t wc, void* data); /* Set of hooks. */ struct iconv_hooks { iconv_unicode_char_hook uc_hook; iconv_wide_char_hook wc_hook; void* data; }; /* Fallback function. Invoked when a small number of bytes could not be converted to a Unicode character. This function should process all bytes from inbuf and may produce replacement Unicode characters by calling the write_replacement callback repeatedly. */ typedef void (*iconv_unicode_mb_to_uc_fallback) (const char* inbuf, size_t inbufsize, void (*write_replacement) (const unsigned int *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); /* Fallback function. Invoked when a Unicode character could not be converted to the target encoding. This function should process the character and may produce replacement bytes (in the target encoding) by calling the write_replacement callback repeatedly. */ typedef void (*iconv_unicode_uc_to_mb_fallback) (unsigned int code, void (*write_replacement) (const char *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); #if @HAVE_WCHAR_T@ /* Fallback function. Invoked when a number of bytes could not be converted to a wide character. This function should process all bytes from inbuf and may produce replacement wide characters by calling the write_replacement callback repeatedly. */ typedef void (*iconv_wchar_mb_to_wc_fallback) (const char* inbuf, size_t inbufsize, void (*write_replacement) (const wchar_t *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); /* Fallback function. Invoked when a wide character could not be converted to the target encoding. This function should process the character and may produce replacement bytes (in the target encoding) by calling the write_replacement callback repeatedly. */ typedef void (*iconv_wchar_wc_to_mb_fallback) (wchar_t code, void (*write_replacement) (const char *buf, size_t buflen, void* callback_arg), void* callback_arg, void* data); #else /* If the wchar_t type does not exist, these two fallback functions are never invoked. Their argument list therefore does not matter. */ typedef void (*iconv_wchar_mb_to_wc_fallback) (); typedef void (*iconv_wchar_wc_to_mb_fallback) (); #endif /* Set of fallbacks. */ struct iconv_fallbacks { iconv_unicode_mb_to_uc_fallback mb_to_uc_fallback; iconv_unicode_uc_to_mb_fallback uc_to_mb_fallback; iconv_wchar_mb_to_wc_fallback mb_to_wc_fallback; iconv_wchar_wc_to_mb_fallback wc_to_mb_fallback; void* data; }; /* Requests for iconvctl. */ #define ICONV_TRIVIALP 0 /* int *argument */ #define ICONV_GET_TRANSLITERATE 1 /* int *argument */ #define ICONV_SET_TRANSLITERATE 2 /* const int *argument */ #define ICONV_GET_DISCARD_ILSEQ 3 /* int *argument */ #define ICONV_SET_DISCARD_ILSEQ 4 /* const int *argument */ #define ICONV_SET_HOOKS 5 /* const struct iconv_hooks *argument */ #define ICONV_SET_FALLBACKS 6 /* const struct iconv_fallbacks *argument */ /* Listing of locale independent encodings. */ #define iconvlist libiconvlist extern void iconvlist (int (*do_one) (unsigned int namescount, const char * const * names, void* data), void* data); /* Canonicalize an encoding name. The result is either a canonical encoding name, or name itself. */ extern const char * iconv_canonicalize (const char * name); /* Support for relocatable packages. */ /* Sets the original and the current installation prefix of the package. Relocation simply replaces a pathname starting with the original prefix by the corresponding pathname with the current prefix instead. Both prefixes should be directory names without trailing slash (i.e. use "" instead of "/"). */ extern void libiconv_set_relocation_prefix (const char *orig_prefix, const char *curr_prefix); #ifdef __cplusplus } #endif #endif #endif /* _LIBICONV_H */

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值