using UnityEngine;
using System.Collections;
public class Graphing : MonoBehaviour {
public int resolution;
private int currentResolution;
private ParticleSystem.Particle[] points;
// Graph position of 800 * 600
private float graphX;
private float graphY;
private float graphZ;
// Graph properties.
private float graphW;
private float graphH;
private float graphF;
// Sine position.
private float sinePos;
// Use this for initialization
void Start () {
// Graph posiotion of 800 * 600
graphX = 2.7f;
graphY = 0.4f;
graphZ = 0.0f;
// Edit initial position
transform.position = new Vector3(graphX, graphY, graphZ);
CreatePoints();
}
// Update is called once per frame
void Update () {
// Graph properties
graphW = GUI_Component.wHSliderValue; // Suitable value is 1.0f
graphH = GUI_Component.hHSliderValue; // Suitable value is 20.0f
graphF = GUI_Component.fHSliderValue; // Suitable value is 3.0f
// Default graph resolution
resolution = (int)GUI_Component.gHSliderValue;
if(currentResolution != resolution){
CreatePoints ();
}
for(int i = 0; i < resolution; i++){
Vector3 p = points[i].position;
p.y = Sine(p.x);
points[i].position = p;
}
particleSystem.SetParticles(points, points.Length);
}
private void CreatePoints(){
if(resolution < 2){
resolution = 2;
}
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, 0.0f, 0.0f);
points[i].color = new Color(255.0f, 255.0f, 255.0f);
points[i].size = 0.1f;
}
}
private float Sine(float x){
float formula = Mathf.PI * x * graphW / graphF + Time.timeSinceLevelLoad;
float sinFormula = Mathf.Sin(formula);
if(sinFormula > 0){
sinFormula = 1;
}
else if(sinFormula < 0){
sinFormula = -1;
}
return (sinFormula / graphH);
}
}