Done!
This commit is contained in:
71
Assets/Script/FunctionLibrary.cs
Normal file
71
Assets/Script/FunctionLibrary.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using UnityEngine;
|
||||
using static UnityEngine.Mathf;
|
||||
|
||||
public static class FunctionLibrary
|
||||
{
|
||||
public delegate Vector3 Function(float u, float v, float y);
|
||||
|
||||
public enum FunctionName { Wave, MultiWave, Ripple, Sphere, Torus }
|
||||
|
||||
private static Function[] functions = { Wave, MultiWave, Ripple, Sphere, Torus };
|
||||
|
||||
public static Function GetFunction(FunctionName name)
|
||||
{
|
||||
return functions[(int)name];
|
||||
}
|
||||
|
||||
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 += 0.5f * Sin(2f * PI * (v + t));
|
||||
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)
|
||||
{
|
||||
var 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;
|
||||
}
|
||||
|
||||
public static Vector3 Sphere(float u, float v, float t)
|
||||
{
|
||||
float r = 0.9f + 0.1f * Sin(PI * (6f * u + 4f * v + t));
|
||||
float s = r * Cos(0.5f * PI * v);
|
||||
Vector3 p;
|
||||
p.x = s * Sin(PI * u);
|
||||
p.y = r * Sin(0.5f * PI * v);
|
||||
p.z = s * Cos(PI * u);
|
||||
return p;
|
||||
}
|
||||
|
||||
public static Vector3 Torus(float u, float v, float t)
|
||||
{
|
||||
float r1 = 0.7f + 0.1f * Sin(PI * (6f * u + 0.5f * t));
|
||||
float r2 = 0.15f + 0.05f * Sin(PI * (8f * u + 4f * v + 2f * t));
|
||||
float s = r1 + r2 * Cos(PI * v);
|
||||
Vector3 p;
|
||||
p.x = s * Sin(PI * u);
|
||||
p.y = r2 * Sin(PI * v);
|
||||
p.z = s * Cos(PI * u);
|
||||
return p;
|
||||
}
|
||||
}
|
11
Assets/Script/FunctionLibrary.cs.meta
Normal file
11
Assets/Script/FunctionLibrary.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5d4ebe0e863dfd49b58925b59880bf7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
48
Assets/Script/Graph.cs
Normal file
48
Assets/Script/Graph.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Graph : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
Transform pointPrefab = default;
|
||||
|
||||
[SerializeField, Range(10, 100)]
|
||||
int resolution = 10;
|
||||
|
||||
[SerializeField]
|
||||
FunctionLibrary.FunctionName function = default;
|
||||
|
||||
Transform[] points;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
var step = 2f / resolution;
|
||||
var scale = Vector3.one * step;
|
||||
points = new Transform[resolution * resolution];
|
||||
for (int i = 0; i < points.Length; i++)
|
||||
{
|
||||
Transform point = Instantiate(pointPrefab);
|
||||
point.localScale = scale;
|
||||
point.SetParent(transform, false);
|
||||
points[i] = point;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
FunctionLibrary.Function f = FunctionLibrary.GetFunction(function);
|
||||
var time = Time.time;
|
||||
var step = 2.0f / resolution;
|
||||
var v = 0.5f * step - 1f;
|
||||
for (int i = 0, x = 0, z = 0; i < points.Length; i++, x++)
|
||||
{
|
||||
if (x == resolution)
|
||||
{
|
||||
x = 0;
|
||||
z += 1;
|
||||
v = (z + 0.5f) * step - 1f;
|
||||
}
|
||||
float u = (x + 0.5f) * step - 1f;
|
||||
points[i].localPosition = f(u, v, time);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Script/Graph.cs.meta
Normal file
11
Assets/Script/Graph.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14cd06bfa92d53e4ebc0cde4a0e39db8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user