Skip to content

tsl transpiler not supporting "switch" #30900

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

Open
Makio64 opened this issue Apr 10, 2025 · 4 comments
Open

tsl transpiler not supporting "switch" #30900

Makio64 opened this issue Apr 10, 2025 · 4 comments
Labels
Enhancement TSL Three.js Shading Language

Comments

@Makio64
Copy link
Contributor

Makio64 commented Apr 10, 2025

Description

currently the transpiler seems not understanding the switch :

vec2 get_vertex(int id)
{
    switch(id){
        case 0: return vec2(1.);
        case 1: return vec2(2.);
        case 2: return vec2(3.);
        case 3: return vec2(4.);
        default: return vec2(0.);
    }
}

transpile to :


import { int, Fn } from 'three/tsl';

export const get_vertex = /*#__PURE__*/ Fn( ( [ id_immutable ] ) => {

	const id = int( id_immutable ).toVar();
	switch( id );

} ).setLayout( {
	name: 'get_vertex',
	type: 'vec2',
	inputs: [
		{ name: 'id', type: 'int' }
	]
} );

Edit : actually i'm not even sure switch is supported by tsl cause it's not in the doc :
https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language

Live example

https://threejs.org/examples/webgpu_tsl_transpiler.html

Version

r175

@Mugen87 Mugen87 added Enhancement TSL Three.js Shading Language labels Apr 10, 2025
@Mugen87
Copy link
Collaborator

Mugen87 commented Apr 13, 2025

Edit : actually i'm not even sure switch is supported by tsl cause it's not in the doc 👍

You are right, there is no Switch()/Case() syntax yet.

You can represent a switch statement always as an if/else statement so we could basically updated the transpiler to do that.

However, given that GLSL and WGSL support switch statements, adding a switch/case syntax could be a worthwhile addition. Maybe like so?

import { Switch, vec2 } from 'three/tsl';

const result = vec2();

Switch( id ).Case( 0, () => {

	result.assign( 1 );

} ).Case( 1, () => {

	result.assign( 2 );

} ).Case( 2, () => {

	result.assign( 3 );

} ).Case( 3, () => {

	result.assign( 4 );

} ).Default( () => {

	result.assign( 0 );

} );

@Makio64
Copy link
Contributor Author

Makio64 commented Apr 26, 2025

@Mugen87 I'm closing this issue, thanks for the fast support on it !

@Makio64 Makio64 closed this as completed Apr 26, 2025
@Mugen87
Copy link
Collaborator

Mugen87 commented Apr 26, 2025

The transpiler does not support the new switch/case syntax though. It might be good for tracking to keep this issue open.

@Makio64
Copy link
Contributor Author

Makio64 commented Apr 26, 2025

@Mugen87 oh right my bad ! thanks !

@Makio64 Makio64 reopened this Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement TSL Three.js Shading Language
Projects
None yet
Development

No branches or pull requests

2 participants