Coder Social home page Coder Social logo

Comments (2)

Mugen87 avatar Mugen87 commented on September 13, 2024

polygon.vertices.push(v0, v1, v2);

I'm afraid this is invalid code since Polygon does not have a vertices property. Besides, it should not be required to explicitly define objects for centroid and plane. They exist by default.

If you want to create polygons from triangles, you have to use the Polygon.fromContour() method.

Creates the polygon based on the given array of points in 3D space. The method assumes the contour (the sequence of points) is defined in CCW order.

from yuka.

pierroo avatar pierroo commented on September 13, 2024

Thank you for this suggestion @Mugen87 ,

I tried with the following:

const createNavMeshFromGeometry = (geometry) => {
  const navMesh = new NavMesh();
  const positions = geometry.attributes.position.array;
  const indices = geometry.index.array;

  const polygons = [];

  for (let i = 0; i < indices.length; i += 3) {
    const a = indices[i];
    const b = indices[i + 1];
    const c = indices[i + 2];

    const v0 = new THREE.Vector3(positions[a * 3], positions[a * 3 + 1], positions[a * 3 + 2]);
    const v1 = new THREE.Vector3(positions[b * 3], positions[b * 3 + 1], positions[b * 3 + 2]);
    const v2 = new THREE.Vector3(positions[c * 3], positions[c * 3 + 1], positions[c * 3 + 2]);

    const points = [v0, v1, v2];
    const polygon = new Polygon().fromContour(points);

    polygons.push(polygon);
  }

  navMesh.fromPolygons(polygons);
  return navMesh;
}

but now it's the navMesh.fromPolygons()
that returns the error:

TypeError: tail.squaredDistanceTo is not a function
    at HalfEdge.squaredLength (yuka.js?v=e42d8424:9052:19)
    at NavMesh.fromPolygons (

so I am a bit clueless how to go from here?

EDIT:

Here is my final function with the help of chatgpt, although I am starting to think this is actually replacing the NavMeshLoader() since it gives me roughly the same result?

export const createNavMeshFromGeometry = (geometry) => {
  const navMesh = new NavMesh();
  const positions = geometry.attributes.position.array;
  const indices = geometry.index.array;

  if (!positions || !indices) {
    console.error('Invalid geometry: missing positions or indices.');
    return;
  }

  const polygons = [];

  for (let i = 0; i < indices.length; i += 3) {
    const a = indices[i];
    const b = indices[i + 1];
    const c = indices[i + 2];

    const v0 = new Vector3(positions[a * 3], positions[a * 3 + 1], positions[a * 3 + 2]);
    const v1 = new Vector3(positions[b * 3], positions[b * 3 + 1], positions[b * 3 + 2]);
    const v2 = new Vector3(positions[c * 3], positions[c * 3 + 1], positions[c * 3 + 2]);

    const polygon = new Polygon().fromContour([v0, v1, v2]);

    if (!polygon.centroid) {
      polygon.centroid = new Vector3();
    }
    if (!polygon.plane) {
      polygon.plane = new THREE.Plane();
    }
    polygon.computeCentroid();

    polygons.push(polygon);
  }

  navMesh.fromPolygons(polygons);
  return navMesh;
};

this screen is the result of my homemade function to transform my buffergeometry:
image

and here is the one when I use NavMeshLoader and give it a gltf (by using another function to turn my buffergeomtry into a gltf but it felt overkill)

image

so I'm not sure how to know if that new method is correct or not, since I'm still at the first step of it all :/
and I'm also a bit surprised at the number of polygons (3500+), since my terrain is just 4500 triangles big?

from yuka.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.