diff --git a/src/components/shape/ColliderShape.ts b/src/components/shape/ColliderShape.ts index 184f9d30..b280e8f8 100644 --- a/src/components/shape/ColliderShape.ts +++ b/src/components/shape/ColliderShape.ts @@ -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, diff --git a/src/components/shape/MeshColliderShape.ts b/src/components/shape/MeshColliderShape.ts index 3971f1c8..961ce162 100644 --- a/src/components/shape/MeshColliderShape.ts +++ b/src/components/shape/MeshColliderShape.ts @@ -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'; /** @@ -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(); @@ -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; } diff --git a/src/io/PickFire.ts b/src/io/PickFire.ts index d645039b..e629e384 100644 --- a/src/io/PickFire.ts +++ b/src/io/PickFire.ts @@ -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 @@ -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 }; } }