UNPKG

@tolokoban/tgd

Version:

ToloGameDev library for WebGL2

131 lines 4.64 kB
import type { WebglParams } from "../context/webgl-params"; import type { TgdProgram } from "../program"; import type { TgdCodeBloc, TgdCodeFunctions } from "../shader/code"; import { TgdContext, type TgdPainterStateOptions, type WebglAttributeType, type WebglUniformType } from ".."; export interface TgdMaterialContext { context: TgdContext; program: TgdProgram; time: number; delta: number; } export interface TgdMaterialOptions { /** * Name of the vec4 attribute holding the vertex position. * Default to `POSITION`. */ attPosition: string; /** * Name of the vec3 attribute holding the normal. * Default to `NORMAL`. */ attNormal: string; /** Name of the vec3 atribute holding the tangent. * Default to `CUSTOM_ATTRIBUTE_3` */ attTangent: string; /** * Name of the vec2 attribute holding the texture coordinates (UV). * Default to `TEXCOORD_0` */ attUV: string; varyings: { [name: string]: WebglAttributeType; }; uniforms: { [name: string]: WebglUniformType; }; /** * Example: `layout(origin_upper_left) in vec4 gl_FragCoord;` */ fragmentShaderHeader?: TgdCodeBloc | (() => TgdCodeBloc); /** * This is the body of the function `void applyMaterial()` and * it must return the color of the current fragment as a `vec4`. */ fragmentShaderCode: TgdCodeBloc | (() => TgdCodeBloc); extraFragmentShaderFunctions: TgdCodeFunctions | (() => TgdCodeFunctions); vertexShaderCode: TgdCodeBloc | (() => TgdCodeBloc); extraVertexShaderFunctions: TgdCodeFunctions | (() => TgdCodeFunctions); /** * Body of the function `vec4 getPosition(vec4 pos)` of the vertex shader. * * By default, this body is `return pos;`. * Yo can use this code to apply any transformation on the vertex position * before convertion to screen space. */ vertexShaderCodeForGetPosition: TgdCodeBloc | (() => TgdCodeBloc); /** * If this function is defined, it will be called at each frame. * Most of the time, it is used tu update the uniforms. */ setUniforms(materialContext: TgdMaterialContext): void; /** * You may need specific CULL, BLEND, DEPTH, ... */ state: Partial<TgdPainterStateOptions>; /** * Cleanup function. */ delete: () => void; /** * If `true`, then the mesh will output the resulting shaders codes * in the console. */ debug: boolean; } export declare class TgdMaterial { debug: boolean; attPosition: string; attNormal: string; attTangent: string; attUV: string; /** * Example: * ``` * { * varNormal: "vec3", * varUV: "vec2" * } * ``` */ readonly varyings: { [name: string]: WebglAttributeType; }; readonly uniforms: { [name: string]: WebglUniformType; }; private readonly _fragmentShaderCode; private readonly _fragmentShaderHeader; /** * The code of a `vec4 applyMaterial()` function. */ get fragmentShaderCode(): TgdCodeBloc; get fragmentShaderHeader(): TgdCodeBloc; private readonly _extraFragmentShaderFunctions; get extraFragmentShaderFunctions(): TgdCodeFunctions; private readonly _vertexShaderCode; /** * The code of a `void applyMaterial(position, normal, uv)` function. */ get vertexShaderCode(): TgdCodeBloc; private readonly _extraVertexShaderFunctions; get extraVertexShaderFunctions(): TgdCodeFunctions; private readonly _vertexShaderCodeForGetPosition?; /** * Body of the function `vec4 getPosition(vec4 pos)` of the vertex shader. * * By default, this body is `return pos;`. * Yo can use this code to apply any transformation on the vertex position * before convertion to screen space. */ get vertexShaderCodeForGetPosition(): TgdCodeBloc | undefined; readonly setUniforms: undefined | ((materialContext: TgdMaterialContext) => void); protected readonly state: Partial<TgdPainterStateOptions>; applyState(context: { gl: WebGL2RenderingContext; webglParams: WebglParams; }, action: () => void): void; readonly delete: () => void; constructor({ attPosition, attNormal, attTangent, attUV, varyings, uniforms, fragmentShaderCode, fragmentShaderHeader, extraFragmentShaderFunctions, vertexShaderCode, extraVertexShaderFunctions, vertexShaderCodeForGetPosition, setUniforms, state, debug, delete: deleteFunction, }: Partial<TgdMaterialOptions>); } //# sourceMappingURL=material.d.ts.map