Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: stencil types #246

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/core/Program.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Program {
this.blendFunc = {};
this.blendEquation = {};
this.stencilFunc = {};
this.stencilOp = {}
this.stencilOp = {};

// set default blendFunc if transparent flagged
if (this.transparent && !this.blendFunc.src) {
Expand Down Expand Up @@ -157,12 +157,11 @@ export class Program {
if (this.blendFunc.src) this.gl.renderer.setBlendFunc(this.blendFunc.src, this.blendFunc.dst, this.blendFunc.srcAlpha, this.blendFunc.dstAlpha);
this.gl.renderer.setBlendEquation(this.blendEquation.modeRGB, this.blendEquation.modeAlpha);

if(this.stencilFunc.func || this.stencilOp.stencilFail) this.gl.renderer.enable(this.gl.STENCIL_TEST)
else this.gl.renderer.disable(this.gl.STENCIL_TEST)

this.gl.renderer.setStencilFunc(this.stencilFunc.func, this.stencilFunc.ref, this.stencilFunc.mask)
this.gl.renderer.setStencilOp(this.stencilOp.stencilFail, this.stencilOp.depthFail, this.stencilOp.depthPass)
if (this.stencilFunc.func || this.stencilOp.stencilFail) this.gl.renderer.enable(this.gl.STENCIL_TEST);
else this.gl.renderer.disable(this.gl.STENCIL_TEST);

this.gl.renderer.setStencilFunc(this.stencilFunc.func, this.stencilFunc.ref, this.stencilFunc.mask);
this.gl.renderer.setStencilOp(this.stencilOp.stencilFail, this.stencilOp.depthFail, this.stencilOp.depthPass);
}

use({ flipFaces = false } = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/RenderTarget.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Texture } from './Texture.js'
import { Texture } from './Texture.js';

export class RenderTarget {
constructor(
Expand Down
31 changes: 14 additions & 17 deletions src/core/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,38 +201,35 @@ export class Renderer {
}

setStencilMask(value) {
if(this.state.stencilMask === value) return;
if (this.state.stencilMask === value) return;
this.state.stencilMask = value;
this.gl.stencilMask(value)
this.gl.stencilMask(value);
}

setStencilFunc(func, ref, mask) {

if((this.state.stencilFunc === func) &&
(this.state.stencilRef === ref) &&
(this.state.stencilFuncMask === mask)
) return;

if (
this.state.stencilFunc === func &&
this.state.stencilRef === ref &&
this.state.stencilFuncMask === mask
)
return;
this.state.stencilFunc = func || this.gl.ALWAYS;
this.state.stencilRef = ref || 0;
this.state.stencilFuncMask = mask || 0;

this.gl.stencilFunc(func || this.gl.ALWAYS, ref || 0, mask || 0);
}

setStencilOp(stencilFail, depthFail, depthPass) {

if(this.state.stencilFail === stencilFail &&
if (
this.state.stencilFail === stencilFail &&
this.state.stencilDepthFail === depthFail &&
this.state.stencilDepthPass === depthPass
) return;

)
return;
this.state.stencilFail = stencilFail;
this.state.stencilDepthFail = depthFail;
this.state.stencilDepthPass = depthPass;

this.gl.stencilOp(stencilFail, depthFail, depthPass);

}

activeTexture(value) {
Expand Down Expand Up @@ -371,9 +368,9 @@ export class Renderer {
}

// Same for stencil
if(this.stencil || (!target || target.stencil)) {
if (this.stencil || !target || target.stencil) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DougLilliequist is this supposed to have an and (&&) operator like the depth buffer?

if (this.stencil && (!target || target.stencil)) {

this.enable(this.gl.STENCIL_TEST);
this.setStencilMask(0xff)
this.setStencilMask(0xff);
}

this.gl.clear(
Expand Down
9 changes: 8 additions & 1 deletion types/core/Program.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OGLRenderingContext, BlendFunc, BlendEquation } from './Renderer';
import type { OGLRenderingContext, BlendFunc, BlendEquation, StencilFunc, StencilOp } from './Renderer';

export interface ProgramOptions {
vertex: string;
Expand Down Expand Up @@ -39,6 +39,9 @@ export class Program {
depthFunc: GLenum;
blendFunc: BlendFunc;
blendEquation: BlendEquation;
stencilRef: GLint;
stencilFunc: StencilFunc;
stencilOp: StencilOp;

vertexShader: WebGLShader;
fragmentShader: WebGLShader;
Expand All @@ -55,6 +58,10 @@ export class Program {

setBlendEquation(modeRGB: GLenum, modeAlpha: GLenum): void;

setStencilFunc(func: GLenum, ref: GLint, mask: GLuint): void;

setStencilOp(stencilFail: GLenum, depthFail: GLenum, depthPass: GLenum): void;

applyState(): void;

use(options?: { flipFaces?: boolean }): void;
Expand Down
2 changes: 2 additions & 0 deletions types/core/RenderTarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface RenderTargetOptions {
depthTexture: boolean;
wrapS: GLenum;
wrapT: GLenum;
wrapR: GLenum;
minFilter: GLenum;
magFilter: GLenum;
type: GLenum;
Expand All @@ -30,6 +31,7 @@ export class RenderTarget {
width: number;
height: number;
depth: boolean;
stencil: boolean;
buffer: WebGLFramebuffer;
target: number;

Expand Down
18 changes: 18 additions & 0 deletions types/core/Renderer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export interface BlendEquation {
modeAlpha?: GLenum;
}

export interface StencilFunc {
func: GLenum;
ref: GLint;
mask: GLuint;
}

export interface StencilOp {
stencilFail: GLenum;
depthFail: GLenum;
depthPass: GLenum;
}

export interface Viewport {
x: number;
y: number;
Expand Down Expand Up @@ -130,6 +142,12 @@ export class Renderer {

setDepthFunc(value: GLenum): void;

setStencilMask(value: GLuint): void;

setStencilFunc(func?: GLenum, ref?: GLint, mask?: GLuint): void;

setStencilOp(stencilFail: GLenum, depthFail: GLenum, depthPass: GLenum): void;

activeTexture(value: number): void;

bindFramebuffer(options?: { target?: GLenum; buffer?: WebGLFramebuffer | null }): void;
Expand Down