1、C#脚本设置shader参数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class class33 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Matrix4x4 SM = new Matrix4x4();//缩放矩阵 随时间动态变换
SM[0, 0] = Mathf.Sin(Time.realtimeSinceStartup)/4+0.5f;
SM[1, 1] = Mathf.Cos(Time.realtimeSinceStartup)/8+0.5f;
SM[2, 2] = Mathf.Sin(Time.realtimeSinceStartup)/6+0.5f;
SM[3, 3] = 1;
Matrix4x4 mvp = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;
GetComponent<Renderer>().material.SetMatrix("sm", SM);
}
}
2、在shader里做变换和颜色显示
Shader "Custom/shader33.1"
{
SubShader
{
pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
float4x4 mvp;
float4x4 rm;
float4x4 sm;
struct v2f
{
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
float4 m = UnityObjectToClipPos(v.vertex);//顶点MVP变换
o.pos = mul(m, sm);//控制缩放
//1、控制顶点颜色
if (v.vertex.x == 0.5 && v.vertex.y == 0.5 && v.vertex.z == -0.5)
o.color = fixed4(1, 0, 0, 1);
else
o.color = fixed4(0, 0, 1, 1);
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
float4 wpos = mul(unity_ObjectToWorld, v.vertex);//转换模型坐标系到世界坐标系
//2、改变物体位置时会改变颜色
if (wpos.x > 0)
o.color = fixed4(1, 0, 0, 1);
else
o.color = fixed4(0, 0, 1, 1);
//3、颜色周期变换
if (v.vertex.x == 0.5 && v.vertex.y == 0.5 && v.vertex.z == -0.5)
o.color = fixed4(_SinTime.w/2+0.5,_CosTime.w/2+0.5, _SinTime.y/2+0.5, 1);
else
o.color = fixed4(0, 0, 1, 1);
return o;
}
fixed4 frag(v2f IN) :COLOR{
return IN.color;
}
ENDCG
}
}
}
3、改变顶点的位置信息
Shader "Custom/shader35"
{
properties{
_R("R",range(0,5))=1
}
SubShader
{
pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
float _R;
struct v2f
{
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
//float2 xy = v.vertex.xz;
//float d =_R-length(xy);//计算模
float4 wpos = mul(unity_ObjectToWorld, v.vertex);//转换到世界坐标系
float2 xy = wpos.xz;
float d = _R - length(xy);
d= d < 0 ? 0 : d;
float height = 1;
float4 uppos = float4(v.vertex.x, height * d, v.vertex.z, v.vertex.w);
o.pos = UnityObjectToClipPos(uppos);//透视
o.color = fixed4(uppos.y,uppos.y,uppos.y,1);
return o;
}
fixed4 frag(v2f IN) :COLOR{
return IN.color;
}
ENDCG
}
}
}
4.顶点扭曲(做周期旋转)
Shader "Custom/shader36"
{
SubShader
{
pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
struct v2f
{
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
float angle = length(v.vertex)*_SinTime.w;
/* float4x4 m = {
float4(cos(angle),0,sin(angle),0),
float4(0,1,0,0),
float4(-sin(angle),0,cos(angle),0),
float4(0,0,0,1)
};*/
float x = v.vertex.x * cos(angle) + v.vertex.z* sin(angle);
float z = v.vertex.x * -sin(angle) + v.vertex.z * cos(angle);
v.vertex.x = x;
v.vertex.z = z;
//float4 rm= mul(m,v.vertex);
//o.pos = UnityObjectToClipPos(rm);
o.pos = UnityObjectToClipPos(v.vertex);
o.color = fixed4(0,1,1,1);
return o;
}
fixed4 frag(v2f IN) :COLOR{
return IN.color;
}
ENDCG
}
}
}
5、实现波浪效果
Shader "Custom/shaderwave"
{
SubShader
{
pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
struct v2f
{
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
//A*SIN(W*X+T)简谐运动公式
//F=1/W
//v.vertex.y += 0.2*sin(v.vertex.x*2 + _Time.y);//横波
//v.vertex.y += 0.2 * sin(-length(v.vertex.xz) * 2 + _Time.y);//圆形波向外扩散
v.vertex.y += 0.2 * sin((v.vertex.x+v.vertex.z)+ _Time.y);//鱼鳞波
v.vertex.y += 0.3 * sin((v.vertex.x - v.vertex.z) + _Time.w);//鱼鳞波叠加
o.pos = UnityObjectToClipPos(v.vertex);
o.color = fixed4(v.vertex.y,v.vertex.y,v.vertex.y,1);
return o;
}
fixed4 frag(v2f IN) :COLOR{
return IN.color;
}
ENDCG
}
}
}
6、实现漫反射和点光源照射
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Custom/diffuse2"
{
SubShader
{
pass {
tags{"LightMode" = "ForwardBase"}//光照模型
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
#include "lighting.cginc"
struct v2f
{
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//o.color = fixed4(0,0,1,1);
float3 N = normalize(v.normal);//法线归一化
float3 L = _WorldSpaceLightPos0;//光源位置
//以下两种方法选1即可
//L = mul(unity_WorldToObject, float4(L, 0)).xyz;//转换光源到模型坐标系
//N = mul(unity_ObjectToWorld, float4(N, 0)).xyz;//转换顶点法线到世界坐标系
/*非等比缩放情况下法向量的世界坐标会有误
需要修改
*/
N = mul( float4(N, 0), unity_WorldToObject).xyz;//转换顶点法线到世界坐标系
N = normalize(N);
float dot1 = saturate(dot(N, L));//控制点积在0-1
//1、设置平行光
o.color = _LightColor0 * dot1;
//o.color.rgb = ShadeVertexLights(v.vertex, v.normal); //光照模型为Vertex
float3 wpos = mul(unity_ObjectToWorld, v.vertex).xyz;
//2、设置点光源
o.color.rgb = o.color.rgb+ Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, wpos, N);
return o;
}
fixed4 frag(v2f IN) :COLOR{
//加入环境光
return IN.color+UNITY_LIGHTMODEL_AMBIENT;
}
ENDCG
}
}
}
效果图:
7、实现镜面反射文章来源:https://www.toymoban.com/news/detail-637045.html
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
Shader "Custom/specular"
{
properties{
_sp("Specular",color) = (1,1,1,1)
_shiness("Shiness",range(1,64)) = 8
}
SubShader
{
pass {
tags{"LightMode" = "ForwardBase"}//光照模型
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "unitycg.cginc"
#include "lighting.cginc"
float4 _sp;
fixed _shiness;
struct v2f
{
float4 pos:POSITION;
fixed4 color : COLOR;
};
v2f vert(appdata_base v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//o.color = fixed4(0,0,1,1);
float3 N = normalize(v.normal);//法线归一化
float3 L = _WorldSpaceLightPos0;//光源位置
//以下两种方法选1即可
//L = mul(unity_WorldToObject, float4(L, 0)).xyz;//转换光源到模型坐标系
//N = mul(unity_ObjectToWorld, float4(N, 0)).xyz;//转换顶点法线到世界坐标系
/*非等比缩放情况下法向量的世界坐标会有误
需要修改
*/
N = mul( float4(N, 0), unity_WorldToObject).xyz;//转换顶点法线到世界坐标系
N = normalize(N);
//diffuse color
float dot1 = saturate(dot(N, L));//控制点积在0-1
o.color = _LightColor0 * dot1;
//o.color.rgb = ShadeVertexLights(v.vertex, v.normal); //Vertex
//specular color
float3 wpos = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 I = -WorldSpaceLightDir(v.vertex);
float3 R = reflect(I, N);
float3 V = WorldSpaceViewDir(v.vertex);//顶点到相机的向量
R = normalize(R);
V = normalize(V);
float specularscale = pow(saturate(dot(R, V)), _shiness);
o.color.rgb += _sp * specularscale;
//float3 I = wpos - _WorldSpaceLightPos0;
return o;
}
fixed4 frag(v2f IN) :COLOR{
//加入环境光
return IN.color+UNITY_LIGHTMODEL_AMBIENT;
}
ENDCG
}
}
}
文章来源地址https://www.toymoban.com/news/detail-637045.html
到了这里,关于shader学习(二)顶点着色器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!