CatLikeCoding Basic No.3

因为后面的圆和环就是跑数学公式了所以偷懒了orz

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.Mathf;

public static class FunctionLibrary 
{//delegate是第一次用到,相当于函数指针,这里是指可以搭载满足输入为3个参数、返回值为Vector3的函数,funcitons就是一个function的数组,每一个元素都是一个满足条件的Function
    public delegate Vector3 Function(float x, float z, float t);
    static Function[] functions = { Wave, MultiWave, Ripple };
    public enum functionname { Wave,MultiWave, Ripple};
    public static Function GetFunction(functionname name)
    {
        return functions[(int)name];//注意这里要变下int类型,虽然是枚举
    }
    public static Vector3 Wave(float u, float v, float t)
    {
        Vector3 p;
        p.x = u;
        p.y = Sin(PI * (u + v + t));
        p.z = v;
        return p;
    }
    public static Vector3 MultiWave(float u, float v, float t)
    {
        Vector3 p;
        p.x = u;
        p.y = Sin(PI * (u + 0.5f*t));
        p.y += Sin(2f * PI * (v + t)) * (1f / 2f);
        p.y += Sin(PI * (u + v + 0.25f * t));
        p.y *= (1f / 2.5f);
        p.z = v;
        return p;
        
    }
    public static Vector3 Ripple(float u, float v, float t)
    {
        float d = Sqrt(u * u + v * v);
        Vector3 p;
        p.x = u;
        p.y = Sin(PI * (4f * d - t));
        p.y /= 1f + 10f * d;
        p.z = v;
        return p;
    }
}

 

 

 

 

 

 

posted @ 2023-01-24 16:29  LOFU  阅读(10)  评论(0编辑  收藏  举报