Skip to content

Commit

Permalink
[Grass] adds grass bending when interacting with player (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabFrancon authored Oct 19, 2023
1 parent 262b8fd commit 9e3cd21
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Assets/Materials/CubeGround/GrassCube.mat
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Material:
m_Floats:
- _AlphaClip: 0
- _AlphaToMask: 0
- _BendInfluenceRadius: 0.15
- _BendIntensity: 5
- _BladeBendCurve: 2.07
- _BladeBendDistance: 0.093
- _BladeBendVariation: 0.368
Expand Down
26 changes: 24 additions & 2 deletions Assets/Scenes/ForskarFredag.unity
Original file line number Diff line number Diff line change
Expand Up @@ -2937,8 +2937,18 @@ PrefabInstance:
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 3121695916486385943, guid: 77a7f27b58093f744a46d44c4ef7538b,
type: 3}
insertIndex: 5
addedObject: {fileID: 1933055332}
m_SourcePrefab: {fileID: 100100000, guid: 77a7f27b58093f744a46d44c4ef7538b, type: 3}
--- !u!1 &769657837 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 3121695916486385943, guid: 77a7f27b58093f744a46d44c4ef7538b,
type: 3}
m_PrefabInstance: {fileID: 769657836}
m_PrefabAsset: {fileID: 0}
--- !u!1 &775372493
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -5192,12 +5202,24 @@ MonoBehaviour:
type: 3}
m_PrefabInstance: {fileID: 769657836}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_GameObject: {fileID: 769657837}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: abb3402c4c18a074792eaf971efb46b1, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1933055332
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 769657837}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1d8e76b2a971b6943a392515a3f2d150, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1933096941
GameObject:
m_ObjectHideFlags: 0
Expand Down
13 changes: 13 additions & 0 deletions Assets/Scripts/Player/PlayerPositionUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using UnityEngine;

public class PlayerPositionUpdater : MonoBehaviour
{
void Update()
{
// Get the player's world position
Vector3 playerWorldPosition = transform.position;

// Update the _PlayerPosition uniform accessible from all shaders
Shader.SetGlobalVector("_PlayerPosition", playerWorldPosition);
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Player/PlayerPositionUpdater.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions Assets/Shaders/URP_ProceduralGrass.shader
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Shader "Universal Render Pipeline/Custom/ProceduralGrass"
_WindFrequency("Wind Pulse Frequency", Range(0, 1)) = 0.01
[NoScaleOffset]_WindMap("Wind Offset Map", 2D) = "bump" {}

[Space(10)][Header(# Player displacement)][Space(10)]
_BendIntensity("Bend Intensity", Range(0, 10)) = 3.0
_BendInfluenceRadius("Bend Influence Radius", Range(0, 0.5)) = 0.15

[Space(10)][Header(# Lighting)][Space(10)]
[Toggle(COMPUTE_LIGHTING)] _ComputeLighting("Compute Lighting", Float) = 0
_ShadowIntensity("Shadow Intensity", Range(0, 1)) = 1.0
Expand Down Expand Up @@ -64,6 +68,10 @@ Shader "Universal Render Pipeline/Custom/ProceduralGrass"
#define UNITY_TWO_PI 6.28318530718f
#define BLADE_SEGMENTS 4

// -------------------------------------
// Global parameters
uniform float3 _PlayerPosition;

// -------------------------------------
// Material textures
sampler2D _BladeTexture;
Expand Down Expand Up @@ -94,6 +102,9 @@ Shader "Universal Render Pipeline/Custom/ProceduralGrass"
float4 _WindMap_ST;
float4 _WindVelocity;
float _WindFrequency;

float _BendIntensity;
float _BendInfluenceRadius;
CBUFFER_END

// -------------------------------------
Expand Down Expand Up @@ -358,9 +369,15 @@ Shader "Universal Render Pipeline/Custom/ProceduralGrass"
float3 windAxis = normalize(float3(windSample.x, windSample.y, 0));
float3x3 windMatrix = BuildRotationMatrix(UNITY_PI * windSample, windAxis);

// Apply player displacement.
float3 playerToBladeVector = origin - mul(unity_WorldToObject, _PlayerPosition);
float bendIntensity = _BendIntensity * smoothstep(_BendInfluenceRadius, 0.f, length(playerToBladeVector));
float3 bendAxis = normalize(float3(dot(playerToBladeVector, tangent), dot(playerToBladeVector, bitangent), 0));
float3x3 playerBendMatrix = BuildRotationMatrix(bendIntensity * UNITY_PI * 0.5f, bendAxis);

// Transform the grass blades to the correct tangent space.
float3x3 baseTransformationMatrix = mul(tangentToLocal, randRotMatrix);
float3x3 tipTransformationMatrix = mul(mul(mul(tangentToLocal, windMatrix), randBendMatrix), randRotMatrix);
float3x3 tipTransformationMatrix = mul(mul(mul(mul(tangentToLocal, windMatrix), playerBendMatrix), randBendMatrix), randRotMatrix);

float falloff = smoothstep(_GrassThreshold, _GrassThreshold + _GrassFalloff, grassVisibility);
float width = lerp(_BladeWidthMin, _BladeWidthMax, GenerateRandom(origin.xzy) * falloff) * _ShadowIntensity;
Expand Down Expand Up @@ -729,9 +746,15 @@ Shader "Universal Render Pipeline/Custom/ProceduralGrass"
float3 windAxis = normalize(float3(windSample.x, windSample.y, 0));
float3x3 windMatrix = BuildRotationMatrix(UNITY_PI * windSample, windAxis);

// Apply player displacement.
float3 playerToBladeVector = origin - mul(unity_WorldToObject, _PlayerPosition);
float bendIntensity = _BendIntensity * smoothstep(_BendInfluenceRadius, 0.f, length(playerToBladeVector));
float3 bendAxis = normalize(float3(dot(playerToBladeVector, tangent), dot(playerToBladeVector, bitangent), 0));
float3x3 playerBendMatrix = BuildRotationMatrix(bendIntensity * UNITY_PI * 0.5f, bendAxis);

// Transform the grass blades to the correct tangent space.
float3x3 baseTransformationMatrix = mul(tangentToLocal, randRotMatrix);
float3x3 tipTransformationMatrix = mul(mul(mul(tangentToLocal, windMatrix), randBendMatrix), randRotMatrix);
float3x3 tipTransformationMatrix = mul(mul(mul(mul(tangentToLocal, windMatrix), playerBendMatrix), randBendMatrix), randRotMatrix);

float falloff = smoothstep(_GrassThreshold, _GrassThreshold + _GrassFalloff, grassVisibility);
float width = lerp(_BladeWidthMin, _BladeWidthMax, GenerateRandom(origin.xzy) * falloff);
Expand Down

0 comments on commit 9e3cd21

Please sign in to comment.