gpujs / gpujs/gpu.js

[Feature Request] Translating JavaScript classes to GLSL

Open
#474 0 comments 2 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.