@@ -44,6 +44,8 @@ const unaryLib = {
44
44
'--' : 'decrement' // decrementBefore
45
45
} ;
46
46
47
+ const textureLookupFunctions = [ 'texture' , 'texture2D' , 'texture3D' , 'textureCube' , 'textureLod' , 'texelFetch' , 'textureGrad' ] ;
48
+
47
49
const isPrimitive = ( value ) => / ^ ( t r u e | f a l s e | - ? ( \d | \. \d ) ) / . test ( value ) ;
48
50
49
51
class TSLEncoder {
@@ -80,25 +82,46 @@ class TSLEncoder {
80
82
emitUniform ( node ) {
81
83
82
84
let code = `const ${ node . name } = ` ;
85
+ this . global . add ( node . name ) ;
83
86
84
87
if ( this . reference === true ) {
85
88
86
89
this . addImport ( 'reference' ) ;
87
90
88
- this . global . add ( node . name ) ;
89
-
90
91
//code += `reference( '${ node.name }', '${ node.type }', uniforms )`;
91
92
92
93
// legacy
93
94
code += `reference( 'value', '${ node . type } ', uniforms[ '${ node . name } ' ] )` ;
94
95
95
96
} else {
96
97
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' ) {
98
111
99
- this . global . add ( node . name ) ;
112
+ this . addImport ( 'texture3D' ) ;
100
113
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
+ }
102
125
103
126
}
104
127
@@ -173,11 +196,43 @@ class TSLEncoder {
173
196
174
197
}
175
198
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 ] } )` ;
177
204
178
- const paramsStr = params . length > 0 ? ' ' + params . join ( ', ' ) + ' ' : '' ;
205
+ if ( node . name === 'texture' || node . name === 'texture2D' || node . name === 'texture3D' || node . name === 'textureCube' ) {
179
206
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
+ }
181
236
182
237
} else if ( node . isReturn ) {
183
238
0 commit comments