Liquid Glass Studio

repository·main·Indexed 19 days ago

https://github.com/iyinchao/liquid-glass-studio

A high-performance web engine designed to recreate Apple's 'Liquid Glass' UI aesthetics. It utilizes WebGL2 and WebGPU to deliver realistic refraction, dispersion, and shape-merging effects via custom shaders and SDF-defined shapes. The engine features a four-pass rendering pipeline (background, vertical blur, horizontal blur, and main composite) and an IMultiPassRenderer abstraction to allow seamless runtime switching between graphics backends.

Tokens
15.8K
Snippets
27
Records
72
Agent score
67%

What's inside liquid-glass-studio

  1. Overview of Liquid Glass Studio features

    main

    Liquid Glass Studio is a web-based recreation of Apple's Liquid Glass UI using WebGL2 and WebGPU. It provides high-performance glass effects with fine-grained customization.

    Core Glass Effects

    • Refraction & Dispersion: Realistic light bending and color splitting.
    • Fresnel reflection: Reflection intensity based on viewing angle.
    • Superellipse shapes: Smooth, organic geometric forms.
    • Blob effect: Smooth shape merging using SDF (Signed Distance Field) functions.
    • Glare: Customizable glare angles.
    • Gaussian blur masking: High-quality blur effects via multipass rendering.
    • Anti-aliasing: Smooth edges for all rendered shapes.

    Key Capabilities

    • Interactive Controls: Real-time parameter adjustments via a Leva UI.
    • Dynamic Backgrounds: Support for both images and videos.
    • Spring Animations: Configurable spring-based shape animations.
    • Dual-Backend Rendering: High-performance graphics via WebGL2 or WebGPU.
  2. Overview of Liquid Glass effects and features

    main

    Liquid Glass Studio provides a web-based implementation of Apple's 'Liquid Glass' effects using WebGL2 and WebGPU shaders. It allows for real-time parameter adjustment via a UI to simulate various glass properties.

    Core Visual Effects

    • Refraction: Simulates light bending when passing through media (e.g., air to glass), causing the background to appear distorted.
    • Dispersion: Simulates light splitting into constituent colors (red, blue, green) during refraction, creating a rainbow effect.
    • Fresnel Reflection: Controls reflection intensity based on viewing angle (higher reflection at grazing angles).
    • Superellipse Shapes: Uses smooth transitions for shapes, similar to iOS icons.
    • Blob Effect (Shape Merging): Uses smooth merging (smin) so transparent shapes blend together like liquid droplets when they get close.
    • Glare: Adds bright lines or points on the glass surface, adjustable by angle, color, and size.
    • Gaussian Blur Masking: Applies smooth, soft blurring to the background.
    • Anti-aliasing: Smooths edges to prevent jagged graphics.

    Technical Capabilities

    • Dual Rendering: Supports both WebGL2 and WebGPU for high-performance graphics.
    • SDF Shapes: Utilizes Signed Distance Fields for smooth shape merging and rendering.
    • Interactive Controls: Real-time parameter adjustment via a Leva UI-based interface.
    • Background Support: Ability to use images or videos as backgrounds.
    • Animations: Uses spring-based animations for natural movement.
  3. Reference the Liquid Glass Studio tech stack and architecture

    main

    Core Tech Stack

    • Framework: React 19 + Vite 6
    • Language: TypeScript (Strict mode, ES2020)
    • Rendering: WebGL2 (GLSL ES 3.0) / WebGPU (WGSL)
    • Animation: @react-spring/web
    • Styling: SCSS Modules, clsx
    • Package Manager: pnpm

    Architecture Patterns

    • State Management: Uses stateRef (ref-based state) for data within the render loop to avoid triggering React re-renders every frame.
    • Rendering Loop: A single requestAnimationFrame loop managed in src/App.tsx.
    • Shaders: All programs share a common vertex shader (fullscreen quad). Shape rendering uses SDF-based (Signed Distance Function) logic with smin (smooth min) for blob/merge effects.
  4. Implement Mouse-Following and Spring-Based Animations

    main

    The primary shape's position and deformation are driven by pointer movement using spring-based physics.

    Position Following

    • The primary shape follows the pointer location on the canvas.
    • Movement uses spring-based easing for smoothness.
    • The system tracks spring velocity to drive animation morph effects.

    Size and Morph Animation

    You can deform the shape's size based on pointer movement speed using the animation morph factor:

    • Animation Morph Factor: A value between 0 and 50.
    • Behavior: When the factor is greater than 0, fast pointer movement causes the shape to stretch in the direction of movement, proportional to the speed and the morph factor.
  5. WebGPU Rendering Components

    main

    The WebGPU implementation relies on several internal orchestration objects:

    • GPUFrameBuffer: Manages GPUTexture resources, specifically using rgba16float for color and depth24plus for depth, with built-in resize support.
    • GPURenderPassObj: Handles the creation of the render pipeline, manages bind groups for uniforms and textures/samplers, and manages the fullscreen quad vertex buffer.
    • GPUMultiPassRenderer: The primary implementation of IMultiPassRenderer. It orchestrates multiple rendering passes, manages global and per-pass uniforms, and resolves dependencies between textures used in different passes.
  6. How the multi-pass rendering pipeline works

    main

    The glass effect is rendered using a four-pass pipeline to achieve high-quality visual results. This pipeline is implemented by both the WebGL2 (MultiPassRenderer) and WebGPU (GPUMultiPassRenderer) backends, which both adhere to the IMultiPassRenderer interface.

    The four passes are:

    1. Background pass: Renders the initial background.
    2. Vertical blur pass: Applies vertical blurring.
    3. Horizontal blur pass: Applies horizontal blurring.
    4. Main composite pass: Combines all previous passes into the final visual output.

    During each animation frame, the active renderer executes these passes in order, passing global and per-pass uniforms. Intermediate pass outputs are connected as texture inputs to subsequent passes.

  7. Understand the Liquid Glass Studio rendering pipeline

    main

    Liquid Glass Studio uses a multi-pass rendering pipeline to achieve high-performance glass effects. The process follows these specific stages:

    1. bgPass: Renders the background (using procedural patterns, textures, or video) and applies shadows.
    2. vBlurPass: Performs a vertical Gaussian blur on the output of the bgPass.
    3. hBlurPass: Performs a horizontal Gaussian blur on the output of the vBlurPass (this two-pass approach is a separable Gaussian blur used for performance).
    4. mainPass: The final composite stage. It applies the glass effect—including refraction, dispersion, Fresnel, and glare—on top of the blurred and unblurred background layers.

    Debugging Tip: You can use the STEP uniform (values 0-9) to visualize these intermediate rendering stages during development.

  8. Supported Background Types in the Background System

    main

    The background system supports four primary categories of backgrounds, which are rendered behind the glass effect:

    1. Procedural Backgrounds: Built-in patterns including checkerboard grid, directional bars, and half-tone split.
    2. Image Backgrounds: Bundled images (e.g., Tahoe light, buildings, text, Tim Cook, UI mockup) loaded as WebGL textures with cover-fit UV mapping.
    3. Video Backgrounds: Bundled looping videos (e.g., fish, traffic, flower) that update the GPU texture every frame. Videos play muted and inline.
    4. Custom Media: Users can upload custom images or videos via a file picker to be used as textures.
  9. Configure and use Leva controls for real-time parameter manipulation

    main

    The project uses Leva as the primary UI for parameter control.

    • Direct Uniform Mapping: Shader uniforms are driven directly by Leva control values every frame within the rendering loop.
    • Custom Components: In addition to standard Leva controls, the project utilizes custom components for specialized inputs:
      • LevaContainer
      • LevaCheckButtons
      • LevaVectorNew
      • LevaButton

    All parameter definitions are centralized in src/Controls.tsx.

  10. Understand the Renderer Abstraction Strategy (WebGL2 vs WebGPU)

    main

    The project uses an IMultiPassRenderer TypeScript interface to allow runtime switching between WebGL2 and WebGPU backends. This abstraction ensures that the main application logic (App.tsx) remains decoupled from the specific graphics API being used.

    Key Architectural Differences

    AspectWebGL2WebGPU
    Contextcanvas.getContext('webgl2')navigator.gpu.requestAdapter()adapter.requestDevice()context.configure()
    Shader LanguageGLSL ES 3.0WGSL
    PipelineStateful (bind program, set uniforms, draw)Pipeline objects (immutable config), bind groups
    UniformsIndividual gl.uniform*() callsUniform buffers (GPUBuffer) + bind groups
    Texturesgl.bindTexture() + gl.activeTexture()GPUTexture + GPUSampler + bind group entries
    FramebuffersFBO + texture attachmentRender pass descriptor with color/depth attachments
    Render LoopBind FBO → use program → set uniforms → draw → unbindCommand encoder → render pass → set pipeline/bindings → draw → submit
    Texture OriginBottom-left (UV y=0 at bottom)Top-left (UV y=0 at top)
    asin() OOBSilent clamp (implementation-defined)Returns NaN (strict)
    Canvas alphaDefault opaque compositingMust set alphaMode: 'opaque' explicitly
  11. Handle Internationalization and Language Switching

    main

    The system supports three languages: English (en-US), Simplified Chinese (zh-CN), and Uzbek (uz-UZ).

    Automatic Detection

    On application load, the UI language is determined by the browser language:

    • zh* $\rightarrow$ Simplified Chinese
    • uz* $\rightarrow$ Uzbek
    • All others $\rightarrow$ English

    Manual Switching

    Users can manually select a different language, which triggers an immediate update of all control labels and UI strings.