Skip to content

AI-native design editor. Open-source Figma alternative.

1.7 KB
Raw Blame
import * as v from 'valibot'

/** One effect of a shader preset, as the `shaders` library names it, with the effects it holds. */
export interface ShaderComponent {
  type: string
  id?: string
  props?: Record<string, unknown>
  children?: ShaderComponent[]
}

const shaderComponentSchema: v.GenericSchema<unknown, ShaderComponent> = v.object({
  type: v.pipe(v.string(), v.nonEmpty(), v.maxLength(100)),
  id: v.optional(v.string()),
  props: v.optional(v.record(v.string(), v.unknown())),
  children: v.optional(v.array(v.lazy(() => shaderComponentSchema)))
})

/**
 * A shader as the `shaders` library describes it and shaders.com exports it: a tree of effects,
 * drawn first to last.
 */
export const shaderPresetSchema = v.object({
  components: v.pipe(v.array(shaderComponentSchema), v.minLength(1)),
  structureVersion: v.optional(v.number())
})

export type ShaderPreset = v.InferOutput<typeof shaderPresetSchema>

/**
 * A paint that draws a shader. The paint itself is an image fill of the shader's still frame, so
 * every format and renderer that reads images shows it; this entry says which image it is, the
 * preset that draws it, and the layer size and preset the frame was rendered for, which is
 * missing until it is first rendered.
 */
export const shaderPaintSchema = v.object({
  image: v.pipe(v.string(), v.nonEmpty()),
  preset: shaderPresetSchema,
  frame: v.optional(
    v.object({
      width: v.pipe(v.number(), v.finite(), v.minValue(0)),
      height: v.pipe(v.number(), v.finite(), v.minValue(0)),
      /** The preset's key (`shaderPresetKey`) when the frame was rendered. */
      preset: v.pipe(v.string(), v.nonEmpty())
    })
  )
})

export type ShaderPaint = v.InferOutput<typeof shaderPaintSchema>