Testing things

This commit is contained in:
2024-06-17 17:58:44 +02:00
parent a70e599c14
commit b5c30a115d
5 changed files with 133 additions and 36 deletions

View File

@@ -14,6 +14,8 @@ export enum CursorType
leftright = "ew-resize",
nesw = "nesw-resize",
nwse = "nwse-resize",
//rotate = "url(public/rotate.png)"
rotate = "crosshair" //tmp
}
export default class Renderer
{
@@ -38,7 +40,7 @@ export default class Renderer
this.camera = new Three.OrthographicCamera();
this.camera.position.z = 500;
this.#zoom = 1;
this.#zoom = 5;
const stats = this.#stats = new Stats();
stats.showPanel(0);

View File

@@ -1,6 +1,6 @@
import * as Three from "three";
import Asset from "../assets/asset.class";
import { Point, intersects, intersectsAsset, sqrtLen } from "../physics/common";
import { Point, intersects, sqrtLen } from "../physics/common";
import Renderer, { CursorType } from "./renderer.class";
import { QUAD } from '../consts';
import { DragMode } from "../main";
@@ -8,10 +8,10 @@ import { DragMode } from "../main";
const _vector = new Three.Vector3();
const orientation = {
0: { x: -1, y: -1 },
1: { x: -1, y: 1 },
2: { x: -1, y: 1 },
3: { x: -1, y: 1 },
0: {neighborX: 2, neighborY: 1, opposite: 3},
1: {neighborX: 3, neighborY: 0, opposite: 2},
2: {neighborX: 0, neighborY: 3, opposite: 1},
3: {neighborX: 1, neighborY: 2, opposite: 0}
};
export default class Selector
@@ -197,6 +197,10 @@ export default class Selector
minY = Selector.#selectionMesh.box.min.y,
maxX = Selector.#selectionMesh.box.max.x,
maxY = Selector.#selectionMesh.box.max.y;
const centerX = minX + (maxX - minX) / 2,
centerY = minY + (maxY - minY) / 2,
centerLen = sqrtLen(x, y, centerX, centerY);
if (sqrtLen(x, y, Selector.gizmoScale[0].position.x, Selector.gizmoScale[0].position.y) <= 500 / Renderer.zoom)
{
@@ -223,6 +227,11 @@ export default class Selector
Renderer.cursor(CursorType.move);
return { mode: DragMode.move };
}
else if (centerLen <= sqrtLen(centerX, centerY, maxX + (maxX - minX) / 2, centerY))
{
Renderer.cursor(CursorType.rotate);
return { mode: DragMode.rotate };
}
return { mode: DragMode.select };
}
@@ -241,25 +250,68 @@ export default class Selector
Selector.gizmoScale[2].position.add({ x: x, y: y, z: 0 });
Selector.gizmoScale[3].position.add({ x: x, y: y, z: 0 });
}
static scale(dragFrom: number, x: number, y: number): void
static rotate(x: number, y: number): void
{
console.log(dragFrom, x, y);
//@ts-ignore
x *= orientation[dragFrom].x;
//@ts-ignore
y *= orientation[dragFrom].y;
Selector.selection.forEach(e => e.scaleTo(e.scaleX + x, e.scaleY + y).moveTo(e.posX - x / 2, e.posY - y / 2).update(false));
Selector.#selectionMesh.box.translate(_vector.set(-x / 2, -y / 2, 0));
Selector.gizmoScale[0].position.add({ x: -x / 2, y: -y / 2, z: 0 });
Selector.gizmoScale[1].position.add({ x: -x / 2, y: -y / 2, z: 0 });
Selector.gizmoScale[2].position.add({ x: -x / 2, y: -y / 2, z: 0 });
Selector.gizmoScale[3].position.add({ x: -x / 2, y: -y / 2, z: 0 });
_vector.set(x, y, 0).normalize()
const rad = Math.atan2(-_vector.y, _vector.x);
Selector.selection.forEach(e => e.rotateTo(e.rot + rad).update(false));
Asset.instance.computeBoundingBox();
Asset.instance.computeBoundingSphere();
}
}
static scale(dragFrom: number, x: number, y: number): void
{
//@ts-ignore
const o = orientation[dragFrom];
Selector.gizmoScale[dragFrom].position.add({ x: x, y: y, z: 0 });
Selector.gizmoScale[o.neighborX].position.add({ x: x, y: 0, z: 0 });
Selector.gizmoScale[o.neighborY].position.add({ x: 0, y: y, z: 0 });
Selector.selection.forEach(e => {
//DEBUG ONLY, IT IS NOT CORRECT
const aabb = { x1: Selector.gizmoScale[0].position.x, y1: Selector.gizmoScale[0].position.y, x2: Selector.gizmoScale[3].position.x, y2: Selector.gizmoScale[3].position.y }
e.matchAABB(aabb).update(false);
});
Asset.instance.computeBoundingBox();
Asset.instance.computeBoundingSphere();
Selector.#selectionMesh.box.setFromPoints([Selector.gizmoScale[0].position, Selector.gizmoScale[3].position]);
Selector.#ghostMesh.box.setFromPoints([Selector.gizmoScale[0].position, Selector.gizmoScale[3].position]);
}
}
/*
//@ts-ignore
const o = orientation[dragFrom];
const sizeX = Selector.gizmoScale[3].position.x - Selector.gizmoScale[0].position.x,
sizeY = Selector.gizmoScale[3].position.y - Selector.gizmoScale[0].position.y;
const centerX = Selector.gizmoScale[0].position.x + sizeX / 2,
centerY = Selector.gizmoScale[0].position.y + sizeY / 2;
Selector.selection.forEach(e => {
const scaleX = (e.aabb.x2 - e.aabb.x1) / sizeX, scaleY = (e.aabb.y2 - e.aabb.y1) / sizeY;
const moveX = (e.posX - centerX) / sizeX, moveY = (e.posY - centerY) / sizeY;
const cos = Math.abs(Math.cos(e.rot)), sin = Math.abs(Math.sin(e.rot));
const localX = x * cos + y * -sin, localY = x * -sin + y * cos;
e.scaleTo(e.scaleX + localX * scaleX, e.scaleY + localY * scaleY).moveTo(e.posX + x / 2 + x * moveX, e.posY + y / 2 + y * moveY).update(false)
});
Asset.instance.computeBoundingBox();
Asset.instance.computeBoundingSphere();
Selector.gizmoScale[dragFrom].position.add({ x: x, y: y, z: 0 });
Selector.gizmoScale[o.neighborX].position.add({ x: x, y: 0, z: 0 });
Selector.gizmoScale[o.neighborY].position.add({ x: 0, y: y, z: 0 });
Selector.#selectionMesh.box.setFromPoints([Selector.gizmoScale[0].position, Selector.gizmoScale[3].position]);
Selector.#ghostMesh.box.setFromPoints([Selector.gizmoScale[0].position, Selector.gizmoScale[3].position]);
*/