-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.jsx
199 lines (168 loc) · 5.97 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import React, { useState, useEffect, useRef } from 'react';
import * as THREE from 'three';
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader';
import * as CANNON from 'cannon-es';
import './index.css';
import { randInt } from 'three/src/math/MathUtils.js';
function App() {
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
const checkMobile = () => {
setIsMobile(window.innerWidth <= 768);
};
checkMobile();
window.addEventListener('resize', checkMobile);
return () => window.removeEventListener('resize', checkMobile);
}, []);
return (
<div className="relative h-screen w-screen overflow-hidden bg-[#140b29]">
<STLViewer
url="3DBenchy.stl"
className="absolute inset-0 w-full h-full"
isMobile={isMobile}
/>
<main className="relative z-10 flex flex-col justify-between h-full p-8 text-white bg-transparent">
<div>
<h1 className="text-5xl font-bold max-md:text-4xl">Reed Graff</h1>
<p className="mt-5 text-base">
I'm the CTO of Compliancy. We help roofers pull permits faster, and we're hiring!<br/>Reed [at] withcompliancy [dot] com
</p>
</div>
<SocialLinks />
</main>
</div>
);
}
function STLViewer({ url, className, isMobile }) {
const mountRef = useRef(null);
useEffect(() => {
if (!mountRef.current) return;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
mountRef.current.appendChild(renderer.domElement);
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const aspectRatio = windowWidth / windowHeight;
camera.aspect = aspectRatio;
camera.updateProjectionMatrix();
let xPosition = (windowWidth * -0.7) / windowWidth * 10;
if (isMobile) {
xPosition = (windowWidth * 0) / windowWidth * 10;
}
camera.lookAt(0, 0, 0);
camera.position.set(xPosition, 0, 20);
const world = new CANNON.World();
world.gravity.set(0, -9.82, 0);
const floorShape = new CANNON.Plane();
const floorMaterial = new CANNON.Material({ restitution: 1.2 });
const floorBody = new CANNON.Body({ mass: 0, material: floorMaterial });
floorBody.addShape(floorShape);
floorBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
floorBody.position.set(0, -5, 0);
world.addBody(floorBody);
let objects = [];
let objectCount = 0;
const maxObjects = randInt(1, 4);
function createObject() {
if (objectCount >= maxObjects) return;
const loader = new STLLoader();
loader.load(url, (geometry) => {
const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
geometry.center();
geometry.computeBoundingBox();
const size = new THREE.Vector3();
geometry.boundingBox.getSize(size);
const maxDim = Math.max(size.x, size.y, size.z);
const scale = 10 / maxDim;
mesh.scale.set(scale, scale, scale);
const shape = new CANNON.Box(new CANNON.Vec3(1, 1, 1));
const body = new CANNON.Body({
mass: 1,
material: new CANNON.Material({ restitution: 0.8 })
});
body.addShape(shape);
world.addBody(body);
const randomQuaternion = new CANNON.Quaternion();
randomQuaternion.setFromEuler(
Math.random() * Math.PI * 2,
Math.random() * Math.PI * 2,
Math.random() * Math.PI * 2
);
body.quaternion.copy(randomQuaternion);
body.position.set(0, 10, 0);
body.velocity.set(0, 0, 0);
body.angularVelocity.set(0, 0, 0);
objects.push({ mesh, body });
objectCount++;
});
}
const intervalId = setInterval(() => {
createObject();
if (objectCount >= maxObjects) {
clearInterval(intervalId);
}
}, 500);
const fixedTimeStep = 1 / 60;
let lastTime;
const animate = (time) => {
requestAnimationFrame(animate);
if (lastTime !== undefined) {
const deltaTime = (time - lastTime) / 1000;
world.step(fixedTimeStep, deltaTime, 3);
}
objects.forEach(({ mesh, body }) => {
mesh.position.copy(body.position);
mesh.quaternion.copy(body.quaternion);
});
renderer.render(scene, camera);
lastTime = time;
};
animate();
const handleResize = () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
};
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
objects.forEach(({ mesh }) => {
if (mesh.geometry) mesh.geometry.dispose();
if (mesh.material) mesh.material.dispose();
});
renderer.dispose();
mountRef.current.removeChild(renderer.domElement);
clearInterval(intervalId);
};
}, [url, isMobile]);
return <div ref={mountRef} className={className} />;
}
export default App;
const socialLinks = [
{ url: 'mailto:[email protected]', label: '[email protected]' },
{ url: 'https://withcompliancy.com/', label: 'Company Website' },
{ url: 'https://www.linkedin.com/in/reedgraff/', label: 'LinkedIn Profile' },
{ url: 'https://github.com/ReedGraff', label: 'GitHub Profile' }
];
function SocialLinks() {
return (
<nav className="flex flex-col text-sm">
{socialLinks.map((link, index) => (
<a
key={index}
href={link.url}
className={`${index > 0 ? 'mt-3' : ''}`}
target="_blank"
rel="noopener noreferrer"
>
{link.label}
</a>
))}
</nav>
);
}