From 27659b0ca155d05cdf9a1a99f236b7da80c2d6ad Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Mon, 10 Jun 2024 00:40:23 +0200 Subject: [PATCH] feat: faces "culling" --- public/debug.png | Bin 0 -> 1100 bytes public/grass.png | Bin 1283 -> 1283 bytes src/face.ts | 8 ++--- src/main.ts | 88 +++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 public/debug.png diff --git a/public/debug.png b/public/debug.png new file mode 100644 index 0000000000000000000000000000000000000000..8299a13ce09c3dc506706a0b12790e83205f7ae7 GIT binary patch literal 1100 zcmV-S1he~zP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D1K~+TK~z{ry_ZjH z6;%|*e+61VNC6wMgc2>WDbWQO39GvBPh!f31Y)Z;VWE))jV#y+hJ{gCxNyOOg!m^h zVS%hPA!99e4sB2sLRHg0nf|p_eRx0YAPEfJ@zXX+x<9-Xwwja` z-%Xf;E!uq@;bY;eRj+8Z5dS+ujoJhg&MNz z&y#)uzHn-CxaxBJv~XoojaU9&GGkf>?OtYki zhdFb`>4s(wig5cjw~8}LGlzS6_RwBz5zQRN#^~x|UKA*ExPG0H z5l)?=ua6ZgIC8}C4&Ib8lx+@r(p$H3@E~{Z1|90ER$YAPQ(BnqYkJ|U75)Jw(m4-# SHlCdT0000 { + const offset = FACES_OFFSETS[face.orientation] + const adjacentBlock = new THREE.Vector3( + block.x + offset.x, + block.y + offset.y, + block.z + offset.z + ) + return !chunk.some((c) => c.equals(adjacentBlock)) + }) +} + +function countVisibleFaces(chunk: THREE.Vector3[]): { [key: string]: number } { + const faceCounts: { [key: string]: number } = { + Up: 0, + North: 0, + South: 0, + East: 0, + West: 0, + Down: 0 + } + + chunk.forEach((block) => { + Object.keys(FACES_OFFSETS).forEach((orientation) => { + const offset = FACES_OFFSETS[orientation] + const adjacentBlock = new THREE.Vector3( + block.x + offset.x, + block.y + offset.y, + block.z + offset.z + ) + if (!chunk.some((c) => c.equals(adjacentBlock))) { + faceCounts[orientation]++ + } + }) + }) + + return faceCounts +} function onTextureLoaded(textures: THREE.Texture[]) { const chunk = [] as THREE.Vector3[] - for (let i = -16; i < 16; i++) { - for (let j = -16; j < 16; j++) { + const radius = 64 + for (let i = -radius; i < radius; i++) { + for (let j = -radius; j < radius; j++) { chunk.push( new THREE.Vector3( i, @@ -36,19 +95,20 @@ function onTextureLoaded(textures: THREE.Texture[]) { } } + const faceCounts = countVisibleFaces(chunk) + const faces = [ - new Face("Up", chunk.length, textures[0]), - new Face("North", chunk.length, textures[1]), - new Face("South", chunk.length, textures[2]), - new Face("East", chunk.length, textures[3]), - new Face("West", chunk.length, textures[4]), - new Face("Down", chunk.length, textures[5]) + new Face("Up", faceCounts.Up, textures[0]), + new Face("North", faceCounts.North, textures[1]), + new Face("South", faceCounts.South, textures[2]), + new Face("East", faceCounts.East, textures[3]), + new Face("West", faceCounts.West, textures[4]), + new Face("Down", faceCounts.Down, textures[5]) ] const cubes = [] as Block[] chunk.forEach((block) => { - // TODO PUSH FACES ONLY IN CONTACT WITH AIR - cubes.push(new Block(faces, block)) + cubes.push(new Block(filterFaces(faces, block, chunk), block)) }) faces.forEach((face) => { @@ -56,9 +116,9 @@ function onTextureLoaded(textures: THREE.Texture[]) { }) } -camera.position.x = 20 -camera.position.y = 20 -camera.position.z = 20 +camera.position.x = 5 +camera.position.y = 5 +camera.position.z = 5 function animate(elapsedTimeMs: number) { renderer.render(scene, camera)