diff --git a/Assets/Materials/CubeGround/GrassCube.mat b/Assets/Materials/CubeGround/GrassCube.mat index ef01b69..10982a7 100644 --- a/Assets/Materials/CubeGround/GrassCube.mat +++ b/Assets/Materials/CubeGround/GrassCube.mat @@ -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 diff --git a/Assets/Scenes/ForskarFredag.unity b/Assets/Scenes/ForskarFredag.unity index cb668e2..8281816 100644 --- a/Assets/Scenes/ForskarFredag.unity +++ b/Assets/Scenes/ForskarFredag.unity @@ -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 @@ -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 diff --git a/Assets/Scripts/Player/PlayerPositionUpdater.cs b/Assets/Scripts/Player/PlayerPositionUpdater.cs new file mode 100644 index 0000000..8f292b3 --- /dev/null +++ b/Assets/Scripts/Player/PlayerPositionUpdater.cs @@ -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); + } +} diff --git a/Assets/Scripts/Player/PlayerPositionUpdater.cs.meta b/Assets/Scripts/Player/PlayerPositionUpdater.cs.meta new file mode 100644 index 0000000..d363758 --- /dev/null +++ b/Assets/Scripts/Player/PlayerPositionUpdater.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1d8e76b2a971b6943a392515a3f2d150 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Shaders/URP_ProceduralGrass.shader b/Assets/Shaders/URP_ProceduralGrass.shader index 037b0f3..1581203 100644 --- a/Assets/Shaders/URP_ProceduralGrass.shader +++ b/Assets/Shaders/URP_ProceduralGrass.shader @@ -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 @@ -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; @@ -94,6 +102,9 @@ Shader "Universal Render Pipeline/Custom/ProceduralGrass" float4 _WindMap_ST; float4 _WindVelocity; float _WindFrequency; + + float _BendIntensity; + float _BendInfluenceRadius; CBUFFER_END // ------------------------------------- @@ -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; @@ -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);