threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CircleGeometryGenerator.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {AGeometryGenerator} from '../AGeometryGenerator'
  2. import {Vector2, Vector3} from 'threepipe'
  3. export interface CircleGeometryGeneratorParams {
  4. radius: number,
  5. segments: number,
  6. thetaStart: number,
  7. thetaLength: number
  8. }
  9. export class CircleGeometryGenerator extends AGeometryGenerator<CircleGeometryGeneratorParams> {
  10. defaultParams = {
  11. radius: 1,
  12. segments: 32,
  13. thetaStart: 0,
  14. thetaLength: Math.PI * 2,
  15. }
  16. protected _generateData(params: CircleGeometryGeneratorParams) {
  17. const {radius, thetaStart, thetaLength} = params
  18. const segments = Math.max(3, params.segments)
  19. // buffers
  20. const indices = []
  21. const vertices = []
  22. const normals = []
  23. const uvs = []
  24. // helper variables
  25. const vertex = new Vector3()
  26. const uv = new Vector2()
  27. // center point
  28. vertices.push(0, 0, 0)
  29. normals.push(0, 0, 1)
  30. uvs.push(0.5, 0.5)
  31. for (let s = 0, i = 3; s <= segments; s++, i += 3) {
  32. const segment = thetaStart + s / segments * thetaLength
  33. // vertex
  34. vertex.x = radius * Math.cos(segment)
  35. vertex.y = radius * Math.sin(segment)
  36. vertices.push(vertex.x, vertex.y, vertex.z)
  37. // normal
  38. normals.push(0, 0, 1)
  39. // uvs
  40. uv.x = (vertices[ i ] / radius + 1) / 2
  41. uv.y = (vertices[ i + 1 ] / radius + 1) / 2
  42. uvs.push(uv.x, uv.y)
  43. }
  44. // indices
  45. for (let i = 1; i <= segments; i++) {
  46. indices.push(i, i + 1, 0)
  47. }
  48. return {indices, vertices, normals, uvs}
  49. }
  50. }