unity shader 顶点动画
好久没写东西了,今天写一个小知识点分享一下,主要是想写写。
内容主要是:unity C#代码生成mesh,然后shader添加顶点动画,如下图:
C#代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class DrawMesh : MonoBehaviour
{
private MeshFilter meshFilter;
private MeshRenderer meshRenderer;
// Start is called before the first frame update
void Start()
{
meshFilter = gameObject.GetComponent<MeshFilter>();
meshRenderer = gameObject.GetComponent<MeshRenderer>();
CreateMesh();
}
// Update is called once per frame
void Update()
{
}
void CreateMesh(){
//mesh
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
vertices.Add(new Vector3(0,0,0));
vertices.Add(new Vector3(0.2f,-1,0));
vertices.Add(new Vector3(0.5f,-1,0));
vertices.Add(new Vector3(0.4f,-0.4f,0));
mesh.vertices = vertices.ToArray();
mesh.SetTriangles(new int[]{0,1,2,2,3,0},0);
List<Color> colors = new List<Color>();
colors.Add(new Color(1,0,0,1));
colors.Add(new Color(0,1,0,1));
colors.Add(new Color(0,1,0,1));
colors.Add(new Color(0.8f,0.2f,0,1));
mesh.colors =colors.ToArray();
meshFilter.mesh = mesh;
}
}
shader代码:
Shader "Unlit/VertextShader"
{
Properties
{
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Cull OFF
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
fixed4 color:Color;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed4 color:Color;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.vertex.x += sin(_Time.y * 1*(1-o.vertex.y))*0.1;
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
本人qq:344810449,欢迎探讨研究。
有unity,shader,小程序,软件开发,游戏制作等需求也可以联系本人,非常乐于助人。
如果觉得还不错给博主来个小惊喜,纯属自愿,不强求: