Shader笔记(_NameTex_ST+序列帧动画)

  • 重复贴图:地表  墙壁 篱笆
  • 实现uv可调,设置细节贴图增大城府次数(Tiling值)  与原贴图颜色混合(相乘) 解决游戏中贴图重复的时候 边界明显的问题和细节 显示问题  很常用:(在面板里的Tiling)
Shader "Custom/BaseShader"
{
	Properties
	{
		_Color("Base Color",Color)=(1,1,1,1)//颜色控制
		_MainTex("Base(RGB)",2D)="white"{}//输入一张半透明的贴图
		//_RSpeed("RotateSpeed",Range(1,100))=30//设置旋转速度
	}
	SubShader
	{
	    tags{"Queue"="Transparent""RenderType"="Transparent""IgnoreProjector"="True"}//透明物体渲染顺序、渲染模式、忽略投影

		Blend SrcAlpha OneMinusSrcAlpha   //渲染状态 混合模式 SrcAlpha+OneMinusSrcAlpha=1  SrcAlpha混合因子
	    Pass
		{
		    Name "Simple"
			//Cull off//正反面都显示

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			sampler2D  _MainTex;
			float4 _Color;
			//float _RSpeed;

			struct v2f//从顶点到片段过度的结构体
			{
			    float4 pos:POSITION;
				float2 uv:TEXCOORD0;
			};//结构体后面要有分号 

			float4 _MainTex_ST;//不需要接口声明
			v2f vert(appdata_base v)//获取的是物体坐标系
			{
				v2f o;
				o.pos=UnityObjectToClipPos(v.vertex);//模型坐标系、观察坐标系、投影坐标系变换矩阵相乘=MVP?最后得到的结果是投影坐标系
				o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);//设置贴图uv可调??TRANSFORM_TEX(uv坐标,贴图)
				return o;
			}

			half4 frag(v2f i):COLOR
			{


			    half4 c=tex2D(_MainTex,i.uv)*_Color;//纹理影射
			    return c;
			}


			ENDCG
		}
	}
}

  • TRANSFORM_TEX()函数与float4 _MainTex_ST;(//不需要接口声明,_ST写在贴图名字后  说明的 UI该贴图进行uv变换)是成对出现的,缺一不可。
  • 在属性里设置一些不可被更改的值。


  • UV乘 是放大、缩小   加是位移
  • frac(x)  返回x的小数部分。
  • 序列帧火焰:
Shader "Unlit/AnimalShader"
{
	Properties
	{
		_Color("Base Color",Color)=(1,1,1,1)//颜色控制
		_MainTex("Base(RGB)",2D)="white"{}//输入一张半透明的贴图
		//_RSpeed("RotateSpeed",Range(1,100))=30//设置旋转速度
		//_DetailTex("DetailTex",2D)="white"{}
	}
	SubShader
	{
	    tags{"Queue"="Transparent""RenderType"="Transparent""IgnoreProjector"="True"}//透明物体渲染顺序、渲染模式、忽略投影

		Blend SrcAlpha OneMinusSrcAlpha   //渲染状态 混合模式 SrcAlpha+OneMinusSrcAlpha=1  SrcAlpha混合因子
	    Pass
		{
		    Name "Simple"
			//Cull off//正反面都显示

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			sampler2D  _MainTex;
			float4 _Color;
			//float _RSpeed;
			//sampler2D _DetailTex;

			struct v2f//从顶点到片段过度的结构体
			{
			    float4 pos:POSITION;
				float2 uv:TEXCOORD0;
			};//结构体后面要有分号 

			float2 moveUV(float2 vertUV)
			{
			    float  textureNum=12.0;//浮点数比整数运算快
				float timePerFram=100;

				float index=frac(_Time.x/textureNum*timePerFram);//值在0,1之间 一共12帧
				float2 uvScale=float2(1/textureNum,1);

				if (index<=uvScale.x)
				    return vertUV*uvScale;
				else if (index<=2*uvScale.x)
				    return vertUV*uvScale+float2(uvScale.x,0.0);
                else if (index<=3*uvScale.x)
				    return vertUV*uvScale+float2(2*uvScale.x,0.0);
				else if (index<=4*uvScale.x)
				    return vertUV*uvScale+float2(3*uvScale.x,0.0);
                else if (index<=5*uvScale.x)
				    return vertUV*uvScale+float2(4*uvScale.x,0.0);
                else if (index<=6*uvScale.x)
				    return vertUV*uvScale+float2(5*uvScale.x,0.0);
				else if (index<=7*uvScale.x)
				    return vertUV*uvScale+float2(6*uvScale.x,0.0);
                else if (index<=8*uvScale.x)
				    return vertUV*uvScale+float2(7*uvScale.x,0.0);
                else if (index<=8*uvScale.x)
				    return vertUV*uvScale+float2(8*uvScale.x,0.0);
                else if (index<=10*uvScale.x)
				    return vertUV*uvScale+float2(9*uvScale.x,0.0);
				else if (index<=11*uvScale.x)
				    return vertUV*uvScale+float2(10*uvScale.x,0.0);
                else 
				    return vertUV*uvScale+float2(11*uvScale.x,0.0);

			}

			//float4 _MainTex_ST;//不需要接口声明
			v2f vert(appdata_base v)//获取的是物体坐标系
			{
				v2f o;
				o.pos=UnityObjectToClipPos(v.vertex);//模型坐标系、观察坐标系、投影坐标系变换矩阵相乘=MVP?最后得到的结果是投影坐标系
				o.uv=moveUV(v.texcoord.xy);//设置贴图uv可调??TRANSFORM_TEX(uv坐标,贴图)
				return o;
			}

			half4 frag(v2f i):COLOR
			{


			    half4 c=tex2D(_MainTex,i.uv)*_Color;//纹理影射
				//half4 d=tex2D(_DetailTex,i.uv);
			    return c;
			}


			ENDCG
		}
	}
}

  • 使面片始终面对摄像机:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BillBoard : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        this.transform.rotation = Camera.main.transform.rotation;
    }
}

猜你喜欢

转载自blog.csdn.net/Vermouthstx/article/details/79943353