Unity Shader简单Shader一

一个简单固定渲染管线 Unity shader,设置颜色

<pre name="code" class="csharp">Shader "Custom/ShowColor" {
	Properties {         // 属性
		_Color ("Color", Color) = (0,0,1,1)   //定义一个颜色
	}
	SubShader {
        Pass{
             Material {
                 Diffuse[_Color]     显示该颜色       
             }
             Lighting On   //打开光照开关,即接受光照
        }
	} 
	FallBack "Diffuse"
}

 

就可以在这里增加颜色了。

第二个Shader,设置图片

Shader "Custom/ShowTexture" {
	Properties {
		_Color ("Color", Color) = (0,0,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
	}
	SubShader {
        Pass{
             Material {
                 Diffuse[_Color]             
             }
             Lighting On
             SetTexture[_MainTex]
             {
                Combine texture*primary, texture*constant   //combine color部分,alpha部分 ,材质 * 顶点颜色
             }
        }
	} 
	FallBack "Diffuse"
}

其中的

Combine  纹理块合并命令

Previous is the the result of the previous SetTexture. 上一次SetTexture的结果

Primary is the color from the lighting calculation or the vertex color if it is bound.来自光照计算的颜色或是当它绑定时的顶点颜色

Texture is the color of the texture specified by [_TextureName] in the SetTexture (see above).在SetTexture中被定义的纹理的颜色

Constant is the color specified in ConstantColor被ConstantColor定义的颜色

http://www.ceeger.com/Components/SL-SetTexture.html点击打开链接

混合两张图片的Shader

Shader "Custom/MergeTexture" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_MainTex2 ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Pass{
             Material {
                 Diffuse[_Color]             
             }
             Lighting On
             SetTexture[_MainTex]
             {
                Combine texture*primary 
             }
             SetTexture[_MainTex2]
             {
                Combine texture*previous
             }
        }
	} 
	FallBack "Diffuse"
}


再来一个顶点片段着色器,

Shader "Custom/CubeTexture" {
	Properties {
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader {
        Pass{
            CGPROGRAM
            //返回值 vertex 顶点着色器 vert 和返回值 fragment 片段着色器 frag ,也就是执行vert 和 frag
            #pragma vertex vert  
            #pragma fragment frag
            #include "UnityCG.cginc"
            sampler2D _MainTex;
            float4 _MainTex_ST;
            struct v2f {                  //声明一个结构体
                  float4  pos : SV_POSITION; //视口位置
                  float2  uv : TEXCOORD0;      //贴图坐标
            } ;
            v2f vert (appdata_base v)         //输入为 appdata_base
            {
                   v2f o;                     //返回值
                   o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
                   o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
                   return o;                     
             }
             float4 frag (v2f i) : COLOR
             {
                   float4 texCol = tex2D(_MainTex,i.uv);
                   float4 outp = texCol;
                   return outp;
              }
              ENDCG
        
        }
	} 
	FallBack "Diffuse"
}

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="csharp"><pre name="code" class="csharp"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;">float4 vertex : POSITION; //顶点坐标</span><br style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;" /><span style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;">float4 tangent : TANGENT; // tangent,三角函数的一种,缩写为tan我们很熟悉了,他的值是mesh到表面法线的正切值</span><br style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;" /><span style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;">float3 normal : NORMAL; //表面法向量,以对象的坐标系标准化至单位长度</span><br style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;" /><span style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;">float4 texcoord : TEXCOORD0;//纹理坐标系的第0个集合</span><br style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;" /><span style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;">float4 texcoord1 : TEXCOORD1; //纹理坐标系的第1个集合</span><br style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;" /><span style="color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px;">fixed4 color : COLOR;//颜色,通常为常数</span>
</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">看一下流水线图形和光栅化操作</span>

 
<img src="https://img-blog.csdn.net/20160606221230093?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" /><img src="https://img-blog.csdn.net/20160606221234030?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" />
 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值