some bullshit
This commit is contained in:
1 parent
5f53708fa1
commit
31baf982f2
4 files changed
+552
-24
No files matched your search
+55
-17
@@ -1,23 +1,16 @@
|
||||
import { PerspectiveCamera, StatsGl } from '@react-three/drei';
|
||||
import { Canvas, useFrame, useThree } from '@react-three/fiber'
|
||||
import { CuboidCollider, Physics, RigidBody } from '@react-three/rapier';
|
||||
import { useRef, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { DirectionalLight, Euler, PCFSoftShadowMap, VSMShadowMap, Vector3 } from 'three';
|
||||
|
||||
function Sub() {
|
||||
const camera = useThree(s => s.camera);
|
||||
|
||||
camera.position.y = 2;
|
||||
camera.position.z = 5;
|
||||
camera.rotation.x = -(Math.PI / 6);
|
||||
return <></>
|
||||
}
|
||||
import { Player } from './Player';
|
||||
|
||||
function HeRotates() {
|
||||
const rotation = useRef(0);
|
||||
const objRef = useRef<DirectionalLight>(null);
|
||||
useFrame((three, dt) => {
|
||||
rotation.current += dt;
|
||||
const radius = 5;
|
||||
const radius = 3;
|
||||
const centerX = 0;
|
||||
const centerY = 0;
|
||||
|
||||
@@ -29,11 +22,46 @@ function HeRotates() {
|
||||
}
|
||||
});
|
||||
|
||||
return <directionalLight color="white" position={[0, 3, 0]} ref={objRef} intensity={3.0} castShadow={true} />
|
||||
return <directionalLight color="white" position={[0, 10, 0]} ref={objRef} intensity={3.0} castShadow={true} />
|
||||
}
|
||||
|
||||
function App() {
|
||||
const testRef = useRef();
|
||||
|
||||
const [cubes, setCubes] = useState([{
|
||||
size: [1, 1, 1] as [number, number, number],
|
||||
position: [0, 1, 0],
|
||||
rotation: [0, 0, 0],
|
||||
color: `#ff00ff`,
|
||||
madeAt: Date.now()
|
||||
}]);
|
||||
|
||||
useEffect(() => {
|
||||
let newCubes = [];
|
||||
for (let i = 0; i < 100; i++) {
|
||||
let size = (Math.random() * 20) + 0.5
|
||||
newCubes.push({
|
||||
size: [
|
||||
size, size, size
|
||||
],
|
||||
position: [
|
||||
(Math.random() * 400) - 200,
|
||||
(Math.random() * 20) + 1,
|
||||
(Math.random() * 400) - 200,
|
||||
],
|
||||
rotation: [
|
||||
(Math.random() * 2 * Math.PI) - Math.PI,
|
||||
(Math.random() * 2 * Math.PI) - Math.PI,
|
||||
(Math.random() * 2 * Math.PI) - Math.PI
|
||||
],
|
||||
color: `#${Math.floor(Math.random() * 256).toString(16).padStart(2, '0')}${Math.floor(Math.random() * 256).toString(16).padStart(2, '0')}${Math.floor(Math.random() * 256).toString(16).padStart(2, '0')}`,
|
||||
madeAt: `${i} + ${Date.now()}`
|
||||
})
|
||||
}
|
||||
|
||||
setCubes(c => ([...c, ...newCubes]));
|
||||
}, []);
|
||||
|
||||
window.amogus = testRef;
|
||||
return (
|
||||
<>
|
||||
@@ -47,27 +75,37 @@ function App() {
|
||||
fov: 90
|
||||
}}
|
||||
>
|
||||
<Sub />
|
||||
<StatsGl />
|
||||
<ambientLight intensity={0.1} />
|
||||
<HeRotates />
|
||||
<Physics
|
||||
gravity={[0, -30, 0]}
|
||||
debug
|
||||
>
|
||||
<Player />
|
||||
<RigidBody friction={0} enabledRotations={[true, true, true]} mass={2}>
|
||||
<mesh position={new Vector3(0, 1.5, 0)} rotation={new Euler(-Math.PI/4, 0, -Math.PI/4)} castShadow={true}>
|
||||
<boxGeometry args={[1, 1, 1]} />
|
||||
<meshStandardMaterial color={'red'} />
|
||||
<meshStandardMaterial color={'orange'} />
|
||||
</mesh>
|
||||
</RigidBody>
|
||||
<RigidBody ref={testRef} friction={0} enabledRotations={[true, true, true]} mass={50} angularVelocity={[50, 50, 50]}>
|
||||
<RigidBody ref={testRef} friction={0} enabledRotations={[true, true, true]} mass={5000} angularVelocity={[10, 40, 10]}>
|
||||
<mesh position={new Vector3(3, 1.5, 0)} rotation={new Euler(-Math.PI/4, 0, -Math.PI/4)} castShadow={true}>
|
||||
<boxGeometry args={[1, 1, 1]} />
|
||||
<meshStandardMaterial color={'red'} />
|
||||
</mesh>
|
||||
</RigidBody>
|
||||
<CuboidCollider args={[5, 1, 5]} position={[0, -1, 0]} friction={2}/>
|
||||
{cubes.map(v => {
|
||||
return <RigidBody key={v.madeAt} density={1} angularVelocity={[10, 10, 10]} friction={0}>
|
||||
<mesh position={new Vector3(...v.position)} rotation={new Euler(...v.rotation)} castShadow={true}>
|
||||
<boxGeometry args={v.size} />
|
||||
<meshStandardMaterial color={v.color} />
|
||||
</mesh>
|
||||
</RigidBody>
|
||||
})}
|
||||
<CuboidCollider args={[300, 1, 300]} position={[0, -1, 0]} friction={2}/>
|
||||
<mesh rotation={new Euler(-Math.PI/2, 0, 0)} receiveShadow={true}>
|
||||
<planeGeometry args={[10, 10]} />
|
||||
<planeGeometry args={[600, 600]} />
|
||||
<meshStandardMaterial color={'white'} />
|
||||
</mesh>
|
||||
</Physics>
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { PerspectiveCamera } from "@react-three/drei";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { CapsuleCollider, CylinderCollider, RapierRigidBody, RigidBody, euler, quat } from "@react-three/rapier";
|
||||
import { useRef } from "react";
|
||||
import { Euler, PerspectiveCamera as PerspectiveCameraImpl, Quaternion, Vector3 } from "three";
|
||||
|
||||
export function Player() {
|
||||
const pos = useRef(new Vector3(5, 5, 0));
|
||||
const rot = useRef(new Euler(0, 0, 0));
|
||||
const rbRef = useRef<RapierRigidBody>(null);
|
||||
const camRef = useRef<PerspectiveCameraImpl>(null);
|
||||
|
||||
useFrame((state, dt) => {
|
||||
if (!camRef.current || !rbRef.current) return;
|
||||
|
||||
let gps = navigator.getGamepads();
|
||||
if (gps.length < 1) return;
|
||||
|
||||
const axes = gps[0]!.axes;
|
||||
|
||||
const [motionZ, motionX, viewSideways, viewVertical] = axes.map(val => Math.abs(val) < 0.5 ? 0 : val);
|
||||
|
||||
if (Math.abs(viewVertical) > 0) {
|
||||
let goal = camRef.current.rotation.x - (viewVertical * dt * 2);
|
||||
|
||||
camRef.current.rotation.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, goal))
|
||||
}
|
||||
|
||||
if (Math.abs(viewSideways) > 0) {
|
||||
let rot = quat(rbRef.current.rotation());
|
||||
rot = rot.multiply(quat().setFromEuler(new Euler(0, (-viewSideways * dt * 2), 0)));
|
||||
rbRef.current.setRotation(rot, true);
|
||||
}
|
||||
|
||||
let rot = quat(rbRef.current.rotation());
|
||||
|
||||
let desiredMotionVector = new Vector3(
|
||||
motionZ * 300,
|
||||
0,
|
||||
motionX * 300
|
||||
);
|
||||
desiredMotionVector.applyQuaternion(rot);
|
||||
|
||||
rbRef.current.applyImpulseAtPoint(
|
||||
desiredMotionVector,
|
||||
rbRef.current.translation(),
|
||||
true
|
||||
);
|
||||
|
||||
if (gps[0].buttons[0].pressed) {
|
||||
rbRef.current.applyImpulseAtPoint(
|
||||
new Vector3(0, 500, 0),
|
||||
rbRef.current.translation(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
// let linvel = rbRef.current.linvel();
|
||||
// linvel.x = Math.max(-1, Math.min(1, linvel.x));
|
||||
// linvel.z = Math.max(-1, Math.min(1, linvel.z));
|
||||
|
||||
// rbRef.current.setLinvel(linvel, true);
|
||||
});
|
||||
|
||||
return <>
|
||||
<RigidBody ref={rbRef} position={pos.current} rotation={rot.current} enabledRotations={[false, true, false]} angularDamping={50.0} density={1}>
|
||||
<PerspectiveCamera ref={camRef} makeDefault fov={90} position={[0, 2, 10]} />
|
||||
<CapsuleCollider args={[2, 3]} position={[0, 0, 0]}/>
|
||||
</RigidBody>
|
||||
</>
|
||||
}
|
||||
Reference in new issue
Block a user