Skip to content

Commit

Permalink
fix(pick): add worldNormal in bound pick
Browse files Browse the repository at this point in the history
  • Loading branch information
lslzl3000 committed Nov 10, 2024
1 parent a7c8d01 commit 4b5bc9c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/shape/ColliderShape.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Matrix4 } from "../../math/Matrix4";
import { Ray } from "../../math/Ray";
import { Vector3 } from "../../math/Vector3";
import { ColliderComponent } from "../ColliderComponent";

export type HitInfo = { intersectPoint?: Vector3; distance: number; collider?: any };
export type HitInfo = { intersectPoint?: Vector3; normal?: Vector3; distance: number; collider?: ColliderComponent };

export enum ColliderShapeType {
None,
Expand Down
15 changes: 12 additions & 3 deletions src/components/shape/MeshColliderShape.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeometryBase, Matrix4, Ray, Triangle, Vector3, ColliderShape, ColliderShapeType, HitInfo } from '@orillusion/core';
import { GeometryBase, Matrix4, Ray, Triangle, Vector3, ColliderShape, ColliderShapeType, HitInfo, VertexAttributeName } from '@orillusion/core';


/**
Expand All @@ -23,8 +23,8 @@ export class MeshColliderShape extends ColliderShape {
if (this.mesh) {
MeshColliderShape.triangle ||= new Triangle(new Vector3(), new Vector3(), new Vector3());

let positionAttribute = this.mesh.getAttribute(`position`);
let indexAttribute = this.mesh.getAttribute(`indices`);
let positionAttribute = this.mesh.getAttribute(VertexAttributeName.position);
let indexAttribute = this.mesh.getAttribute(VertexAttributeName.indices);

let helpMatrix = ColliderShape.helpMatrix;
helpMatrix.copyFrom(fromMatrix).invert();
Expand Down Expand Up @@ -62,6 +62,15 @@ export class MeshColliderShape extends ColliderShape {
this._pickRet ||= { intersectPoint: new Vector3(), distance: 0 };
this._pickRet.intersectPoint = pick;
this._pickRet.distance = Vector3.distance(helpRay.origin, pick);

let normalAttribute = this.mesh.getAttribute(VertexAttributeName.normal);
if(normalAttribute){
let normalData = normalAttribute.data;
let normal = new Vector3(normalData[i1], normalData[i1 + 1], normalData[i1 + 2]);
fromMatrix.transformVector(normal, normal)
normal.normalize();
this._pickRet.normal = normal
}
return this._pickRet;
}

Expand Down
7 changes: 4 additions & 3 deletions src/io/PickFire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ColliderComponent } from '../components/ColliderComponent';
import { View3D } from '../core/View3D';
import { PointerEvent3D } from '../event/eventConst/PointerEvent3D';
import { HitInfo } from '../components/shape/ColliderShape';
import { ComponentCollect } from '..';
import { ComponentCollect, Matrix4 } from '..';

/**
* Management and triggering for picking 3D objects
Expand Down Expand Up @@ -143,16 +143,17 @@ export class PickFire extends CEventDispatcher {
if(Engine3D.setting.pick.mode == `pixel`)
return {
worldPos: this._pickCompute.getPickWorldPosition(),
worldNormal: this._pickCompute.getPickWorldNormal(),
screenUv: this._pickCompute.getPickScreenUV(),
meshID: this._pickCompute.getPickMeshID(),
worldNormal: this._pickCompute.getPickWorldNormal(),
};
else{
let intersection = this._interestList[0]
return {
worldPos: intersection.intersectPoint,
worldNormal: intersection.normal,
meshID: intersection.collider.transform.worldMatrix.index,
distance: intersection.distance,
collider: intersection.collider
};
}
}
Expand Down

0 comments on commit 4b5bc9c

Please sign in to comment.