Skip to content

Commit

Permalink
feat (Nodes): more tslFn related typings (#744)
Browse files Browse the repository at this point in the history
* feat (Nodes): update rest of `tslFn` functions

* feat (Nodes): Support `tslFn` with array arguments

If the argument is an array, the array will be destructed.

See: https://github.com/mrdoob/three.js/blob/r160/examples/jsm/nodes/materialx/lib/mx_noise.js

* feat (Nodes): Update MaterialX nodes

- Fix type of mx_hsv, mx_noise, MaterialXNodes
- Add missing methods of mx_noise
- Add mx_transform_color
- Update test cases
    - the directory name was `materials` instead of `materialx`, fixed this

`mx_noise.js` exports requires all arguments to exist, while `MaterialXNodes.js` exports don't

* feat (Nodes): `tslFn` parameters are proxied

* refactor: pnpm format

* test: Fix test case for ShaderNode.ts

I forgot to put a thing in `assetSwizzable()`

* refactor: `pnpm format`

* fix: Fix import statements on materialx files

* fix: Fix the ShaderNodeObject circular reference issue

The error is since `ContextNode` already has a member `.context` and `ShaderNodeObject` also gives it a method `.context`

See: #744 (comment)

I believe the behavior that the node class properties takes precedance than NodeElements is same as the actual implementation

See: https://github.com/mrdoob/three.js/blob/r163/examples/jsm/nodes/shadernode/ShaderNode.js
  • Loading branch information
0b5vr authored Apr 12, 2024
1 parent a1bb2fb commit 4b13e2a
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Node from "../../core/Node.js";
import { ShaderNode } from "../../shadernode/ShaderNode.js";
import OperatorNode from "../../math/OperatorNode.js";
import { ShaderNodeObject } from "../../shadernode/ShaderNode.js";

declare const V_GGX_SmithCorrelated: ShaderNode<{ alpha: Node; dotNL: Node; dotNV: Node }>;
declare const V_GGX_SmithCorrelated: (args: {
alpha: Node;
dotNL: Node;
dotNV: Node;
}) => ShaderNodeObject<OperatorNode>;

export default V_GGX_SmithCorrelated;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ShaderNode } from "../../shadernode/ShaderNode.js";
import MathNode from "../../math/MathNode.js";
import { ShaderNodeObject } from "../../shadernode/ShaderNode.js";

declare const getGeometryRoughness: ShaderNode;
declare const getGeometryRoughness: () => ShaderNodeObject<MathNode>;

export default getGeometryRoughness;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Node from "../../core/Node.js";
import { ShaderNode } from "../../shadernode/ShaderNode.js";
import MathNode from "../../math/MathNode.js";
import { ShaderNodeObject } from "../../shadernode/ShaderNode.js";

declare const getRoughness: ShaderNode<{ roughness: Node }>;
declare const getRoughness: (args: { roughness: Node }) => ShaderNodeObject<MathNode>;

export default getRoughness;
9 changes: 7 additions & 2 deletions types/three/examples/jsm/nodes/lighting/LightUtils.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import Node from "../core/Node.js";
import { ShaderNode } from "../shadernode/ShaderNode.js";
import CondNode from "../math/CondNode.js";
import { ShaderNodeObject } from "../shadernode/ShaderNode.js";

export const getDistanceAttenuation: ShaderNode<{ lightDistance: Node; cutoffDistance: Node; decayExponent: Node }>;
export const getDistanceAttenuation: (args: {
lightDistance: Node;
cutoffDistance: Node;
decayExponent: Node;
}) => ShaderNodeObject<CondNode>;
68 changes: 36 additions & 32 deletions types/three/examples/jsm/nodes/materialx/MaterialXNodes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,106 @@ import Node from "../core/Node.js";
import MathNode from "../math/MathNode.js";
import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js";
import { mx_hsvtorgb, mx_rgbtohsv } from "./lib/mx_hsv.js";
import { mx_srgb_texture_to_lin_rec709 } from "./lib/mx_transform_color.js";

export function mx_aastep(threshold?: NodeRepresentation, value?: NodeRepresentation): ShaderNodeObject<MathNode>;
export function mx_aastep(threshold: NodeRepresentation, value: NodeRepresentation): ShaderNodeObject<MathNode>;

