Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: Change IVoxel interface to include neighbours #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/exports.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Terrain } from './terrain/terrain'
export type { IVoxelMap, IVoxel, IVoxelMaterial } from './terrain/i-voxel-map'
export { ENeighbour } from './terrain/i-voxel-map'
38 changes: 32 additions & 6 deletions src/lib/terrain/i-voxel-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,42 @@ interface IVoxelMaterial {
readonly color: Color
}

enum ENeighbour {
xMyMzM, // whether or not there is a neighbour voxel at coords { X - 1, Y - 1, Z - 1 }
xMyMz0, // whether or not there is a neighbour voxel at coords { X - 1, Y - 1, Z }
xMyMzP, // whether or not there is a neighbour voxel at coords { X - 1, Y - 1, Z + 1 }
xMy0zM, // whether or not there is a neighbour voxel at coords { X - 1, Y , Z - 1 }
xMy0z0, // whether or not there is a neighbour voxel at coords { X - 1, Y , Z }
xMy0zP, // whether or not there is a neighbour voxel at coords { X - 1, Y , Z + 1 }
xMyPzM, // whether or not there is a neighbour voxel at coords { X - 1, Y + 1, Z - 1 }
xMyPz0, // whether or not there is a neighbour voxel at coords { X - 1, Y + 1, Z }
xMyPzP, // whether or not there is a neighbour voxel at coords { X - 1, Y + 1, Z + 1 }
x0yMzM, // whether or not there is a neighbour voxel at coords { X , Y - 1, Z - 1 }
x0yMz0, // whether or not there is a neighbour voxel at coords { X , Y - 1, Z }
x0yMzP, // whether or not there is a neighbour voxel at coords { X , Y - 1, Z + 1 }
x0y0zM, // whether or not there is a neighbour voxel at coords { X , Y , Z - 1 }
x0y0zP, // whether or not there is a neighbour voxel at coords { X , Y , Z + 1 }
x0yPzM, // whether or not there is a neighbour voxel at coords { X , Y + 1, Z - 1 }
x0yPz0, // whether or not there is a neighbour voxel at coords { X , Y + 1, Z }
x0yPzP, // whether or not there is a neighbour voxel at coords { X , Y + 1, Z + 1 }
xPyMzM, // whether or not there is a neighbour voxel at coords { X + 1, Y - 1, Z - 1 }
xPyMz0, // whether or not there is a neighbour voxel at coords { X + 1, Y - 1, Z }
xPyMzP, // whether or not there is a neighbour voxel at coords { X + 1, Y - 1, Z + 1 }
xPy0zM, // whether or not there is a neighbour voxel at coords { X + 1, Y , Z - 1 }
xPy0z0, // whether or not there is a neighbour voxel at coords { X + 1, Y , Z }
xPy0zP, // whether or not there is a neighbour voxel at coords { X + 1, Y , Z + 1 }
xPyPzM, // whether or not there is a neighbour voxel at coords { X + 1, Y + 1, Z - 1 }
xPyPz0, // whether or not there is a neighbour voxel at coords { X + 1, Y + 1, Z }
xPyPzP, // whether or not there is a neighbour voxel at coords { X + 1, Y + 1, Z + 1 }
}

/**
* A representation of a voxel.
*/
interface IVoxel {
readonly position: Uint3
readonly materialId: number
readonly neighbours: Record<ENeighbour, boolean>;
}

/**
Expand Down Expand Up @@ -58,11 +88,7 @@ interface IVoxelMap {
* @param to End of the subsection (exclusive)
*/
iterateOnVoxels(from: Uint3, to: Uint3): Generator<IVoxel>

/**
* @returns whether or not a voxel exists at these coordinates.
*/
voxelExists(x: number, y: number, z: number): boolean
}

export type { IVoxel, IVoxelMap, IVoxelMaterial }
export type { IVoxel, IVoxelMap, IVoxelMaterial };
export { ENeighbour };
71 changes: 44 additions & 27 deletions src/lib/terrain/patch/patch-factory/cube.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as THREE from '../../../three-usage'
import { ENeighbour } from '../../i-voxel-map'

