- Home /
Recolor Grass
Hey, I'm having some tough time adding grass to my game. I wanted each individual grass initial color to be the same as the ground's texture color at the exact spot. I managed doing it - making a grass particle system and changing ParticleSystem.Particle.color for each particle (don't know if its the most efficient way...).
The problem is that I want each individual grass particle (or maybe I'll be better off using detail mesh instead?) to change its color to red whenever a blood splatter decal touches it. Now, doing a loop through all the grass particles to determine which one is touching the decal is really going to cost performance.
Can you suggest any sufficient (or possible) way? (I'm guessing you could somehow do it using shaders and grass detail mesh, though I know nothing about them.)
Here's my current code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Grass_Particles_controller : MonoBehaviour {
public float planeWidth;
public float planeHeight;
[Space(5)]
public float rotationMin;
public float rotationMax;
[Space(5)]
public float strechMin;
public float stretchMax;
[Space(5)]
public int grassAmount;
[Space(10)]
[Header("Texture")]
public GameObject texturePlane;
ParticleSystem ps;
ParticleSystem.Particle[] grassList;
bool toSetParticles = false;
int texW,texH;
Color32[] colorGrid;
void Start()
{
gameObject.GetComponent<ParticleSystemRenderer>().pivot = new Vector3(0f,-0.5f,0f);
ps = gameObject.GetComponent<ParticleSystem>();
GetColorGrid();
GenerateGrass();
}
void Update()
{
if(toSetParticles){ ps.SetParticles(grassList,grassAmount); toSetParticles=false;}
}
void GenerateGrass()
{
grassList = new ParticleSystem.Particle[grassAmount];
ps.Clear();
for(int i = 0; i<grassAmount; i++)
{
Vector3 gPosition = new Vector3(Random.Range(-(planeWidth/2),planeWidth/2),0,Random.Range(-(planeHeight/2),planeHeight/2));
grassList[i].size = Random.Range(strechMin,stretchMax);
grassList[i].position = gPosition;
grassList[i].rotation = Random.Range(rotationMin,rotationMax);
Color32 newColor = GetPointColorFromPlane(gPosition);
grassList[i].color = (Color)newColor;
}
toSetParticles = true;
}
void RecalculateColors()
{
ps.Clear();
for(int i = 0; i<grassAmount; i++)
{
Color32 newColor = GetPointColorFromPlane(grassList[i].position);
grassList[i].color = (Color)newColor;
}
toSetParticles = true;
}
void GetColorGrid()
{
Texture2D planeTexture = texturePlane.GetComponent<Renderer>().material.mainTexture as Texture2D;
texW = planeTexture.width;
texH = planeTexture.height;
colorGrid = planeTexture.GetPixels32();
}
Color32 GetPointColorFromPlane(Vector3 grassPosition)
{
RaycastHit[] hits;
hits = Physics.RaycastAll(grassPosition+Vector3.up*0.001f,Vector3.down);
Color32 target = Color.white;
foreach (RaycastHit hit in hits)
{
if(hit.transform.tag == "Floor")
{
Vector2 uvCoord = hit.textureCoord;
int texelX = Mathf.FloorToInt(uvCoord.x * texW);
int texely = Mathf.FloorToInt(uvCoord.y * texH);
target = colorGrid[texely*texW + texelX];
}
}
return target;
}
}
thanx!