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

src/buffer/cylindergeometry-buffer.js

  1. /**
  2. * @file Cylinder Geometry Buffer
  3. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  4. * @private
  5. */
  6.  
  7. import { Matrix4, Vector3, CylinderBufferGeometry } from '../../lib/three.es6.js'
  8.  
  9. import { defaults } from '../utils.js'
  10. import { calculateCenterArray, serialBlockArray } from '../math/array-utils.js'
  11. import GeometryBuffer from './geometry-buffer.js'
  12.  
  13. const scale = new Vector3()
  14. const eye = new Vector3()
  15. const target = new Vector3()
  16. const up = new Vector3(0, 1, 0)
  17.  
  18. /**
  19. * Cylinder geometry buffer.
  20. *
  21. * @example
  22. * var cylinderGeometryBuffer = new CylinderGeometryBuffer({
  23. * position1: new Float32Array([ 0, 0, 0 ]),
  24. * position2: new Float32Array([ 1, 1, 1 ]),
  25. * color: new Float32Array([ 1, 0, 0 ]),
  26. * color2: new Float32Array([ 0, 1, 0 ]),
  27. * radius: new Float32Array([ 1 ])
  28. * });
  29. */
  30. class CylinderGeometryBuffer extends GeometryBuffer {
  31. /**
  32. * @param {Object} data - buffer data
  33. * @param {Float32Array} data.position1 - from positions
  34. * @param {Float32Array} data.position2 - to positions
  35. * @param {Float32Array} data.color - from colors
  36. * @param {Float32Array} data.color2 - to colors
  37. * @param {Float32Array} data.radius - radii
  38. * @param {Picker} [data.picking] - picking ids
  39. * @param {BufferParameters} [params] - parameters object
  40. */
  41. constructor (data, params) {
  42. const d = data || {}
  43. const p = params || {}
  44.  
  45. const radialSegments = defaults(p.radialSegments, 10)
  46. const openEnded = defaults(p.openEnded, true)
  47. const matrix = new Matrix4().makeRotationX(Math.PI / 2)
  48.  
  49. const geo = new CylinderBufferGeometry(
  50. 1, // radiusTop,
  51. 1, // radiusBottom,
  52. 1, // height,
  53. radialSegments, // radialSegments,
  54. 1, // heightSegments,
  55. openEnded // openEnded
  56. )
  57. geo.applyMatrix(matrix)
  58.  
  59. const n = d.position1.length
  60. const m = d.radius.length
  61.  
  62. //
  63.  
  64. const geoLength = geo.attributes.position.array.length / 3
  65. const count = n / 3
  66. const primitiveId = new Float32Array(count * 2 * geoLength)
  67. serialBlockArray(count, geoLength, 0, primitiveId)
  68. serialBlockArray(count, geoLength, count * geoLength, primitiveId)
  69.  
  70. //
  71.  
  72. const position = new Float32Array(n * 2)
  73. const color = new Float32Array(n * 2)
  74.  
  75. super({
  76. position: position,
  77. color: color,
  78. primitiveId: primitiveId,
  79. picking: d.picking
  80. }, p, geo)
  81.  
  82. this.__center = new Float32Array(n)
  83.  
  84. this._position = position
  85. this._color = color
  86. this._from = new Float32Array(n * 2)
  87. this._to = new Float32Array(n * 2)
  88. this._radius = new Float32Array(m * 2)
  89.  
  90. this.setAttributes(d, true)
  91. }
  92.  
  93. applyPositionTransform (matrix, i, i3) {
  94. eye.fromArray(this._from, i3)
  95. target.fromArray(this._to, i3)
  96. matrix.lookAt(eye, target, up)
  97.  
  98. const r = this._radius[ i ]
  99. scale.set(r, r, eye.distanceTo(target))
  100. matrix.scale(scale)
  101. }
  102.  
  103. setAttributes (data, initNormals) {
  104. const meshData = {}
  105.  
  106. if (data.position1 && data.position2) {
  107. calculateCenterArray(
  108. data.position1, data.position2, this.__center
  109. )
  110. calculateCenterArray(
  111. data.position1, this.__center, this._position
  112. )
  113. calculateCenterArray(
  114. this.__center, data.position2, this._position, data.position1.length
  115. )
  116. this._from.set(data.position1)
  117. this._from.set(this.__center, data.position1.length)
  118. this._to.set(this.__center)
  119. this._to.set(data.position2, this.__center.length)
  120. meshData.position = this._position
  121. }
  122.  
  123. if (data.color && data.color2) {
  124. this._color.set(data.color)
  125. this._color.set(data.color2, data.color.length)
  126. meshData.color = this._color
  127. }
  128.  
  129. if (data.radius) {
  130. this._radius.set(data.radius)
  131. this._radius.set(data.radius, data.radius.length)
  132. meshData.radius = this._radius
  133. }
  134.  
  135. super.setAttributes(meshData, initNormals)
  136. }
  137.  
  138. get updateNormals () { return true }
  139. }
  140.  
  141. export default CylinderGeometryBuffer