Hi
I just read an article about how to make grass, now I want to step forward, let other objects affect the grass.
Here is the original article Unity Grass Shader Tutorial
And here is my C# code to pass positions to the shader.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveInterActive : MonoBehaviour
{
// Start is called before the first frame update
public GameObject grassModel;
public Material grassMat;
public Transform[] obstacles;
private Vector4[] obstaclePositions = new Vector4[100];
private MeshFilter meshFilter;
private MeshRenderer meshRenderer;
void Start()
{
meshFilter = gameObject.AddComponent<MeshFilter> () as MeshFilter;
meshRenderer = gameObject.AddComponent<MeshRenderer> () as MeshRenderer;
meshRenderer.material = grassMat;
}
// Update is called once per frame
void Update()
{
for (int n = 0; n < obstacles.Length; n++) {
obstaclePositions [n] = obstacles [n].position;
Debug.Log(obstacles [n].position);
}
Shader.SetGlobalFloat("_PositionArray", obstacles.Length);
Shader.SetGlobalVectorArray("_ObstaclePositions", obstaclePositions);
//Debug.Log(obstaclePositions [0]);
}
}
And the debug tells me, I go the positions correctly at c# Part.
But when I try to get the distance through this:
float4 wodpos = mul(unity_ObjectToWorld, pos);
for (int n = 0; n < _PositionArray; n++){
float dis = distance(wodpos,_ObstaclePositions[n]);
(dis<0)?(height=0):(height=1);
};
....
triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight,segmentForward, float2(0, t), transformationMatrixFacing));
triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward,float2(1, t), transformationMatrixFacing));
triStream.Append(GenerateGrassVertex(pos, 0, height,forward, float2(0.5, 1), transformationMatrix));
I assume that I need to convert the vertex pos to world cord, but I am not sure. Did I miss anything?