-
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.
Merge pull request #113 from pmndrs/ballBox
Ball box
- Loading branch information
Showing
43 changed files
with
15,049 additions
and
5,473 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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,155 @@ | ||
import { Physics, RigidBody } from "@react-three/jolt"; | ||
import { useDemo } from "../App"; | ||
import { useMemo, useEffect, useState, Fragment } from "react"; | ||
//import * as THREE from "three"; | ||
import { useThree } from "@react-three/fiber"; | ||
import InitJolt from "../jolt/Distribution/jolt-physics.wasm-compat"; | ||
|
||
import { JoltBolt } from "./Bodies/joltBolt"; | ||
import { BoxContainer } from "./Bodies/BoxContainer"; | ||
import Changer from "./Bodies/Changer"; | ||
import Scaler from "./Bodies/Scaler"; | ||
|
||
// fix typescript to know about permissions | ||
declare global { | ||
interface Window { | ||
DeviceMotionEvent: { | ||
prototype: DeviceMotionEvent; | ||
new (type: string, eventInitDict?: DeviceMotionEventInit): DeviceMotionEvent; | ||
requestPermission?: () => Promise<PermissionState>; | ||
}; | ||
} | ||
} | ||
export function BallBox() { | ||
const { debug, paused, interpolate, physicsKey } = useDemo(); | ||
const { controls, camera } = useThree(); | ||
//disable controls | ||
useEffect(() => { | ||
if (!controls) return; | ||
//@ts-ignore | ||
controls.rotate(0, 0, false); | ||
setTimeout(() => { | ||
//@ts-ignore | ||
controls.enabled = false; | ||
}, 100); | ||
return () => { | ||
//@ts-ignore | ||
controls!.enabled = true; | ||
}; | ||
}, [controls, camera]); | ||
|
||
const defaultBodySettings = { | ||
mRestitution: 0.5 | ||
}; | ||
|
||
const positions = useMemo(() => { | ||
const allPos = []; | ||
|
||
for (let i = 0; i < 15; i++) { | ||
allPos.push([Math.random() * 20 - 10, Math.random() * 10, 0]); | ||
} | ||
return allPos; | ||
}, []); | ||
|
||
const [gravity, setGravity] = useState([0, -9.8, 0]); | ||
|
||
const updateGravityOnMouse = (e: MouseEvent) => { | ||
// if the right click isn't pressed, don't update the gravity | ||
if (!e.buttons || e.buttons !== 2) return; | ||
|
||
const x = (e.clientX / window.innerWidth) * 20 - 10; | ||
const y = (e.clientY / window.innerHeight) * 20 - 10; | ||
setGravity([x, -y, 0]); | ||
}; | ||
// attach event listener to mouse move with removal on return | ||
useEffect(() => { | ||
window.addEventListener("mousemove", updateGravityOnMouse); | ||
function preventDefault(e: MouseEvent) { | ||
e.preventDefault(); | ||
} | ||
window.addEventListener("contextmenu", preventDefault); | ||
return () => { | ||
window.removeEventListener("mousemove", updateGravityOnMouse); | ||
window.removeEventListener("contextmenu", preventDefault); | ||
}; | ||
}, []); | ||
|
||
// detect device orientation and set gravity | ||
const updateGravityOnDevice = (e: DeviceMotionEvent) => { | ||
if (!e.accelerationIncludingGravity || e.accelerationIncludingGravity.x === null) return; | ||
const { x, y, z } = e.accelerationIncludingGravity!; | ||
console.log("setting from device", e); | ||
setGravity([x || 0, y || 0, z || 0]); | ||
}; | ||
|
||
// attach event listener to device orientation with removal on return | ||
useEffect(() => { | ||
//@ts-ignore | ||
if (typeof DeviceMotionEvent.requestPermission === "function") { | ||
//@ts-ignore | ||
DeviceMotionEvent.requestPermission() | ||
.then((permissionState: PermissionState) => { | ||
if (permissionState === "granted") { | ||
window.addEventListener("devicemotion", updateGravityOnDevice); | ||
} | ||
}) | ||
.catch(console.error); | ||
} else { | ||
// handle regular non iOS 13+ devices | ||
window.addEventListener("devicemotion", updateGravityOnDevice); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener("devicemotion", updateGravityOnDevice); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Physics | ||
module={InitJolt} | ||
paused={paused} | ||
key={physicsKey} | ||
interpolate={interpolate} | ||
debug={debug} | ||
gravity={gravity} | ||
defaultBodySettings={defaultBodySettings} | ||
> | ||
<BoxContainer /> | ||
|
||
<RigidBody | ||
scale={[0.03, 0.03, 0.03]} | ||
rotation={[3.14, 0, 0]} | ||
position={[-1, 3, 1]} | ||
onlyInitialize | ||
> | ||
<JoltBolt /> | ||
</RigidBody> | ||
|
||
{positions.map((pos) => ( | ||
<Fragment key={pos.toString()}> | ||
<Scaler position={pos} /> | ||
<Changer | ||
position={[pos[0] - Math.random(), pos[1] + Math.random(), pos[2]]} | ||
/> | ||
</Fragment> | ||
))} | ||
</Physics> | ||
<ambientLight intensity={0.5} /> | ||
<directionalLight | ||
position={[-29, 5, 20]} | ||
shadow-camera-bottom={-16} | ||
shadow-camera-top={16} | ||
shadow-camera-left={-16} | ||
shadow-camera-right={16} | ||
shadow-camera-near={0.1} | ||
shadow-camera-far={70} | ||
shadow-mapSize-width={1024} | ||
shadow-bias={0.001} | ||
shadow-normalBias={0.03} | ||
intensity={3} | ||
castShadow | ||
/> | ||
</> | ||
); | ||
} |
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,66 @@ | ||
import { useThree } from "@react-three/fiber"; | ||
import { RigidBody, Shape } from "@react-three/jolt"; | ||
|
||
export function BoxContainer(props: any) { | ||
// we need to get window data | ||
const { viewport } = useThree(); | ||
const boxColor = "#38165c"; | ||
|
||
return ( | ||
<group {...props}> | ||
<Wall | ||
scale={[viewport.width, 1, 7]} | ||
color={boxColor} | ||
position={[0, viewport.height / 2 - 1, 2]} | ||
/> | ||
<Wall | ||
scale={[viewport.width, 1, 5]} | ||
color={boxColor} | ||
position={[0, -viewport.height / 2 + 1, 0]} | ||
/> | ||
|
||
<Wall | ||
scale={[1, viewport.height, 5]} | ||
color={boxColor} | ||
position={[-viewport.width / 2 + 1, 0, 0]} | ||
/> | ||
<Wall | ||
scale={[1, viewport.height, 5]} | ||
color={boxColor} | ||
position={[viewport.width / 2 - 1, 0, 0]} | ||
/> | ||
|
||
<Wall | ||
scale={[viewport.width + 1, viewport.height + 0.01, 1]} | ||
color={boxColor} | ||
position={[0, 0, -1]} | ||
cast={false} | ||
/> | ||
{props.children} | ||
<RigidBody | ||
scale={[viewport.width, viewport.height, 1]} | ||
position={[0, 0, 4]} | ||
type="static" | ||
> | ||
<Shape size={[1, 1, 1]} /> | ||
</RigidBody> | ||
</group> | ||
); | ||
} | ||
|
||
function Wall({ | ||
cast = true, | ||
receive = true, | ||
position = [0, 0, 0], | ||
color = "#114929", | ||
scale = [1, 1, 1] | ||
}) { | ||
return ( | ||
<RigidBody position={position} scale={scale} type="static"> | ||
<mesh castShadow={cast} receiveShadow={receive}> | ||
<boxGeometry args={[1, 1, 1]} /> | ||
<meshStandardMaterial color={color} /> | ||
</mesh> | ||
</RigidBody> | ||
); | ||
} |
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,65 @@ | ||
// this body changes between shapes but keeps the same rigidBdy | ||
|
||
// use memo so it doesn't cycle on inputs | ||
import { RigidBody, getShapeSettingsFromGeometry, BodyState } from "@react-three/jolt"; | ||
import { memo, useMemo, useRef, useState } from "react"; | ||
import * as THREE from "three"; | ||
type ChangerProps = { | ||
position?: number[]; | ||
}; | ||
|
||
const Changer: React.FC<ChangerProps> = memo((props) => { | ||
//hold the refs | ||
const meshRef = useRef<THREE.Mesh | null>(null); | ||
const bodyRef = useRef<BodyState>(); | ||
|
||
// original color: #6200B3 | ||
const [activeColor, setActiveColor] = useState("#188FA7"); | ||
const currentShape = useRef(0); | ||
//create the geometries | ||
const geometries = useMemo(() => { | ||
const geoArray = []; | ||
// sphere | ||
const sphereGeo = new THREE.SphereGeometry(1.3, 32, 32); | ||
const sphereShape = getShapeSettingsFromGeometry(sphereGeo)!.shapeSettings!.Create().Get(); | ||
geoArray.push({ geo: sphereGeo, shape: sphereShape, color: "#188FA7" }); | ||
|
||
// box | ||
const boxGeo = new THREE.BoxGeometry(2, 2, 2); | ||
const boxShape = getShapeSettingsFromGeometry(boxGeo)!.shapeSettings!.Create().Get(); | ||
geoArray.push({ geo: boxGeo, shape: boxShape, color: "#DC6A07" }); | ||
|
||
//pyramid tetrahedron | ||
const tetraGeo = new THREE.TetrahedronGeometry(2); | ||
const tetraShape = getShapeSettingsFromGeometry(tetraGeo, "convex")! | ||
.shapeSettings!.Create() | ||
.Get(); | ||
geoArray.push({ geo: tetraGeo, shape: tetraShape, color: "#70D6EB" }); | ||
|
||
//torus knot | ||
const torusGeo = new THREE.TorusKnotGeometry(1, 0.4, 64, 8); | ||
const torusShape = getShapeSettingsFromGeometry(torusGeo)!.shapeSettings!.Create().Get(); | ||
geoArray.push({ geo: torusGeo, shape: torusShape, color: "#72E1D1" }); | ||
return geoArray; | ||
}, []); | ||
|
||
// function to change things | ||
const nextShape = () => { | ||
currentShape.current = (currentShape.current + 1) % geometries.length; | ||
const { geo, shape, color } = geometries[currentShape.current]; | ||
setActiveColor(color); | ||
(bodyRef.current as BodyState).shape = shape; | ||
if (meshRef.current) meshRef.current.geometry = geo; | ||
}; | ||
|
||
return ( | ||
<RigidBody {...props} ref={bodyRef} onlyInitialize> | ||
<mesh ref={meshRef} onClick={() => nextShape()} castShadow receiveShadow> | ||
<sphereGeometry args={[1.3, 32, 32]} /> | ||
<meshStandardMaterial color={activeColor} /> | ||
</mesh> | ||
</RigidBody> | ||
); | ||
}); | ||
|
||
export default Changer; |
Oops, something went wrong.