Skip to content

Commit

Permalink
feat(webgl): add DrawFlags opts for draw()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 12, 2020
1 parent 0e5cc2b commit 800382b
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions packages/webgl/src/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,33 @@ import { error } from "./error";
import { bindTextures } from "./texture";
import type { ModelSpec } from "./api/model";

export const draw = (specs: ModelSpec | ModelSpec[]) => {
export interface DrawFlags {
/**
* Unless false (default: true), bind modelspec's textures
*/
tex: boolean;
/**
* Unless false (default: true), bind modelspec's shader
*/
shader: boolean;
/**
* Unless false (default: true), apply shader's `state` opts
*/
state: boolean;
}

export const draw = (
specs: ModelSpec | ModelSpec[],
opts: Partial<DrawFlags> = {}
) => {
const _specs = isArray(specs) ? specs : [specs];
for (let i = 0, n = _specs.length; i < n; i++) {
const spec = _specs[i];
const indices = spec.indices;
const gl = spec.shader.gl;
spec.textures && bindTextures(spec.textures);
spec.shader.prepareState();
spec.shader.bind(spec);
opts.tex !== false && spec.textures && bindTextures(spec.textures);
opts.state !== false && spec.shader.prepareState();
opts.shader !== false && spec.shader.bind(spec);
if (indices && indices.buffer) {
indices.buffer.bind();
if (spec.instances) {
Expand All @@ -34,7 +52,7 @@ export const draw = (specs: ModelSpec | ModelSpec[]) => {
gl.drawArrays(spec.mode!, 0, spec.num);
}
}
spec.shader.unbind(<any>null);
opts.shader !== false && spec.shader.unbind(<any>null);
}
};

Expand Down

0 comments on commit 800382b

Please sign in to comment.