Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Pass a object position to shader and calculate the distance

Discussion in 'Shaders' started by Isaac_1993, Jun 3, 2020.

  1. Isaac_1993

    Isaac_1993

    Joined:
    May 1, 2020
    Posts:
    7
    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 https://roystan.net/articles/grass-shader.html

    And here is my C# code to pass positions to the shader.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveInterActive : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public GameObject grassModel;
    9.     public Material grassMat;
    10.     public Transform[] obstacles;
    11.  
    12.     private Vector4[] obstaclePositions = new Vector4[100];
    13.     private MeshFilter meshFilter;
    14.     private MeshRenderer meshRenderer;
    15.     void Start()
    16.     {
    17.         meshFilter = gameObject.AddComponent<MeshFilter> () as MeshFilter;
    18.         meshRenderer = gameObject.AddComponent<MeshRenderer> () as MeshRenderer;
    19.         meshRenderer.material = grassMat;
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.     for (int n = 0; n < obstacles.Length; n++) {
    27.             obstaclePositions [n] = obstacles [n].position;
    28.             Debug.Log(obstacles [n].position);
    29.         }
    30.  
    31.     Shader.SetGlobalFloat("_PositionArray", obstacles.Length);
    32.     Shader.SetGlobalVectorArray("_ObstaclePositions", obstaclePositions);
    33.     //Debug.Log(obstaclePositions [0]);
    34.  
    35.     }
    36.    
    37.  
    38. }
    39.  
    And the debug tells me, I go the positions correctly at c# Part.

    But when I try to get the distance through this:

    Code (CSharp):
    1. float4 wodpos = mul(unity_ObjectToWorld, pos);
    2. for (int n = 0; n < _PositionArray; n++){
    3.              float dis =  distance(wodpos,_ObstaclePositions[n]);
    4. (dis<0)?(height=0):(height=1);
    5.             };
    6. ....
    7. triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight,segmentForward, float2(0, t), transformationMatrixFacing));
    8. triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward,float2(1, t), transformationMatrixFacing));
    9. triStream.Append(GenerateGrassVertex(pos, 0, height,forward, float2(0.5, 1), transformationMatrix));
    10.  
    11.  
    I assume that I need to convert the vertex pos to world cord, but I am not sure. Did I miss anything?
     
    Last edited: Jun 3, 2020
  2. Isaac_1993

    Isaac_1993

    Joined:
    May 1, 2020
    Posts:
    7
    UPDATE
    i pass the dis to color, and I found it works. So the problem probably is
    Code (CSharp):
    1. dis = dis - _EffectRadius;
    2. dis>0?(height= (rand(pos.zyx) * 2 - 1) * _BladeHeightRandom + _BladeHeight):(height=0);
    always goes to the false condition value? Did anybody know why?
     

    Attached Files:

  3. Isaac_1993

    Isaac_1993

    Joined:
    May 1, 2020
    Posts:
    7
    Is this because I work with geometry shader?
     
  4. Isaac_1993

    Isaac_1993

    Joined:
    May 1, 2020
    Posts:
    7
    I got this, problem solved