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

终于,我们可以在播放模式下改变函数图形了!

虽然每次选择另一个函数的时候都要重新创建图形,其他时候基本不用改变。所以不用在每次更新的时候都计算点。然而,如果给函数加上时间就不一样了。例如,改变一下正弦函数,变为f(x) = (sin(2πx + Δ) + 1) / 2,Δ等于播放时间。这将形成一个缓慢的正弦波动画。这里是完整的脚本。

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
using UnityEngine;
 
public class Grapher1 : MonoBehaviour {
 
public enum FunctionOption {
Linear,
Exponential,
Parabola,
Sine
}
 
private delegate float FunctionDelegate (float x);
private static FunctionDelegate[] functionDelegates = {
Linear,
Exponential,
Parabola,
Sine
};
 
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];
float increment = 1f / (resolution - 1);
for (int i = 0; i < resolution; i++) {
float x = i * increment;
points[i].position = new Vector3(x, 0f, 0f);
points[i].color = new Color(x, 0f, 0f);
points[i].size = 0.1f;
}
}
 
void Update () {
if (currentResolution != resolution || points == null) {
CreatePoints();
}
FunctionDelegate f = functionDelegates[(int)function];
for (int i = 0; i < resolution; i++) {
Vector3 p = points[i].position;
p.y = f(p.x);
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 (float x) {
return x;
}
 
private static float Exponential (float x) {
return x * x;
}
 
private static float Parabola (float x){
x = 2f * x - 1f;
return x * x;
}
 
private static float Sine (float x){
return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x + Time.timeSinceLevelLoad);
}
}

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

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