Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GLTF to Convex Hull Collision and Trimesh Examples #285

Merged
merged 8 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update glbtoConvexHull.ts
Capta1nCool authored Jul 12, 2024
commit a0bca64b7b7ee8330954590ed797fb28dce8315d
36 changes: 23 additions & 13 deletions testbed3d/src/demos/glbtoConvexHull.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Testbed } from "../Testbed";
import { Vector3, Object3D, Mesh, BufferGeometry, BufferAttribute, TriangleStripDrawMode } from "three";
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import type {Testbed} from "../Testbed";
import {
Vector3,
Object3D,
Mesh,
BufferGeometry,
BufferAttribute,
TriangleStripDrawMode,
} from "three";
import {GLTFLoader} from "three/examples/jsm/loaders/GLTFLoader";
type RAPIER_API = typeof import("@dimforge/rapier3d");

export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
@@ -15,12 +22,12 @@ export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {

// Adding the 3d model

let loader = new GLTFLoader()
let loader = new GLTFLoader();

loader.load('./sword.glb', (gltf) => {
gltf.scene.position.set(0, 1.2, 0)
gltf.scene.scale.set(3, 3, 3)
testbed.graphics.scene.add(gltf.scene)
loader.load("./sword.glb", (gltf) => {
gltf.scene.position.set(0, 1.2, 0);
gltf.scene.scale.set(3, 3, 3);
testbed.graphics.scene.add(gltf.scene);
testbed.parameters.debugRender = true;
gltf.scene.updateMatrixWorld(true); // ensure world matrix is up to date

@@ -31,7 +38,9 @@ export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
if ((child as Mesh).isMesh && (child as Mesh).geometry) {
const mesh = child as Mesh;
const geometry = mesh.geometry as BufferGeometry;
const positionAttribute = geometry.getAttribute('position') as BufferAttribute;
const positionAttribute = geometry.getAttribute(
"position",
) as BufferAttribute;

for (let i = 0, l = positionAttribute.count; i < l; i++) {
v.fromBufferAttribute(positionAttribute, i);
@@ -44,15 +53,16 @@ export function initWorld(RAPIER: RAPIER_API, testbed: Testbed) {
const rigidBodyDesc = RAPIER.RigidBodyDesc.fixed();
const rigidBody = world.createRigidBody(rigidBodyDesc);

const colliderDesc = RAPIER.ColliderDesc.convexHull(new Float32Array(positions));
const colliderDesc = RAPIER.ColliderDesc.convexHull(
new Float32Array(positions),
);
world.createCollider(colliderDesc, rigidBody);
});


testbed.setWorld(world);
let cameraPosition = {
eye: { x: 10.0, y: 5.0, z: 10.0 },
target: { x: 0.0, y: 0.0, z: 0.0 },
eye: {x: 10.0, y: 5.0, z: 10.0},
target: {x: 0.0, y: 0.0, z: 0.0},
};
testbed.lookAt(cameraPosition);
}