export function mx_ramplr(
valuel?: NodeRepresentation,
valuer?: NodeRepresentation,
texcoord?: ShaderNodeObject<Node>,
valuel: NodeRepresentation,
valuer: NodeRepresentation,
texcoord?: NodeRepresentation,
): ShaderNodeObject<MathNode>;
export function mx_ramptb(
valuet?: NodeRepresentation,
valueb?: NodeRepresentation,
texcoord?: ShaderNodeObject<Node>,
valuet: NodeRepresentation,
valueb: NodeRepresentation,
texcoord?: NodeRepresentation,
): ShaderNodeObject<MathNode>;

export function mx_splitlr(
valuel?: NodeRepresentation,
valuer?: NodeRepresentation,
center?: NodeRepresentation,
texcoord?: ShaderNodeObject<Node>,
valuel: NodeRepresentation,
valuer: NodeRepresentation,
center: NodeRepresentation,
texcoord?: NodeRepresentation,
): ShaderNodeObject<MathNode>;
export function mx_splittb(
valuet?: NodeRepresentation,
valueb?: NodeRepresentation,
center?: NodeRepresentation,
texcoord?: ShaderNodeObject<Node>,
valuet: NodeRepresentation,
valueb: NodeRepresentation,
center: NodeRepresentation,
texcoord?: NodeRepresentation,
): ShaderNodeObject<MathNode>;

export function mx_transform_uv(
uv_scale?: NodeRepresentation,
uv_offset?: NodeRepresentation,
uv_geo?: ShaderNodeObject<Node>,
uv_geo?: NodeRepresentation,
): ShaderNodeObject<Node>;

