File size: 2,801 Bytes
b797337 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import { Node, TSL } from 'three/webgpu';
class ProxyCallNode extends Node {
static get type() {
return 'ProxyCallNode';
}
constructor( proxyNode, params ) {
super();
this.proxyNode = proxyNode;
this.params = params;
}
setup() {
return this.proxyNode.proxyNode.call( ...this.params );
}
}
export class NodeProxy {
get isNode() {
return true;
}
// getter for the node being proxied to
get proxyNode() {
const { proxyObject, proxyProperty } = this;
const properties = proxyProperty.split( '.' );
let value = proxyObject;
for ( let i = 0, l = properties.length; i < l; i ++ ) {
value = value?.[ properties[ i ] ];
}
if ( value && 'functionNode' in value ) {
return value.functionNode;
} else {
return value ?? null;
}
}
constructor( property, object = null ) {
// store the proxy property and objects so they can be changed later
this.proxyObject = object;
this.proxyProperty = property;
// set up a proxy to redirect all calls to the proxied node in order to avoid replicating
// expected members for all node types.
return new Proxy( this, {
get( target, property ) {
if ( property in target ) {
return Reflect.get( target, property );
} else {
const node = target.proxyNode;
if ( ! node ) {
return undefined;
}
const value = Reflect.get( node, property );
if ( typeof value === 'function' ) {
return value.bind( node );
} else {
return value;
}
}
},
set( target, property, value ) {
if ( property in target ) {
return Reflect.set( target, property, value );
} else {
throw new Error( 'NodeProxy: Cannot set members of proxied nodes.' );
}
},
} );
}
}
export const proxy = ( ...args ) => {
return new NodeProxy( ...args );
};
export const proxyFn = ( ...args ) => {
const nodeProxy = new NodeProxy( ...args );
const fn = ( ...params ) => new ProxyCallNode( nodeProxy, params );
return TSL.nodeProxyConstructor( fn, nodeProxy );
};
//
// A node proxy that falls back to the active context
export class ContextNodeProxy extends NodeProxy {
get proxyNode() {
return super.proxyNode || this.fallbackNode;
}
constructor( path, def ) {
super( path, null );
Object.defineProperty( this, 'fallbackNode', {
value: def,
} );
}
setup( builder ) {
this.proxyObject = builder.getContext();
return this.proxyNode.setup( builder );
}
}
export const contextProxy = ( ...args ) => {
return new ContextNodeProxy( ...args );
};
export const contextProxyFn = ( ...args ) => {
const nodeProxy = new ContextNodeProxy( ...args );
const fn = ( ...params ) => new ProxyCallNode( nodeProxy, params );
return TSL.nodeProxyConstructor( fn, nodeProxy );
};
|