geom.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. function geom_make_plane(size_x: f32 = 1.0, size_y: f32 = 1.0, verts_x: i32 = 2, verts_y: i32 = 2, uv_scale: f32 = 1.0): raw_mesh_t {
  2. let mesh: raw_mesh_t = {};
  3. mesh.scale_pos = 1.0;
  4. mesh.scale_tex = 1.0;
  5. mesh.name = "";
  6. mesh.has_next = false;
  7. // Pack positions to (-1, 1) range
  8. let half_x: f32 = size_x / 2;
  9. let half_y: f32 = size_y / 2;
  10. mesh.scale_pos = math_max(half_x, half_y);
  11. let inv: f32 = (1 / mesh.scale_pos) * 32767;
  12. mesh.posa = i16_array_create(verts_x * verts_y * 4);
  13. mesh.nora = i16_array_create(verts_x * verts_y * 2);
  14. mesh.texa = i16_array_create(verts_x * verts_y * 2);
  15. mesh.inda = u32_array_create((verts_x - 1) * (verts_y - 1) * 6);
  16. let step_x: f32 = size_x / (verts_x - 1);
  17. let step_y: f32 = size_y / (verts_y - 1);
  18. for (let i: i32 = 0; i < verts_x * verts_y; ++i) {
  19. let x: f32 = (i % verts_x) * step_x - half_x;
  20. let y: f32 = math_floor(i / verts_x) * step_y - half_y;
  21. mesh.posa[i * 4 ] = math_floor(x * inv);
  22. mesh.posa[i * 4 + 1] = math_floor(y * inv);
  23. mesh.posa[i * 4 + 2] = 0;
  24. mesh.nora[i * 2 ] = 0;
  25. mesh.nora[i * 2 + 1] = 0;
  26. mesh.posa[i * 4 + 3] = 32767;
  27. x = (i % verts_x) / (verts_x - 1);
  28. y = 1.0 - math_floor(i / verts_x) / (verts_y - 1);
  29. mesh.texa[i * 2 ] = (math_floor(x * 32767 * uv_scale) - 1) % 32767;
  30. mesh.texa[i * 2 + 1] = (math_floor(y * 32767 * uv_scale) - 1) % 32767;
  31. }
  32. for (let i: i32 = 0; i < (verts_x - 1) * (verts_y - 1); ++i) {
  33. let x: f32 = i % (verts_x - 1);
  34. let y: f32 = math_floor(i / (verts_y - 1));
  35. mesh.inda[i * 6 ] = y * verts_x + x;
  36. mesh.inda[i * 6 + 1] = y * verts_x + x + 1;
  37. mesh.inda[i * 6 + 2] = (y + 1) * verts_x + x;
  38. mesh.inda[i * 6 + 3] = y * verts_x + x + 1;
  39. mesh.inda[i * 6 + 4] = (y + 1) * verts_x + x + 1;
  40. mesh.inda[i * 6 + 5] = (y + 1) * verts_x + x;
  41. }
  42. return mesh;
  43. }
  44. function geom_make_uv_sphere(radius: f32 = 1.0, widthSegments: i32 = 32, heightSegments: i32 = 16, stretch_uv: bool = true, uvScale: f32 = 1.0): raw_mesh_t {
  45. let mesh: raw_mesh_t = {};
  46. mesh.scale_pos = 1.0;
  47. mesh.scale_tex = 1.0;
  48. mesh.name = "";
  49. mesh.has_next = false;
  50. // Pack positions to (-1, 1) range
  51. mesh.scale_pos = radius;
  52. mesh.scale_tex = uvScale;
  53. let inv: f32 = (1 / mesh.scale_pos) * 32767;
  54. let pi2: f32 = math_pi() * 2;
  55. let width_verts: i32 = widthSegments + 1;
  56. let height_verts: i32 = heightSegments + 1;
  57. mesh.posa = i16_array_create(width_verts * height_verts * 4);
  58. mesh.nora = i16_array_create(width_verts * height_verts * 2);
  59. mesh.texa = i16_array_create(width_verts * height_verts * 2);
  60. mesh.inda = u32_array_create(widthSegments * heightSegments * 6 - widthSegments * 6);
  61. let nor: vec4_t = vec4_create();
  62. let pos: i32 = 0;
  63. for (let y: i32 = 0; y < height_verts; ++y) {
  64. let v: f32 = y / heightSegments;
  65. let v_flip: f32 = 1.0 - v;
  66. if (!stretch_uv) {
  67. v_flip /= 2;
  68. }
  69. let u_off: f32 = y == 0 ? 0.5 / widthSegments : y == heightSegments ? -0.5 / widthSegments : 0.0;
  70. for (let x: i32 = 0; x < width_verts; ++x) {
  71. let u: f32 = x / widthSegments;
  72. let u_pi2: f32 = u * pi2;
  73. let v_pi: f32 = v * math_pi();
  74. let v_pi_sin: f32 = math_sin(v_pi);
  75. let vx: f32 = -radius * math_cos(u_pi2) * v_pi_sin;
  76. let vy: f32 = radius * math_sin(u_pi2) * v_pi_sin;
  77. let vz: f32 = -radius * math_cos(v_pi);
  78. let i4: i32 = pos * 4;
  79. let i2: i32 = pos * 2;
  80. mesh.posa[i4 ] = math_floor(vx * inv);
  81. mesh.posa[i4 + 1] = math_floor(vy * inv);
  82. mesh.posa[i4 + 2] = math_floor(vz * inv);
  83. vec4_normalize(vec4_set(nor, vx, vy, vz));
  84. mesh.posa[i4 + 3] = math_floor(nor.z * 32767);
  85. mesh.nora[i2 ] = math_floor(nor.x * 32767);
  86. mesh.nora[i2 + 1] = math_floor(nor.y * 32767);
  87. mesh.texa[i2 ] = (math_floor((u + u_off) * 32767) - 1) % 32767;
  88. mesh.texa[i2 + 1] = (math_floor(v_flip * 32767) - 1) % 32767;
  89. pos++;
  90. }
  91. }
  92. pos = 0;
  93. let height_segments1: i32 = heightSegments - 1;
  94. for (let y: i32 = 0; y < heightSegments; ++y) {
  95. for (let x: i32 = 0; x < widthSegments; ++x) {
  96. let x1: i32 = x + 1;
  97. let y1: i32 = y + 1;
  98. let a: f32 = y * width_verts + x1;
  99. let b: f32 = y * width_verts + x;
  100. let c: f32 = y1 * width_verts + x;
  101. let d: f32 = y1 * width_verts + x1;
  102. if (y > 0) {
  103. mesh.inda[pos++] = a;
  104. mesh.inda[pos++] = b;
  105. mesh.inda[pos++] = d;
  106. }
  107. if (y < height_segments1) {
  108. mesh.inda[pos++] = b;
  109. mesh.inda[pos++] = c;
  110. mesh.inda[pos++] = d;
  111. }
  112. }
  113. }
  114. return mesh;
  115. }
  116. type raw_mesh_t = {
  117. posa?: i16_array_t;
  118. nora?: i16_array_t;
  119. texa?: i16_array_t;
  120. inda?: u32_array_t;
  121. scale_pos?: f32;
  122. scale_tex?: f32;
  123. name?: string;
  124. has_next?: bool;
  125. };