目标:
需要实现通过改变mesh的顶点坐标来实现面片类似海浪一样的流动。
两个方案:shader或者脚本做顶点坐标修改
在unity中创建表面shader
k = 2π/wavelength ,wavelength 是 波长
正弦波 y = 振幅 * sin(k * x )
流动偏移 y = 振幅 * sin(k * ( x - 速度 * 时长))
---------------------------------------------------正弦到这其实就可以了,形就有了
接收阴影,就需要设置法线
求导: (sinf)' = f' * cos f
f = k * ( x - 速度 * 时长),这块看做一个整体,y = a * sin f ,那(y)' = (a * sing f)'
推出 = a * k * cos f
完整shader:
Shader "MyShader/Waves"
{
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
_Amplitude("Amplitude", float) = 1
_Wavelength("Wavelength", float) = 10
_Speed ("Speed", float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
//addshadow :接受阴影 vertex:vert 顶点坐标
#pragma surface surf Standard fullforwardshadows vertex:vert addshadow
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _Amplitude,_Wavelength, _Speed;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
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;
}
void vert(inout appdata_full vertexData)
{
float3 p = vertexData.vertex.xyz;
float k = 2 * UNITY_PI /_Wavelength;
float f = k * (p.x - _Speed * _Time.y);
// p.y = _Amplitude * sin(k * (p.x - _Speed * _Time.y));
p.y = _Amplitude * sin(f);
float3 tangent = normalize(float3(1,k*_Amplitude * cos(f),0));//法线
float3 normal = float3(-tangent.y,tangent.x,0);
vertexData.vertex.xyz = p;
vertexData.normal = normal;
}
ENDCG
}
FallBack "Diffuse"
}
通过脚本做顶点坐标修改
代码备份
using System;
using UnityEngine;
public class Test : MonoBehaviour
{
public MeshFilter m_TargetMeshFilter = null;
private Mesh m_TargetMesh = null;
public float A = 2;//振幅
public float speed = 1;//水流速度
public float wavelength = 10;//波长
private Vector3[] baseVertices;//顶点坐标
private void Start()
{
m_TargetMesh = m_TargetMeshFilter.mesh;
baseVertices = m_TargetMesh.vertices;
}
private void Update()
{
if (m_TargetMesh != null)
{
Vector3[] temp = m_TargetMesh.vertices;
for (int i = 0; i < temp.Length; i++)
{
Vector3 target = temp[i];
Vector3 v = baseVertices[i];
float k = 2 * Mathf.PI / wavelength;
target.y = A * (float)Math.Sin(k * (v.x - speed * Time.fixedTime));
temp[i] = new Vector3(target.x, target.y, target.z);
Debug.Log(target);
}
m_TargetMesh.vertices = temp;
// 重新计算Mesh的法线和边界
m_TargetMesh.RecalculateNormals();
m_TargetMesh.RecalculateBounds();
}
}
}