Skip to content

Commit

Permalink
Add createConvex
Browse files Browse the repository at this point in the history
  • Loading branch information
Supun-ascentic committed Dec 29, 2023
1 parent e67ba3f commit 448d2d1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions createConvexRegionHelper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @author Mugen87 / https://github.com/Mugen87
*/

import * as THREE from "three";

function createConvexRegionHelper(navMesh) {
const regions = navMesh.regions;

const geometry = new THREE.BufferGeometry();
const material = new THREE.MeshBasicMaterial({
vertexColors: true,
});

const positions = [];
const colors = [];

const color = new THREE.Color();

for (let region of regions) {
// one color for each convex region

color.setHex(Math.random() * 0xffffff);

// count edges

let edge = region.edge;
const edges = [];

do {
edges.push(edge);

edge = edge.next;
} while (edge !== region.edge);

// triangulate

const triangleCount = edges.length - 2;

for (let i = 1, l = triangleCount; i <= l; i++) {
const v1 = edges[0].vertex;
const v2 = edges[i + 0].vertex;
const v3 = edges[i + 1].vertex;

positions.push(v1.x, v1.y, v1.z);
positions.push(v2.x, v2.y, v2.z);
positions.push(v3.x, v3.y, v3.z);

colors.push(color.r, color.g, color.b);
colors.push(color.r, color.g, color.b);
colors.push(color.r, color.g, color.b);
}
}

geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));

return { geometry, material };
}

export { createConvexRegionHelper };
2 changes: 1 addition & 1 deletion useNavMesh.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from "three";
import { NavMeshLoader, Vector3 } from "yuka";
import { create } from "zustand";
import { createConvexRegionHelper } from "../utils/createConvexRegionHelper";
import { createConvexRegionHelper } from "./createConvexRegionHelper";

export const useNavMesh = create((set, get) => ({
raycaster: new THREE.Raycaster(),
Expand Down

0 comments on commit 448d2d1

Please sign in to comment.