threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TorusGeometryGenerator.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {AGeometryGenerator} from '../AGeometryGenerator'
  2. import {Vector3} from 'threepipe'
  3. export interface TorusGeometryGeneratorParams {
  4. radius: number,
  5. tube: number,
  6. radialSegments: number,
  7. tubularSegments: number,
  8. arc: number
  9. }
  10. export class TorusGeometryGenerator extends AGeometryGenerator<TorusGeometryGeneratorParams> {
  11. defaultParams = {
  12. radius: 1,
  13. tube: 0.4,
  14. radialSegments: 12,
  15. tubularSegments: 48,
  16. arc: Math.PI * 2,
  17. }
  18. protected _generateData(params: TorusGeometryGeneratorParams) {
  19. const {radius, tube, arc} = params
  20. let {radialSegments, tubularSegments} = params
  21. radialSegments = Math.floor(radialSegments)
  22. tubularSegments = Math.floor(tubularSegments)
  23. // buffers
  24. const indices = []
  25. const vertices = []
  26. const normals = []
  27. const uvs = []
  28. // helper variables
  29. const center = new Vector3()
  30. const vertex = new Vector3()
  31. const normal = new Vector3()
  32. // generate vertices, normals and uvs
  33. for (let j = 0; j <= radialSegments; j++) {
  34. for (let i = 0; i <= tubularSegments; i++) {
  35. const u = i / tubularSegments * arc
  36. const v = j / radialSegments * Math.PI * 2
  37. // vertex
  38. vertex.x = (radius + tube * Math.cos(v)) * Math.cos(u)
  39. vertex.y = (radius + tube * Math.cos(v)) * Math.sin(u)
  40. vertex.z = tube * Math.sin(v)
  41. vertices.push(vertex.x, vertex.y, vertex.z)
  42. // normal
  43. center.x = radius * Math.cos(u)
  44. center.y = radius * Math.sin(u)
  45. normal.subVectors(vertex, center).normalize()
  46. normals.push(normal.x, normal.y, normal.z)
  47. // uv
  48. uvs.push(i / tubularSegments)
  49. uvs.push(j / radialSegments)
  50. }
  51. }
  52. // generate indices
  53. for (let j = 1; j <= radialSegments; j++) {
  54. for (let i = 1; i <= tubularSegments; i++) {
  55. // indices
  56. const a = (tubularSegments + 1) * j + i - 1
  57. const b = (tubularSegments + 1) * (j - 1) + i - 1
  58. const c = (tubularSegments + 1) * (j - 1) + i
  59. const d = (tubularSegments + 1) * j + i
  60. // faces
  61. indices.push(a, b, d)
  62. indices.push(b, c, d)
  63. }
  64. }
  65. return {indices, vertices, normals, uvs}
  66. }
  67. }