Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Fn to return void #1464

Merged
merged 6 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions examples-testing/changes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -13549,6 +13549,65 @@ index 3331adc8..17d73367 100644
const time = (currentTime - startTime) / 1000;

object.position.y = 0.8;
diff --git a/examples-testing/examples/webgpu_compute_audio.ts b/examples-testing/examples/webgpu_compute_audio.ts
index 4e567e9c..847c42ef 100644
--- a/examples-testing/examples/webgpu_compute_audio.ts
+++ b/examples-testing/examples/webgpu_compute_audio.ts
@@ -1,17 +1,27 @@
-import * as THREE from 'three';
-import { Fn, uniform, instanceIndex, instancedArray, float, texture, screenUV, color } from 'three/tsl';
+import * as THREE from 'three/webgpu';
+import {
+ Fn,
+ uniform,
+ instanceIndex,
+ instancedArray,
+ float,
+ texture,
+ screenUV,
+ color,
+ ShaderNodeObject,
+} from 'three/tsl';

import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

-let camera, scene, renderer;
-let computeNode;
-let waveBuffer, sampleRate;
-let waveArray;
-let currentAudio, currentAnalyser;
+let camera: THREE.PerspectiveCamera, scene: THREE.Scene, renderer: THREE.WebGPURenderer;
+let computeNode: ShaderNodeObject<THREE.ComputeNode>;
+let waveBuffer: Float32Array, sampleRate: number;
+let waveArray: ShaderNodeObject<THREE.StorageBufferNode>;
+let currentAudio: AudioBufferSourceNode, currentAnalyser: AnalyserNode;
const analyserBuffer = new Uint8Array(1024);
-let analyserTexture;
+let analyserTexture: THREE.DataTexture;

-const startButton = document.getElementById('startButton');
+const startButton = document.getElementById('startButton')!;
startButton.addEventListener('click', init);

