babylon.groundMesh.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. module BABYLON {
  2. export class GroundMesh extends Mesh {
  3. public chunkSize = 128; // Aiming to get around 128 indices per submesh
  4. private _worldInverse = new BABYLON.Matrix();
  5. public _subdivisions: number;
  6. constructor(name: string, scene: Scene) {
  7. super(name, scene);
  8. }
  9. public get subdivisions(): number {
  10. return this._subdivisions;
  11. }
  12. public optimize(subdivisions?: number): void {
  13. if (this.getTotalVertices() < 2000) {
  14. Tools.Warn("Optimizing GroundMesh requires at least 2000 vertices.");
  15. }
  16. this.subdivide(subdivisions || this._subdivisions);
  17. this.createOrUpdateSubmeshesOctree();
  18. }
  19. public getHeightAtCoordinates(x: number, z: number): number {
  20. var ray = new BABYLON.Ray(new BABYLON.Vector3(x, this.getBoundingInfo().boundingBox.maximumWorld.y + 1, z), new BABYLON.Vector3(0, -1, 0));
  21. this.getWorldMatrix().invertToRef(this._worldInverse);
  22. ray = BABYLON.Ray.Transform(ray, this._worldInverse);
  23. var pickInfo = this.intersects(ray);
  24. if (pickInfo.hit) {
  25. var result = BABYLON.Vector3.TransformCoordinates(pickInfo.pickedPoint, this.getWorldMatrix());
  26. return result.y;
  27. }
  28. return 0;
  29. }
  30. }
  31. }