const vertices = {
ppp: new THREE.Vector3(1, 1, 1),
Expand All @@ -12,14 +13,10 @@ const vertices = {
}
type FaceVertex = {
readonly vertex: THREE.Vector3
readonly shadowingNeighbourVoxels: [
THREE.Vector3,
THREE.Vector3,
THREE.Vector3,
]
readonly shadowingNeighbourVoxels: [ENeighbour, ENeighbour, ENeighbour]
readonly edgeNeighbourVoxels: {
readonly x: [THREE.Vector3, THREE.Vector3]
readonly y: [THREE.Vector3, THREE.Vector3]
readonly x: [ENeighbour, ENeighbour]
readonly y: [ENeighbour, ENeighbour]
}
}

Expand All @@ -41,6 +38,7 @@ type Face = {
readonly normal: THREE.Vector3
readonly uvUp: THREE.Vector3
readonly uvRight: THREE.Vector3
readonly facingNeighbour: ENeighbour;
}

const faceIndices: [number, number, number, number, number, number] = [
Expand All @@ -62,62 +60,81 @@ function buildFace(
const uvLeft = uvRight.clone().multiplyScalar(-1)
const uvDown = uvUp.clone().multiplyScalar(-1)

const getValueCode = (value: number): string => {
if (value === -1) {
return "M";
} else if (value === 1) {
return "P";
}
return "0";
};

const getNeighbourCode = (vec3: THREE.Vector3): ENeighbour => {
const key = `x${getValueCode(vec3.x)}y${getValueCode(vec3.y)}z${getValueCode(vec3.z)}`;
const result = ENeighbour[key as any];
if (typeof result === "undefined"){
throw new Error("Should not happen");
}
return result as unknown as ENeighbour;
};

return {
id: iF++,
type,
vertices: [
{
vertex: v00,
shadowingNeighbourVoxels: [
new THREE.Vector3().subVectors(normal, uvRight),
new THREE.Vector3().subVectors(normal, uvUp),
new THREE.Vector3().subVectors(normal, uvRight).sub(uvUp),
getNeighbourCode(new THREE.Vector3().subVectors(normal, uvRight)),
getNeighbourCode(new THREE.Vector3().subVectors(normal, uvUp)),
getNeighbourCode(new THREE.Vector3().subVectors(normal, uvRight).sub(uvUp)),
],
edgeNeighbourVoxels: {
x: [uvLeft, new THREE.Vector3().addVectors(uvLeft, normal)],
y: [uvDown, new THREE.Vector3().addVectors(uvDown, normal)],
x: [getNeighbourCode(uvLeft), getNeighbourCode(new THREE.Vector3().addVectors(uvLeft, normal))],
y: [getNeighbourCode(uvDown), getNeighbourCode(new THREE.Vector3().addVectors(uvDown, normal))],
},
},
{
vertex: v01,
shadowingNeighbourVoxels: [
new THREE.Vector3().subVectors(normal, uvRight),
new THREE.Vector3().addVectors(normal, uvUp),
new THREE.Vector3().subVectors(normal, uvRight).add(uvUp),
getNeighbourCode(new THREE.Vector3().subVectors(normal, uvRight)),
getNeighbourCode(new THREE.Vector3().addVectors(normal, uvUp)),
getNeighbourCode(new THREE.Vector3().subVectors(normal, uvRight).add(uvUp)),
],
edgeNeighbourVoxels: {
x: [uvLeft, new THREE.Vector3().addVectors(uvLeft, normal)],
y: [uvUp, new THREE.Vector3().addVectors(uvUp, normal)],
x: [getNeighbourCode(uvLeft), getNeighbourCode(new THREE.Vector3().addVectors(uvLeft, normal))],
y: [getNeighbourCode(uvUp), getNeighbourCode(new THREE.Vector3().addVectors(uvUp, normal))],
},
},
{
vertex: v10,
shadowingNeighbourVoxels: [
new THREE.Vector3().addVectors(normal, uvRight),
new THREE.Vector3().subVectors(normal, uvUp),
new THREE.Vector3().addVectors(normal, uvRight).sub(uvUp),
getNeighbourCode(new THREE.Vector3().addVectors(normal, uvRight)),
getNeighbourCode(new THREE.Vector3().subVectors(normal, uvUp)),
getNeighbourCode(new THREE.Vector3().addVectors(normal, uvRight).sub(uvUp)),
],
edgeNeighbourVoxels: {
x: [uvRight, new THREE.Vector3().addVectors(uvRight, normal)],
y: [uvDown, new THREE.Vector3().addVectors(uvDown, normal)],
x: [getNeighbourCode(uvRight), getNeighbourCode(new THREE.Vector3().addVectors(uvRight, normal))],
y: [getNeighbourCode(uvDown), getNeighbourCode(new THREE.Vector3().addVectors(uvDown, normal))],
},
},
{
vertex: v11,
shadowingNeighbourVoxels: [
new THREE.Vector3().addVectors(normal, uvRight),
new THREE.Vector3().addVectors(normal, uvUp),
new THREE.Vector3().addVectors(normal, uvRight).add(uvUp),
getNeighbourCode(new THREE.Vector3().addVectors(normal, uvRight)),
getNeighbourCode(new THREE.Vector3().addVectors(normal, uvUp)),
getNeighbourCode(new THREE.Vector3().addVectors(normal, uvRight).add(uvUp)),
],
edgeNeighbourVoxels: {
x: [uvRight, new THREE.Vector3().addVectors(uvRight, normal)],
y: [uvUp, new THREE.Vector3().addVectors(uvUp, normal)],
x: [getNeighbourCode(uvRight), getNeighbourCode(new THREE.Vector3().addVectors(uvRight, normal))],
y: [getNeighbourCode(uvUp), getNeighbourCode(new THREE.Vector3().addVectors(uvUp, normal))],
},
},
],
normal,
uvUp,
uvRight,
facingNeighbour: getNeighbourCode(normal),
}
}

Expand Down
27 changes: 4 additions & 23 deletions src/lib/terrain/patch/patch-factory/patch-factory-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ abstract class PatchFactoryBase {
)

for (const face of Object.values(Cube.faces)) {
if (
this.map.voxelExists(
voxelWorldPosition.x + face.normal.x,
voxelWorldPosition.y + face.normal.y,
voxelWorldPosition.z + face.normal.z,
)
) {
if (voxel.neighbours[face.facingNeighbour]) {
// this face will be hidden -> skip it
continue
}
Expand All @@ -155,12 +149,7 @@ abstract class PatchFactoryBase {
(faceVertex: Cube.FaceVertex): VertexData => {
let ao = 0
const [a, b, c] = faceVertex.shadowingNeighbourVoxels.map(
neighbourVoxel =>
this.map.voxelExists(
voxelWorldPosition.x + neighbourVoxel.x,
voxelWorldPosition.y + neighbourVoxel.y,
voxelWorldPosition.z + neighbourVoxel.z,
),
neighbourVoxel => voxel.neighbours[neighbourVoxel]
) as [boolean, boolean, boolean]
if (a && b) {
ao = 3
Expand All @@ -172,18 +161,10 @@ abstract class PatchFactoryBase {
let roundnessY = true
if (faceVertex.edgeNeighbourVoxels) {
for (const neighbourVoxel of faceVertex.edgeNeighbourVoxels.x) {
roundnessX &&= !this.map.voxelExists(
voxelWorldPosition.x + neighbourVoxel.x,
voxelWorldPosition.y + neighbourVoxel.y,
voxelWorldPosition.z + neighbourVoxel.z,
)
roundnessX &&= !voxel.neighbours[neighbourVoxel];
}
for (const neighbourVoxel of faceVertex.edgeNeighbourVoxels.y) {
roundnessY &&= !this.map.voxelExists(
voxelWorldPosition.x + neighbourVoxel.x,
voxelWorldPosition.y + neighbourVoxel.y,
voxelWorldPosition.z + neighbourVoxel.z,
)
roundnessY &&= !voxel.neighbours[neighbourVoxel];
}
}
return {
Expand Down
34 changes: 33 additions & 1 deletion src/test/voxel-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,49 @@ class VoxelMap implements AresRpgEngine.IVoxelMap {
if (voxel) {
position.y = voxel.y
if (from.y <= position.y && position.y < to.y) {
const neighbours: Record<AresRpgEngine.ENeighbour, boolean> = [
this.voxelExists(position.x - 1, position.y - 1, position.z - 1),
this.voxelExists(position.x - 1, position.y - 1, position.z + 0),
this.voxelExists(position.x - 1, position.y - 1, position.z + 1),
this.voxelExists(position.x - 1, position.y + 0, position.z - 1),
this.voxelExists(position.x - 1, position.y + 0, position.z + 0),
this.voxelExists(position.x - 1, position.y + 0, position.z + 1),
this.voxelExists(position.x - 1, position.y + 1, position.z - 1),
this.voxelExists(position.x - 1, position.y + 1, position.z + 0),
this.voxelExists(position.x - 1, position.y + 1, position.z + 1),

this.voxelExists(position.x + 0, position.y - 1, position.z - 1),
this.voxelExists(position.x + 0, position.y - 1, position.z + 0),
this.voxelExists(position.x + 0, position.y - 1, position.z + 1),
this.voxelExists(position.x + 0, position.y + 0, position.z - 1),
this.voxelExists(position.x + 0, position.y + 0, position.z + 1),
this.voxelExists(position.x + 0, position.y + 1, position.z - 1),
this.voxelExists(position.x + 0, position.y + 1, position.z + 0),
this.voxelExists(position.x + 0, position.y + 1, position.z + 1),

this.voxelExists(position.x + 1, position.y - 1, position.z - 1),
this.voxelExists(position.x + 1, position.y - 1, position.z + 0),
this.voxelExists(position.x + 1, position.y - 1, position.z + 1),
this.voxelExists(position.x + 1, position.y + 0, position.z - 1),
this.voxelExists(position.x + 1, position.y + 0, position.z + 0),
this.voxelExists(position.x + 1, position.y + 0, position.z + 1),
this.voxelExists(position.x + 1, position.y + 1, position.z - 1),
this.voxelExists(position.x + 1, position.y + 1, position.z + 0),
this.voxelExists(position.x + 1, position.y + 1, position.z + 1),
];

yield {
position,
materialId: voxel.type,
neighbours,
}
}
}
}
}
}

public voxelExists(x: number, y: number, z: number): boolean {
private voxelExists(x: number, y: number, z: number): boolean {
const voxel = this.getVoxel(x, z)
return voxel?.y === y
}
Expand Down
Loading