onejs-sl 0.1.8 → 0.1.10

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.
package/src/sl.ts CHANGED
@@ -25,7 +25,7 @@
25
25
  import { parseColor as parseHex } from "./color"
26
26
  import {
27
27
  Builder, INPUTS, SLError, SL_IR_VERSION, TYPE, hashProgram, widthName,
28
- type InputName, type NodeRef, type Program, type SLNode, type SLType,
28
+ type InputName, type NodeRef, type Program, type SLNode, type SLType, type UniformControl,
29
29
  } from "./ir"
30
30
  import { SLOP, type SLOpCode } from "./ops"
31
31
  import { SL_SDF_PARAMS, SL_SDF_SHAPES, type SlSdfKind } from "./shapes"
@@ -254,6 +254,10 @@ export interface ProgramInputs {
254
254
  resolution: Vec2
255
255
  time: Float
256
256
  aspect: Float
257
+ /** One pixel, in uv: `1 / resolution`. Recorded only if read. */
258
+ texel: Vec2
259
+ /** `(uv - 0.5) * vec2(aspect, 1)`: 0 at the centre, and a circle stays round. Recorded only if read. */
260
+ centered: Vec2
257
261
  }
258
262
 
259
263
  /**
@@ -271,6 +275,12 @@ export function program(fn: (inputs: ProgramInputs) => Vec4): Program {
271
275
  const ref = b.add({ k: "input", type: width as SLType, name: name as InputName })
272
276
  ;(inputs as any)[name] = mk(b, ref, width as SLType)
273
277
  }
278
+ // Getters, so a program that never reads one records nothing for it
279
+ // (DERIVED_INPUTS in ir.ts). A second read is hash consed to the first.
280
+ Object.defineProperties(inputs, {
281
+ texel: { enumerable: true, get: () => vec2(1, 1).div(inputs.resolution) },
282
+ centered: { enumerable: true, get: () => inputs.uv.sub(0.5).mul(vec2(inputs.aspect, 1)) },
283
+ })
274
284
  const out = fn(inputs)
275
285
  if (!(out instanceof Val)) throw new SLError("a program must return an sl value, not " + typeof out)
276
286
  if (out.width !== TYPE.VEC4) {
@@ -655,40 +665,49 @@ export function voronoi(p: Vec2): Float {
655
665
  return mk(p.owner, p.owner.call(SLOP.VORONOI, TYPE.FLOAT, [p.ref]), TYPE.FLOAT)
656
666
  }
657
667
 
668
+ /**
669
+ * A uniform: a value the host sets by name, with the default the program
670
+ * starts from. The last argument says how a host presents it, a slider, a
671
+ * checkbox, a dropdown, a heading or a label, and is what `[Range(0, 2)]` and
672
+ * the other attributes write in a `.sl` file. It never changes the hash.
673
+ *
674
+ * sl.uniform.float("warp", 1, { range: { min: 0, max: 2 } })
675
+ */
658
676
  export const uniform = {
659
- float(name: string, value = 0): Float {
677
+ float(name: string, value = 0, control: UniformControl = {}): Float {
660
678
  const b = ctx()
661
- return mk(b, b.uniform(name, TYPE.FLOAT, [value]), TYPE.FLOAT)
679
+ return mk(b, b.uniform(name, TYPE.FLOAT, [value], false, control), TYPE.FLOAT)
662
680
  },
663
- vec2(name: string, value: [number, number] = [0, 0]): Vec2 {
681
+ vec2(name: string, value: [number, number] = [0, 0], control: UniformControl = {}): Vec2 {
664
682
  const b = ctx()
665
- return mk(b, b.uniform(name, TYPE.VEC2, value), TYPE.VEC2)
683
+ return mk(b, b.uniform(name, TYPE.VEC2, value, false, control), TYPE.VEC2)
666
684
  },
667
- vec3(name: string, value: [number, number, number] = [0, 0, 0]): Vec3 {
685
+ vec3(name: string, value: [number, number, number] = [0, 0, 0], control: UniformControl = {}): Vec3 {
668
686
  const b = ctx()
669
- return mk(b, b.uniform(name, TYPE.VEC3, value), TYPE.VEC3)
687
+ return mk(b, b.uniform(name, TYPE.VEC3, value, false, control), TYPE.VEC3)
670
688
  },
671
- vec4(name: string, value: [number, number, number, number] = [0, 0, 0, 1]): Vec4 {
689
+ vec4(name: string, value: [number, number, number, number] = [0, 0, 0, 1], control: UniformControl = {}): Vec4 {
672
690
  const b = ctx()
673
- return mk(b, b.uniform(name, TYPE.VEC4, value), TYPE.VEC4)
691
+ return mk(b, b.uniform(name, TYPE.VEC4, value, false, control), TYPE.VEC4)
674
692
  },
675
693
  /**
676
- * A colour, defaulting to `hex` as written (sRGB, the way CSS reads it).
677
- * Returns it converted to linear, as a hex literal is, and marks the
678
- * declaration `colour` so a host can offer a colour picker for it. Three
679
- * components when `width` is 3, dropping the alpha.
694
+ * A colour, defaulting to `hex` as written (sRGB, the way CSS reads it), or
695
+ * to components written the same way. Returns it converted to linear, as a
696
+ * hex literal is, and marks the declaration `colour` so a host can offer a
697
+ * colour picker for it. Three components when `width` is 3, dropping the
698
+ * alpha.
680
699
  */
681
- colour: ((name: string, hex: string, width: 3 | 4 = 4): Vec3 | Vec4 => {
700
+ colour: ((name: string, hex: string | readonly number[], width: 3 | 4 = 4, control: UniformControl = {}): Vec3 | Vec4 => {
682
701
  const b = ctx()
683
- const c = parseHex(hex)
702
+ const c = typeof hex === "string" ? parseHex(hex) : [hex[0] ?? 0, hex[1] ?? 0, hex[2] ?? 0, hex[3] ?? 1]
684
703
  const type = width === 3 ? TYPE.VEC3 : TYPE.VEC4
685
- const raw = mk(b, b.uniform(name, type, c.slice(0, width), true), type)
704
+ const raw = mk(b, b.uniform(name, type, c.slice(0, width), true, control), type)
686
705
  return toLinear(raw) as Vec3 | Vec4
687
706
  }) as {
688
- (name: string, hex: string): Vec4
689
- (name: string, hex: string, width: 3): Vec3
690
- (name: string, hex: string, width: 4): Vec4
691
- (name: string, hex: string, width: 3 | 4): Vec3 | Vec4
707
+ (name: string, hex: string | readonly number[]): Vec4
708
+ (name: string, hex: string | readonly number[], width: 3, control?: UniformControl): Vec3
709
+ (name: string, hex: string | readonly number[], width: 4, control?: UniformControl): Vec4
710
+ (name: string, hex: string | readonly number[], width: 3 | 4, control?: UniformControl): Vec3 | Vec4
692
711
  },
693
712
  }
694
713
 
package/src/tables.ts CHANGED
@@ -13,9 +13,12 @@ export {
13
13
  SLOP, SL_ARITY, SL_NAME, SL_HLSL, SL_CALL_NAMES, SL_GLSL_HINT, SL_UNIMPLEMENTED,
14
14
  } from "./ops"
15
15
  export type { SLSurface } from "./ops"
16
- export { INPUTS } from "./ir"
16
+ export { DERIVED_INPUTS, INPUTS, SOURCE_INPUTS } from "./ir"
17
+ export type { DerivedInputName, InputName, SourceInputName } from "./ir"
17
18
  export { SL_SDF_SHAPES, SL_SDF_PARAMS } from "./shapes"
18
- export { BUILTIN_DOCS, BUILTIN_PARAMS, INPUT_DOCS, LIB_SIGNATURES, SL_SDF_PARAM_NAMES } from "./params"
19
+ export { ATTRIBUTE_DOCS, BUILTIN_DOCS, BUILTIN_PARAMS, INPUT_DOCS, LIB_SIGNATURES, SL_SDF_PARAM_NAMES } from "./params"
20
+ export { ATTRIBUTE_NAMES } from "./lang/attributes"
21
+ export type { AttributeName } from "./lang/attributes"
19
22
  export { PRELUDE_DOCS, PRELUDE_NAMES } from "./lang/prelude-source"
20
23
  export { SL_KEYWORDS, SL_TYPES } from "./lang/words"
21
24
  export { TYPE_WIDTH } from "./lang/ast"