Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: add pivot/anchor point support for Sprite component

esenginea945174385bd
diff --git a/editor/src/bounds/BoundsProvider.ts b/editor/src/bounds/BoundsProvider.ts
index 77cd80048..33b26ec8c 100644
--- a/editor/src/bounds/BoundsProvider.ts
+++ b/editor/src/bounds/BoundsProvider.ts
@@ -6,6 +6,8 @@
export interface Bounds {
width: number;
height: number;
+ offsetX?: number;
+ offsetY?: number;
}
export interface BoundsProvider {
diff --git a/editor/src/bounds/SpriteBoundsProvider.ts b/editor/src/bounds/SpriteBoundsProvider.ts
index d9c9f34cd..f66b9f15b 100644
--- a/editor/src/bounds/SpriteBoundsProvider.ts
+++ b/editor/src/bounds/SpriteBoundsProvider.ts
@@ -8,7 +8,14 @@ import type { Bounds, BoundsProvider } from './BoundsProvider';
export const spriteBoundsProvider: BoundsProvider = {
getBounds(data: any): Bounds | null {
if (data?.size?.x && data?.size?.y) {
- return { width: data.size.x, height: data.size.y };
+ const pivotX = data.pivot?.x ?? 0.5;
+ const pivotY = data.pivot?.y ?? 0.5;
+ return {
+ width: data.size.x,
+ height: data.size.y,
+ offsetX: (0.5 - pivotX) * data.size.x,
+ offsetY: (0.5 - pivotY) * data.size.y,
+ };
}
return null;
}
diff --git a/editor/src/gizmos/PivotOverlay.ts b/editor/src/gizmos/PivotOverlay.ts
new file mode 100644
index 000000000..be9ec681c
--- /dev/null
+++ b/editor/src/gizmos/PivotOverlay.ts
@@ -0,0 +1,229 @@
+import type { Entity } from 'esengine';
+import type { EditorStore } from '../store/EditorStore';
+import type { EntityData } from '../types/SceneTypes';
+import { quatToEuler } from '../math/Transform';
+
+const PIVOT_COLOR = 'rgba(255, 165, 0, 0.9)';
+const PIVOT_COLOR_HOVER = 'rgba(255, 200, 50, 1.0)';
+const HANDLE_RADIUS_PX = 5;
+const CROSSHAIR_SIZE_PX = 10;
+const HIT_RADIUS_PX = 8;
+
+export interface PivotOverlayContext {
+ ctx: CanvasRenderingContext2D;
+ zoom: number;
+ store: EditorStore;
+}
+
+interface DragState {
+ entity: Entity;
+ startWorldX: number;
+ startWorldY: number;
+ originalPivot: { x: number; y: number };
+}
+
+export class PivotOverlay {
+ private dragState_: DragState | null = null;
+ private hovered_ = false;
+
+ drawAll(octx: PivotOverlayContext): void {
+ const { store } = octx;
+ if (store.selectedEntities.size === 0) return;
+
+ for (const entityId of store.selectedEntities) {
+ const entityData = store.getEntityData(entityId);
+ if (!entityData) continue;
+ if (!store.isEntityVisible(entityId)) continue;
+
+ const spriteComp = entityData.components.find(c => c.type === 'Sprite');
+ if (!spriteComp) continue;
+
+ this.drawPivotHandle(octx, entityData, spriteComp.data);
+ }
+ }
+
+ hitTest(worldX: number, worldY: number, octx: PivotOverlayContext): boolean {
+ const { store } = octx;
+ if (store.selectedEntities.size === 0) return false;
+
+ for (const entityId of store.selectedEntities) {
+ const entityData = store.getEntityData(entityId);
+ if (!entityData) continue;
+
+ const spriteComp = entityData.components.find(c => c.type === 'Sprite');
+ if (!spriteComp) continue;
+
+ if (this.isNearPivot(worldX, worldY, octx, entityData, spriteComp.data)) {
+ this.hovered_ = true;
+ return true;
+ }
+ }
+
+ this.hovered_ = false;
+ return false;
+ }
+
+ onDragStart(worldX: number, worldY: number, octx: PivotOverlayContext): boolean {
+ const { store } = octx;
+
+ for (const entityId of store.selectedEntities) {
+ const entityData = store.getEntityData(entityId);
+ if (!entityData) continue;
+
+ const spriteComp = entityData.components.find(c => c.type === 'Sprite');
+ if (!spriteComp) continue;
+
+ if (this.isNearPivot(worldX, worldY, octx, entityData, spriteComp.data)) {
+ const pivot = spriteComp.data.pivot as { x: number; y: number } | undefined;
+ this.dragState_ = {
+ entity: entityId as Entity,
+ startWorldX: worldX,
+ startWorldY: worldY,
+ originalPivot: { x: pivot?.x ?? 0.5, y: pivot?.y ?? 0.5 },
+ };
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ onDrag(worldX: number, worldY: number, octx: PivotOverlayContext): void {
+ if (!this.dragState_) return;
+
+ const { store } = octx;
+ const { entity, startWorldX, startWorldY, originalPivot } = this.dragState_;
+
+ const worldTransform = store.getWorldTransform(entity as number);
+ const scale = worldTransform.scale;
+ const euler = quatToEuler(worldTransform.rotation);
+ const rad = euler.z * Math.PI / 180;
+
+ const dx = worldX - startWorldX;
+ const dy = worldY - startWorldY;
+ const localDx = dx * Math.cos(rad) + dy * Math.sin(rad);
+ const localDy = -dx * Math.sin(rad) + dy * Math.cos(rad);
+
+ const comp = store.getComponent(entity, 'Sprite');
+ if (!comp) return;
+
+ const size = comp.data.size as { x: number; y: number };
+ const sizeWorldX = size.x * Math.abs(scale.x);
+ const sizeWorldY = size.y * Math.abs(scale.y);
+
+ if (sizeWorldX === 0 || sizeWorldY === 0) return;
+
+ const newPivot = {
+ x: Math.max(0, Math.min(1, originalPivot.x - localDx / sizeWorldX)),
+ y: Math.max(0, Math.min(1, originalPivot.y - localDy / sizeWorldY)),
+ };
+
+ store.updatePropertyDirect(entity, 'Sprite', 'pivot', newPivot);
+ }
+
+ onDragEnd(octx: PivotOverlayContext): void {
+ if (!this.dragState_) return;
+
+ const { entity, originalPivot } = this.dragState_;
+ const { store } = octx;
+ const comp = store.getComponent(entity, 'Sprite');
+
+ if (comp) {
+ const newPivot = comp.data.pivot as { x: number; y: number };
+ if (originalPivot.x !== newPivot.x || originalPivot.y !== newPivot.y) {
+ store.updateProperty(entity, 'Sprite', 'pivot', originalPivot, newPivot);
+ }
+ }
+
+ this.dragState_ = null;
+ }
+
+ isDragging(): boolean {
+ return this.dragState_ !== null;
+ }
+
+ getCursor(): string {
+ if (this.dragState_) return 'grabbing';
+ if (this.hovered_) return 'grab';
+ return '';
+ }
+
+ private drawPivotHandle(
+ octx: PivotOverlayContext,
+ entityData: EntityData,
+ spriteData: Record<string, unknown>
+ ): void {
+ const { ctx, zoom, store } = octx;
+ const worldTransform = store.getWorldTransform(entityData.id);
+ const pos = worldTransform.position;
+ const scale = worldTransform.scale;
+ const euler = quatToEuler(worldTransform.rotation);
+ const rad = -euler.z * Math.PI / 180;
+
+ const size = spriteData.size as { x: number; y: number };
+ const pivot = spriteData.pivot as { x: number; y: number } | undefined;
+ const px = pivot?.x ?? 0.5;
+ const py = pivot?.y ?? 0.5;
+
+ const offsetX = (0.5 - px) * size.x * scale.x;
+ const offsetY = (0.5 - py) * size.y * scale.y;
+
+ ctx.save();
+ ctx.translate(pos.x, -pos.y);
+ ctx.rotate(rad);
+ ctx.translate(offsetX, -offsetY);
+
+ const color = this.hovered_ || this.dragState_ ? PIVOT_COLOR_HOVER : PIVOT_COLOR;
+ const r = HANDLE_RADIUS_PX / zoom;
+ const cross = CROSSHAIR_SIZE_PX / zoom;
+
+ ctx.strokeStyle = color;
+ ctx.fillStyle = color;
+ ctx.lineWidth = 1.5 / zoom;
+
+ ctx.beginPath();
+ ctx.arc(0, 0, r, 0, Math.PI * 2);
+ ctx.fill();
+
+ ctx.beginPath();
+ ctx.moveTo(-cross, 0);
+ ctx.lineTo(cross, 0);
+ ctx.moveTo(0, -cross);
+ ctx.lineTo(0, cross);
+ ctx.stroke();
+
+ ctx.restore();
+ }
+
+ private isNearPivot(
+ worldX: number,
+ worldY: number,
+ octx: PivotOverlayContext,
+ entityData: EntityData,
+ spriteData: Record<string, unknown>
+ ): boolean {
+ const { zoom, store } = octx;
+ const worldTransform = store.getWorldTransform(entityData.id);
+ const pos = worldTransform.position;
+ const scale = worldTransform.scale;
+ const euler = quatToEuler(worldTransform.rotation);
+ const rad = euler.z * Math.PI / 180;
+
+ const size = spriteData.size as { x: number; y: number };
+ const pivot = spriteData.pivot as { x: number; y: number } | undefined;
+ const px = pivot?.x ?? 0.5;
+ const py = pivot?.y ?? 0.5;
+
+ const offsetX = (0.5 - px) * size.x * scale.x;
+ const offsetY = (0.5 - py) * size.y * scale.y;
+
+ const pivotWorldX = pos.x + offsetX * Math.cos(rad) - offsetY * Math.sin(rad);
+ const pivotWorldY = pos.y + offsetX * Math.sin(rad) + offsetY * Math.cos(rad);
+
+ const dx = worldX - pivotWorldX;
+ const dy = worldY - pivotWorldY;
+ const hitRadius = HIT_RADIUS_PX / zoom;
+
+ return dx * dx + dy * dy <= hitRadius * hitRadius;
+ }
+}
diff --git a/editor/src/panels/scene-view/SceneViewInput.ts b/editor/src/panels/scene-view/SceneViewInput.ts
index ed82eab1d..fe1e05d40 100644
--- a/editor/src/panels/scene-view/SceneViewInput.ts
+++ b/editor/src/panels/scene-view/SceneViewInput.ts
@@ -3,6 +3,7 @@ import type { EditorStore } from '../../store/EditorStore';
import type { GizmoContext } from '../../gizmos';
import { GizmoManager } from '../../gizmos';
import { ColliderOverlay } from '../../gizmos/ColliderOverlay';
+import { PivotOverlay } from '../../gizmos/PivotOverlay';
import { getSettingsValue } from '../../settings/SettingsRegistry';
import { getClipboardService } from '../../services';
import type { CameraController } from './CameraController';
@@ -19,6 +20,7 @@ export interface InputDeps {
marquee: MarqueeSelection;
gizmoManager: GizmoManager;
colliderOverlay: ColliderOverlay;
+ pivotOverlay: PivotOverlay;
getBounds: BoundsProvider;
createGizmoContext: (ctx: CanvasRenderingContext2D) => GizmoContext;
createOverlayContext: () => { ctx: CanvasRenderingContext2D; zoom: number; store: EditorStore } | null;
@@ -182,6 +184,14 @@ export class SceneViewInput {
}
}
+ if (hasSelection) {
+ const octx = this.deps_.createOverlayContext();
+ if (octx && this.deps_.pivotOverlay.onDragStart(worldX, worldY, octx)) {
+ this.startDocumentDrag();
+ return;
+ }
+ }
+
if (gizmoManager.getActiveId() === 'select') {
const hits = this.findEntitiesAtPosition(worldX, worldY);
if (hits.length === 0) {
@@ -219,6 +229,15 @@ export class SceneViewInput {
return;
}
+ if (this.deps_.pivotOverlay.isDragging()) {
+ const octx = this.deps_.createOverlayContext();
+ if (octx) {
+ this.deps_.pivotOverlay.onDrag(worldX, worldY, octx);
+ this.deps_.requestRender();
+ }
+ return;
+ }
+
const hasSelection = store.selectedEntities.size > 0;
if (gizmoManager.getActiveId() !== 'select' && hasSelection) {
@@ -237,6 +256,17 @@ export class SceneViewInput {
}
}
}
+
+ if (hasSelection) {
+ const octx = this.deps_.createOverlayContext();
+ if (octx) {
+ this.deps_.pivotOverlay.hitTest(worldX, worldY, octx);
+ const pivotCursor = this.deps_.pivotOverlay.getCursor();
+ if (pivotCursor) {
+ this.deps_.canvas.style.cursor = pivotCursor;
+ }
+ }
+ }
}
private onMouseUp(e: MouseEvent): void {
@@ -273,11 +303,19 @@ export class SceneViewInput {
}
this.skipNextClick_ = didMove;
}
+
+ if (this.deps_.pivotOverlay.isDragging()) {
+ const octx = this.deps_.createOverlayContext();
+ if (octx) {
+ this.deps_.pivotOverlay.onDragEnd(octx);
+ }
+ this.skipNextClick_ = didMove;
+ }
}
private onMouseLeave(_e: MouseEvent): void {
const { camera, gizmoManager, colliderOverlay, marquee } = this.deps_;
- if (camera.isDragging || gizmoManager.isDragging() || colliderOverlay.isDragging() || marquee.active) {
+ if (camera.isDragging || gizmoManager.isDragging() || colliderOverlay.isDragging() || this.deps_.pivotOverlay.isDragging() || marquee.active) {
return;
}
gizmoManager.resetHover();
diff --git a/editor/src/panels/scene-view/SceneViewPanel.ts b/editor/src/panels/scene-view/SceneViewPanel.ts
index 4517972e5..98369220f 100644
--- a/editor/src/panels/scene-view/SceneViewPanel.ts
+++ b/editor/src/panels/scene-view/SceneViewPanel.ts
@@ -12,6 +12,7 @@ import { GizmoManager } from '../../gizmos';
import type { GizmoContext } from '../../gizmos';
import { ColliderOverlay } from '../../gizmos/ColliderOverlay';
import { ParticleOverlay } from '../../gizmos/ParticleOverlay';
+import { PivotOverlay } from '../../gizmos/PivotOverlay';
import { getSettingsValue, onSettingsChange } from '../../settings/SettingsRegistry';
import type { EntityData } from '../../types/SceneTypes';
@@ -61,6 +62,7 @@ export class SceneViewPanel {
private gizmoManager_: GizmoManager;
private colliderOverlay_: ColliderOverlay;
private particleOverlay_: ParticleOverlay;
+ private pivotOverlay_: PivotOverlay;
private camera_: CameraController;
private toolbar_: GizmoToolbar;
@@ -89,6 +91,7 @@ export class SceneViewPanel {
this.gizmoManager_ = new GizmoManager();
this.colliderOverlay_ = new ColliderOverlay();
this.particleOverlay_ = new ParticleOverlay();
+ this.pivotOverlay_ = new PivotOverlay();
this.tilemapOverlay_ = new TilemapOverlay();
this.tilemapOverlay_.setRenderCallback(() => this.requestRender());
@@ -135,6 +138,7 @@ export class SceneViewPanel {
marquee: this.marquee_,
gizmoManager: this.gizmoManager_,
colliderOverlay: this.colliderOverlay_,
+ pivotOverlay: this.pivotOverlay_,
getBounds: (entityData) => this.getEntityBoundsWithSpine(entityData),
createGizmoContext: (ctx) => this.createGizmoContext(ctx),
createOverlayContext: () => this.createOverlayContext(),
@@ -645,6 +649,7 @@ export class SceneViewPanel {
});
}
this.particleOverlay_.drawAll({ ctx, zoom: oScale, store: this.store_ });
+ this.pivotOverlay_.drawAll({ ctx, zoom: oScale, store: this.store_ });
const hasSelection = this.store_.selectedEntities.size > 0;
@@ -725,6 +730,7 @@ export class SceneViewPanel {
});
}
this.particleOverlay_.drawAll({ ctx, zoom: oScale, store: this.store_ });
+ this.pivotOverlay_.drawAll({ ctx, zoom: oScale, store: this.store_ });
if (selectedEntity !== null && this.store_.isEntityVisible(selectedEntity as number)) {
if (this.gizmoManager_.getActiveId() !== 'select' && getSettingsValue<boolean>('scene.showGizmos')) {
diff --git a/editor/src/plugins/sprite.ts b/editor/src/plugins/sprite.ts
index 0ffaae342..089d5479a 100644
--- a/editor/src/plugins/sprite.ts
+++ b/editor/src/plugins/sprite.ts
@@ -21,6 +21,7 @@ const SpriteSchema: ComponentSchema = {
{ name: 'material', type: 'material-file' },
{ name: 'color', type: 'color' },
{ name: 'size', type: 'vec2', hiddenWhen: { hasComponent: 'UIRect' } },
+ { name: 'pivot', type: 'vec2' },
{ name: 'uvOffset', type: 'vec2' },
{ name: 'uvScale', type: 'vec2' },
{ name: 'layer', type: 'number', min: LAYER_MIN, max: LAYER_MAX },
diff --git a/sdk/src/component.ts b/sdk/src/component.ts
index 5a0c65cbd..67ba6f0d2 100644
--- a/sdk/src/component.ts
+++ b/sdk/src/component.ts
@@ -268,6 +268,7 @@ export interface SpriteData {
texture: number;
color: Color;
size: Vec2;
+ pivot: Vec2;
uvOffset: Vec2;
uvScale: Vec2;
layer: number;
@@ -389,6 +390,7 @@ export const Sprite = defineBuiltin<SpriteData>('Sprite', {
texture: INVALID_TEXTURE,
color: { r: 1, g: 1, b: 1, a: 1 },
size: { x: DEFAULT_SPRITE_SIZE.x, y: DEFAULT_SPRITE_SIZE.y },
+ pivot: { x: 0.5, y: 0.5 },
uvOffset: { x: 0, y: 0 },
uvScale: { x: 1, y: 1 },
layer: 0,
diff --git a/src/esengine/bindings/WebBindings.generated.cpp b/src/esengine/bindings/WebBindings.generated.cpp
index 0f496f1af..ef3cb1530 100644
--- a/src/esengine/bindings/WebBindings.generated.cpp
+++ b/src/esengine/bindings/WebBindings.generated.cpp
@@ -409,6 +409,7 @@ struct SpriteJS {
u32 texture;
glm::vec4 color;
glm::vec2 size;
+ glm::vec2 pivot;
glm::vec2 uvOffset;
glm::vec2 uvScale;
i32 layer;
@@ -423,6 +424,7 @@ esengine::ecs::Sprite spriteFromJS(const SpriteJS& js) {
c.texture = resource::TextureHandle(js.texture);
c.color = js.color;
c.size = js.size;
+ c.pivot = js.pivot;
c.uvOffset = js.uvOffset;
c.uvScale = js.uvScale;
c.layer = js.layer;
@@ -438,6 +440,7 @@ SpriteJS spriteToJS(const esengine::ecs::Sprite& c) {
js.texture = c.texture.id();
js.color = c.color;
js.size = c.size;
+ js.pivot = c.pivot;
js.uvOffset = c.uvOffset;
js.uvScale = c.uvScale;
js.layer = c.layer;
@@ -783,6 +786,7 @@ EMSCRIPTEN_BINDINGS(esengine_components) {
.field("texture", &SpriteJS::texture)
.field("color", &SpriteJS::color)
.field("size", &SpriteJS::size)
+ .field("pivot", &SpriteJS::pivot)
.field("uvOffset", &SpriteJS::uvOffset)
.field("uvScale", &SpriteJS::uvScale)
.field("layer", &SpriteJS::layer)
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 8e9d56ef5..e92580ea5 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -362,13 +362,14 @@ static_assert(offsetof(ecs::Transform, worldScale) == 68);
static_assert(offsetof(ecs::Sprite, texture) == 0);
static_assert(offsetof(ecs::Sprite, color) == 4);
static_assert(offsetof(ecs::Sprite, size) == 20);
-static_assert(offsetof(ecs::Sprite, uvOffset) == 28);
-static_assert(offsetof(ecs::Sprite, uvScale) == 36);
-static_assert(offsetof(ecs::Sprite, layer) == 44);
-static_assert(offsetof(ecs::Sprite, flipX) == 48);
-static_assert(offsetof(ecs::Sprite, flipY) == 49);
-static_assert(offsetof(ecs::Sprite, material) == 52);
-static_assert(offsetof(ecs::Sprite, enabled) == 56);
+static_assert(offsetof(ecs::Sprite, pivot) == 28);
+static_assert(offsetof(ecs::Sprite, uvOffset) == 36);
+static_assert(offsetof(ecs::Sprite, uvScale) == 44);
+static_assert(offsetof(ecs::Sprite, layer) == 52);
+static_assert(offsetof(ecs::Sprite, flipX) == 56);
+static_assert(offsetof(ecs::Sprite, flipY) == 57);
+static_assert(offsetof(ecs::Sprite, material) == 60);
+static_assert(offsetof(ecs::Sprite, enabled) == 64);
int getTransformPtr(ecs::Registry& r, u32 e) {
auto* t = r.tryGet<ecs::Transform>(static_cast<Entity>(e));
diff --git a/src/esengine/ecs/components/Sprite.hpp b/src/esengine/ecs/components/Sprite.hpp
index 3c9665fde..fc2331780 100644
--- a/src/esengine/ecs/components/Sprite.hpp
+++ b/src/esengine/ecs/components/Sprite.hpp
@@ -55,6 +55,10 @@ struct Sprite {
ES_PROPERTY()
glm::vec2 size{1.0f, 1.0f};
+ /** @brief Pivot point (0,0 = bottom-left, 0.5,0.5 = center, 1,1 = top-right) */
+ ES_PROPERTY()
+ glm::vec2 pivot{0.5f, 0.5f};
+
/** @brief UV coordinate offset for sprite sheets */
ES_PROPERTY()
glm::vec2 uvOffset{0.0f, 0.0f};
diff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cpp
index 0f680eb7a..42fe8e23d 100644
--- a/src/esengine/renderer/plugins/SpritePlugin.cpp
+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp
@@ -35,8 +35,12 @@ void SpritePlugin::collect(
const auto& rotation = transform.worldRotation;
const auto& scale = transform.worldScale;
- glm::vec3 halfExtents = glm::vec3(sprite.size.x * scale.x, sprite.size.y * scale.y, 0.0f) * 0.5f;
- if (!frustum.intersectsAABB(position, halfExtents)) {
+ glm::vec2 finalSize = sprite.size * glm::vec2(scale);
+ glm::vec2 pivotOffset((0.5f - sprite.pivot.x) * finalSize.x,
+ (0.5f - sprite.pivot.y) * finalSize.y);
+ glm::vec3 aabbCenter = position + glm::vec3(pivotOffset, 0.0f);
+ glm::vec3 halfExtents = glm::vec3(std::abs(finalSize.x), std::abs(finalSize.y), 0.0f) * 0.5f;
+ if (!frustum.intersectsAABB(aabbCenter, halfExtents)) {
continue;
}
@@ -74,20 +78,21 @@ void SpritePlugin::collect(
uvSc.y = -uvSc.y;
}
- glm::vec2 finalSize = sprite.size * glm::vec2(scale);
u32 shaderId = batch_shader_id_;
BlendMode blend = BlendMode::Normal;
if (useNineSlice) {
emitNineSlice(buffers, draw_list,
- glm::vec2(position), finalSize, angle, position.z,
+ glm::vec2(position), finalSize, sprite.pivot,
+ angle, position.z,
textureId, texSize, sliceBorder,
sprite.color, uvOff, uvSc,
entity, ctx.current_stage, sprite.layer,
blend, shaderId, clips);
} else {
emitQuad(buffers, draw_list,
- glm::vec2(position), finalSize, angle, position.z,
+ glm::vec2(position), finalSize, sprite.pivot,
+ angle, position.z,
textureId, sprite.color, uvOff, uvSc,
entity, ctx.current_stage, sprite.layer,
blend, shaderId, clips);
@@ -98,6 +103,7 @@ void SpritePlugin::collect(
void SpritePlugin::emitQuad(
TransientBufferPool& buffers, DrawList& draw_list,
const glm::vec2& position, const glm::vec2& size,
+ const glm::vec2& pivot,
f32 angle, f32 depth, u32 textureId,
const glm::vec4& color,
const glm::vec2& uvOffset, const glm::vec2& uvScale,
@@ -106,13 +112,15 @@ void SpritePlugin::emitQuad(
const ClipState& clips
) {
BatchVertex verts[4];
+ f32 ox = 0.5f - pivot.x;
+ f32 oy = 0.5f - pivot.y;
if (std::abs(angle) > 0.001f) {
f32 cosA = std::cos(angle);
f32 sinA = std::sin(angle);
for (u32 i = 0; i < 4; ++i) {
- f32 lx = QUAD_POSITIONS[i].x * size.x;
- f32 ly = QUAD_POSITIONS[i].y * size.y;
+ f32 lx = (QUAD_POSITIONS[i].x + ox) * size.x;
+ f32 ly = (QUAD_POSITIONS[i].y + oy) * size.y;
verts[i].position = glm::vec2(
position.x + lx * cosA - ly * sinA,
position.y + lx * sinA + ly * cosA
@@ -123,8 +131,8 @@ void SpritePlugin::emitQuad(
} else {
for (u32 i = 0; i < 4; ++i) {
verts[i].position = glm::vec2(
- position.x + QUAD_POSITIONS[i].x * size.x,
- position.y + QUAD_POSITIONS[i].y * size.y
+ position.x + (QUAD_POSITIONS[i].x + ox) * size.x,
+ position.y + (QUAD_POSITIONS[i].y + oy) * size.y
);
verts[i].color = packColor(color);
verts[i].texCoord = QUAD_TEX_COORDS[i] * uvScale + uvOffset;
@@ -162,6 +170,7 @@ void SpritePlugin::emitQuad(
void SpritePlugin::emitNineSlice(
TransientBufferPool& buffers, DrawList& draw_list,
const glm::vec2& position, const glm::vec2& size,
+ const glm::vec2& pivot,
f32 angle, f32 depth, u32 textureId,
const glm::vec2& texSize, const resource::SliceBorder& border,
const glm::vec4& color,
@@ -175,8 +184,8 @@ void SpritePlugin::emitNineSlice(
f32 T = border.top;
f32 B = border.bottom;
- f32 baseX = position.x - size.x * 0.5f;
- f32 baseY = position.y - size.y * 0.5f;
+ f32 baseX = position.x - size.x * pivot.x;
+ f32 baseY = position.y - size.y * pivot.y;
f32 x[4] = { baseX, baseX + L, baseX + size.x - R, baseX + size.x };
f32 y[4] = { baseY, baseY + B, baseY + size.y - T, baseY + size.y };
diff --git a/src/esengine/renderer/plugins/SpritePlugin.hpp b/src/esengine/renderer/plugins/SpritePlugin.hpp
index 9e1770044..22d306467 100644
--- a/src/esengine/renderer/plugins/SpritePlugin.hpp
+++ b/src/esengine/renderer/plugins/SpritePlugin.hpp
@@ -54,6 +54,7 @@ private:
void emitQuad(
TransientBufferPool& buffers, DrawList& draw_list,
const glm::vec2& position, const glm::vec2& size,
+ const glm::vec2& pivot,
f32 angle, f32 depth, u32 textureId,
const glm::vec4& color,
const glm::vec2& uvOffset, const glm::vec2& uvScale,
@@ -65,6 +66,7 @@ private:
void emitNineSlice(
TransientBufferPool& buffers, DrawList& draw_list,
const glm::vec2& position, const glm::vec2& size,
+ const glm::vec2& pivot,
f32 angle, f32 depth, u32 textureId,
const glm::vec2& texSize, const resource::SliceBorder& border,
const glm::vec4& color,