Famous / Famous/engine

More modular context implementation (custom renderers)

Open
#288 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.7k
Forks
249
PR merge metrics
No merged PRs in 30d

Description

Context is a bit hardcoded atm, and it's something I've wanted to change for a while. I would really like to see this refactored so that custom renderers can register their own commands.

For example, currently we handle the GL_AMBIENT_LIGHT command as a part of the switch statement in compositor.

`Context.js`

```
case 'GL_AMBIENT_LIGHT':
if (!this.WebGLRenderer) this.initWebGL();
this.WebGLRenderer.setAmbientLightColor(
path,
commands[++localIterator],
commands[++localIterator],
commands[++localIterator]
);
break;
```

A more modular way to do this would be to have the renderer have a list of its commands to register.

`WebGLRenderer.js`

```
WebGLRenderer.COMMANDS = {
'GL_AMBIENT_LIGHT': 'setAmbientLightColor'
}
```

And context would simply register renderers and all their commands.

`Context.js`

```
this.addRenderer('webgl-default', WebGLRenderer);
```

As far as handling how many arguments are passed in, I would propose that we just pass the path, command array, and local iterator into every handler function. For example:

`WebGLRenderer.js`

```
WebGLRenderer.prototype.setAmbientLightColor = function setAmbientLightColor(path, commands, iterator) {
this.ambientLightColor[0] = commands[++iterator];
this.ambientLightColor[1] = commands[++iterator];
this.ambientLightColor[2] = commands[++iterator];
return iterator;
};
```

I think this would make both adding new renderers and adding new commands easier.

This would also let us avoid having this everywhere in Context:

`Context.js`

```
if (this.webGLRenderer) {
...
}
```

P.S. I know https://github.com/Famous/engine/pull/281 is changing the switch statement but it shouldn't affect this implementation all that much.

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.