-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move Floor component to @react-three/jolt-addons
- Loading branch information
1 parent
94033f4
commit 1098f17
Showing
12 changed files
with
87 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ThreeElements } from '@react-three/fiber'; | ||
import { RigidBody, Vector3Tuple } from '@react-three/jolt'; | ||
import React from 'react'; | ||
|
||
export type FloorProps = { | ||
size?: number; | ||
position?: Vector3Tuple; | ||
rotation?: Vector3Tuple; | ||
children?: React.ReactNode; | ||
} & ThreeElements['mesh']; | ||
|
||
export const Floor = (props: FloorProps) => { | ||
const { size = 20, position = [0, 0, 0], rotation = [0, 0, 0], ...rest } = props; | ||
|
||
return ( | ||
<RigidBody position={position} rotation={rotation} type="static"> | ||
<mesh receiveShadow scale-y={1} {...rest}> | ||
<boxGeometry args={[size, 0.5, size]} /> | ||
</mesh> | ||
</RigidBody> | ||
); | ||
}; |
44 changes: 44 additions & 0 deletions
44
packages/react-three-jolt-addons/src/components/MeshFloor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// MeshFloor for Jolt | ||
/* NOTE this WILL NOT work well with a worker version | ||
this script creates and uses a JOLT Body and passes it to the | ||
physics system. In a worker the system will be in isolation and | ||
it's unclear if a body is a transferable object. | ||
EXPECT this to fail in a worker. | ||
However, it is a good example of how to create/use jolt directly so I'm using it | ||
if You really wanted a body like this, probably use the heigtfield instead | ||
*/ | ||
//import { RigidBody } from './RidgedBody'; | ||
import { RigidBody, createMeshFloor, createMeshFromShape } from '@react-three/jolt'; | ||
import { useEffect, useRef } from 'react'; | ||
import { useJolt } from '@react-three/jolt'; | ||
import * as THREE from 'three'; | ||
import React from 'react'; | ||
|
||
export const MeshFloor = ({ size = 20, position = [0, 0, 0], ...rest }) => { | ||
const meshRef = useRef<THREE.Mesh>(null); | ||
const { bodySystem } = useJolt(); | ||
|
||
useEffect(() => { | ||
// generate the jolt body | ||
const floorBodySettings = createMeshFloor(30, 1, 4, 0, 5, 0); | ||
const rawBody = bodySystem.bodyInterface.CreateBody(floorBodySettings); | ||
|
||
//now we can make a mesh using the body with the helper | ||
const floorMesh = createMeshFromShape(rawBody.GetShape()); | ||
if (meshRef.current) { | ||
meshRef.current.geometry = floorMesh; | ||
// push the body onto the system | ||
bodySystem.addExistingBody(meshRef.current, rawBody, { | ||
bodyType: 'static' | ||
}); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<RigidBody position={position} type="static"> | ||
<mesh ref={meshRef} position-y={0.1} {...rest}> | ||
<boxGeometry args={[size, 0.5, size]} /> | ||
<meshStandardMaterial color="grey" /> | ||
</mesh> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Floor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import * as _fiber from '@react-three/fiber'; | ||
|
||
export const placeholder = 0; | ||
export * from './components'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters