Skip to content

Commit

Permalink
feat(graphics): add uv scaling option
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Oct 1, 2024
1 parent b338423 commit 93312c5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-nails-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Add instance UV scaling
1 change: 1 addition & 0 deletions packages/ted/src/actor-components/sprite-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default class TSpriteComponent extends TTexturedMeshComponent {
options: {
texture: this.texture.uuid!,
colorFilter: this.colorFilter,
instanceUVScales: this.instanceUVScales,
},
},
layer: this.layer,
Expand Down
3 changes: 3 additions & 0 deletions packages/ted/src/actor-components/textured-mesh-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default class TTexturedMeshComponent extends TSceneComponent {

public colorFilter?: vec4;

public instanceUVScales?: [number, number];

constructor(actor: TActor, bodyOptions?: TPhysicsBodyOptions) {
super(actor, bodyOptions);

Expand All @@ -42,6 +44,7 @@ export default class TTexturedMeshComponent extends TSceneComponent {
options: {
texture: this.texture.uuid!,
colorFilter: this.colorFilter,
instanceUVScales: this.instanceUVScales,
},
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/ted/src/renderer/frame-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface TSerializedTexturedMaterial {
options: {
texture: string;
instanceUVs?: number[];
instanceUVScales?: [number, number];
colorFilter?: vec4;
};
}
6 changes: 6 additions & 0 deletions packages/ted/src/renderer/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ export default class TProgram implements IAsset {
this.program,
'uPalette',
);

this.uniformLocations.uInstanceUVScale = gl.getUniformLocation(
this.program,
'uInstanceUVScale',
);

this.uniformLocations.uEnableInstanceUVs = gl.getUniformLocation(
this.program,
'uEnableInstanceUVs',
Expand Down
4 changes: 2 additions & 2 deletions packages/ted/src/renderer/renderable-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export default class TRenderableTexture {
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, this.filter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, this.filter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.generateMipmap(gl.TEXTURE_2D);
}
Expand Down
14 changes: 13 additions & 1 deletion packages/ted/src/renderer/renderable-textured-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default class TRenderableTexturedMesh {

// Instance buffers, used to override the UVs of the mesh
private instanceUVBuffer?: WebGLBuffer;

private colorFilterUniformLocation?: WebGLUniformLocation;

private vao?: WebGLVertexArrayObject;
Expand All @@ -33,6 +32,7 @@ export default class TRenderableTexturedMesh {
texture: TRenderableTexture,
m: mat4,
instanceUVs?: number[],
instanceUVScales?: [number, number],
colorFilter: vec4 = vec4.fromValues(1, 1, 1, 1),
) {
if (this.positionBuffer === undefined) {
Expand Down Expand Up @@ -95,6 +95,18 @@ export default class TRenderableTexturedMesh {
);
}

if (instanceUVScales) {
gl.uniform2fv(
texturedProgram.program!.uniformLocations.uInstanceUVScale,
new Float32Array(instanceUVScales),
);
} else {
gl.uniform2fv(
texturedProgram.program!.uniformLocations.uInstanceUVScale,
new Float32Array([1, 1]),
);
}

const vertexCount = this.indexes.length;
const type = gl.UNSIGNED_SHORT;
const offset = 0;
Expand Down
2 changes: 2 additions & 0 deletions packages/ted/src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default class TRenderer {
texture,
task.transform,
task.material.options.instanceUVs,
task.material.options.instanceUVScales,
task.material.options.colorFilter,
);
}
Expand All @@ -222,6 +223,7 @@ export default class TRenderer {
texture,
task.transform,
task.material.options.instanceUVs,
task.material.options.instanceUVScales,
task.material.options.colorFilter,
);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/ted/src/shaders/textured.program
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ uniform Settings {

out highp vec2 vUV;

uniform vec2 uInstanceUVScale;

void main() {
gl_Position = uVPMatrix * uMMatrix * aVertexPosition;

vUV = aVertexInstanceUV * uEnableInstanceUVs + aVertexUV * (1.0 - uEnableInstanceUVs);
vUV = (aVertexInstanceUV * uEnableInstanceUVs + aVertexUV * (1.0 - uEnableInstanceUVs)) * uInstanceUVScale;
}
----
#version 300 es
Expand All @@ -33,4 +35,4 @@ out vec4 outputColor;
void main() {
vec4 textureColor = texture(uTexture, vUV);
outputColor = textureColor * uColorFilter;
}
}

0 comments on commit 93312c5

Please sign in to comment.