AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > Unity3D

【Unity C#编程】图表 可视化数据:添加多个维度

51自学网 2014-05-24 http://www.51zixue.net

我们已经准备好在数学函数加入Z。例如:改变抛物线函数为f(x,z) = 1 - (2x - 1)2 × (2z - 1)2。我们还可以拓展一下正弦函数,分层多个正弦来得到一个复合的振荡效果。

1
2
3
4
5
6
7
8
9
10
11
12
private static float Parabola (Vector3 p, float t){
p.x += p.x - 1f;
p.z += p.z - 1f;
return 1f - p.x * p.x * p.z * p.z;
}
 
private static float Sine (Vector3 p, float t){
return 0.50f +
0.25f * Mathf.Sin(4f * Mathf.PI * p.x + 4f * t) * Mathf.Sin(2f * Mathf.PI * p.z + t) +
0.10f * Mathf.Cos(3f * Mathf.PI * p.x + 5f * t) * Mathf.Cos(5f * Mathf.PI * p.z + 3f * t) +
0.15f * Mathf.Sin(Mathf.PI * p.x + 0.6f * t);
}

更加有趣的抛物线和正弦函数

让我们以添加Ripple函数来完成Grapher2,这是从网格中心发出的单一正弦波。这里是完整的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using UnityEngine;
 
public class Grapher2 : MonoBehaviour {
 
public enum FunctionOption {
Linear,
Exponential,
Parabola,
Sine,
Ripple
}
 
private delegate float FunctionDelegate (Vector3 p, float t);
private static FunctionDelegate[] functionDelegates = {
Linear,
Exponential,
Parabola,
Sine,
Ripple
};
 
public FunctionOption function;
 
[Range(10, 100)]
public int resolution = 10;
 
private int currentResolution;
private ParticleSystem.Particle[] points;
 
private void CreatePoints () {
currentResolution = resolution;
points = new ParticleSystem.Particle[resolution * resolution];
float increment = 1f / (resolution - 1);
int i = 0;
for (int x = 0; x < resolution; x++) {
for (int z = 0; z < resolution; z++) {
Vector3 p = new Vector3(x * increment, 0f, z * increment);
points[i].position = p;
points[i].color = new Color(p.x, 0f, p.z);
points[i++].size = 0.1f;
}
}
}
 
void Update () {
if (currentResolution != resolution || points == null) {
CreatePoints();
}
FunctionDelegate f = functionDelegates[(int)function];
float t = Time.timeSinceLevelLoad;
for (int i = 0; i < points.Length; i++) {
Vector3 p = points[i].position;
p.y = f(p, t);
points[i].position = p;
Color c = points[i].color;
c.g = p.y;
points[i].color = c;
}
particleSystem.SetParticles(points, points.Length);
}
 
private static float Linear (Vector3 p, float t) {
return p.x;
}
 
private static float Exponential (Vector3 p, float t) {
return p.x * p.x;
}
 
private static float Parabola (Vector3 p, float t){
p.x = 2f * p.x - 1f;
p.z = 2f * p.z - 1f;
return 1f - p.x * p.x * p.z * p.z;
}
 
private static float Sine (Vector3 p, float t){
return 0.50f +
0.25f * Mathf.Sin(4 * Mathf.PI * p.x + 4 * t) * Mathf.Sin(2 * Mathf.PI * p.z + t) +
0.10f * Mathf.Cos(3 * Mathf.PI * p.x + 5 * t) * Mathf.Cos(5 * Mathf.PI * p.z + 3 * t) +
0.15f * Mathf.Sin(Mathf.PI * p.x + 0.6f * t);
}
 
private static float Ripple (Vector3 p, float t){
p.x -= 0.5f;
p.z -= 0.5f;
float squareRadius = p.x * p.x + p.z * p.z;
return 0.5f + Mathf.Sin(15f * Mathf.PI * squareRadius - 2f * t) / (2f + 100f * squareRadius);
}
}

建议使用电驴(eMule)下载分享的资源。

说明
:本教程来源互联网或网友分享或出版商宣传分享,仅为学习研究或媒体推广,51zixue.net不保证资料的完整性。
 
上一篇:【Unity C#编程】图表 可视化数据:创建图表  下一篇:【Unity C#编程】图表 可视化数据:3D展示