Skip to content
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

Translating JavaScript classes to GLSL #3

Open
jarble opened this issue May 21, 2019 · 0 comments
Open

Translating JavaScript classes to GLSL #3

jarble opened this issue May 21, 2019 · 0 comments

Comments

@jarble
Copy link

jarble commented May 21, 2019

I recently discovered a way to translate JavaScript classes into GLSL, but it seems that js2glsl cannot do this yet.

A JavaScript class could be written like this:

class Stuff{
    
    constructor(a,b){
        this.a=a;
        this.b=b;
    }
    
    add(){
        return this.a + this.b;
    }
}

And then JS2GLSL could generate a struct with a "constructor" and an "instance method:"

struct Stuff{
    float a;
    float b;
};
Stuff new(int a,int b){
    Stuff self;
    self.a=a;
    self.b=b;
    return self;
}
    
int add(Stuff self){
    return self.a+self.b;
}

Then a "class" could be initialized in GLSL:

Stuff an_instance = new(1.0,2.0);

GLSL allows functions to be overloaded, so it's possible to define multiple "classes" and "constructors" in this way. Since this is a reserved word in GLSL, I replaced it with self.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant