[Feature Request] Translating JavaScript classes to GLSL
- Dominant language
- JavaScript
- Stars
- 15.5k
- Forks
- 663
- PR merge metrics
- No merged PRs in 30d
Description
I recently discovered a simple way to translate JavaScript classes into GLSL., but I'm not sure if gpu.js can do this yet. This is a JavaScript class:
```
class Circle{
constructor(radius){
this.radius = 1;
}
area(){
return Math.PI*this.radius*this.radius;
}
}
```
In GLSL, the same class could be defined in this way:
```
#define PI 3.1415926538
struct Circle{
float radius;
};
Circle new(float radius){
Circle self; //"this" is a reserved word in GLSL, so "self" is used here instead
self.radius = 1.0;
return self;
}
float area(Circle self){
return PI*self.radius*self.radius;
}
```
And then an "instance" of the class can be initialized:
```
Circle anInstance = new(3.0);
float the_area = area(anInstance);
```
Since functions in GLSL can be overloaded, it would be possible to define constructors for multiple "classes" in this way.
Contributor guide
Assessment
This issue has not been assessed yet.