Created
May 5, 2014 13:10
-
-
Save Ushio/adc37bda02f0bc777b28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class MainController : MonoBehaviour { | |
public Transform ConePrefab; | |
private SimplexNoiseGenerator _noiseGenerator = new SimplexNoiseGenerator(); | |
private List<Transform> _objects = new List<Transform>(); | |
private static readonly float CameraHeight = 5.0f; | |
private Transform _mouseObject; | |
void Start () { | |
int N = 50; | |
for(int i = 0 ; i < N ; ++i) | |
{ | |
var hsvColor = new HSBColor(remap(i, 0, N - 1, 0.0f, 1.0f), 1.0f, 1.0f); | |
var newObject = GameObject.Instantiate(ConePrefab) as Transform; | |
var coneRenderer = newObject.FindChild("Cone").GetComponent<Renderer>(); | |
coneRenderer.material.color = hsvColor.ToColor(); | |
_objects.Add(newObject); | |
} | |
_mouseObject = GameObject.Instantiate(ConePrefab) as Transform; | |
var mouseConeRenderer = _mouseObject.FindChild("Cone").GetComponent<Renderer>(); | |
mouseConeRenderer.material.color = new Color(0.5f, 0.5f, 0.5f); | |
} | |
float remap(float value, float inputMin, float inputMax, float outputMin, float outputMax) | |
{ | |
return (value - inputMin) * ((outputMax - outputMin) / (inputMax - inputMin)) + outputMin; | |
} | |
// Update is called once per frame | |
void Update () { | |
var aspect = (float)Screen.width / (float)Screen.height; | |
var cameraWidth = CameraHeight * aspect; | |
var elapsedTime = Time.time * 0.1f; | |
for(int i = 0 ; i < _objects.Count ; ++i) | |
{ | |
var o = _objects[i]; | |
float xNoise = _noiseGenerator.noise(i, elapsedTime, 100.0f); | |
float yNoise = _noiseGenerator.noise(i, elapsedTime, 220.0f); | |
float x = remap(xNoise, -1.0f, 1.0f, -cameraWidth * 4.0f, cameraWidth * 4.0f); | |
float offsetx = remap(i, 0, _objects.Count - 1, -1.0f, 1.0f); | |
float y = remap(yNoise, -1.0f, 1.0f, -CameraHeight * 4.0f, CameraHeight * 4.0f); | |
var p = o.position; | |
p.x = x + offsetx; | |
p.y = y; | |
o.position = p; | |
} | |
var mouseP = _mouseObject.position; | |
mouseP.x = remap (Input.mousePosition.x, 0, Screen.width, -cameraWidth, cameraWidth); | |
mouseP.y = remap (Input.mousePosition.y, 0, Screen.height, -CameraHeight, CameraHeight); | |
_mouseObject.position = mouseP; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment