Skip to content

Egecekic/Unity-Grass-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Unity Procedural Grass System 🌾

A high-performance, noise-based procedural grass rendering system for Unity using GPU instancing and custom shaders. Inspired by Ghost of Tsushima's dynamic grass movement.

Unity Version License

✨ Features

  • 🌊 Dynamic Wind System - Noise-driven wind with traveling waves across the grass field
  • ⚡ GPU Instancing - Efficiently renders 10,000+ grass blades using Graphics.DrawMeshInstanced
  • 🎨 Organic Movement - Multi-layered noise (FBM) for natural, non-repetitive wind patterns
  • 🧭 Dynamic Wind Direction - Wind direction calculated per-blade using 2D Perlin-like noise
  • 🎛️ Fully Customizable - Complete control over density, size, wind strength, and appearance
  • 🎮 Game-Ready - Optimized for real-time rendering with automatic batching

📦 Components

Adsız video ‐ Clipchamp ile yapıldı (2)

GrassGenerator.cs

The main script that handles grass generation and rendering:

  • Procedural mesh generation for individual grass blades
  • Grid-based distribution with position randomization
  • Automatic batch rendering for performance optimization
  • Configurable density (default: 10,000 blades on 50x50 unit area)

GrassShaderAdvanced.shader

Custom vertex/fragment shader with advanced wind simulation:

  • Noise-based wind system with multiple layers
  • Dynamic wind direction using 2D Perlin-like noise
  • Height-based vertex displacement for realistic bending
  • Gradient coloring from base to tip
  • EaseIn curve shaping for smooth wind transitions

🚀 Quick Start

  1. Import Files

    • Add GrassGenerator.cs to your Unity project
    • Add GrassShaderAdvanced.shader to your project
  2. Setup Scene

    • Create an empty GameObject in your scene
    • Attach the GrassGenerator component
  3. Create Material

    • Create a new Material
    • Select Custom/GrassShaderAdvanced shader
    • Assign the material to the GrassGenerator script
  4. Configure & Run

    • Adjust parameters in the Inspector
    • Enter Play mode to see the grass

⚙️ Key Parameters

Density Settings

Parameter Default Description
Grass Blades 10000 Total number of grass blades
Num Grass X 100 Grid columns
Num Grass Z 100 Grid rows
Grass Patch Size 50 Area size in Unity units

Wind System

Parameter Default Description
Wind Strength 0.5 Overall wind intensity
Wind Speed 2.0 Time multiplier for wind animation
Wind Direction Scale 0.05 Larger values = smaller wind patterns
Wind Strength Scale 0.25 Variation frequency
Wind Max Lean Angle 1.25 Maximum blade bending
Wind Ease Power 2.0 Wind curve smoothness

Appearance

Parameter Default Description
Color (0.2, 0.8, 0.2) Base grass color
Tip Color (0.4, 1.0, 0.4) Grass tip color
Bend Amount 0.3 Static blade curvature

🎨 Recommended Presets

Ghost of Tsushima Style

Wind Strength: 0.4-0.6
Wind Speed: 1.5-2.5
Wind Direction Scale: 0.05
Wind Strength Scale: 0.25
Wind Max Lean Angle: 1.0-1.5
Wind Ease Power: 2.0-3.0

Gentle Breeze

Wind Strength: 0.3
Wind Speed: 1.0
Wind Direction Scale: 0.02
Wind Strength Scale: 0.15

Stormy Weather

Wind Strength: 1.0
Wind Speed: 4.0
Wind Direction Scale: 0.1
Wind Strength Scale: 0.5

📊 Performance

  • Rendering Method: GPU Instancing via Graphics.DrawMeshInstanced
  • Automatic Batching: Splits draws to respect Unity's 1023 instance limit
  • Target Hardware: Medium to high-end hardware
  • 10,000 blades: ~60 FPS on mid-range GPUs
  • Optimization Tips:
    • Reduce grassBlades count for lower-end hardware
    • Adjust grassBladeVertices (default: 15) for detail vs performance trade-off

🛠️ Technical Details

Wind Algorithm

The shader uses a sophisticated multi-layer noise system:

  1. Direction Noise: Samples 2D noise to determine wind direction (0-360°)
  2. Strength Noise: Secondary noise layer for wind intensity variation
  3. FBM Detail: Fractal Brownian Motion adds micro-detail
  4. Gust System: Pow-shaped noise creates sudden wind bursts
  5. Per-Blade Variation: Phase-shifted sine waves prevent synchronization

Shader Pipeline

  • Vertex Stage: Calculates wind displacement, rotation, and position
  • Fragment Stage: Applies gradient coloring and noise-based shading
  • Instancing: Uses UNITY_VERTEX_INPUT_INSTANCE_ID for unique per-blade randomness

🔧 Requirements

  • Unity 2020.3 or higher
  • Shader Model 3.5+ (DX11/Metal/Vulkan)
  • GPU with instancing support

🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Share your improvements

📧 Contact

For questions or feedback, please open an issue on GitHub.

🙏 Acknowledgments

  • Inspired by the beautiful grass rendering in Ghost of Tsushima
  • Built using Unity's GPU instancing system
  • Noise functions adapted from classic Perlin noise algorithms

Made with ❤️ for the Unity community

About

Unity Procedural Grass System A high-performance, noise-based procedural grass rendering system for Unity using GPU instancing and custom shaders.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors