Skip to content

Instantly share code, notes, and snippets.

@jade-itworkswhy
Created July 24, 2019 08:44
Show Gist options
  • Save jade-itworkswhy/b4f2133a122be460f394d4c8546ad9d0 to your computer and use it in GitHub Desktop.
Save jade-itworkswhy/b4f2133a122be460f394d4c8546ad9d0 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VertexObjectSpawner : MonoBehaviour
{
#region Variables
public GameObject[] objects;
public Mesh mesh;
public GameObject spawnHolder;
public Transform offsetter;
public float objectSpawnSpeed = 1f;
public Vector3 inverseRotation;
public bool coroutined = true;
public KeyCode togglePhysicsKey = KeyCode.Space;
public KeyCode toggleCoroutinedPhysicssKey = KeyCode.T;
public bool correctRotation = true;
public bool toggleWithCoroutine = false;
public bool toggleWithCoroutineReverse = false;
#endregion
#region Texture Color Parsing
public GameObject model;
public Texture2D sourceTex;
public float warpFactor = 1.0F;
private Texture2D destTex;
private Color[] destPix;
private Color[] vertColor;
#endregion
private Quaternion objectRotation;
private WaitForSeconds wait;
private List<GameObject> spawned = new List<GameObject>();
private void Awake(){
GetTextureVertexColor();
}
private void Start()
{
if (!spawnHolder)
{
spawnHolder = new GameObject("Spawn Holder");
}
wait = new WaitForSeconds(objectSpawnSpeed);
if (coroutined)
{
StartCoroutine(Spawn());
}
else
{
Spawn01();
}
}
private void Update()
{
if (Input.GetKeyDown(togglePhysicsKey))
{
if (toggleWithCoroutine)
{
StartCoroutine(TogglePhysics01(!spawned[spawned.Count - 1].GetComponent<Rigidbody>().isKinematic, toggleWithCoroutineReverse));
}
else
{
TogglePhysics(!spawned[spawned.Count - 1].GetComponent<Rigidbody>().isKinematic);
}
}
if (Input.GetKeyDown(toggleCoroutinedPhysicssKey))
{
toggleWithCoroutineReverse = !toggleWithCoroutineReverse;
toggleWithCoroutine = !toggleWithCoroutine;
}
}
private IEnumerator Spawn()
{
objectRotation = offsetter.rotation;
for (int i = 0; i < mesh.vertexCount; i++)
{
GameObject spawn = Instantiate(objects[Random.Range(0, objects.Length)], mesh.vertices[i] + offsetter.position, Quaternion.Euler(inverseRotation), spawnHolder.transform);
spawn.GetComponent<Renderer>().sharedMaterial.color = vertColor[i];
spawned.Add(spawn);
yield return objectSpawnSpeed;
}
if (correctRotation)
{
offsetter.rotation = Quaternion.identity;
}
}
private void Spawn01()
{
objectRotation = offsetter.rotation;
for (int i = 0; i < mesh.vertexCount; i++)
{
GameObject spawn = Instantiate(objects[Random.Range(0, objects.Length)], mesh.vertices[i] + offsetter.position, Quaternion.Euler(inverseRotation), spawnHolder.transform);
spawn.GetComponent<Renderer>().sharedMaterial.color = vertColor[i];
spawned.Add(spawn);
}
if (correctRotation)
{
offsetter.rotation = Quaternion.identity;
}
}
private void TogglePhysics(bool kinematic)
{
for (int i = 0; i < spawned.Count; i++)
{
spawned[i].GetComponent<Rigidbody>().isKinematic = kinematic;
}
}
private IEnumerator TogglePhysics01(bool kinematic, bool reverse)
{
if (reverse)
{
for (int i = spawned.Count - 1; i > 0; i--)
{
spawned[i].GetComponent<Rigidbody>().isKinematic = kinematic;
yield return objectSpawnSpeed;
}
}
else
{
for (int i = 0; i < spawned.Count; i++)
{
spawned[i].GetComponent<Rigidbody>().isKinematic = kinematic;
yield return objectSpawnSpeed;
}
}
}
public void GetTextureVertexColor()
{
Vector3[] vertices = mesh.vertices;
Vector2[] uvs = new Vector2[vertices.Length];
for(int i =0; i< uvs.Length; i++){
uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
}
destTex = new Texture2D(sourceTex.width, sourceTex.height);
destPix = new Color[destTex.width * destTex.height];
vertColor = new Color[vertices.Length];
// Trying to map the uv color to instantiated cubes.....
int y = 0;
while (y < destTex.height / Mathf.Sqrt(uvs.Length))
{
int x = 0;
while (x < destTex.width / Mathf.Sqrt(uvs.Length))
{
// float xFrac = x * uvs[y * destTex.width + x].x / (destTex.width - 1);
// float yFrac = y * uvs[y * destTex.width + x].y / (destTex.height - 1);
float xFrac = x * uvs[(x+1)*(y+1)].x / (destTex.width - 1);
float yFrac = y * uvs[(x+1)*(y+1)].y / (destTex.height - 1);
// float warpXFrac = Mathf.Pow(xFrac, warpFactor);
// float warpYFrac = Mathf.Pow(yFrac, warpFactor);
// destPix[y * destTex.width + x] = sourceTex.GetPixelBilinear(xFrac, yFrac);
vertColor[y * destTex.width + x] = sourceTex.GetPixelBilinear(xFrac, yFrac);
x++;
}
y++;
}
Mesh modelMesh = model.GetComponent<MeshFilter>().mesh;
Color32[] colors = new Color32[vertices.Length];
int j = 0;
while (j < vertices.Length) {
colors[j] = Color.Lerp(Color.red, Color.green, vertices[j].y);
j++;
}
modelMesh.colors32 = colors;
Debug.Log(uvs.Length);
destTex.SetPixels(destPix);
Debug.Log(vertColor.Length);
Debug.Log(destTex.height / Mathf.Sqrt(uvs.Length) * destTex.width / Mathf.Sqrt(uvs.Length));
destTex.Apply();
// GetComponent<Renderer>().material.mainTexture = destTex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment