babylon.rayHelper.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. module BABYLON {
  2. export class RayHelper {
  3. public ray:Ray;
  4. private _renderPoints: Vector3[];
  5. private _renderLine: LinesMesh;
  6. private _renderFunction: () => void;
  7. private _scene: Scene;
  8. private _updateToMeshFunction: () => void;
  9. private _attachedToMesh: AbstractMesh;
  10. private _meshSpaceDirection: Vector3;
  11. private _meshSpaceOrigin: Vector3;
  12. public static CreateAndShow(ray: Ray, scene: Scene, color:Color3): RayHelper {
  13. var helper = new RayHelper(ray);
  14. helper.show(scene, color);
  15. return helper;
  16. }
  17. constructor(ray:Ray) {
  18. this.ray = ray;
  19. }
  20. public show(scene:Scene, color:Color3): void{
  21. if(!this._renderFunction){
  22. var ray = this.ray;
  23. this._renderFunction = this._render.bind(this);
  24. this._scene = scene;
  25. this._renderPoints = [ray.origin, ray.origin.add(ray.direction.scale(ray.length))];
  26. this._renderLine = Mesh.CreateLines("ray", this._renderPoints, scene, true);
  27. this._scene.registerBeforeRender(this._renderFunction);
  28. }
  29. if (color) {
  30. this._renderLine.color.copyFrom(color);
  31. }
  32. }
  33. public hide(): void{
  34. if(this._renderFunction){
  35. this._scene.unregisterBeforeRender(this._renderFunction);
  36. this._scene = null;
  37. this._renderFunction = null;
  38. this._renderLine.dispose();
  39. this._renderLine = null;
  40. this._renderPoints = null;
  41. }
  42. }
  43. private _render(): void {
  44. var ray = this.ray;
  45. var point = this._renderPoints[1];
  46. var len = Math.min(ray.length, 1000000);
  47. point.copyFrom(ray.direction);
  48. point.scaleInPlace(len);
  49. point.addInPlace(ray.origin);
  50. Mesh.CreateLines("ray", this._renderPoints, this._scene, true, this._renderLine);
  51. }
  52. public attachToMesh(mesh:AbstractMesh, meshSpaceDirection?:Vector3, meshSpaceOrigin?:Vector3, length?:number): void{
  53. this._attachedToMesh = mesh;
  54. var ray = this.ray;
  55. if(!ray.direction){
  56. ray.direction = Vector3.Zero();
  57. }
  58. if(!ray.origin){
  59. ray.origin = Vector3.Zero();
  60. }
  61. if(length){
  62. ray.length = length;
  63. }
  64. if(!meshSpaceOrigin){
  65. meshSpaceOrigin = Vector3.Zero();
  66. }
  67. if(!meshSpaceDirection){
  68. // -1 so that this will work with Mesh.lookAt
  69. meshSpaceDirection = new Vector3(0, 0, -1);
  70. }
  71. if(!this._meshSpaceDirection){
  72. this._meshSpaceDirection = meshSpaceDirection.clone();
  73. this._meshSpaceOrigin = meshSpaceOrigin.clone();
  74. }else{
  75. this._meshSpaceDirection.copyFrom(meshSpaceDirection);
  76. this._meshSpaceOrigin.copyFrom(meshSpaceOrigin);
  77. }
  78. if(!this._updateToMeshFunction){
  79. this._updateToMeshFunction = this._updateToMesh.bind(this);
  80. this._attachedToMesh.getScene().registerBeforeRender(this._updateToMeshFunction);
  81. }
  82. this._updateToMesh();
  83. }
  84. public detachFromMesh(): void{
  85. if(this._attachedToMesh){
  86. this._attachedToMesh.getScene().unregisterBeforeRender(this._updateToMeshFunction);
  87. this._attachedToMesh = null;
  88. this._updateToMeshFunction = null;
  89. }
  90. }
  91. private _updateToMesh(): void{
  92. var ray = this.ray;
  93. if(this._attachedToMesh._isDisposed){
  94. this.detachFromMesh();
  95. return;
  96. }
  97. this._attachedToMesh.getDirectionToRef(this._meshSpaceDirection, ray.direction);
  98. Vector3.TransformCoordinatesToRef(this._meshSpaceOrigin, this._attachedToMesh.getWorldMatrix(), ray.origin);
  99. }
  100. public dispose(): void{
  101. this.hide();
  102. this.detachFromMesh();
  103. this.ray = null;
  104. }
  105. }
  106. }