From bbe9486882e6e10992b0bde6d7f44fa63c7e6981 Mon Sep 17 00:00:00 2001 From: miko Date: Sun, 21 Aug 2022 01:40:17 +0200 Subject: [PATCH 1/3] feat: fn/func in nodes --- types/three/examples/jsm/nodes/Nodes.d.ts | 3 +- .../examples/jsm/nodes/core/CodeNode.d.ts | 4 +- .../jsm/nodes/core/FunctionCallNode.d.ts | 10 ++-- .../examples/jsm/nodes/core/FunctionNode.d.ts | 10 ++-- .../nodes/materialx/functions/lib/mx_hsv.d.ts | 4 ++ .../materialx/functions/lib/mx_noise.d.ts | 6 ++ .../jsm/nodes/shadernode/ShaderNode.d.ts | 18 +++--- .../shadernode/ShaderNodeBaseElements.d.ts | 32 ++++++++--- types/three/test/nodes/nodes-FunctionNode.ts | 57 +++++++++++++++++++ types/three/test/nodes/nodes-ShaderNode.ts | 1 - types/three/test/nodes/nodes-materialx.ts | 15 +++++ types/three/tsconfig.json | 2 + 12 files changed, 134 insertions(+), 28 deletions(-) create mode 100644 types/three/examples/jsm/nodes/materialx/functions/lib/mx_hsv.d.ts create mode 100644 types/three/examples/jsm/nodes/materialx/functions/lib/mx_noise.d.ts create mode 100644 types/three/test/nodes/nodes-FunctionNode.ts create mode 100644 types/three/test/nodes/nodes-materialx.ts diff --git a/types/three/examples/jsm/nodes/Nodes.d.ts b/types/three/examples/jsm/nodes/Nodes.d.ts index d194de5ab..7fa559a0f 100644 --- a/types/three/examples/jsm/nodes/Nodes.d.ts +++ b/types/three/examples/jsm/nodes/Nodes.d.ts @@ -7,7 +7,7 @@ import ConstNode from './core/ConstNode'; import ContextNode from './core/ContextNode'; import ExpressionNode from './core/ExpressionNode'; import FunctionCallNode from './core/FunctionCallNode'; -import FunctionNode from './core/FunctionNode'; +import FunctionNode, { FunctionNodeArguments } from './core/FunctionNode'; import InstanceIndexNode from './core/InstanceIndexNode'; import Node from './core/Node'; import NodeAttribute from './core/NodeAttribute'; @@ -118,6 +118,7 @@ export { ExpressionNode, FunctionCallNode, FunctionNode, + FunctionNodeArguments, InstanceIndexNode, Node, NodeAttribute, diff --git a/types/three/examples/jsm/nodes/core/CodeNode.d.ts b/types/three/examples/jsm/nodes/core/CodeNode.d.ts index c24c0e805..7ac8b841b 100644 --- a/types/three/examples/jsm/nodes/core/CodeNode.d.ts +++ b/types/three/examples/jsm/nodes/core/CodeNode.d.ts @@ -3,13 +3,13 @@ import Node from './Node'; import NodeBuilder from './NodeBuilder'; export interface CodeNodeInclude { - build(): void; + build(builder: NodeBuilder): void; } export default class CodeNode extends Node { isCodeNode: true; code: string; - constructor(code: string, nodeType?: NodeTypeOption); + constructor(code?: string, includes?: CodeNodeInclude[]); setIncludes(includes: CodeNodeInclude[]): this; getIncludes(builder: NodeBuilder): CodeNodeInclude[]; diff --git a/types/three/examples/jsm/nodes/core/FunctionCallNode.d.ts b/types/three/examples/jsm/nodes/core/FunctionCallNode.d.ts index 4712e2dc9..6e62e28aa 100644 --- a/types/three/examples/jsm/nodes/core/FunctionCallNode.d.ts +++ b/types/three/examples/jsm/nodes/core/FunctionCallNode.d.ts @@ -2,12 +2,12 @@ import FunctionNode from './FunctionNode'; import TempNode from './TempNode'; import Node from './Node'; -export default class FunctionCallNode extends TempNode { - functionNode: FunctionNode; +export default class FunctionCallNode

extends TempNode { + functionNode: FunctionNode

; parameters: { [name: string]: Node }; - constructor(functionNode?: FunctionNode, parameters?: { [name: string]: Node }); + constructor(functionNode?: FunctionNode

, parameters?: P); - setParameters(parameters: { [name: string]: Node }): this; - getParameters(): { [name: string]: Node }; + setParameters(parameters: P): this; + getParameters(): P; } diff --git a/types/three/examples/jsm/nodes/core/FunctionNode.d.ts b/types/three/examples/jsm/nodes/core/FunctionNode.d.ts index 33ed738e2..9fc5bb593 100644 --- a/types/three/examples/jsm/nodes/core/FunctionNode.d.ts +++ b/types/three/examples/jsm/nodes/core/FunctionNode.d.ts @@ -1,15 +1,17 @@ -import CodeNode from './CodeNode'; +import CodeNode, { CodeNodeInclude } from './CodeNode'; import FunctionCallNode from './FunctionCallNode'; import NodeBuilder from './NodeBuilder'; import NodeFunction from './NodeFunction'; import NodeFunctionInput from './NodeFunctionInput'; import Node from './Node'; -export default class FunctionNode extends CodeNode { +export type FunctionNodeArguments = Node[] | { [name: string]: Node }; + +export default class FunctionNode

extends CodeNode { keywords: { [key: string]: Node }; - constructor(code?: string); + constructor(code?: string, includes?: CodeNodeInclude[]); getInputs(builder: NodeBuilder): NodeFunctionInput[]; getNodeFunction(builder: NodeBuilder): NodeFunction; - call(parameters: { [name: string]: Node }): FunctionCallNode; + call(parameters: P): FunctionCallNode

; } diff --git a/types/three/examples/jsm/nodes/materialx/functions/lib/mx_hsv.d.ts b/types/three/examples/jsm/nodes/materialx/functions/lib/mx_hsv.d.ts new file mode 100644 index 000000000..a4bc45a77 --- /dev/null +++ b/types/three/examples/jsm/nodes/materialx/functions/lib/mx_hsv.d.ts @@ -0,0 +1,4 @@ +import { Fn, Node, Swizzable } from '../../../Nodes'; + +export function mx_hsvtorgb(...params: Fn<[Node]>): Swizzable; +export function mx_rgbtohsv(...params: Fn<[Node]>): Swizzable; diff --git a/types/three/examples/jsm/nodes/materialx/functions/lib/mx_noise.d.ts b/types/three/examples/jsm/nodes/materialx/functions/lib/mx_noise.d.ts new file mode 100644 index 000000000..428957b99 --- /dev/null +++ b/types/three/examples/jsm/nodes/materialx/functions/lib/mx_noise.d.ts @@ -0,0 +1,6 @@ +import { Fn, Node, Swizzable } from '../../../Nodes'; + +export function mx_perlin_noise_float(...params: Fn<[Node]>): Swizzable; +export function mx_cell_noise_float(...params: Fn<[Node]>): Swizzable; +export function mx_worley_noise_float(...params: Fn<[Node]>): Swizzable; +export function mx_fractal_noise_float(...params: Fn<[Node, Node, Node, Node]>): Swizzable; diff --git a/types/three/examples/jsm/nodes/shadernode/ShaderNode.d.ts b/types/three/examples/jsm/nodes/shadernode/ShaderNode.d.ts index 900a3b3e5..849744a47 100644 --- a/types/three/examples/jsm/nodes/shadernode/ShaderNode.d.ts +++ b/types/three/examples/jsm/nodes/shadernode/ShaderNode.d.ts @@ -7,17 +7,19 @@ export type Swizzable = T & }; /** anything that can be passed to {@link nodeObject} and returns a proxy */ -export type NodeRepresentation = number | boolean | Node | Swizzable; +export type NodeRepresentation = number | boolean | Node | Swizzable; /** anything that can be passed to {@link nodeObject} */ export type NodeObjectOption = NodeRepresentation | string; -// same logic as in ShaderNodeObject +// same logic as in ShaderNodeObject: number,boolean,node->swizzable, otherwise do nothing export type NodeObject = T extends Node ? Swizzable : T extends number | boolean ? Swizzable : T; -type MakeObjectOption = T extends Node ? NodeRepresentation : T; +// opposite of NodeObject: node -> node|swizzable|boolean|number, otherwise do nothing +type Proxied = T extends Node ? NodeRepresentation : T; // https://github.com/microsoft/TypeScript/issues/42435#issuecomment-765557874 -type MakeObjectOptions = [...{ [index in keyof T]: MakeObjectOption }]; +export type ProxiedTuple = [...{ [index in keyof T]: Proxied }]; +export type ProxiedObject = { [index in keyof T]: Proxied }; type RemoveTail = T extends [unknown, ...infer X] ? X : []; type RemoveHeadAndTail = T extends [unknown, ...infer X, unknown] ? X : []; @@ -121,22 +123,22 @@ export function nodeArray(obj: readonly [...T]): N export function nodeProxy( nodeClass: T, -): (...params: MakeObjectOptions>) => Swizzable>; +): (...params: ProxiedTuple>) => Swizzable>; export function nodeProxy>( nodeClass: T, scope: S, -): (...params: MakeObjectOptions>>) => Swizzable>; +): (...params: ProxiedTuple>>) => Swizzable>; export function nodeProxy>( nodeClass: T, scope: S, factor: NodeObjectOption, -): (...params: MakeObjectOptions>>) => Swizzable>; +): (...params: ProxiedTuple>>) => Swizzable>; export function nodeImmutable( nodeClass: T, - ...params: MakeObjectOptions> + ...params: ProxiedTuple> ): Swizzable>; export class ShaderNode { diff --git a/types/three/examples/jsm/nodes/shadernode/ShaderNodeBaseElements.d.ts b/types/three/examples/jsm/nodes/shadernode/ShaderNodeBaseElements.d.ts index 3b74fe54e..7c213a740 100644 --- a/types/three/examples/jsm/nodes/shadernode/ShaderNodeBaseElements.d.ts +++ b/types/three/examples/jsm/nodes/shadernode/ShaderNodeBaseElements.d.ts @@ -13,6 +13,8 @@ import { Swizzable, NodeRepresentation, NodeOrType, + ProxiedObject, + ProxiedTuple, } from './ShaderNode'; import { Material, Texture } from '../../../../src/Three'; import { NodeTypeOption, NodeUserData, NodeValueOption } from '../core/constants'; @@ -22,13 +24,14 @@ import { BypassNode, CameraNode, CodeNode, + CodeNodeInclude, ComputeNode, ContextNode, - ConvertNode, ExpressionNode, FrontFacingNode, FunctionCallNode, FunctionNode, + FunctionNodeArguments, InstanceIndexNode, MaterialNode, MaterialReferenceNode, @@ -83,8 +86,6 @@ export const imat4: ConvertType; export const umat4: ConvertType; export const bmat4: ConvertType; -export function func(code: string): ShaderNode; - export function uniform(nodeOrType: Node | Swizzable | NodeValueOption): Swizzable; export function attribute(attributeName: string, nodeType: NodeTypeOption): Swizzable; @@ -95,10 +96,27 @@ export function code(code: string, nodeType?: NodeTypeOption): Swizzable; export function expression(snipped?: string, nodeType?: NodeTypeOption): Swizzable; -export function call( - functionNode?: NodeRepresentation, - parameters?: { [name: string]: Node }, -): Swizzable; +// definition: const call = nodeProxy(FunctionCallNode); +export function call

( + functionNode?: FunctionNode

, + parameters?: ProxiedObject

, +): Swizzable>; + +export type Fn

= P extends readonly [...unknown[]] + ? ProxiedTuple

+ : readonly [ProxiedObject

]; + +// tslint:disable:no-unnecessary-generics +export function func

( + code: string, + includes?: CodeNodeInclude[], +): { call: (...params: Fn

) => Swizzable }; + +export function fn

( + code: string, + includes?: CodeNodeInclude[], +): (...params: Fn

) => Swizzable; +// tslint:enable:no-unnecessary-generics export const instanceIndex: Swizzable; export function label(node: NodeRepresentation, name?: string): Swizzable; diff --git a/types/three/test/nodes/nodes-FunctionNode.ts b/types/three/test/nodes/nodes-FunctionNode.ts new file mode 100644 index 000000000..ea8e67a07 --- /dev/null +++ b/types/three/test/nodes/nodes-FunctionNode.ts @@ -0,0 +1,57 @@ +/** + * Various tests of func, fn and call + */ + +import { + code, + fn, + uv, + func, + Node, + FunctionNode, + call, + Swizzable, + FunctionCallNode, +} from 'three/examples/jsm/nodes/Nodes'; + +import { ProxiedObject } from 'three/examples/jsm/nodes/shadernode/ShaderNode'; + +export const mx_noise = code('whatever'); +const includes = [mx_noise]; + +const someFunc1 = new FunctionNode<[a: Node]>(); +const someFunc2 = new FunctionNode<{ a: Node }>(); + +// tslint:disable-next-line:no-unnecessary-generics +function assertSwizzable(_s: Swizzable) {} + +type a = ProxiedObject; + +assertSwizzable>(call(someFunc1, [1])); +assertSwizzable>(call(someFunc1, [uv()])); +assertSwizzable>(call(someFunc1, [uv().xy])); +assertSwizzable>(call(someFunc2, { a: 1 })); +assertSwizzable>(call(someFunc2, { a: uv() })); +assertSwizzable>(call(someFunc2, { a: uv().xy })); + +export const mx_cell_noise_float_call = func<[Node]>('float mx_cell_noise_float( vec3 p )', includes); +export const mx_worley_noise_float_call = func<[Node, Node, Node]>( + 'float mx_worley_noise_float( vec3 p, float jitter, int metric )', + includes, +); +export const ab_call = func<{ a: Node; b: Node }>('float mx_cell_noise_float( vec3 p )', includes); + +assertSwizzable(mx_cell_noise_float_call.call(uv())); +assertSwizzable(mx_worley_noise_float_call.call(uv(), 1, 1)); +assertSwizzable(ab_call.call({ a: 1, b: uv() })); + +export const mx_cell_noise_float = fn<[Node]>('float mx_cell_noise_float( vec3 p )', includes); +export const mx_worley_noise_float = fn<[Node, Node, Node]>( + 'float mx_worley_noise_float( vec3 p, float jitter, int metric )', + includes, +); +export const ab = fn<{ a: Node; b: Node }>('float mx_cell_noise_float( vec3 p )', includes); + +assertSwizzable(mx_cell_noise_float(uv())); +assertSwizzable(mx_worley_noise_float(uv(), 1, 1)); +assertSwizzable(ab({ a: 1, b: uv() })); diff --git a/types/three/test/nodes/nodes-ShaderNode.ts b/types/three/test/nodes/nodes-ShaderNode.ts index 47ea44467..0bb5fb9b5 100644 --- a/types/three/test/nodes/nodes-ShaderNode.ts +++ b/types/three/test/nodes/nodes-ShaderNode.ts @@ -22,7 +22,6 @@ import { ConvertType, Swizzable } from 'three/examples/jsm/nodes/shadernode/Shad // just to type check // tslint:disable-next-line:no-unnecessary-generics function assertSwizzable(_s: Swizzable) {} -function assertNode(_s: Node) {} export const color = new ConvertType('color'); const s = color(1); diff --git a/types/three/test/nodes/nodes-materialx.ts b/types/three/test/nodes/nodes-materialx.ts new file mode 100644 index 000000000..a1e6a87af --- /dev/null +++ b/types/three/test/nodes/nodes-materialx.ts @@ -0,0 +1,15 @@ +import { + mx_perlin_noise_float, + mx_cell_noise_float, + mx_worley_noise_float, + mx_fractal_noise_float, +} from 'three/examples/jsm/nodes/materialx/functions/lib/mx_noise'; + +import { mx_hsvtorgb, mx_rgbtohsv } from 'three/examples/jsm/nodes/materialx/functions/lib/mx_hsv'; + +mx_perlin_noise_float(1); +mx_cell_noise_float(1); +mx_worley_noise_float(1); +mx_fractal_noise_float(1, 1, 1, 1); +mx_hsvtorgb(1); +mx_rgbtohsv(1); diff --git a/types/three/tsconfig.json b/types/three/tsconfig.json index dac57dc63..4f63754b3 100644 --- a/types/three/tsconfig.json +++ b/types/three/tsconfig.json @@ -63,6 +63,8 @@ "test/misc/misc-gpucomputationrender.ts", "test/nodes/nodes-ColorAdjustmentNode.ts", "test/nodes/nodes-Iridescence.ts", + "test/nodes/nodes-FunctionNode.ts", + "test/nodes/nodes-materialx.ts", "test/nodes/nodes-ShaderNode.ts", "test/nodes/nodes-ShaderNodeBaseElements.ts", "test/nodes/nodes-ShaderNodeElements.ts", From 1f32d5055a79ef4a9b9999a7160cf1bc9b2ddbf6 Mon Sep 17 00:00:00 2001 From: miko Date: Sun, 21 Aug 2022 10:40:16 +0200 Subject: [PATCH 2/3] fix: tests --- package.json | 4 ++-- types/three/OTHER_FILES.txt | 3 +-- types/three/tslint.json | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 99e3005e2..dca9186cc 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "scripts": { "prepare": "husky install", "test-all": "node node_modules/@definitelytyped/dtslint-runner/dist/index.js --path .", - "test": "dtslint types", - "lint": "dtslint types", + "test": "node node_modules/@definitelytyped/dtslint/dist/index.js types/three", + "lint": "node node_modules/@definitelytyped/dtslint/dist/index.js types/three", "prettier:write": "prettier --write .", "prettier:check": "prettier --check .", "contributors:add": "all-contributors add" diff --git a/types/three/OTHER_FILES.txt b/types/three/OTHER_FILES.txt index e83485aa4..d12674f78 100644 --- a/types/three/OTHER_FILES.txt +++ b/types/three/OTHER_FILES.txt @@ -1,5 +1,4 @@ examples/jsm/animation/AnimationClipCreator.d.ts -examples/jsm/csm/CSMFrustum.d.ts examples/jsm/csm/CSMShader.d.ts examples/jsm/curves/NURBSCurve.d.ts examples/jsm/curves/NURBSSurface.d.ts @@ -65,8 +64,8 @@ examples/jsm/loaders/PRWMLoader.d.ts examples/jsm/loaders/PVRLoader.d.ts examples/jsm/loaders/STLLoader.d.ts examples/jsm/loaders/TDSLoader.d.ts +examples/jsm/loaders/TIFFLoader.d.ts examples/jsm/loaders/TiltLoader.d.ts -examples/jsm/loaders/TTFLoader.d.ts examples/jsm/loaders/VRMLLoader.d.ts examples/jsm/loaders/VTKLoader.d.ts examples/jsm/loaders/XYZLoader.d.ts diff --git a/types/three/tslint.json b/types/three/tslint.json index 3db14f85e..794cb4bf3 100644 --- a/types/three/tslint.json +++ b/types/three/tslint.json @@ -1 +1 @@ -{ "extends": "dtslint/dt.json" } +{ "extends": "@definitelytyped/dtslint/dt.json" } From f7828f2d684989c4d83ed4b755d944ced68ef070 Mon Sep 17 00:00:00 2001 From: miko Date: Tue, 23 Aug 2022 00:03:34 +0200 Subject: [PATCH 3/3] style: added myself as a contributor (second try) --- .all-contributorsrc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index ca45394d6..acdb29c62 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -361,14 +361,23 @@ ] }, { - "login": "cosformula", + "login": "cosformula", "name": "cosformula", "avatar_url": "https://avatars.githubusercontent.com/u/18232501?v=4", "profile": "https://github.com/cosformula", - "contributions": [ + "contributions": [ + "code" + ] + }, + { + "login": "miko3k", + "name": "Peter Hanula", + "avatar_url": "https://avatars.githubusercontent.com/u/8658482?v=4", + "profile": "https://github.com/miko3k", + "contributions": [ "code" ] - } + } ], "skipCi": true, "contributorsPerLine": 7