Skip to content

Commit

Permalink
update(core): State update check and add ColorLike type
Browse files Browse the repository at this point in the history
  • Loading branch information
sakitam-fdd committed Jul 2, 2022
1 parent 1cdcaf2 commit 6c07801
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/core/State.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WithNull } from '../types';
import { isNull, isUndef } from '../utils';
import Color from '../math/Color';
import Color, { ColorLike } from '../math/Color';
import Base from './Base';
import Renderer from './Renderer';

Expand Down Expand Up @@ -52,7 +52,7 @@ interface StateOptions {

clearAlpha: number;

clearColor: Color;
clearColor: ColorLike;

cullFace: GLenum;

Expand Down Expand Up @@ -399,18 +399,24 @@ export default class State extends Base {
* @param alpha
*/
setClearAlpha (alpha: number) {
this.#state.clearAlpha = alpha;
if (this.#state.clearAlpha !== alpha) {
this.#state.clearAlpha = alpha;
}
}

/**
* 设置清屏颜色和透明度值
* @param color 颜色
* @param alpha 透明度
*/
setClearColor (color: Color, alpha: number) {
this.#state.clearColor = color;
this.#state.clearAlpha = alpha;
this.gl.clearColor(color.r, color.g, color.b, alpha);
setClearColor (color: ColorLike, alpha?: number) {
if (this.#state.clearAlpha !== alpha || this.#state.clearColor !== color) {
this.#state.clearColor = color;
if (!isUndef(alpha) && !isNull(alpha)) {
this.#state.clearAlpha = alpha;
}
this.gl.clearColor(color.r, color.g, color.b, (!isUndef(this.#state.clearAlpha) && !isNull(this.#state.clearAlpha) ? this.#state.clearAlpha : color.a) as number);
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/math/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,10 @@ export default class Color {
.concat(String(this.a), '})');
}
}

export type ColorLike = Color | {
r: number;
g: number;
b: number;
a?: number;
}

0 comments on commit 6c07801

Please sign in to comment.