Shader学习Day02 固定管线着色器、ShaderLab

固定管线着色器

Shader基本语法

LearnShaderLab

//创建Shader,标注Shader所在路径
Shader "MyShader/LearnShaderLab"
{
    //设置Shader外部属性
    Properties
    {
      
    }
    
    //子着色器  【高质量】
    //真正去渲染的,每个都是独立的单元,不是为了叠加,而是为了区分设备
    SubShader
    {
        //渲染正面
        //固定管线着色器至少要有一个pass通道
        Pass   //路径
        {
           
        }
        
        //渲染背面
        Pass
        {
        
        }
    }
    
    //子着色器  【中质量】
    SubShader
    {
        Pass   //路径
        {
           
        }
    }
    
    //子着色器  【低质量】
    SubShader
    {
        Pass   //路径
        {
           
        }
    }
    
    //备胎  最简单的Shader,只有一个漫反射
    Fallback "Diffuse"
}

1.Properties 外部属性

常用外部属性

Shader "MyShader/ShaderLabProperties"
{
    Properties{
      //属性名  面板名称  数据类型 默认值  【后面不加;】
      _MyIntValue("一个整数属性",Int) = 20   
      _MyFloatValue("一个小数类型",Float) =  3.14 
      _MyRangeValue("一个范围小数属性",Range(0,10)) = 3
      _MyColorValue("一个颜色属性",Color) = (1,0,0,1)
      _MyVectorValue("四维数属性",Vector) = (1,2,3,4)
      //图片
      _My2DValue("2阶图片",2D) = "white"{}
      _MyRectValue("非2阶图片",Rect) = ""{}
      _My3DValue("3阶图片",cube) = ""{}
    }
    
    SubShader{
      Pass{
      
      }
    }
}

 纯色处理

Shader "MyShader/Fixed/Fixed001"
{
   Properties
   {
      _MainColor("纯色",Color) = (1,0,0,1)
   }
   
   SubShader
   {
     Pass 
     {
        //设置纯色渲染
        Color(0,1,0,1)
        
        //设置可变纯色渲染
        Color[_MainColor]
        
     }
   }
}

镜面反射和漫反射

Shader "MyShader/Fixed/Fixed002"
{
   Properties
   {
      _DiffuseColor("漫反射颜色",Color) = (1,0,0,1)
      _AmbientColor("环境光颜色",Color) = (0,0,1,1)
      _SpecularColor("高光颜色",Color) =(1,1,1,1)
      _Shininess("光泽度",Range(0.1,2)) = 2
      _EmissionColor("自发光",Color) = (1,0,0,1)
   }
   
   SubShader
   {
     Pass 
     {
        //开启顶点光照命令
        Lighting on
        
        //开启镜面反射命令
        SeparateSpecular on
        
        //材质命令
        Material
        {
          //漫反射效果命令
          //Diffuse(1,0,0,1)
          Diffuse[_DiffuseColor]
          //环境光颜色
          Ambient[_AmbientColor]
          //高光颜色
          Specular[_SpecularColor]
          //光泽度
          Shininess[_Shininess]
          //自发光
          Emission[_EmissionColor]
         }
     }
   }
}

 

2.Tags标签

子着色器标签:

Pass通道标签 :

渲染队列:Queue

3.常用渲染

标签  、正面和背面剔除

Shader "MyShader/Fixed/Fixed003"
{
   Properties
   {
       _MainColor("主颜色",Color) = (1,0,0,1)
   }
   
   SubShader
   {
     Pass 
     {
         //剔出背面
         Cull back
         
         Lighting on
         
         Material
         {
           Diffuse[_MainColor]
         }
     }
     
     Pass 
     {
         //剔出正面
         Cull Front
         
         Color(1,0,1,1)
     }
     
   }
}

4.深度测试和深度缓存

渲染队列

