Skip to content

Commit

Permalink
fix: matrix stack cant scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightre committed Aug 31, 2024
1 parent 9578761 commit 81fee73
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 17 deletions.
5 changes: 4 additions & 1 deletion demo/matrix_stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const linePath = [new Vec2(0, 0), new Vec2(0, 50), new Vec2(120, 100), new Vec2(
const drawGraphicDemo = () => {
rapid.startGraphicDraw()
const s = Math.sin(time)
linePath[1].x = s * 20

rapid.addGraphicVertex(50 + s * 50, 100, red)
rapid.addGraphicVertex(200, 50, blue)
rapid.addGraphicVertex(200 + s * 100, 200, yellow)
Expand All @@ -35,6 +37,7 @@ const drawGraphicDemo = () => {
}
const drawMatrixStackDemo = () => {
const s = Math.sin(time)

rapid.matrixStack.translate(150, 50)
rapid.save() // save matrixStack
rapid.matrixStack.rotate(s * 0.5 + 0.1)
Expand All @@ -60,12 +63,12 @@ const drawMatrixStackDemo = () => {
rapid.renderSprite(cat, 0, 0, red)
rapid.renderSprite(text, 200, 0)
text.setText("time:" + Math.round(time))
linePath[1].x = s * 20
}
let time = 0

const render = () => {
time += 0.1

rapid.startRender()

drawMatrixStackDemo()
Expand Down
5 changes: 5 additions & 0 deletions dist/interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,10 @@ export interface IRenderLineOptions extends ILineOptions {
points: Vec2[];
color?: Color;
}
export interface IGraphicOptions {
points: Vec2[];
color?: Color;
drawType?: number;
}
export type UniformType = Record<string, number | Array<any>>;
export type Images = ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas;
2 changes: 1 addition & 1 deletion dist/math.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export declare class MatrixStack extends DynamicArrayBuffer {
* @param x - The amount to scale the matrix horizontally.
* @param y - The amount to scale the matrix vertically. If not specified, x is used for both horizontal and vertical scaling.
*/
scale(x: number | Vec2, y: number): void;
scale(x: number | Vec2, y?: number): void;
apply(x: number | Vec2, y: number): Vec2 | number[];
getInverse(): Float32Array;
getTransform(): Float32Array;
Expand Down
2 changes: 1 addition & 1 deletion dist/rapid.global.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/rapid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/rapid.umd.cjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/render.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRapiadOptions, IRenderLineOptions, IRenderSpriteOptions, WebGLContext } from "./interface";
import { IGraphicOptions, IRapiadOptions, IRenderLineOptions, IRenderSpriteOptions, WebGLContext } from "./interface";
import { Color, MatrixStack, Vec2 } from "./math";
import RenderRegion from "./regions/region";
import { Texture, TextureCache } from "./texture";
Expand Down Expand Up @@ -75,7 +75,7 @@ declare class Rapid {
*/
renderSprite(texture: Texture, offsetX?: number, offsetY?: number, options?: IRenderSpriteOptions | Color): void;
renderLine(offsetX: number | undefined, offsetY: number | undefined, options: IRenderLineOptions): void;
renderGraphic(offsetX: number | undefined, offsetY: number | undefined, vertexs: Vec2[], color?: Color, drawType?: number): void;
renderGraphic(offsetX: number | undefined, offsetY: number | undefined, options: IGraphicOptions | Vec2[]): void;
/**
* Starts a graphic drawing process with an optional custom shader.
* @param customShader - An optional custom shader to use.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rapid-render",
"version": "0.0.7",
"version": "0.0.8",
"type": "module",
"files": [
"dist"
Expand Down
5 changes: 5 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,10 @@ export interface IRenderLineOptions extends ILineOptions {
points: Vec2[],
color?: Color
}
export interface IGraphicOptions {
points: Vec2[],
color?: Color,
drawType?: number
}
export type UniformType = Record<string, number | Array<any>>
export type Images = ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas
4 changes: 3 additions & 1 deletion src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ export class MatrixStack extends DynamicArrayBuffer {
* @param x - The amount to scale the matrix horizontally.
* @param y - The amount to scale the matrix vertically. If not specified, x is used for both horizontal and vertical scaling.
*/
scale(x: number | Vec2, y: number): void {
scale(x: number | Vec2, y?: number): void {
if (x instanceof Vec2) {
return this.scale(x.x, x.y)
}
if (!y) y = x
debugger
const offset = this.usedElemNum - MATRIX_SIZE
const arr = this.typedArray
arr[offset + 0] = arr[offset + 0] * x;
Expand Down
19 changes: 11 additions & 8 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRapiadOptions, IRenderLineOptions, IRenderSpriteOptions, WebGLContext } from "./interface"
import { IGraphicOptions, IRapiadOptions, IRenderLineOptions, IRenderSpriteOptions, WebGLContext } from "./interface"
import { getStrokeGeometry } from "./line"
import { Color, MatrixStack, Vec2 } from "./math"
import GraphicRegion from "./regions/graphic_region"
Expand Down Expand Up @@ -164,16 +164,19 @@ class Rapid {
}

renderLine(offsetX: number = 0, offsetY: number = 0, options: IRenderLineOptions) {
const vertexs = getStrokeGeometry(options.points, options);
this.renderGraphic(offsetX, offsetY, vertexs, options.color, this.gl.TRIANGLES)
const points = getStrokeGeometry(options.points, options);
this.renderGraphic(offsetX, offsetY, { color: options.color, drawType: this.gl.TRIANGLES, points })
}
renderGraphic(offsetX: number = 0, offsetY: number = 0, vertexs: Vec2[], color?: Color, drawType?: number) {
renderGraphic(offsetX: number = 0, offsetY: number = 0, options: IGraphicOptions | Vec2[]): void {
if (options instanceof Array) {
return this.renderGraphic(offsetX, offsetY, { points: options })
}
this.startGraphicDraw()
if (drawType) {
(this.currentRegion as GraphicRegion).drawType = drawType
if (options.drawType) {
(this.currentRegion as GraphicRegion).drawType = options.drawType
}
vertexs.forEach(vec => {
this.addGraphicVertex(vec.x + offsetX, vec.y + offsetY, color || this.defaultColorBlack)
options.points.forEach(vec => {
this.addGraphicVertex(vec.x + offsetX, vec.y + offsetY, options.color || this.defaultColorBlack)
})
this.endGraphicDraw()
}
Expand Down

0 comments on commit 81fee73

Please sign in to comment.