API Docs for:
Show:

File: ../src/components/spline.js

  1. ///@INFO: UNCOMMON
  2. /**
  3. * Spline allows to define splines in 3D
  4. * @class Spline
  5. * @constructor
  6. * @param {String} object to configure from
  7. */
  8.  
  9. function Spline( o )
  10. {
  11. this.enabled = true;
  12. this._render_in_viewport = false;
  13. this.path = new LS.Path();
  14. this._type = LS.Path.LINE;
  15. this._must_update = false;
  16.  
  17. if(o)
  18. this.configure(o);
  19.  
  20. this._max_points = 1024;
  21. this._range = 0;
  22. }
  23.  
  24. Object.defineProperty( Spline.prototype, 'render_in_viewport', {
  25. get: function() { return this._render_in_viewport; },
  26. set: function(v) {
  27. if(this._render_in_viewport == v)
  28. return;
  29. this._render_in_viewport = v;
  30. //set events
  31. if(!this._root || !this._root.scene)
  32. return;
  33. if(v)
  34. LEvent.bind( this._root.scene, "collectRenderInstances", this.onCollectInstances, this );
  35. else
  36. LEvent.unbind( this._root.scene, "collectRenderInstances", this.onCollectInstances, this );
  37. },
  38. enumerable: true
  39. });
  40.  
  41. Object.defineProperty( Spline.prototype, 'type', {
  42. get: function() { return this.path.type; },
  43. set: function(v) {
  44. this.path.type = v;
  45. this._must_update = true;
  46. },
  47. enumerable: true
  48. });
  49.  
  50.  
  51. Spline["@type"] = { type: "enum", values: { line: LS.Path.LINE, spline: LS.Path.SPLINE, bezier: LS.Path.BEZIER } };
  52.  
  53. Spline.prototype.onAddedToScene = function(scene)
  54. {
  55. if(this._render_in_viewport)
  56. LEvent.bind( this._root.scene, "collectRenderInstances", this.onCollectInstances, this );
  57. }
  58.  
  59. Spline.prototype.onRemovedFromScene = function(scene)
  60. {
  61. if(this._render_in_viewport)
  62. LEvent.unbind( this._root.scene, "collectRenderInstances", this.onCollectInstances, this );
  63. }
  64.  
  65. Spline.prototype.onCollectInstances = function(e, instances)
  66. {
  67. if(!this.enabled)
  68. return;
  69.  
  70. if(!this._root)
  71. return;
  72.  
  73. if(this.path.getSegments() == 0)
  74. return;
  75.  
  76. if(!this._mesh || this._must_update)
  77. this.updateMesh();
  78.  
  79. var RI = this._render_instance;
  80. if(!RI)
  81. this._render_instance = RI = new LS.RenderInstance(this._root, this);
  82.  
  83. if(this._root.transform)
  84. this._root.transform.getGlobalMatrix( RI.matrix );
  85. RI.setMatrix( RI.matrix ); //force normal
  86. //mat4.multiplyVec3( RI.center, RI.matrix, vec3.create() );
  87. mat4.getTranslation( RI.center, RI.matrix );
  88. RI.setMesh( this._mesh, gl.LINE_STRIP );
  89. RI.setRange( 0, this._range );
  90. RI.flags = RI_DEFAULT_FLAGS;
  91. RI.applyNodeFlags();
  92. RI.setMaterial( this.material || this._root.getMaterial() );
  93.  
  94. instances.push(RI);
  95. }
  96.  
  97. Spline.prototype.updateMesh = function()
  98. {
  99. if(!this._mesh)
  100. this._mesh = GL.Mesh.load( { vertices: new Float32Array( this._max_points * 3 ) } );
  101. var vertices_buffer = this._mesh.getVertexBuffer("vertices");
  102. var vertices_data = vertices_buffer.data;
  103.  
  104. var total = 0;
  105.  
  106. if(this.path.type == LS.Path.LINE)
  107. total = this.path.getSegments() + 1;
  108. else
  109. total = this.path.getSegments() * 10; //10 points per segment
  110.  
  111. if(total > this._max_points)
  112. total = this._max_points;
  113.  
  114. this.path.samplePointsTyped( total, vertices_data );
  115. vertices_buffer.upload( gl.STREAM_TYPE );
  116.  
  117. this._range = total;
  118.  
  119. this._must_update = false;
  120. }
  121.  
  122. Spline.prototype.addPoint = function( point )
  123. {
  124. this.path.addPoint( point );
  125. this._must_update = true;
  126. }
  127.  
  128. //not finished yet
  129. //LS.registerComponent( Spline );