Skip to content

Commit

Permalink
remove per grass seed. Now append ID instead of posWS float3
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinLeung-NiloCat committed Jul 28, 2020
1 parent e7a1e1d commit a7f7074
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@

half _RandomNormal;

StructuredBuffer<float4> _TransformBuffer;
StructuredBuffer<float3> _AllInstancesTransformBuffer;
StructuredBuffer<uint> _VisibleInstanceOnlyTransformIDBuffer;
CBUFFER_END

sampler2D _GrassBendingRT;
Expand Down Expand Up @@ -140,13 +141,9 @@
{
Varyings OUT;

//bufferData.xyz is posWS inside ComputeBuffer
//bufferData.w is scaleWS inside ComputeBuffer
float4 bufferData = _TransformBuffer[instanceID];
float3 perGrassPivotPosWS = bufferData.xyz;//we transform to posWS in C# now
float seed01 = bufferData.w;
float seedNegative1ToPositive1 = seed01 * 2 - 1;
float perGrassHeight = lerp(2,5,seed01) * _GrassHeight;
float3 perGrassPivotPosWS = _AllInstancesTransformBuffer[_VisibleInstanceOnlyTransformIDBuffer[instanceID]];//we pre-transform to posWS in C# now

float perGrassHeight = lerp(2,5,(sin(perGrassPivotPosWS.x*23.4643 + perGrassPivotPosWS.z) * 0.45 + 0.55)) * _GrassHeight;

//get "is grass stepped" data(bending) from RT
float2 grassBendingUV = ((perGrassPivotPosWS.xz - _PivotPosWS.xz) / _BoundSize) * 0.5 + 0.5;//claculate where is this grass inside bound (can optimize to 2 MAD)
Expand All @@ -159,7 +156,7 @@
float3 cameraTransformForwardWS = -UNITY_MATRIX_V[2].xyz;//UNITY_MATRIX_V[2].xyz == -1 * world space camera Forward unit vector

//Expand Billboard (billboard Left+right)
float3 positionOS = IN.positionOS.x * cameraTransformRightWS * _GrassWidth;
float3 positionOS = IN.positionOS.x * cameraTransformRightWS * _GrassWidth * (sin(perGrassPivotPosWS.x*95.4643 + perGrassPivotPosWS.z) * 0.45 + 0.55);//random width from posXZ, min 0.1

//Expand Billboard (billboard Up)
positionOS += IN.positionOS.y * cameraTransformUpWS;
Expand Down Expand Up @@ -206,7 +203,7 @@
#else
mainLight = GetMainLight();
#endif
half3 randomAddToN = (_RandomNormal* seedNegative1ToPositive1 + wind * -0.25) * cameraTransformRightWS;//random normal per grass
half3 randomAddToN = (_RandomNormal* sin(perGrassPivotPosWS.x * 82.32523 + perGrassPivotPosWS.z) + wind * -0.25) * cameraTransformRightWS;//random normal per grass
//default grass's normal is pointing 100% upward in world space, it is an important but simple grass normal trick
//-apply random to normal else lighting is too uniform
//-apply cameraTransformForwardWS to normal because grass is billboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void LateUpdate()
cullingComputeShader.SetMatrix("_VPMatrix", vp);
cullingComputeShader.SetFloat("_MaxDrawDistance", drawDistance);
cullingComputeShader.SetBuffer(0, "_AllInstancesTransformBuffer", allInstanceTransformBuffer);
cullingComputeShader.SetBuffer(0, "_VisibleInstanceOnlyTransformBuffer", visibleInstanceOnlyTransformBuffer);
cullingComputeShader.SetBuffer(0, "_VisibleInstanceOnlyTransformIDBuffer", visibleInstanceOnlyTransformBuffer);
cullingComputeShader.Dispatch(0, Mathf.CeilToInt(instanceCount/1024), 1, 1);
ComputeBuffer.CopyCount(visibleInstanceOnlyTransformBuffer, argsBuffer, 4);

Expand Down Expand Up @@ -122,37 +122,41 @@ void UpdateAllInstanceTransformBufferIfNeeded()
///////////////////////////
if (allInstanceTransformBuffer != null)
allInstanceTransformBuffer.Release();
allInstanceTransformBuffer = new ComputeBuffer(instanceCount, sizeof(float)*4); //float4 per grass
allInstanceTransformBuffer = new ComputeBuffer(instanceCount, sizeof(float)*3); //float3 posWS only, per grass

if (visibleInstanceOnlyTransformBuffer != null)
visibleInstanceOnlyTransformBuffer.Release();
visibleInstanceOnlyTransformBuffer = new ComputeBuffer(instanceCount, sizeof(float) * 4, ComputeBufferType.Append);
visibleInstanceOnlyTransformBuffer = new ComputeBuffer(instanceCount, sizeof(uint), ComputeBufferType.Append); //uint only, per visible grass

//keep grass visual the same
UnityEngine.Random.InitState(123);

//spawn grass inside gizmo cube
Vector4[] positions = new Vector4[instanceCount];
Vector3[] positions = new Vector3[instanceCount];
for (int i = 0; i < instanceCount; i++)
{
Vector3 pos = Vector3.zero;

//local pos (can define any local pos here, random is just an example)
//can define any posWS in this section, random is just an example
//TODO: allow API call to set posWS
//===================================================
//local pos
pos.x = UnityEngine.Random.Range(-1f, 1f);
pos.y = 0;
pos.z = UnityEngine.Random.Range(-1f, 1f);

//transform to posWS in C#
pos.x *= transform.lossyScale.x;
pos.z *= transform.lossyScale.z;
pos += transform.position;
//===================================================

float seed = UnityEngine.Random.Range(0f, 1f);

positions[i] = new Vector4(pos.x,pos.y,pos.z, seed);
positions[i] = new Vector3(pos.x,pos.y,pos.z);
}

allInstanceTransformBuffer.SetData(positions);
instanceMaterial.SetBuffer("_TransformBuffer", visibleInstanceOnlyTransformBuffer);
instanceMaterial.SetBuffer("_AllInstancesTransformBuffer", allInstanceTransformBuffer);
instanceMaterial.SetBuffer("_VisibleInstanceOnlyTransformIDBuffer", visibleInstanceOnlyTransformBuffer);

///////////////////////////
// Indirect args buffer
Expand Down

0 comments on commit a7f7074

Please sign in to comment.