Files
kaiju/Assets/CubeController.cs
T
2022-04-05 14:04:05 +10:00

116 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CubeController : TripsitterAve {
public GameObject prefab;
public float minSize = 2;
public float maxSize = 4;
public Material cubeMatLit;
private Texture2D texture;
private Color[] colourCache;
void Start() {
CreateCubes(0f, 0f, 0f, 100f, 50f, 100f);
}
void Update() {
UpdateTexture();
}
private void InitialiseTexture() {
Debug.Log("Reinitialising spectrum texture...");
texture = new Texture2D(SpectrumSize, 1, TextureFormat.RFloat, false);
colourCache = new Color[SpectrumSize];
texture.name = $"AudioSpectrumData{SpectrumSize}";
}
private void UpdateTexture() {
// init
if (texture == null) InitialiseTexture();
var spectrumData = GetSpectrumData();
for (int i = 0; i < SpectrumSize; i++) {
colourCache[i] = new Color(spectrumData[i], 0, 0, 0);
}
texture.SetPixels(colourCache);
texture.Apply();
cubeMatLit.SetTexture("_AudioTexture", texture);
}
// bool IsLeft(GameObject cube) {
// float up = cube.transform.up;
// return up < yBottom && up > yTop;
// }
// bool IsRight(GameObject cube) {
// float up = cube.transform.up;
// return up < yBottom && up > yTop;
// }
// bool IsBottom(GameObject cube) {
// float up = cube.transform.up;
// return up < yBottom && up > yTop;
// }
// bool IsTop(GameObject cube) {
// float up = cube.transform.up;
// return up < yBottom && up > yTop;
// }
void CreateCubes(float currentX, float currentZ, float currentY, float xWidth, float yWidth, float zWidth) {
float xMargin = 10;
float yMargin = 5;
float zMargin = 10;
float xLeft = xMargin;
float xRight = xWidth - xMargin;
float yBottom = yMargin;
float yTop = yWidth - yMargin;
float zBack = zWidth - zMargin;
// int i = 0;
while (currentY < yWidth) {
while (currentZ < zWidth) {
while (currentX < xWidth) {
bool ok = currentX < xLeft ||
currentX > xRight ||
currentY < yBottom ||
currentY > yTop ||
currentZ > zBack;
float size = Random.Range(minSize, maxSize);
if (ok) {
GameObject cube = GameObject.Instantiate(prefab);
cube.transform.parent = gameObject.transform;
cube.transform.localScale *= size;
float x = currentX + (size / 2f);
float y = currentY + (size / 2f);
if (currentY > yTop) y = currentY - (size / 2f);
float z = currentZ + (size / 2f);
if (currentZ > zBack) z = currentZ - (size / 2f);
cube.transform.Translate(x, y, z);
if (Random.Range(0, 100) > 70) cube.GetComponent<Renderer>().material = cubeMatLit;
}
currentX = currentX + size;
}
currentX = 0;
currentZ = currentZ + maxSize;
}
currentZ = 0;
currentY = currentY + maxSize;
}
}
}