Unity学习笔记:Shader相关CG语言的基本语法结构

  Shader的基本功能格式↓

Shader "MyCustom/MyShader" {
	Properties {//属性
	// 属性名字  界面显示名  类型     值
		_Color("MyColor",color) = (1,0,0,1)
		_AmbientColor("AmbientColor",color) = (1,0,0,1)
		_SpecularColor("SpecularColor",color) = (1,0,0,1)
		_Shininess("Shininess",range(0,8)) = 3
		_Emission("EmissionColor",color) = (1,1,1,1)
		_MainTexture("MainTexture",2d) = ""
	}
	SubShader {//shader算法
		pass//通道(存储图像的色彩)
		{
			//color(1,0,0,1) //()固定值
			//color[_Color]//【】使用参数值
			material
			{	//漫反射 与灯光配合,将光打到物体表面,我们才能看到物体
				diffuse[_Color]
				ambient[_AmbientColor]//环境光
				specular[_SpecularColor]//高光
				shininess[_Shininess]//滑动条
				emission[_Emission]//自发光
			}
			lighting on//启动光照
			separateSpecular on //启用高光
			SetTexture[_MainTexture]
			{
			//与材质效果混合
			combine Texture*previous double
			}
		}
	}
}

  在Shader结构中添加使用CG代码块,语法结构如下↓

Shader "Custom/NewSurfaceShader" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }//描述渲染类型,当前是不透明的物体
		//Tags { "RenderType"="Opaque" "queue" = "transparent"}//透明的物体
		LOD 200 //层级细节
		
		CGPROGRAM//CG代码块
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows//编译指令
//surface表面着色器 surf调用的方法 Standard基于物理的光照模型(原来是漫反射 Lambert)
//fullforwardshadows 阴影表现(平行光、点光、聚光都是有阴影的)

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0 //GPU硬件支持3.0 不写默认是2.0

		sampler2D _MainTex;//CG 中的图片类型

		struct Input {
			float2 uv_MainTex;//记录uv纹理坐标
		};

		half _Glossiness;//CG中的浮点型
		half _Metallic;
		fixed4 _Color;//CG中的四阶向量

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;//金属光泽表现
			o.Smoothness = _Glossiness;//高光光泽度
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

CG代码块是独立的,shader中的属性都要在CG代码块中重新声明。

猜你喜欢

转载自blog.csdn.net/huanyu0127/article/details/107075990