TSL Transpiler: Introduce Linker and improvements#31314
Merged
Conversation
Contributor
|
Test this PR in // Three.js Transpiler r178dev
import { pow2, float, sub, sqrt, EPSILON, max, div, Fn } from 'three/tsl';
// Put here your GLSL code to transpile to TSL:
export const V_GGX_SmithCorrelated = /*#__PURE__*/ Fn( ( [ alpha, dotNL, dotNV ] ) => {
const a2 = pow2( alpha );
const gv = dotNL.mul( sqrt( a2.add( sub( 1.0, a2 ).mul( pow2( dotNV ) ) ) ) );
const gl = dotNV.mul( sqrt( a2.add( sub( 1.0, a2 ).mul( pow2( dotNL ) ) ) ) );
return div( 0.5, max( gv.add( gl ), EPSILON ) );
}, { alpha: 'float', dotNL: 'float', dotNV: 'float', return: 'float' } ); |
Collaborator
|
Other test failing : float mag = dot(p, p);
p = abs(p) / mag + vec3(-0.5, -0.4, -1.487);const mag = dot( p, p );
p.assign( abs( p ).div( mag ).add( vec3( - 0.5.sub( 0.4 ).sub( 1.487 ) ) ) );
|
Contributor
|
Just going through some of the old issues: #30858: TSL Transpiler not working with vec2[4] name vec2[4] marching_square(vec2 uv, float level)
{
vec2 edge[4] = vec2[](vec2(-50.), vec2(-50.),vec2(-50.), vec2(-50.));
return edge;
}// Three.js Transpiler r178dev
import { property } from 'three/tsl';
const [ = property( 'vec2' ), float = property( 'vec2' );#30885: TSL Transpiler not working with struct struct Edges {
vec2 a
vec2 b
vec2 c
vec2 d
}
Edges marching_square(vec2 uv, float level) {
Edges e
e.a = vec2(-50.)
e.b = vec2(-50.)
e.c = vec2(-50.)
e.d = vec2(-50.)
return e
}// Three.js Transpiler r178dev
// Put here your GLSL code to transpile to TSL:
- 50.;#31094: Sampler 2D Conversion Trouble (seems like the sampler issue has been solved, but there's a dangling comma in the layout properties of the main function) varying float nShiftPower;
uniform sampler2D map;
uniform float time;
varying vec2 vUv;
vec4 rgbShift( float n, vec2 uvs, sampler2D m ){
vec4 final = vec4(0.0, 0.0, 0.0, 1.0);
if( n > 0.0 ){
float d1 = n * 0.001;
float d2 = n * 0.002;
vec2 q = uvs;
float x = n * sin(0.3*time+q.y*21.0)*sin(0.7*time+q.y*29.0)*sin(0.3+0.33*time+q.y*31.0)*0.0017;
float R = texture2D(map,vec2(x + uvs.x+ d1 ,uvs.y+ d1)).x+0.05;
float G = texture2D(map,vec2(x + uvs.x ,uvs.y- d2)).y+0.05;
float B = texture2D(map,vec2(x + uvs.x- d2 ,uvs.y )).z+0.05;
float nshiftQuater = n * 0.75;
R += 0.08*texture2D(map,nshiftQuater*vec2(x+0.025, -0.027)+ n * vec2(uvs.x+0.001,uvs.y+0.001)).x;
G += 0.05*texture2D(map,nshiftQuater*vec2(x-0.022, -0.02) + n * vec2(uvs.x+0.000,uvs.y-0.002)).y;
B += 0.08*texture2D(map,nshiftQuater*vec2(x-0.02, -0.018) + n * vec2(uvs.x-0.002,uvs.y+0.000)).z;
final = vec4(R, G, B, 1.0);
}
else {
final = texture2D(m, uvs);
}
return final;
}
void main(){
gl_FragColor = getColor( nShiftPower,vUv, map );
}// Three.js Transpiler r178dev
import { varying, float, texture, uniform, vec2, vec4, mul, sin, add, If, Fn } from 'three/tsl';
const nShiftPower = varying( float(), 'nShiftPower' );
const map = texture( /* <THREE.Texture> */ );
const time = uniform( 'float' );
const vUv = varying( vec2(), 'vUv' );
export const rgbShift = /*#__PURE__*/ Fn( ( [ n, uvs, m ] ) => {
const final = vec4( 0.0, 0.0, 0.0, 1.0 ).toVar();
If( n.greaterThan( 0.0 ), () => {
const d1 = n.mul( 0.001 );
const d2 = n.mul( 0.002 );
const q = uvs;
const x = n.mul( sin( mul( 0.3, time ).add( q.y.mul( 21.0 ) ) ) ).mul( sin( mul( 0.7, time ).add( q.y.mul( 29.0 ) ) ) ).mul( sin( add( 0.3, mul( 0.33, time ) ).add( q.y.mul( 31.0 ) ) ) ).mul( 0.0017 );
const R = map.sample( vec2( x.add( uvs.x ).add( d1 ), uvs.y.add( d1 ) ) ).x.add( 0.05 ).toVar();
const G = map.sample( vec2( x.add( uvs.x ), uvs.y.sub( d2 ) ) ).y.add( 0.05 ).toVar();
const B = map.sample( vec2( x.add( uvs.x.sub( d2 ) ), uvs.y ) ).z.add( 0.05 ).toVar();
const nshiftQuater = n.mul( 0.75 );
R.addAssign( mul( 0.08, map.sample( nshiftQuater.mul( vec2( x.add( 0.025 ), - 0.027 ) ).add( n.mul( vec2( uvs.x.add( 0.001 ), uvs.y.add( 0.001 ) ) ) ) ).x ) );
G.addAssign( mul( 0.05, map.sample( nshiftQuater.mul( vec2( x.sub( 0.022 ), - 0.02 ) ).add( n.mul( vec2( uvs.x.add( 0.000 ), uvs.y.sub( 0.002 ) ) ) ) ).y ) );
B.addAssign( mul( 0.08, map.sample( nshiftQuater.mul( vec2( x.sub( 0.02 ), - 0.018 ) ).add( n.mul( vec2( uvs.x.sub( 0.002 ), uvs.y.add( 0.000 ) ) ) ) ).z ) );
final.assign( vec4( R, G, B, 1.0 ) );
} ).Else( () => {
final.assign( m.sample( uvs ) );
} );
return final;
}, { n: 'float', uvs: 'vec2', m: 'sampler2D', return: 'vec4' } );
export const main = /*#__PURE__*/ Fn( () => {
gl_FragColor.assign( getColor( nShiftPower, vUv, map ) );
}, { , return: 'void' } ); |
Collaborator
Author
|
Thanks for the tests.
Thanks for reminding, I'll leave them to other PRs due to complexity. |
This was referenced Jun 28, 2025
Open
This was referenced Jun 30, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #31274
Description
Many improvements have been made to the transpiler including the
Linkerwhich helps with flow control when encoding in TSL, and in the future in WGSL.In the comparison images below, we can see the code much closer to that created by a programmer than by a string-based transpiler.
Summary
LinkerLinker, as it is possible to know which variable was assigned or accessed.WhilesupportCommentsupportCode
Old
Now