@rosalana/sandbox 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { ShaderFunction, ShaderUniform, WebGLVersion } from "../types";
2
2
  import Parser from "./parser";
3
+ import type ModuleRegistry from "./module_registry";
3
4
  export default class Compilable {
4
5
  /** Flag to track if the shader has been compiled */
5
6
  protected isCompiled: boolean;
@@ -29,7 +30,7 @@ export default class Compilable {
29
30
  /**
30
31
  * Compile the shader source, resolving all imports
31
32
  */
32
- compile(): string;
33
+ compile(runtimeModules?: ModuleRegistry): string;
33
34
  /**
34
35
  * Process all #import directives
35
36
  */
@@ -0,0 +1,64 @@
1
+ import type { TextureOptions, TextureSource, WebGLContext } from "../types";
2
+ /**
3
+ * Handles a single WebGL texture.
4
+ * Manages texture creation, pixel upload, binding, and location caching.
5
+ * Mirrors the Uniform class pattern for consistency.
6
+ */
7
+ export default class Texture {
8
+ readonly name: string;
9
+ private gl;
10
+ private texture;
11
+ private location;
12
+ private locationResolved;
13
+ private source;
14
+ private options;
15
+ private dynamicOverride;
16
+ private needsUpload;
17
+ private needsReupload;
18
+ constructor(gl: WebGLContext, name: string, source: TextureSource, options?: TextureOptions);
19
+ /**
20
+ * Update the texture source.
21
+ * Marks texture for re-upload on next bind.
22
+ * Respects the original `dynamic` option if explicitly set, otherwise re-infers from source type.
23
+ */
24
+ setSource(source: TextureSource): void;
25
+ /**
26
+ * Resolve and cache uniform location from program.
27
+ * Returns null if the sampler uniform doesn't exist in the shader.
28
+ */
29
+ resolveLocation(gl: WebGLContext, program: WebGLProgram): WebGLUniformLocation | null;
30
+ /**
31
+ * Invalidate cached location (call when program changes).
32
+ */
33
+ invalidateLocation(): void;
34
+ /**
35
+ * Bind texture to a texture unit and set the sampler uniform.
36
+ * @param program - Current WebGL program (for location resolution)
37
+ * @param unit - Texture unit index (0, 1, 2, ...)
38
+ */
39
+ upload(program: WebGLProgram, unit: number): void;
40
+ /**
41
+ * Cleanup WebGL texture resource.
42
+ */
43
+ destroy(): void;
44
+ /**
45
+ * Upload pixel data from source and apply texture parameters.
46
+ */
47
+ private uploadPixels;
48
+ /**
49
+ * Apply texture wrap and filter parameters.
50
+ */
51
+ private applyParameters;
52
+ /**
53
+ * Resolve wrap mode string to WebGL constant.
54
+ */
55
+ private static resolveWrap;
56
+ /**
57
+ * Resolve texture options with defaults.
58
+ */
59
+ private static resolveOptions;
60
+ /**
61
+ * Check if a source is dynamic (needs re-upload every frame).
62
+ */
63
+ private static isDynamicSource;
64
+ }
@@ -0,0 +1,48 @@
1
+ import type { TextureOptions, TextureSource, WebGLContext } from "../types";
2
+ import Texture from "./texture";
3
+ /**
4
+ * Manages a collection of textures with fluent API.
5
+ * Assigns texture units sequentially and handles program attachment.
6
+ * Mirrors the Uniforms class pattern for consistency.
7
+ */
8
+ export default class Textures {
9
+ private gl;
10
+ private program;
11
+ private textures;
12
+ constructor(gl: WebGLContext);
13
+ /**
14
+ * Attach to a WebGL program.
15
+ * Invalidates all cached locations since they're program-specific.
16
+ */
17
+ attachProgram(program: WebGLProgram): this;
18
+ /**
19
+ * Set a texture.
20
+ * Creates the texture entry if it doesn't exist, updates source if it does.
21
+ */
22
+ set(name: string, source: TextureSource, options?: TextureOptions): this;
23
+ /**
24
+ * Get a texture by name.
25
+ */
26
+ get(name: string): Texture | undefined;
27
+ /**
28
+ * Check if a texture exists.
29
+ */
30
+ has(name: string): boolean;
31
+ /**
32
+ * Remove a texture and free its GPU resources.
33
+ */
34
+ delete(name: string): boolean;
35
+ /**
36
+ * Upload all textures to their respective texture units.
37
+ * Units are assigned sequentially (0, 1, 2, ...).
38
+ */
39
+ uploadAll(): this;
40
+ /**
41
+ * Get texture count.
42
+ */
43
+ get size(): number;
44
+ /**
45
+ * Cleanup all textures.
46
+ */
47
+ destroy(): void;
48
+ }
@@ -1,7 +1,8 @@
1
- import type { AnyUniformValue, ResolvedSandboxOptions, UniformSchema, WebGLContext, WebGLVersion } from "../types";
1
+ import type { AnyUniformValue, ResolvedSandboxOptions, TextureOptions, TextureSchema, TextureSource, UniformSchema, WebGLContext, WebGLVersion } from "../types";
2
2
  import Clock from "./clock";
3
3
  import Hooks from "./hooks";
4
4
  import Shader from "./shader";
5
+ import ModuleRegistry from "./module_registry";
5
6
  /**
6
7
  * Main WebGL orchestrator.
7
8
  * Manages context, program, geometry, uniforms, and rendering loop.
@@ -15,10 +16,12 @@ export default class WebGL {
15
16
  private _program;
16
17
  private _geometry;
17
18
  private _uniforms;
19
+ private _textures;
18
20
  private _clock;
19
21
  private _resolution;
20
22
  private _mouse;
21
23
  private _version;
24
+ private _runtimeModules;
22
25
  playing: boolean;
23
26
  private constructor();
24
27
  /**
@@ -59,6 +62,18 @@ export default class WebGL {
59
62
  * Get current uniform value.
60
63
  */
61
64
  getUniform<T extends AnyUniformValue>(name: string): T | undefined;
65
+ /**
66
+ * Set a texture.
67
+ */
68
+ texture(name: string, source: TextureSource, options?: TextureOptions): this;
69
+ /**
70
+ * Set multiple textures from a schema.
71
+ */
72
+ texturesFromSchema(schema: TextureSchema): this;
73
+ /**
74
+ * Remove a texture.
75
+ */
76
+ removeTexture(name: string): this;
62
77
  /**
63
78
  * Compile and link shaders.
64
79
  * Errors are handled via onError callback, never thrown.
@@ -80,6 +95,10 @@ export default class WebGL {
80
95
  * Get WebGL context.
81
96
  */
82
97
  getContext(): WebGLContext;
98
+ /**
99
+ * Get registered runtime modules.
100
+ */
101
+ getUsingModules(): ModuleRegistry;
83
102
  /**
84
103
  * Get detected WebGL version.
85
104
  */
package/dist/types.d.ts CHANGED
@@ -30,6 +30,8 @@ export interface SandboxOptions {
30
30
  uniforms?: UniformSchema;
31
31
  /** Configure used modules behavior */
32
32
  modules?: Record<string, Record<string, AnyUniformValue>>;
33
+ /** Initial textures to bind */
34
+ textures?: TextureSchema;
33
35
  }
34
36
  /** Resolved sandbox options with all defaults applied */
35
37
  export type ResolvedSandboxOptions = Omit<Required<SandboxOptions>, "vertex" | "fragment"> & {
@@ -121,6 +123,37 @@ export interface UniformEntry {
121
123
  export type DrawMode = "TRIANGLES" | "TRIANGLE_STRIP" | "TRIANGLE_FAN";
122
124
  /** Render callback signature */
123
125
  export type HookCallback = (clock: ClockState) => void | false;
126
+ /** Valid texture source types */
127
+ export type TextureSource = HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap | ImageData | OffscreenCanvas;
128
+ /** Texture wrapping mode */
129
+ export type TextureWrap = "clamp" | "repeat" | "mirror";
130
+ /** Texture filter mode */
131
+ export type TextureFilter = "nearest" | "linear";
132
+ /** Per-texture configuration */
133
+ export interface TextureOptions {
134
+ /** Wrapping mode for both axes (shorthand for wrapS + wrapT) */
135
+ wrap?: TextureWrap;
136
+ /** Wrapping mode for S (horizontal) axis */
137
+ wrapS?: TextureWrap;
138
+ /** Wrapping mode for T (vertical) axis */
139
+ wrapT?: TextureWrap;
140
+ /** Minification filter */
141
+ minFilter?: TextureFilter;
142
+ /** Magnification filter */
143
+ magFilter?: TextureFilter;
144
+ /** Flip texture vertically on upload (default: true) */
145
+ flipY?: boolean;
146
+ /** Re-upload pixels every frame for animated sources (default: true for video, false otherwise) */
147
+ dynamic?: boolean;
148
+ }
149
+ /** Resolved texture options with all defaults applied */
150
+ export type ResolvedTextureOptions = Required<TextureOptions>;
151
+ /** Schema for defining multiple textures at once */
152
+ export interface TextureSchema {
153
+ [name: string]: TextureSource | ({
154
+ source: TextureSource;
155
+ } & TextureOptions);
156
+ }
124
157
  /** GLSL uniform types */
125
158
  export type GLSLType = "float" | "int" | "bool" | "vec2" | "vec3" | "vec4" | "ivec2" | "ivec3" | "ivec4" | "bvec2" | "bvec3" | "bvec4" | "mat2" | "mat3" | "mat4" | "sampler2D" | "samplerCube";
126
159
  export type GLSLVariable = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rosalana/sandbox",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Lightweight WebGL wrapper for simple, beautiful shader effects",
5
5
  "keywords": [
6
6
  "webgl",