async function playAudioBuffer() {
@@ -46,7 +56,7 @@ async function playAudioBuffer() {
}

async function init() {
- const overlay = document.getElementById('overlay');
+ const overlay = document.getElementById('overlay')!;
overlay.remove();

// audio buffer
@@ -92,7 +102,7 @@ async function init() {

const time = index.mul(pitch);

- let wave = originalWave.element(time);
+ let wave: ShaderNodeObject<THREE.Node> = originalWave.element(time);

// delay

diff --git a/examples-testing/examples/webgpu_custom_fog_background.ts b/examples-testing/examples/webgpu_custom_fog_background.ts
index baca16cb..b01cd750 100644
--- a/examples-testing/examples/webgpu_custom_fog_background.ts
Expand Down
1 change: 0 additions & 1 deletion examples-testing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const exceptionList = [
'webgl_worker_offscreencanvas',
'webgpu_backdrop',
'webgpu_backdrop_water',
'webgpu_compute_audio',
'webgpu_compute_birds',
'webgpu_compute_geometry',
'webgpu_compute_particles',
Expand Down
5 changes: 3 additions & 2 deletions types/three/src/nodes/accessors/Arrays.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TypedArray } from "../../core/BufferAttribute.js";
import { ShaderNodeObject } from "../tsl/TSLCore.js";
import StorageBufferNode from "./StorageBufferNode.js";

export const attributeArray: (count: number, type?: string) => ShaderNodeObject<StorageBufferNode>;
export const attributeArray: (count: TypedArray | number, type?: string) => ShaderNodeObject<StorageBufferNode>;

export const instancedArray: (count: number, type?: string) => ShaderNodeObject<StorageBufferNode>;
export const instancedArray: (count: TypedArray | number, type?: string) => ShaderNodeObject<StorageBufferNode>;
8 changes: 4 additions & 4 deletions types/three/src/nodes/accessors/BufferNode.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import UniformNode from "../core/UniformNode.js";
import { NodeOrType, ShaderNodeObject } from "../tsl/TSLCore.js";

export default class BufferNode extends UniformNode<unknown> {
export default class BufferNode<TValue> extends UniformNode<TValue> {
isBufferNode: true;

bufferType: string;
bufferCount: number;

constructor(value: unknown, bufferType: string, bufferCount?: number);
constructor(value: TValue, bufferType: string, bufferCount?: number);
}

export const buffer: (
export const buffer: <TValue>(
value: unknown,
nodeOrType: NodeOrType,
count: number,
) => ShaderNodeObject<BufferNode>;
) => ShaderNodeObject<BufferNode<TValue>>;
2 changes: 1 addition & 1 deletion types/three/src/nodes/accessors/StorageBufferNode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NodeRepresentation, ShaderNodeObject } from "../tsl/TSLCore.js";
import StorageArrayElementNode from "../utils/StorageArrayElementNode.js";
import BufferNode from "./BufferNode.js";

export default class StorageBufferNode extends BufferNode {
export default class StorageBufferNode extends BufferNode<StorageBufferAttribute | StorageInstancedBufferAttribute> {
readonly isStorageBufferNode: true;

access: NodeAccess;
Expand Down
2 changes: 1 addition & 1 deletion types/three/src/nodes/accessors/UniformArrayNode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare class UniformArrayElementNode extends ArrayElementNode {
constructor(arrayBuffer: Node, indexNode: Node);
}

declare class UniformArrayNode extends BufferNode {
declare class UniformArrayNode extends BufferNode<unknown[]> {
array: unknown[];
elementType: string | null;
paddedType: string;
Expand Down
29 changes: 17 additions & 12 deletions types/three/src/nodes/tsl/TSLCore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ type ConstructedNode<T> = T extends new(...args: any[]) => infer R ? (R extends

export type NodeOrType = Node | string;

declare class ShaderCallNodeInternal extends Node {
}

declare class ShaderNodeInternal extends Node {}

export const defined: (v: unknown) => unknown;

export const getConstNodeType: (value: NodeOrType) => string | null;
Expand Down Expand Up @@ -215,20 +220,20 @@ interface Layout {
inputs: { name: string; type: string }[];
}

interface ShaderNodeFn<Args extends readonly unknown[], R extends Node = ShaderNodeObject<Node>> {
(...args: Args): R;
shaderNode: R;
setLayout: (layout: Layout) => ShaderNodeFn<Args, R>;
once: () => ShaderNodeFn<Args, R>;
interface ShaderNodeFn<Args extends readonly unknown[]> {
(...args: Args): ShaderNodeObject<ShaderCallNodeInternal>;
shaderNode: ShaderNodeObject<ShaderNodeInternal>;
setLayout: (layout: Layout) => this;
once: () => this;
}

export function Fn<R extends Node = ShaderNodeObject<Node>>(jsFunc: () => R): ShaderNodeFn<[], R>;
export function Fn<T extends any[], R extends Node = ShaderNodeObject<Node>>(
jsFunc: (args: T) => R,
): ShaderNodeFn<ProxiedTuple<T>, R>;
export function Fn<T extends { [key: string]: unknown }, R extends Node = ShaderNodeObject<Node>>(
jsFunc: (args: T) => R,
): ShaderNodeFn<[ProxiedObject<T>], R>;
export function Fn(jsFunc: () => void): ShaderNodeFn<[]>;
export function Fn<T extends readonly unknown[]>(
jsFunc: (args: T) => void,
): ShaderNodeFn<ProxiedTuple<T>>;
export function Fn<T extends { readonly [key: string]: unknown }>(
jsFunc: (args: T) => void,
): ShaderNodeFn<[ProxiedObject<T>]>;

/**
* @deprecated tslFn() has been renamed to Fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { BufferAttribute, TypedArray } from "../../core/BufferAttribute.js";
export default class StorageBufferAttribute extends BufferAttribute {
readonly isStorageBufferAttribute: true;

constructor(array: TypedArray, itemSize: number);
constructor(array: TypedArray | number, itemSize: number);
}
Loading