Skip to content

Commit

Permalink
feat(webgl): add DefShaderOpts, rename ShaderOpts
Browse files Browse the repository at this point in the history
- add DefShaderOpts
- update defShader(), shaderSourceFromAST()
- rename ShaderOpts => ShaderPresetOpts
- update shader presets
  • Loading branch information
postspectacular committed Oct 8, 2021
1 parent a693a31 commit ef46bf5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
9 changes: 8 additions & 1 deletion packages/webgl/src/api/shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export interface ShaderState {
stencilMask: number;
}

export interface ShaderOpts<T> {
export interface ShaderPresetOpts<T> {
instancePos: string;
instanceColor: string;
color: string;
Expand All @@ -293,6 +293,13 @@ export interface ShaderOpts<T> {
state: Partial<ShaderState>;
}

export interface DefShaderOpts {
/**
* Number of fractional digits for GLSL float literals
*/
prec: number;
}

export interface IShader extends IBind<ModelSpec>, IRelease {
gl: WebGLRenderingContext;
attribs: IObjectOf<ShaderAttrib>;
Expand Down
15 changes: 11 additions & 4 deletions packages/webgl/src/shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { GLSL } from "./api/glsl";
import type { ModelAttributeSpecs, ModelSpec } from "./api/model";
import {
DEFAULT_OUTPUT,
DefShaderOpts,
GLSLDeclPrefixes,
IShader,
ShaderAttrib,
Expand Down Expand Up @@ -203,15 +204,19 @@ export class Shader implements IShader {
}
}

export const defShader = (gl: WebGLRenderingContext, spec: ShaderSpec) => {
export const defShader = (
gl: WebGLRenderingContext,
spec: ShaderSpec,
opts?: Partial<DefShaderOpts>
) => {
const version = isGL2Context(gl)
? GLSLVersion.GLES_300
: GLSLVersion.GLES_100;
const srcVS = isFunction(spec.vs)
? shaderSourceFromAST(spec, "vs", version)
? shaderSourceFromAST(spec, "vs", version, opts)
: prepareShaderSource(spec, "vs", version);
const srcFS = isFunction(spec.fs)
? shaderSourceFromAST(spec, "fs", version)
? shaderSourceFromAST(spec, "fs", version, opts)
: prepareShaderSource(spec, "fs", version);
LOGGER.debug(srcVS);
LOGGER.debug(srcFS);
Expand Down Expand Up @@ -342,7 +347,8 @@ const compileUniformDecls = (spec: ShaderSpec, acc: IObjectOf<Sym<any>>) => {
export const shaderSourceFromAST = (
spec: ShaderSpec,
type: ShaderType,
version: GLSLVersion
version: GLSLVersion,
opts: Partial<DefShaderOpts> = {}
) => {
let prelude = compilePrelude(spec, version);
const inputs: IObjectOf<Sym<any>> = {};
Expand Down Expand Up @@ -374,6 +380,7 @@ export const shaderSourceFromAST = (
type,
version,
prelude,
prec: opts.prec,
});
return (
target(
Expand Down
4 changes: 2 additions & 2 deletions packages/webgl/src/shaders/lambert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import { $ } from "@thi.ng/shader-ast/ast/swizzle";
import { normalize } from "@thi.ng/shader-ast/builtin/math";
import { texture } from "@thi.ng/shader-ast/builtin/texture";
import type { Material } from "../api/material";
import type { ShaderOpts, ShaderSpec } from "../api/shader";
import type { ShaderPresetOpts, ShaderSpec } from "../api/shader";
import { defMaterial } from "../material";
import { autoNormalMatrix2 } from "../matrices";
import { colorAttrib, positionAttrib } from "../utils";

export interface LambertOpts
extends ShaderOpts<Pick<Material, "ambientCol" | "diffuseCol">> {
extends ShaderPresetOpts<Pick<Material, "ambientCol" | "diffuseCol">> {
bidir: boolean;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/webgl/src/shaders/phong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { $ } from "@thi.ng/shader-ast/ast/swizzle";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import { dot, max, normalize, pow } from "@thi.ng/shader-ast/builtin/math";
import type { Material } from "../api/material";
import type { ShaderOpts, ShaderSpec } from "../api/shader";
import type { ShaderPresetOpts, ShaderSpec } from "../api/shader";
import { defMaterial } from "../material";
import { autoNormalMatrix1 } from "../matrices";
import { colorAttrib, positionAttrib } from "../utils";

export type PhongOpts = ShaderOpts<
export type PhongOpts = ShaderPresetOpts<
Pick<Material, "ambientCol" | "diffuseCol" | "specularCol">
>;

Expand Down
6 changes: 3 additions & 3 deletions packages/webgl/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IObjectOf } from "@thi.ng/api";
import type { Sym, Term } from "@thi.ng/shader-ast";
import { add, mul } from "@thi.ng/shader-ast/ast/ops";
import type { ShaderOpts } from "./api/shader";
import type { ShaderPresetOpts } from "./api/shader";

export const positionAttrib = (
opts: Partial<ShaderOpts<any>>,
opts: Partial<ShaderPresetOpts<any>>,
attribs: IObjectOf<Sym<any>>,
pos = "position"
) =>
Expand All @@ -13,7 +13,7 @@ export const positionAttrib = (
: attribs[pos];

export const colorAttrib = (
opts: Partial<ShaderOpts<any>>,
opts: Partial<ShaderPresetOpts<any>>,
attribs: IObjectOf<Sym<any>>,
fallback: Sym<"vec3">
): Term<"vec3"> =>
Expand Down

0 comments on commit ef46bf5

Please sign in to comment.