Newer
Older
BlackoutClient / Assets / Scripts / Pulse.cs
using KdTree.Math;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pulse : MonoBehaviour
{

    private static Vector3[] randomSpherePoints = null;
    private static KdTree.KdTree<float, int> randomSphereKd = new KdTree.KdTree<float, int>(3, new FloatMath());

    private static int randomSpherePointsCount = 300;
    private static void GenerateRandomPoints()
    {
        randomSpherePoints = new Vector3[randomSpherePointsCount];
        for (int i = 0; i < randomSpherePointsCount; i++)
        {
            bool ok = false;
            while (!ok)
            {
                Vector3 r = UnityEngine.Random.insideUnitSphere;
                if (r.sqrMagnitude > 0.000001f)
                {
                    randomSpherePoints[i] = r.normalized;
                    ok = true;
                }
            }
        }
    }


    public float Lobes = 10;
    public float TimeScale = 1;
    public float Amount = 0.6f;

    private Mesh mesh;
    private Vector3[] originalPoints;
    private Vector3[] originalNormals;
    private Vector3[] points;
    // Start is called before the first frame update
    void Start()
    {
        MeshFilter mf = GetComponent<MeshFilter>();

        //mesh = GameObject.Instantiate(mf.mesh);
        //mf.mesh = mesh;

        mesh = mf.mesh;
        

        mesh.RecalculateNormals();

        originalPoints = mesh.vertices;
        originalNormals = mesh.normals;
        points = new Vector3[originalPoints.Length];
    }

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < originalPoints.Length; i++)
        {
            Vector3 o = originalPoints[i];
            Vector3 n = originalNormals[i];

            float angleUp = Mathf.Atan2(new Vector2(o.x, o.z).magnitude, o.y);

            float expand = Mathf.Sin(Mathf.Atan2(o.x, o.z) * Mathf.Round(Lobes * angleUp) * Mathf.Sin(angleUp * Lobes) + Time.realtimeSinceStartup * Mathf.PI * TimeScale) * Amount;
            points[i] = o + n * expand;
        }

        mesh.vertices = points;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
    }
}