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代码块中重新声明。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值