export function mx_noise_float(
texcoord?: ShaderNodeObject<Node>,
amplitude?: NodeRepresentation,
export function mx_safepower(in1: NodeRepresentation, in2?: NodeRepresentation): ShaderNodeObject<Node>;

export function mx_contrast(
input: NodeRepresentation,
amount?: NodeRepresentation,
pivot?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_noise_vec2(
texcoord?: ShaderNodeObject<Node>,

export function mx_noise_float(
texcoord?: NodeRepresentation,
amplitude?: NodeRepresentation,
pivot?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_noise_vec3(
texcoord?: ShaderNodeObject<Node>,
texcoord?: NodeRepresentation,
amplitude?: NodeRepresentation,
pivot?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_noise_vec4(
texcoord?: ShaderNodeObject<Node>,
texcoord?: NodeRepresentation,
amplitude?: NodeRepresentation,
pivot?: NodeRepresentation,
): ShaderNodeObject<Node>;

export function mx_worley_noise_float(
texcoord?: ShaderNodeObject<Node>,
texcoord?: NodeRepresentation,
jitter?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_worley_noise_vec2(
texcoord?: ShaderNodeObject<Node>,
texcoord?: NodeRepresentation,
jitter?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_worley_noise_vec3(
texcoord?: ShaderNodeObject<Node>,
texcoord?: NodeRepresentation,
jitter?: NodeRepresentation,
): ShaderNodeObject<Node>;

export function mx_cell_noise_float(texcoord?: ShaderNodeObject<Node>): ShaderNodeObject<Node>;
export function mx_cell_noise_float(texcoord?: NodeRepresentation): ShaderNodeObject<Node>;

export function mx_fractal_noise_float(
position?: ShaderNodeObject<Node>,
position?: NodeRepresentation,
octaves?: NodeRepresentation,
lacunarity?: NodeRepresentation,
diminish?: NodeRepresentation,
amplitude?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_fractal_noise_vec2(
position?: ShaderNodeObject<Node>,
position?: NodeRepresentation,
octaves?: NodeRepresentation,
lacunarity?: NodeRepresentation,
diminish?: NodeRepresentation,
amplitude?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_fractal_noise_vec3(
position?: ShaderNodeObject<Node>,
position?: NodeRepresentation,
octaves?: NodeRepresentation,
lacunarity?: NodeRepresentation,
diminish?: NodeRepresentation,
amplitude?: NodeRepresentation,
): ShaderNodeObject<Node>;
export function mx_fractal_noise_vec4(
position?: ShaderNodeObject<Node>,
position?: NodeRepresentation,
octaves?: NodeRepresentation,
lacunarity?: NodeRepresentation,
diminish?: NodeRepresentation,
amplitude?: NodeRepresentation,
): ShaderNodeObject<Node>;

export { mx_hsvtorgb, mx_rgbtohsv };
export { mx_hsvtorgb, mx_rgbtohsv, mx_srgb_texture_to_lin_rec709 };
7 changes: 3 additions & 4 deletions types/three/examples/jsm/nodes/materialx/lib/mx_hsv.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Fn } from "../../code/FunctionNode.js";
import Node from "../../core/Node.js";
import { ShaderNodeObject } from "../../shadernode/ShaderNode.js";
import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js";

export function mx_hsvtorgb(...params: Fn<[Node]>): ShaderNodeObject<Node>;
export function mx_rgbtohsv(...params: Fn<[Node]>): ShaderNodeObject<Node>;
export const mx_hsvtorgb: (hsv_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_rgbtohsv: (c_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
133 changes: 127 additions & 6 deletions types/three/examples/jsm/nodes/materialx/lib/mx_noise.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,129 @@
import { Fn } from "../../code/FunctionNode.js";
import Node from "../../core/Node.js";
import { ShaderNodeObject } from "../../shadernode/ShaderNode.js";
import VarNode from "../../core/VarNode.js";
import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js";

export function mx_perlin_noise_float(...params: Fn<[Node]>): ShaderNodeObject<Node>;
export function mx_cell_noise_float(...params: Fn<[Node]>): ShaderNodeObject<Node>;
export function mx_worley_noise_float(...params: Fn<[Node]>): ShaderNodeObject<Node>;
export function mx_fractal_noise_float(...params: Fn<[Node, Node, Node, Node]>): ShaderNodeObject<Node>;
export const mx_select: (
b_immutable: NodeRepresentation,
t_immutable: NodeRepresentation,
f_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_negate_if: (
val_immutable: NodeRepresentation,
b_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_floor: (x_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_floorfrac: (x_immutable: NodeRepresentation, i: ShaderNodeObject<VarNode>) => ShaderNodeObject<Node>;
export const mx_bilerp: (
v0_immutable: NodeRepresentation,
v1_immutable: NodeRepresentation,
v2_immutable: NodeRepresentation,
v3_immutable: NodeRepresentation,
s_immutable: NodeRepresentation,
t_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_trilerp: (
v0_immutable: NodeRepresentation,
v1_immutable: NodeRepresentation,
v2_immutable: NodeRepresentation,
v3_immutable: NodeRepresentation,
v4_immutable: NodeRepresentation,
v5_immutable: NodeRepresentation,
v6_immutable: NodeRepresentation,
v7_immutable: NodeRepresentation,
s_immutable: NodeRepresentation,
t_immutable: NodeRepresentation,
r_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_gradient_float: (
hash_immutable: NodeRepresentation,
x_immutable: NodeRepresentation,
y_immutable: NodeRepresentation,
z_immutable?: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_gradient_vec3: (
hash_immutable: NodeRepresentation,
x_immutable: NodeRepresentation,
y_immutable: NodeRepresentation,
z_immutable?: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_gradient_scale2d: (v_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_gradient_scale3d: (v_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_rotl32: (x_immutable: NodeRepresentation, k_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_bjmix: (
a: ShaderNodeObject<VarNode>,
b: ShaderNodeObject<VarNode>,
c: ShaderNodeObject<VarNode>,
) => ShaderNodeObject<Node>;
export const mx_bjfinal: (
a_immutable: NodeRepresentation,
b_immutable: NodeRepresentation,
c_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_bits_to_01: (bits_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_fade: (t_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_hash_int: (
x_immutable: NodeRepresentation,
y_immutable?: NodeRepresentation,
z_immutable?: NodeRepresentation,
xx_immutable?: NodeRepresentation,
yy_immutable?: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_hash_vec3: (
x_immutable: NodeRepresentation,
y_immutable: NodeRepresentation,
z_immutable?: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_perlin_noise_float: (p_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_perlin_noise_vec3: (p_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_cell_noise_float: (p_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_cell_noise_vec3: (p_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
export const mx_fractal_noise_float: (
p_immutable: NodeRepresentation,
octaves_immutable: NodeRepresentation,
lacunarity_immutable: NodeRepresentation,
diminish_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_fractal_noise_vec3: (
p_immutable: NodeRepresentation,
octaves_immutable: NodeRepresentation,
lacunarity_immutable: NodeRepresentation,
diminish_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_fractal_noise_vec2: (
p_immutable: NodeRepresentation,
octaves_immutable: NodeRepresentation,
lacunarity_immutable: NodeRepresentation,
diminish_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_fractal_noise_vec4: (
p_immutable: NodeRepresentation,
octaves_immutable: NodeRepresentation,
lacunarity_immutable: NodeRepresentation,
diminish_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_worley_distance: (
p_immutable: NodeRepresentation,
x_immutable: NodeRepresentation,
y_immutable: NodeRepresentation,
z_immutable: NodeRepresentation,
xoff_immutable: NodeRepresentation,
yoff_immutable: NodeRepresentation,
zoff_immutable: NodeRepresentation,
jitter_immutable: NodeRepresentation,
metric_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_worley_noise_float: (
p_immutable: NodeRepresentation,
jitter_immutable: NodeRepresentation,
metric_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_worley_noise_vec2: (
p_immutable: NodeRepresentation,
jitter_immutable: NodeRepresentation,
metric_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
export const mx_worley_noise_vec3: (
p_immutable: NodeRepresentation,
jitter_immutable: NodeRepresentation,
metric_immutable: NodeRepresentation,
) => ShaderNodeObject<Node>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Node from "../../core/Node.js";
import { NodeRepresentation, ShaderNodeObject } from "../../shadernode/ShaderNode.js";

export const mx_srgb_texture_to_lin_rec709: (color_immutable: NodeRepresentation) => ShaderNodeObject<Node>;
9 changes: 6 additions & 3 deletions types/three/examples/jsm/nodes/shadernode/ShaderNode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export type Swizzable<T extends Node = Node> =
export type ShaderNodeObject<T extends Node> =
& T
& {
[Key in keyof NodeElements]: NodeElements[Key] extends (node: T, ...args: infer Args) => infer R
? (...args: Args) => R
[Key in keyof NodeElements]: T extends { [K in Key]: infer M } ? M
: NodeElements[Key] extends (node: T, ...args: infer Args) => infer R ? (...args: Args) => R
: never;
}
& Swizzable<T>;
Expand Down Expand Up @@ -198,9 +198,12 @@ export function nodeImmutable<T>(
): ShaderNodeObject<ConstructedNode<T>>;

export function tslFn<R extends Node = ShaderNodeObject<Node>>(jsFunc: () => R): () => R;
export function tslFn<T extends any[], R extends Node = ShaderNodeObject<Node>>(
jsFunc: (args: T) => R,
): (...args: ProxiedTuple<T>) => R;
export function tslFn<T extends AnyObject, R extends Node = ShaderNodeObject<Node>>(
jsFunc: (args: T) => R,
): (args: T) => R;
): (args: ProxiedObject<T>) => R;

export function append(node: Node): Node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ assertSwizzable<Node>(shader.call({ a: s, b: new ConstNode(1) }));
const fnWithoutArgs = tslFn(() => vec3(1, 2, 3));
assertSwizzable<Node>(fnWithoutArgs());

const fnWithArrayArgs = tslFn(([a, b]: [a: ShaderNodeObject<Node>, b: ShaderNodeObject<Node>]) => a.add(b));
assertSwizzable<Node>(fnWithArrayArgs(0.5, color(0.0, 0.25, 0.5)));

const fnWithArgs = tslFn(({ a, b }: { a: ShaderNodeObject<Node>; b: ShaderNodeObject<Node> }) => a.add(b));
assertSwizzable<Node>(fnWithArgs({ a: color(0, 0.5, 0), b: color(1, 0.5, 0) }));
assertSwizzable<Node>(fnWithArgs({ a: 0.5, b: color(0.0, 0.25, 0.5) }));
15 changes: 0 additions & 15 deletions types/three/test/unit/examples/jsm/nodes/materials/lib/mx_noise.ts

This file was deleted.

Loading

0 comments on commit 4b13e2a

Please sign in to comment.