holoride
Search Results for

    Show / Hide Table of Contents
    ❮ Extension Packages
    Trajectory Estimation ❯

    Background Fading

    The Background Fading extension package for the Universal Render Pipeline (URP) contains shaders and a Scriptable Renderer Feature that fades distant geometry into the skybox. This is used to hide world-edge spawning and pop-in artifacts in procedurally generated environments.


    Purpose & Overview

    In procedurally generated environments, spawning or unloading chunks at the horizon can cause visible pop-in. This package hides these transitions by calculating the horizontal distance of rendered pixels to a focal target (e.g., the camera or a vehicle) and blending them into the skybox.

    The render feature runs on the CPU to update global shader uniforms and configure URP's depth copy pass (_CameraDepthTexture). The actual distance calculation and blending occur within custom shaders on the GPU, meaning the render feature itself does not issue draw calls.

    Data Flow

    graph TD
        TargetPos["Focal Target Position <br> (Camera or TargetSetter)"] -->|CPU Update| CustomPass["BackgroundFadingFeature <br> (CPU Uniform Binder)"]
        CustomPass -->|Configures| URP["URP Depth-Scheduling"]
        URP -->|Generates standard| DepthBuffer["_CameraDepthTexture"]
        
        DepthBuffer -->|Sampled by| ApplyFading["ApplyBackgroundFading <br> (HLSL Shaders / Shader Graph)"]
        TargetPos -->|Read via uniform| ApplyFading
        
        OriginalColor["Original Geometry Color"] --> ApplyFading
        SkyboxColor["Skybox Cubemap Color"] --> ApplyFading
        
        ApplyFading -->|"Smoothstep & Bias blending"| FinalOutput["Final Screen Color <br> (Blended Horizon)"]
    

    Installation

    1. Open the Unity Package Manager (Window > Package Manager).
    2. Add the package com.holoride.elasticsdk.backgroundfading to your project.

    Configuration & Usage

    Integrating Background Fading in your scene involves three steps:

    1. Register the Renderer Feature

    Add the BackgroundFadingFeature to your active URP Renderer Asset (configured inside your Universal Render Pipeline Asset):

    1. In the Project window, select your active Universal Renderer asset (e.g., Universal3D_Renderer).
    2. In the Inspector, click Add Renderer Feature and select Background Fading Feature.
    3. Configure the settings:
      • Max Distance: The distance in meters at which fading is complete (100% skybox). Default is 100.
      • Smoothness: The smoothness factor of the fading curve (from 0 to 1). Default is 0.5.

    2. Configure the Fading Center

    The renderer feature measures distance purely in the horizontal X-Z plane relative to a specific coordinate, ignoring height (the Y-axis). This prevents altitude variations or hilly terrain from distorting the fade calculations.

    By default, if no active target-setter is present, the system tracks the current active rendering camera's position on every frame.

    To track a custom target (such as your player's vehicle or a custom anchor), use one of the following methods:

    Method A: Using the BackgroundFadingTargetSetter Component (Recommended)

    1. Add the BackgroundFadingTargetSetter component to a GameObject (e.g., your vehicle or camera).
    2. Configure the Target field in the Inspector. If left unassigned, it defaults to its own transform.

    Note: The component registers its target directly on the active feature, resulting in no CPU update/late update overhead.

    Method B: Custom Scripting

    To update the focal target via C#, register a Transform directly to the BackgroundFadingFeature.Instance.TargetTransform property:

    using Holoride.ElasticSDK;
    using UnityEngine;
    
    public class CustomFadingController : MonoBehaviour
    {
        [SerializeField]
        private Transform target;
    
        private void Start()
        {
            if (this.target != null && BackgroundFadingFeature.Instance != null)
            {
                BackgroundFadingFeature.Instance.TargetTransform = this.target;
            }
        }
    }
    

    Using the Shader Graph Skybox Template

    You can use the pre-built skybox shader directly, or create a custom skybox shader using the provided Shader Graph template.

    Option A: Using the Pre-Built Skybox Shader (Direct)

    1. Create a new Material in your project.
    2. In the Shader dropdown in the Inspector, select Holoride/BackgroundFadingSkybox.
    3. Assign your HDR skybox Cubemap to the texture slot.
    4. Open the Lighting panel (Window > Rendering > Lighting), go to the Environment tab, and assign this material to the Skybox Material slot.
    5. Ensure that the BackgroundFadingFeature is registered in your URP renderer.

    Option B: Creating a Custom Skybox via the Template Browser

    Building a custom skybox shader is easier using the Background Fading Skybox template:

    1. In the Project window, right-click and select Create > Shader Graph > Holoride > Background Fading Skybox (or open the Unity Shader Graph Template Browser and select Background Fading Skybox under the Holoride category).
    2. This creates a new Shader Graph instance pre-configured with all required properties, depth sampling nodes, and horizontal distance calculation logic.
    3. Open the newly created graph to customize its visual effects (such as adding procedural clouds, stars, or color adjustments) while preserving the pre-configured background fading integration.

    Custom Material Integration Options

    You can also integrate background fading into your own custom materials:

    Option A: Handwritten Shaders (Custom HLSL)

    Include the common library and use SampleBackgroundFadingAlpha in your fragment shader:

    1. Include the library file:
      #include "Packages/com.holoride.elasticsdk.backgroundfading/Runtime/Shaders/BackgroundFadingCommon.hlsl"
      
    2. Call SampleBackgroundFadingAlpha with screen UV coordinates:
      half4 frag(Varyings i) : SV_Target
      {
          half4 baseColor = CalculateBaseColor(i);
          float2 screenUV = i.screenPos.xy / i.screenPos.w;
      
          // Sample background fading alpha based on scene depth and focal target
          baseColor.a = SampleBackgroundFadingAlpha(screenUV);
          return baseColor;
      }
      

    The SampleBackgroundFadingAlpha function handles:

    • Sampling the camera depth texture _CameraDepthTexture.
    • Reconstructing the world space position of each screen pixel and calculating its horizontal distance to the target.
    • Computing the smoothstep transition curve using the global _BackgroundFadingMaxDistance and _BackgroundFadingSmoothness values.

    Option B: Shader Graph Integration

    To integrate background fading into custom Shader Graphs, use a Custom Function Node:

    1. Setup the Custom Function Node

    1. Create a Custom Function node in your Shader Graph.
    2. Set Type to File.
    3. In the Source field, reference the library file: Packages/com.holoride.elasticsdk.backgroundfading/Runtime/Shaders/BackgroundFadingCommon.hlsl

    2. Configure the Port Settings

    You can set up the node for either complete fading calculations or raw distance calculations:

    Use Case 1: Complete Fading Alpha

    This node calculates the fading alpha value ready to be plugged into the Alpha slot of your Master Stack.

    • Name: Set to SampleBackgroundFadingAlpha.
    • Inputs:
      • screenUV (Vector2) — Screen-space UV coordinates (e.g., from a Screen Position node).
    • Outputs:
      • Out (Vector1) — The final fading alpha value (0.0 to 1.0).
    Use Case 2: Pure Distance Sampling (Advanced Blending)

    If you want to use the raw distance factor for custom effects (such as custom particle blending or glowing boundaries), you can query the horizontal distance factor.

    • Name: Set to CalculateDistanceFactor.
    • Inputs:
      • uv (Vector2) — Screen-space UV coordinates.
      • depth (Vector1) — Scene depth (from a Scene Depth node).
    • Outputs:
      • Out (Vector1) — The normalized horizontal distance (0.0 to 1.0) of that screen pixel from the focal target.
    Use Case 3: Transparent Geometry Fading

    Transparent objects (rendered after the opaque pass) do not write to the Depth Buffer and cannot use screen-space depth sampling. Instead, they must fade out based on their world position.

    The package provides a dedicated sub-graph for this: CalculateBackgroundFadingAlphaFromWorldPos.

    • Inputs:
      • none (The sub-graph automatically queries the shader's internal World Position node).
    • Outputs:
      • Out (Vector1) — The resulting alpha value (0.0 to 1.0) based on horizontal X-Z distance. Connect this to the Master Stack's Alpha port.

    Note: In custom HLSL fragment shaders, include BackgroundFadingCommon.hlsl and call CalculateBackgroundFadingAlphaFromWorldPos(worldPos) directly.


    ❮ Extension Packages
    Trajectory Estimation ❯
    In This Article

    Back to top
    ©   holoride
    Privacy   Imprint