[Feature Request] Translating JavaScript classes to GLSL
- Lingua principale
- JavaScript
- Stelle
- 15.5k
- Fork
- 663
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.