Skip to content

More modular context implementation (custom renderers) #288

@solomon-gumball

Description

@solomon-gumball

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 #281 is changing the switch statement but it shouldn't affect this implementation all that much.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions