Skip to content

Commit

Permalink
extracted triangles directly from geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
kpal81xd committed Apr 30, 2024
1 parent ed2d246 commit baa9661
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 60 deletions.
73 changes: 41 additions & 32 deletions src/extras/gizmo/axis-shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BLEND_NORMAL } from '../../scene/constants.js';
import { createShaderFromCode } from '../../scene/shader-lib/utils.js';

import { COLOR_GRAY } from './default-colors.js';
import { MeshTriData } from './mesh-tri-data.js';
import { TriData } from './tri-data.js';
import { Mesh } from '../../scene/mesh.js';
import { BoxGeometry } from '../../scene/geometry/box-geometry.js';
import { CylinderGeometry } from '../../scene/geometry/cylinder-geometry.js';
Expand All @@ -21,16 +21,20 @@ import { TorusGeometry } from '../../scene/geometry/torus-geometry.js';
const SHADOW_DAMP_SCALE = 0.25;
const SHADOW_DAMP_OFFSET = 0.75;
const SHADOW_MESH_MAP = new Map();

const TORUS_RENDER_SEGMENTS = 80;
const TORUS_INTERSECT_SEGMENTS = 20;

const LIGHT_DIR = new Vec3(1, 2, 3);
const MESH_TEMPLATES = {

const GEOMETRIES = {
box: BoxGeometry,
cone: ConeGeometry,
cylinder: CylinderGeometry,
plane: PlaneGeometry,
torus: TorusGeometry
};

const SHADER = {
vert: /* glsl */`
attribute vec3 vertex_position;
Expand Down Expand Up @@ -68,12 +72,12 @@ const tmpV2 = new Vec3();
const tmpQ1 = new Quat();

function createShadowMesh(device, entity, type, color = Color.WHITE, templateOpts) {
const geomTemplate = MESH_TEMPLATES[type];
if (!geomTemplate) {
const Geometry = GEOMETRIES[type];
if (!Geometry) {
throw new Error('Invalid primitive type.');
}

const geom = new geomTemplate(templateOpts);
const geom = new Geometry(templateOpts);
geom.colors = [];

const wtm = entity.getWorldTransform().clone().invert();
Expand All @@ -83,7 +87,12 @@ function createShadowMesh(device, entity, type, color = Color.WHITE, templateOpt
const numVertices = geom.positions.length / 3;
const shadow = calculateShadow(tmpV1, numVertices, geom.normals);
for (let i = 0; i < shadow.length; i++) {
geom.colors.push(shadow[i] * color.r * 255, shadow[i] * color.g * 255, shadow[i] * color.b * 255, color.a * 255);
geom.colors.push(
shadow[i] * color.r * 0xFF,
shadow[i] * color.g * 0xFF,
shadow[i] * color.b * 0xFF,
color.a * 0xFF
);
}

const shadowMesh = Mesh.fromGeometry(device, geom);
Expand Down Expand Up @@ -145,7 +154,7 @@ class AxisShape {

entity;

meshTriDataList = [];
triData = [];

meshInstances = [];

Expand Down Expand Up @@ -256,9 +265,9 @@ class AxisArrow extends AxisShape {
constructor(device, options = {}) {
super(device, options);

this.meshTriDataList = [
new MeshTriData(Mesh.fromGeometry(this.device, new ConeGeometry())),
new MeshTriData(Mesh.fromGeometry(this.device, new CylinderGeometry()), 1)
this.triData = [
new TriData(new ConeGeometry()),

This comment has been minimized.

Copy link
@mvaligursky

mvaligursky Apr 30, 2024

Contributor

so much nicer!

new TriData(new CylinderGeometry(), 1)
];

this._createArrow();
Expand Down Expand Up @@ -342,7 +351,7 @@ class AxisArrow extends AxisShape {
tmpV1.set(0, this._gap + this._arrowLength * 0.5 + this._lineLength, 0);
tmpQ1.set(0, 0, 0, 1);
tmpV2.set(this._arrowThickness, this._arrowLength, this._arrowThickness);
this.meshTriDataList[0].setTransform(tmpV1, tmpQ1, tmpV2);
this.triData[0].setTransform(tmpV1, tmpQ1, tmpV2);

this._head.setLocalPosition(0, this._gap + this._arrowLength * 0.5 + this._lineLength, 0);
this._head.setLocalScale(this._arrowThickness, this._arrowLength, this._arrowThickness);
Expand All @@ -353,7 +362,7 @@ class AxisArrow extends AxisShape {
tmpV1.set(0, this._gap + this._lineLength * 0.5, 0);
tmpQ1.set(0, 0, 0, 1);
tmpV2.set(this._lineThickness + this._tolerance, this._lineLength, this._lineThickness + this._tolerance);
this.meshTriDataList[1].setTransform(tmpV1, tmpQ1, tmpV2);
this.triData[1].setTransform(tmpV1, tmpQ1, tmpV2);

// render
this._line.setLocalPosition(0, this._gap + this._lineLength * 0.5, 0);
Expand All @@ -369,8 +378,8 @@ class AxisBoxCenter extends AxisShape {
constructor(device, options = {}) {
super(device, options);

this.meshTriDataList = [
new MeshTriData(Mesh.fromGeometry(this.device, new BoxGeometry()), 2)
this.triData = [
new TriData(new BoxGeometry(), 2)
];

this._createCenter();
Expand Down Expand Up @@ -422,9 +431,9 @@ class AxisBoxLine extends AxisShape {
constructor(device, options = {}) {
super(device, options);

this.meshTriDataList = [
new MeshTriData(Mesh.fromGeometry(this.device, new BoxGeometry())),
new MeshTriData(Mesh.fromGeometry(this.device, new CylinderGeometry()), 1)
this.triData = [
new TriData(new BoxGeometry()),
new TriData(new CylinderGeometry(), 1)
];

this._createBoxLine();
Expand Down Expand Up @@ -500,7 +509,7 @@ class AxisBoxLine extends AxisShape {
tmpV1.set(0, this._gap + this._boxSize * 0.5 + this._lineLength, 0);
tmpQ1.set(0, 0, 0, 1);
tmpV2.set(this._boxSize, this._boxSize, this._boxSize);
this.meshTriDataList[0].setTransform(tmpV1, tmpQ1, tmpV2);
this.triData[0].setTransform(tmpV1, tmpQ1, tmpV2);

// render
this._box.setLocalPosition(0, this._gap + this._boxSize * 0.5 + this._lineLength, 0);
Expand All @@ -512,7 +521,7 @@ class AxisBoxLine extends AxisShape {
tmpV1.set(0, this._gap + this._lineLength * 0.5, 0);
tmpQ1.set(0, 0, 0, 1);
tmpV2.set(this._lineThickness + this._tolerance, this._lineLength, this._lineThickness + this._tolerance);
this.meshTriDataList[1].setTransform(tmpV1, tmpQ1, tmpV2);
this.triData[1].setTransform(tmpV1, tmpQ1, tmpV2);

// render
this._line.setLocalPosition(0, this._gap + this._lineLength * 0.5, 0);
Expand All @@ -538,23 +547,23 @@ class AxisDisk extends AxisShape {
this._ringRadius = options.ringRadius ?? this._ringRadius;
this._sectorAngle = options.sectorAngle ?? this._sectorAngle;

this.meshTriDataList = [
new MeshTriData(this._createIntersectTorus())
this.triData = [
new TriData(this._createTorusGeometry())
];

this._createDisk();
}

_createIntersectTorus() {
return Mesh.fromGeometry(this.device, new TorusGeometry({
_createTorusGeometry() {
return new TorusGeometry({
tubeRadius: this._tubeRadius + this._tolerance,
ringRadius: this._ringRadius,
sectorAngle: this._sectorAngle,
segments: TORUS_INTERSECT_SEGMENTS
}));
});
}

_createRenderTorus(sectorAngle) {
_createTorusMesh(sectorAngle) {
const color = this._disabled ? this._disabledColor : this._defaultColor;
return createShadowMesh(this.device, this.entity, 'torus', color, {
tubeRadius: this._tubeRadius,
Expand All @@ -569,8 +578,8 @@ class AxisDisk extends AxisShape {

// arc/circle
this._addRenderMeshes(this.entity, [
this._createRenderTorus(this._sectorAngle),
this._createRenderTorus(360)
this._createTorusMesh(this._sectorAngle),
this._createTorusMesh(360)
]);
this.drag(false);
}
Expand Down Expand Up @@ -604,11 +613,11 @@ class AxisDisk extends AxisShape {

_updateTransform() {
// intersect
this.meshTriDataList[0].setTris(this._createIntersectTorus());
this.triData[0].setTris(this._createTorusGeometry());

// render
this.meshInstances[0].mesh = this._createRenderTorus(this._sectorAngle);
this.meshInstances[1].mesh = this._createRenderTorus(360);
this.meshInstances[0].mesh = this._createTorusMesh(this._sectorAngle);
this.meshInstances[1].mesh = this._createTorusMesh(360);
}

drag(state) {
Expand Down Expand Up @@ -637,8 +646,8 @@ class AxisPlane extends AxisShape {
constructor(device, options = {}) {
super(device, options);

this.meshTriDataList = [
new MeshTriData(Mesh.fromGeometry(this.device, new PlaneGeometry()))
this.triData = [
new TriData(new PlaneGeometry())
];

this._createPlane();
Expand Down
12 changes: 6 additions & 6 deletions src/extras/gizmo/gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ class Gizmo extends EventHandler {

/**
* @typedef IntersectData
* @property {import('./mesh-tri-data.js').MeshTriData[]} meshTriDataList -
* The array of {@link MeshTriData}
* @property {import('./tri-data.js').TriData[]} triData - The array of {@link TriData}
* @property {import('../../scene/graph-node.js').GraphNode} parent - The mesh parent node.
* @property {import('../../scene/mesh-instance.js').MeshInstance[]} meshInstances -
* array of mesh instances for rendering
Expand All @@ -237,7 +236,8 @@ class Gizmo extends EventHandler {
* Creates a new Gizmo object.
*
* @param {import('../../framework/app-base.js').AppBase} app - The application instance.
* @param {import('../../framework/components/camera/component.js').CameraComponent} camera - The camera component.
* @param {import('../../framework/components/camera/component.js').CameraComponent} camera -

Check failure on line 239 in src/extras/gizmo/gizmo.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
* The camera component.
* @param {import('../../scene/layer.js').Layer} layer - The render layer.
* @example
* const gizmo = new pc.Gizmo(app, camera, layer);
Expand Down Expand Up @@ -381,10 +381,10 @@ class Gizmo extends EventHandler {

const selection = [];
for (let i = 0; i < this.intersectData.length; i++) {
const { meshTriDataList, parent, meshInstances } = this.intersectData[i];
const { triData, parent, meshInstances } = this.intersectData[i];
const wtm = parent.getWorldTransform().clone();
for (let j = 0; j < meshTriDataList.length; j++) {
const { tris, ptm, priority } = meshTriDataList[j];
for (let j = 0; j < triData.length; j++) {
const { tris, ptm, priority } = triData[j];
tmpM1.copy(wtm).mul(ptm);
tmpM2.copy(tmpM1).invert();
tmpM2.transformPoint(start, tmpR1.origin);
Expand Down
2 changes: 1 addition & 1 deletion src/extras/gizmo/transform-gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ class TransformGizmo extends Gizmo {
const shape = this._shapes[key];
this.root.addChild(shape.entity);
this.intersectData.push({
meshTriDataList: shape.meshTriDataList,
triData: shape.triData,
parent: shape.entity,
meshInstances: shape.meshInstances
});
Expand Down
41 changes: 20 additions & 21 deletions src/extras/gizmo/mesh-tri-data.js → src/extras/gizmo/tri-data.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Vec3 } from '../../core/math/vec3.js';
import { Quat } from '../../core/math/quat.js';
import { Mat4 } from '../..//core/math/mat4.js';
import { Mat4 } from '../../core/math/mat4.js';
import { Tri } from '../../core/shape/tri.js';
import { Mesh } from '../../scene/mesh.js';
import { Geometry } from '../../scene/geometry/geometry.js';

// temporary variables
const tmpV1 = new Vec3();
const tmpV2 = new Vec3();
const tmpV3 = new Vec3();

/**
* The class for holding mesh triangle data.
* The class for holding triangle data.
*
* @ignore
*/
class MeshTriData {
class TriData {
/**
* The priority of the triangle data (Used for intersection ordering).
* priority = 0 - no priority
Expand All @@ -25,21 +25,25 @@ class MeshTriData {
_priority = 0;

/**
* The transform of the mesh.
* The transform of the triangles.
*
* @type {Mat4}
*/
_ptm = new Mat4();

/**
* The array of triangles for the mesh.
* The array of triangles for the geometry.
*
* @type {Tri[]}
*/
tris;
tris = [];

constructor(mesh, priority = 0) {
this.setTris(mesh);
/**
* @param {Geometry} geometry - The geometry to create the triangle data from.
* @param {number} [priority] - The priority of the triangle data.
*/
constructor(geometry, priority = 0) {
this.setTris(geometry);
this._priority = priority;
}

Expand All @@ -51,15 +55,10 @@ class MeshTriData {
return this._priority;
}

_trisFromMesh(mesh, destroy = true) {
_fromGeometry(geometry) {
const tris = [];
const pos = [];
const indices = [];
mesh.getPositions(pos);
mesh.getIndices(indices);
if (destroy) {
mesh.destroy();
}
const pos = geometry.positions;
const indices = geometry.indices;

for (let k = 0; k < indices.length; k += 3) {
const i1 = indices[k];
Expand All @@ -79,12 +78,12 @@ class MeshTriData {
this.ptm.setTRS(pos, rot, scale);
}

setTris(mesh) {
if (!mesh || !(mesh instanceof Mesh)) {
setTris(geometry) {
if (!geometry || !(geometry instanceof Geometry)) {
throw new Error('No mesh provided.');
}
this.tris = this._trisFromMesh(mesh);
this.tris = this._fromGeometry(geometry);
}
}

export { MeshTriData };
export { TriData };

0 comments on commit baa9661

Please sign in to comment.