Skip to content

Commit

Permalink
🎨 change m4 method names and organized code
Browse files Browse the repository at this point in the history
  • Loading branch information
slooi committed Sep 25, 2024
1 parent 8cdfa51 commit 926d1c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './App.css'
import { useEffect, useRef, useState } from 'react'
import { createWebglRenderer } from './webgl'

import { UserInputHandler } from './UserInputHandler'

Check failure on line 4 in src/App.tsx

View workflow job for this annotation

GitHub Actions / build-and-deploy

Cannot find module './UserInputHandler' or its corresponding type declarations.

// const renderer = createWebglRenderer()

Expand All @@ -14,30 +14,32 @@ function App() {
const [renderer, setRenderer] = useState<ReturnType<typeof createWebglRenderer> | null>(null)
const canvasRef = useRef<HTMLCanvasElement>(null)

const userInputHandler = new UserInputHandler()
userInputHandler.on("w", () => { console.log("asdasd") })
useEffect(() => {
if (canvasRef.current) {
const newRenderer = createWebglRenderer(canvasRef.current)
setTimeout(() => setRenderer(newRenderer), 200)
}
}, [])

const a = async (e: MouseEvent) => await canvasRef.current?.requestPointerLock()

Check failure on line 26 in src/App.tsx

View workflow job for this annotation

GitHub Actions / build-and-deploy

'e' is declared but its value is never read.
const b = (e: MouseEvent) => {
console.log("e", e.movementX, e.movementY)
setRotation(val => ({ x: val.x - e.movementY, y: val.y - e.movementX, z: val.z }))
}

useEffect(() => {
const a = async (e: MouseEvent) => {
console.log("e", e)
await canvasRef.current?.requestPointerLock()
};
canvasRef.current?.addEventListener("click", a);

const b: (this: Document, ev: MouseEvent) => any = e => {
console.log("e", e.movementX, e.movementY)
setRotation(val => { return { x: val.x - e.movementY, y: val.y - e.movementX, z: val.z } })
}
document.addEventListener("mousemove", b);

return () => {
canvasRef.current?.removeEventListener("click", a)
document.removeEventListener("mousemove", b)
}
}, [canvasRef])


useEffect(() => {
if (!renderer) return
console.log("scale, translation, rotation, originTranslation", scale, translation, rotation, originTranslation)
Expand Down
7 changes: 6 additions & 1 deletion src/m4.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

/* HELPER FUNCTIONS */
const m4 = {
scale: (m4x4: Float32Array, sx: number, sy: number, sz: number) => m4x4 = m4.dot(m4.scaling(sx, sy, sz), m4x4),
translate: (m4x4: Float32Array, tx: number, ty: number, tz: number) => m4x4 = m4.dot(m4.translation(tx, ty, tz), m4x4),
rotateX: (m4x4: Float32Array, degreeX: number) => m4x4 = m4.dot(m4.rotationX(degreeX), m4x4),
rotateY: (m4x4: Float32Array, degreeY: number) => m4x4 = m4.dot(m4.rotationY(degreeY), m4x4),
rotateZ: (m4x4: Float32Array, degreeZ: number) => m4x4 = m4.dot(m4.rotationZ(degreeZ), m4x4),
identity: () => {
return new Float32Array([
1, 0, 0, 0,
Expand All @@ -9,7 +14,7 @@ const m4 = {
0, 0, 0, 1
])
},
scale: (scaleX: number, scaleY: number, scaleZ: number) => {
scaling: (scaleX: number, scaleY: number, scaleZ: number) => {
return new Float32Array([
scaleX, 0, 0, 0,
0, scaleY, 0, 0,
Expand Down

0 comments on commit 926d1c9

Please sign in to comment.