babylon.ray.tests.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Describes the test suite.
  3. */
  4. describe('Babylon Ray', function() {
  5. let subject: BABYLON.Engine;
  6. this.timeout(10000);
  7. /**
  8. * Loads the dependencies.
  9. */
  10. before(function(done) {
  11. this.timeout(180000);
  12. (BABYLONDEVTOOLS).Loader
  13. .useDist()
  14. .testMode()
  15. .load(function() {
  16. // Force apply promise polyfill for consistent behavior between chrome headless, IE11, and other browsers.
  17. BABYLON.PromisePolyfill.Apply(true);
  18. done();
  19. });
  20. });
  21. /**
  22. * Create a new engine subject before each test.
  23. */
  24. beforeEach(function() {
  25. subject = new BABYLON.NullEngine({
  26. renderHeight: 256,
  27. renderWidth: 256,
  28. textureSize: 256,
  29. deterministicLockstep: false,
  30. lockstepMaxSteps: 1
  31. });
  32. // Avoid creating normals in PBR materials.
  33. subject.getCaps().standardDerivatives = true;
  34. });
  35. /**
  36. * Ray tests.
  37. */
  38. describe('#ray', () => {
  39. it('pickWithRay', () => {
  40. const scene = new BABYLON.Scene(subject);
  41. const vertexData = new BABYLON.VertexData();
  42. vertexData.indices = [0, 1, 2];
  43. vertexData.positions = [
  44. 0, 0, 0,
  45. 1, 0, 0,
  46. 0, 1, 0,
  47. ];
  48. vertexData.normals = [
  49. 0, 0, -1,
  50. 1, 0, -1,
  51. 0, 1, -1,
  52. ];
  53. const mesh = new BABYLON.Mesh("triangle", scene);
  54. const geometry = new BABYLON.Geometry("triangle", scene, vertexData);
  55. geometry.applyToMesh(mesh);
  56. const direction = BABYLON.Vector3.Forward();
  57. for (const index of vertexData.indices) {
  58. const position = BABYLON.Vector3.FromArray(vertexData.positions, index * 3);
  59. const normal = BABYLON.Vector3.FromArray(vertexData.normals, index * 3).normalize();
  60. const origin = new BABYLON.Vector3(position.x, position.y, position.z - 1);
  61. const ray = new BABYLON.Ray(origin, direction);
  62. const hit = scene.pickWithRay(ray);
  63. expect(hit.hit, `[${index}] hit.hit`).to.be.true;
  64. expect(BABYLON.Vector3.DistanceSquared(hit.pickedPoint, position), `[${index}] hit.pickedPoint`).to.equal(0);
  65. expect(BABYLON.Vector3.DistanceSquared(hit.getNormal(), normal), `[${index}] hit.getNormal()`).to.equal(0);
  66. }
  67. });
  68. });
  69. });