NGL@1.0.0-beta.7 Home Manual Reference Source Gallery

src/representation/buffer-representation.js

  1. /**
  2. * @file Buffer Representation
  3. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  4. * @private
  5. */
  6.  
  7. import Representation from './representation.js'
  8.  
  9. /**
  10. * Representation for showing buffer objects. Good for efficiently showing
  11. * large amounts of geometric primitives e.g. spheres via {@link SphereBuffer}.
  12. * Smaller numbers of geometric primitives are more easily shown with help
  13. * from the {@link Shape} class.
  14. *
  15. * __Name:__ _buffer_
  16. *
  17. * @example
  18. * // add a single red sphere from a buffer to a shape instance
  19. * var shape = new NGL.Shape( "shape" );
  20. * var sphereBuffer = new NGL.SphereBuffer( {
  21. * position: new Float32Array( [ 0, 0, 0 ] ),
  22. * color: new Float32Array( [ 1, 0, 0 ] ),
  23. * radius: new Float32Array( [ 1 ] )
  24. * } );
  25. * shape.addBuffer( sphereBuffer );
  26. * var shapeComp = stage.addComponentFromObject( shape );
  27. * shapeComp.addRepresentation( "buffer" );
  28. *
  29. * @example
  30. * // add a single red sphere from a buffer to a structure component instance
  31. * stage.loadFile( "rcsb://1crn" ).then( function( o ){
  32. * var sphereBuffer = new NGL.SphereBuffer( {
  33. * position: new Float32Array( [ 0, 0, 0 ] ),
  34. * color: new Float32Array( [ 1, 0, 0 ] ),
  35. * radius: new Float32Array( [ 1 ] )
  36. * } );
  37. * o.addBufferRepresentation( sphereBuffer, { opacity: 0.5 } );
  38. * } );
  39. */
  40. class BufferRepresentation extends Representation {
  41. /**
  42. * Create Buffer representation
  43. * @param {Buffer} buffer - a buffer object
  44. * @param {Viewer} viewer - a viewer object
  45. * @param {RepresentationParameters} params - representation parameters
  46. */
  47. constructor (buffer, viewer, params) {
  48. if (!Array.isArray(buffer)) {
  49. buffer = [ buffer ]
  50. }
  51.  
  52. super(buffer, viewer, params)
  53.  
  54. this.type = 'buffer'
  55.  
  56. this.parameters = Object.assign({
  57.  
  58. }, this.parameters, {
  59.  
  60. colorScheme: null,
  61. colorScale: null,
  62. colorValue: null,
  63. colorDomain: null,
  64. colorMode: null
  65.  
  66. })
  67.  
  68. this.buffer = buffer
  69.  
  70. this.init(params)
  71. }
  72.  
  73. init (params) {
  74. super.init(params)
  75.  
  76. this.build()
  77. }
  78.  
  79. create () {
  80. this.bufferList.push.apply(this.bufferList, this.buffer)
  81. }
  82.  
  83. attach (callback) {
  84. this.bufferList.forEach(buffer => {
  85. this.viewer.add(buffer)
  86. buffer.setParameters(this.getBufferParams())
  87. })
  88. this.setVisibility(this.visible)
  89.  
  90. callback()
  91. }
  92. }
  93.  
  94. export default BufferRepresentation