docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Add foam with a script

    To add caustics or foam, or get information about water surface displacement due to waves and ripples, get buffers from the GetFoamBuffer API.

    Example: Add foam

    using UnityEngine;
    using UnityEngine.Rendering.HighDefinition;
    
    public class WaterFoamExample : MonoBehaviour
    {
        // Reference to the water surface component
        public WaterSurface waterSurface;
    
        // The area of the water surface where the foam buffer should be queried
        public Vector2 foamArea;
    
        // Material to apply the foam effect
        public Material waterMaterial;
    
        // Shader property name for foam texture in the water material
        private readonly string _foamTextureProperty = "_FoamTex";
    
        void Start()
        {
            // Get the foam buffer for the specified 2D area on the water surface
            Texture foamBuffer = waterSurface.GetFoamBuffer(out foamArea);
    
            if (foamBuffer != null)
            {
                // Apply the foam buffer as a texture to the water material
                waterMaterial.SetTexture(_foamTextureProperty, foamBuffer);
                Debug.Log("Foam buffer applied successfully.");
            }
            else
            {
                Debug.LogWarning("Foam buffer could not be retrieved.");
            }
        }
    }
    
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)