Shader "MyShader/Fixed/Fixed004"
{
   Properties
   {
       _MainColor("主颜色",Color) = (1,0,0,1)
   }
   
   SubShader
   {
     //关闭深度测试
     ZTest  off
     //SubShader标签
     //默认是Geometry(几何体)-2000
     Tags
     {
       
        "Queue" = "Transparent+15"
     }
     Pass 
     {
         Lighting on
         
         Material
         {
           Diffuse[_MainColor]
         }
     }
     
     Pass 
     {
         //剔出正面
         Cull Front
         
         Color(1,0,1,1)
     }
     
   }
} 
Shader "MyShader/Fixed/Fixed005"
{
   Properties
   {
       _MainColor("主颜色",Color) = (1,0,0,1)
   }
   
   SubShader
   {
     //关闭深度测试
     ZTest  off
     
     Tags
     {
       "Queue" = "OverLay"
     }
      
     Pass 
     {
         Lighting on
         
         Material
         {
           Diffuse[_MainColor]
         }
     }
   }
}
Shader "MyShader/Fixed/Fixed006"
{
   Properties
   {
       _MainColor("主颜色",Color) = (1,0,0,1)
   }
   
   SubShader
   {
     Pass 
     {
         //Greater后面不能接数
         //深度测试大于【被其他对象挡住】
         ZTest  Greater 
        
         Color(0,1,0,1)
         
     }
     
     Pass 
     {
         //Greater后面不能接数
         //深度测试大于【被其他对象挡住】
         ZTest  LEqual 
         
          
         //关闭深度缓存
         ZWrite Off
         
         
         Lighting on
         
         Material
         {
           Diffuse[_MainColor]
           
           Ambient(1,1,1,1)
           
           
         }
     }
   }
}

5.混合

图片

Shader "MyShader/Fixed/Fixed007"
{
   Properties
   {
     _MainTexture("主纹理",2D) = ""{}
   }
   
   SubShader
   {
     Pass 
     {
         SetTexture[_MainTexture]
         {
            
         }
         
     }
   }
}

颜色和图片(混合)

Shader "MyShader/Fixed/Fixed008"
{
   Properties
   {
     _MainColor("主颜色",Color)= (1,0,0,1)
     _MainTexture("主纹理",2D) = ""{}
   }
   
   SubShader
   {
     Pass 
     {
         /*Lighting On
         
         Material
         {
            Diffuse[_MainColor]
         }*/
         
         Color[_MainColor]
         
         SetTexture[_MainTexture]
         {
            /*//图片和颜色混合
            //顶点光照下的颜色结果 + 当前设置的纹理图片
            combine primary * texture //DOUBLE*//**/
            
            ConstantColor[_MainColor]
            
            combine constant + texture
         }
         
     }
   }
}

图片和图片(混合)

Shader "MyShader/Fixed/Fixed009"
{
   Properties
   {
     _MainTexture("主纹理",2D) = ""{}
     _DetailTexture("细节纹理",2D) = ""{}
   }
   
   SubShader
   {
     Pass 
     {
         SetTexture[_MainTexture]
         
         SetTexture[_DetailTexture]
         {
            //当前纹理和上一次Setture的结果混合
             combine texture * previous DOUBLE
         }
     }
   }
}

混合lerp

Shader "MyShader/Fixed/Fixed010"
{
   Properties
   {
     _FirstTexture("图片1",2D) = ""{}
     _SecondTexture("图片2",2D) = ""{}
     _BlendScale("混合比例",Range(0,1)) = 0
   }
   
   SubShader
   {
     Pass 
     {
         SetTexture[_FirstTexture]
         {
           constantcolor(1,0,0,0.1)
           combine texture * constant
         }
         SetTexture[_SecondTexture]
         {
            constantcolor(0,0,0,[_BlendScale])
            //       源1           源2        源3
            combine previous lerp (constant) texture
         }
     }
   }
}

半透明混合

Shader "MyShader/Fixed/Fixed011"
{
   //半透明的混合效果
   Properties
   {
     _MainCol("主颜色",Color) = (1,0,0,0.5)
     _MainTex("主纹理",2D) = ""{}
   }
   
   SubShader
   { 
     Tags{"Queue" = "Transparent"}
     Blend SrcAlpha OneMinusSrcAlpha
     
     Pass 
     {
         Lighting On
         Blend SrcAlpha OneMinusSrcAlpha
         Material
         {
            Diffuse[_MainCol]
         }
        
         SetTexture[_MainTex]
         {
            combine texture + primary
         }
     }
   }
}

6.AlphaTest

Shader "MyShader/Fixed/Fixed012"
{
   //半透明的混合效果
   Properties
   {
     _MainTex("主纹理",2D) = ""{}
     
     _AlphaTestValue("测试标准",Range(0,1)) = 0
   }
   
   SubShader
   { 
     AlphaTest Greater [_AlphaTestValue]

     Pass 
     {
         SetTexture[_MainTex]
     }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值