Skip to content

Commit 27a593b

Browse files
authored
TSL Transpiler: Basic texture support. (#30521)
* TSL Transpiler: Basic texture support. * TSLEncoder: Fix placeholders.
1 parent 9c01215 commit 27a593b

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

examples/jsm/transpiler/GLSLDecoder.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const glslToTSL = {
3434
inversesqrt: 'inverseSqrt'
3535
};
3636

37+
const samplers = [ 'sampler1D', 'sampler2D', 'sampler2DArray', 'sampler2DShadow', 'sampler2DArrayShadow', 'isampler2D', 'isampler2DArray', 'usampler2D', 'usampler2DArray' ];
38+
const samplersCube = [ 'samplerCube', 'samplerCubeShadow', 'usamplerCube', 'isamplerCube' ];
39+
const samplers3D = [ 'sampler3D', 'isampler3D', 'usampler3D' ];
40+
3741
const spaceRegExp = /^((\t| )\n*)+/;
3842
const lineRegExp = /^\n+/;
3943
const commentRegExp = /^\/\*[\s\S]*?\*\//;
@@ -719,9 +723,15 @@ class GLSLDecoder {
719723

720724
const tokens = this.readTokensUntil( ';' );
721725

722-
const type = tokens[ 1 ].str;
726+
let type = tokens[ 1 ].str;
723727
const name = tokens[ 2 ].str;
724728

729+
// GLSL to TSL types
730+
731+
if ( samplers.includes( type ) ) type = 'texture';
732+
else if ( samplersCube.includes( type ) ) type = 'cubeTexture';
733+
else if ( samplers3D.includes( type ) ) type = 'texture3D';
734+
725735
return new Uniform( type, name );
726736

727737
}

examples/jsm/transpiler/TSLEncoder.js

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const unaryLib = {
4444
'--': 'decrement' // decrementBefore
4545
};
4646

47+
const textureLookupFunctions = [ 'texture', 'texture2D', 'texture3D', 'textureCube', 'textureLod', 'texelFetch', 'textureGrad' ];
48+
4749
const isPrimitive = ( value ) => /^(true|false|-?(\d|\.\d))/.test( value );
4850

4951
class TSLEncoder {
@@ -80,25 +82,46 @@ class TSLEncoder {
8082
emitUniform( node ) {
8183

8284
let code = `const ${ node.name } = `;
85+
this.global.add( node.name );
8386

8487
if ( this.reference === true ) {
8588

8689
this.addImport( 'reference' );
8790

88-
this.global.add( node.name );
89-
9091
//code += `reference( '${ node.name }', '${ node.type }', uniforms )`;
9192

9293
// legacy
9394
code += `reference( 'value', '${ node.type }', uniforms[ '${ node.name }' ] )`;
9495

9596
} else {
9697

97-
this.addImport( 'uniform' );
98+
if ( node.type === 'texture' ) {
99+
100+
this.addImport( 'texture' );
101+
102+
code += 'texture( /* <THREE.Texture> */ )';
103+
104+
} else if ( node.type === 'cubeTexture' ) {
105+
106+
this.addImport( 'cubeTexture' );
107+
108+
code += 'cubeTexture( /* <THREE.CubeTexture> */ )';
109+
110+
} else if ( node.type === 'texture3D' ) {
98111

99-
this.global.add( node.name );
112+
this.addImport( 'texture3D' );
100113

101-
code += `uniform( '${ node.type }' )`;
114+
code += 'texture3D( /* <THREE.Data3DTexture> */ )';
115+
116+
} else {
117+
118+
// default uniform
119+
120+
this.addImport( 'uniform' );
121+
122+
code += `uniform( '${ node.type }' )`;
123+
124+
}
102125

103126
}
104127

@@ -173,11 +196,43 @@ class TSLEncoder {
173196

174197
}
175198

176-
this.addImport( node.name );
199+
// handle texture lookup function calls in separate branch
200+
201+
if ( textureLookupFunctions.includes( node.name ) ) {
202+
203+
code = `${ params[ 0 ] }.sample( ${ params[ 1 ] } )`;
177204

178-
const paramsStr = params.length > 0 ? ' ' + params.join( ', ' ) + ' ' : '';
205+
if ( node.name === 'texture' || node.name === 'texture2D' || node.name === 'texture3D' || node.name === 'textureCube' ) {
179206

180-
code = `${ node.name }(${ paramsStr })`;
207+
if ( params.length === 3 ) {
208+
209+
code += `.bias( ${ params[ 2 ] } )`;
210+
211+
}
212+
213+
} else if ( node.name === 'textureLod' ) {
214+
215+
code += `.level( ${ params[ 2 ] } )`;
216+
217+
} else if ( node.name === 'textureGrad' ) {
218+
219+
code += `.grad( ${ params[ 2 ] }, ${ params[ 3 ] } )`;
220+
221+
} else if ( node.name === 'texelFetch' ) {
222+
223+
code += '.setSampler( false )';
224+
225+
}
226+
227+
} else {
228+
229+
this.addImport( node.name );
230+
231+
const paramsStr = params.length > 0 ? ' ' + params.join( ', ' ) + ' ' : '';
232+
233+
code = `${ node.name }(${ paramsStr })`;
234+
235+
}
181236

182237
} else if ( node.isReturn ) {
183238

0 commit comments

Comments
 (0)