TeapotGeometry.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Matrix4,
  5. Vector3,
  6. Vector4
  7. } from 'three';
  8. /**
  9. * Tessellates the famous Utah teapot database by Martin Newell into triangles.
  10. *
  11. * The teapot should normally be rendered as a double sided object, since for some
  12. * patches both sides can be seen, e.g., the gap around the lid and inside the spout.
  13. *
  14. * Segments 'n' determines the number of triangles output. Total triangles = 32*2*n*n - 8*n
  15. * (degenerates at the top and bottom cusps are deleted).
  16. *
  17. * Code based on [SPD software]{@link http://tog.acm.org/resources/SPD/}
  18. * Created for the Udacity course [Interactive Rendering]{@link http://bit.ly/ericity}
  19. *
  20. * ```js
  21. * const geometry = new TeapotGeometry( 50, 18 );
  22. * const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  23. * const teapot = new THREE.Mesh( geometry, material );
  24. * scene.add( teapot );
  25. * ```
  26. *
  27. * @augments BufferGeometry
  28. * @three_import import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  29. */
  30. class TeapotGeometry extends BufferGeometry {
  31. /**
  32. * Constructs a new teapot geometry.
  33. *
  34. * @param {number} [size=50] - Relative scale of the teapot.
  35. * @param {number} [segments=10] - Number of line segments to subdivide each patch edge.
  36. * @param {boolean} [bottom=true] - Whether the bottom of the teapot is generated or not.
  37. * @param {boolean} [lid=true] - Whether the lid is generated or not.
  38. * @param {boolean} [body=true] - Whether the body is generated or not.
  39. * @param {boolean} [fitLid=true] - Whether the lid is slightly stretched to prevent gaps between the body and lid or not.
  40. * @param {boolean} [blinn=true] - Whether the teapot is scaled vertically for better aesthetics or not.
  41. */
  42. constructor( size = 50, segments = 10, bottom = true, lid = true, body = true, fitLid = true, blinn = true ) {
  43. // 32 * 4 * 4 Bezier spline patches
  44. const teapotPatches = [
  45. /*rim*/
  46. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  47. 3, 16, 17, 18, 7, 19, 20, 21, 11, 22, 23, 24, 15, 25, 26, 27,
  48. 18, 28, 29, 30, 21, 31, 32, 33, 24, 34, 35, 36, 27, 37, 38, 39,
  49. 30, 40, 41, 0, 33, 42, 43, 4, 36, 44, 45, 8, 39, 46, 47, 12,
  50. /*body*/
  51. 12, 13, 14, 15, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  52. 15, 25, 26, 27, 51, 60, 61, 62, 55, 63, 64, 65, 59, 66, 67, 68,
  53. 27, 37, 38, 39, 62, 69, 70, 71, 65, 72, 73, 74, 68, 75, 76, 77,
  54. 39, 46, 47, 12, 71, 78, 79, 48, 74, 80, 81, 52, 77, 82, 83, 56,
  55. 56, 57, 58, 59, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  56. 59, 66, 67, 68, 87, 96, 97, 98, 91, 99, 100, 101, 95, 102, 103, 104,
  57. 68, 75, 76, 77, 98, 105, 106, 107, 101, 108, 109, 110, 104, 111, 112, 113,
  58. 77, 82, 83, 56, 107, 114, 115, 84, 110, 116, 117, 88, 113, 118, 119, 92,
  59. /*handle*/
  60. 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
  61. 123, 136, 137, 120, 127, 138, 139, 124, 131, 140, 141, 128, 135, 142, 143, 132,
  62. 132, 133, 134, 135, 144, 145, 146, 147, 148, 149, 150, 151, 68, 152, 153, 154,
  63. 135, 142, 143, 132, 147, 155, 156, 144, 151, 157, 158, 148, 154, 159, 160, 68,
  64. /*spout*/
  65. 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
  66. 164, 177, 178, 161, 168, 179, 180, 165, 172, 181, 182, 169, 176, 183, 184, 173,
  67. 173, 174, 175, 176, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
  68. 176, 183, 184, 173, 188, 197, 198, 185, 192, 199, 200, 189, 196, 201, 202, 193,
  69. /*lid*/
  70. 203, 203, 203, 203, 204, 205, 206, 207, 208, 208, 208, 208, 209, 210, 211, 212,
  71. 203, 203, 203, 203, 207, 213, 214, 215, 208, 208, 208, 208, 212, 216, 217, 218,
  72. 203, 203, 203, 203, 215, 219, 220, 221, 208, 208, 208, 208, 218, 222, 223, 224,
  73. 203, 203, 203, 203, 221, 225, 226, 204, 208, 208, 208, 208, 224, 227, 228, 209,
  74. 209, 210, 211, 212, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
  75. 212, 216, 217, 218, 232, 241, 242, 243, 236, 244, 245, 246, 240, 247, 248, 249,
  76. 218, 222, 223, 224, 243, 250, 251, 252, 246, 253, 254, 255, 249, 256, 257, 258,
  77. 224, 227, 228, 209, 252, 259, 260, 229, 255, 261, 262, 233, 258, 263, 264, 237,
  78. /*bottom*/
  79. 265, 265, 265, 265, 266, 267, 268, 269, 270, 271, 272, 273, 92, 119, 118, 113,
  80. 265, 265, 265, 265, 269, 274, 275, 276, 273, 277, 278, 279, 113, 112, 111, 104,
  81. 265, 265, 265, 265, 276, 280, 281, 282, 279, 283, 284, 285, 104, 103, 102, 95,
  82. 265, 265, 265, 265, 282, 286, 287, 266, 285, 288, 289, 270, 95, 94, 93, 92
  83. ];
  84. const teapotVertices = [
  85. 1.4, 0, 2.4,
  86. 1.4, - 0.784, 2.4,
  87. 0.784, - 1.4, 2.4,
  88. 0, - 1.4, 2.4,
  89. 1.3375, 0, 2.53125,
  90. 1.3375, - 0.749, 2.53125,
  91. 0.749, - 1.3375, 2.53125,
  92. 0, - 1.3375, 2.53125,
  93. 1.4375, 0, 2.53125,
  94. 1.4375, - 0.805, 2.53125,
  95. 0.805, - 1.4375, 2.53125,
  96. 0, - 1.4375, 2.53125,
  97. 1.5, 0, 2.4,
  98. 1.5, - 0.84, 2.4,
  99. 0.84, - 1.5, 2.4,
  100. 0, - 1.5, 2.4,
  101. - 0.784, - 1.4, 2.4,
  102. - 1.4, - 0.784, 2.4,
  103. - 1.4, 0, 2.4,
  104. - 0.749, - 1.3375, 2.53125,
  105. - 1.3375, - 0.749, 2.53125,
  106. - 1.3375, 0, 2.53125,
  107. - 0.805, - 1.4375, 2.53125,
  108. - 1.4375, - 0.805, 2.53125,
  109. - 1.4375, 0, 2.53125,
  110. - 0.84, - 1.5, 2.4,
  111. - 1.5, - 0.84, 2.4,
  112. - 1.5, 0, 2.4,
  113. - 1.4, 0.784, 2.4,
  114. - 0.784, 1.4, 2.4,
  115. 0, 1.4, 2.4,
  116. - 1.3375, 0.749, 2.53125,
  117. - 0.749, 1.3375, 2.53125,
  118. 0, 1.3375, 2.53125,
  119. - 1.4375, 0.805, 2.53125,
  120. - 0.805, 1.4375, 2.53125,
  121. 0, 1.4375, 2.53125,
  122. - 1.5, 0.84, 2.4,
  123. - 0.84, 1.5, 2.4,
  124. 0, 1.5, 2.4,
  125. 0.784, 1.4, 2.4,
  126. 1.4, 0.784, 2.4,
  127. 0.749, 1.3375, 2.53125,
  128. 1.3375, 0.749, 2.53125,
  129. 0.805, 1.4375, 2.53125,
  130. 1.4375, 0.805, 2.53125,
  131. 0.84, 1.5, 2.4,
  132. 1.5, 0.84, 2.4,
  133. 1.75, 0, 1.875,
  134. 1.75, - 0.98, 1.875,
  135. 0.98, - 1.75, 1.875,
  136. 0, - 1.75, 1.875,
  137. 2, 0, 1.35,
  138. 2, - 1.12, 1.35,
  139. 1.12, - 2, 1.35,
  140. 0, - 2, 1.35,
  141. 2, 0, 0.9,
  142. 2, - 1.12, 0.9,
  143. 1.12, - 2, 0.9,
  144. 0, - 2, 0.9,
  145. - 0.98, - 1.75, 1.875,
  146. - 1.75, - 0.98, 1.875,
  147. - 1.75, 0, 1.875,
  148. - 1.12, - 2, 1.35,
  149. - 2, - 1.12, 1.35,
  150. - 2, 0, 1.35,
  151. - 1.12, - 2, 0.9,
  152. - 2, - 1.12, 0.9,
  153. - 2, 0, 0.9,
  154. - 1.75, 0.98, 1.875,
  155. - 0.98, 1.75, 1.875,
  156. 0, 1.75, 1.875,
  157. - 2, 1.12, 1.35,
  158. - 1.12, 2, 1.35,
  159. 0, 2, 1.35,
  160. - 2, 1.12, 0.9,
  161. - 1.12, 2, 0.9,
  162. 0, 2, 0.9,
  163. 0.98, 1.75, 1.875,
  164. 1.75, 0.98, 1.875,
  165. 1.12, 2, 1.35,
  166. 2, 1.12, 1.35,
  167. 1.12, 2, 0.9,
  168. 2, 1.12, 0.9,
  169. 2, 0, 0.45,
  170. 2, - 1.12, 0.45,
  171. 1.12, - 2, 0.45,
  172. 0, - 2, 0.45,
  173. 1.5, 0, 0.225,
  174. 1.5, - 0.84, 0.225,
  175. 0.84, - 1.5, 0.225,
  176. 0, - 1.5, 0.225,
  177. 1.5, 0, 0.15,
  178. 1.5, - 0.84, 0.15,
  179. 0.84, - 1.5, 0.15,
  180. 0, - 1.5, 0.15,
  181. - 1.12, - 2, 0.45,
  182. - 2, - 1.12, 0.45,
  183. - 2, 0, 0.45,
  184. - 0.84, - 1.5, 0.225,
  185. - 1.5, - 0.84, 0.225,
  186. - 1.5, 0, 0.225,
  187. - 0.84, - 1.5, 0.15,
  188. - 1.5, - 0.84, 0.15,
  189. - 1.5, 0, 0.15,
  190. - 2, 1.12, 0.45,
  191. - 1.12, 2, 0.45,
  192. 0, 2, 0.45,
  193. - 1.5, 0.84, 0.225,
  194. - 0.84, 1.5, 0.225,
  195. 0, 1.5, 0.225,
  196. - 1.5, 0.84, 0.15,
  197. - 0.84, 1.5, 0.15,
  198. 0, 1.5, 0.15,
  199. 1.12, 2, 0.45,
  200. 2, 1.12, 0.45,
  201. 0.84, 1.5, 0.225,
  202. 1.5, 0.84, 0.225,
  203. 0.84, 1.5, 0.15,
  204. 1.5, 0.84, 0.15,
  205. - 1.6, 0, 2.025,
  206. - 1.6, - 0.3, 2.025,
  207. - 1.5, - 0.3, 2.25,
  208. - 1.5, 0, 2.25,
  209. - 2.3, 0, 2.025,
  210. - 2.3, - 0.3, 2.025,
  211. - 2.5, - 0.3, 2.25,
  212. - 2.5, 0, 2.25,
  213. - 2.7, 0, 2.025,
  214. - 2.7, - 0.3, 2.025,
  215. - 3, - 0.3, 2.25,
  216. - 3, 0, 2.25,
  217. - 2.7, 0, 1.8,
  218. - 2.7, - 0.3, 1.8,
  219. - 3, - 0.3, 1.8,
  220. - 3, 0, 1.8,
  221. - 1.5, 0.3, 2.25,
  222. - 1.6, 0.3, 2.025,
  223. - 2.5, 0.3, 2.25,
  224. - 2.3, 0.3, 2.025,
  225. - 3, 0.3, 2.25,
  226. - 2.7, 0.3, 2.025,
  227. - 3, 0.3, 1.8,
  228. - 2.7, 0.3, 1.8,
  229. - 2.7, 0, 1.575,
  230. - 2.7, - 0.3, 1.575,
  231. - 3, - 0.3, 1.35,
  232. - 3, 0, 1.35,
  233. - 2.5, 0, 1.125,
  234. - 2.5, - 0.3, 1.125,
  235. - 2.65, - 0.3, 0.9375,
  236. - 2.65, 0, 0.9375,
  237. - 2, - 0.3, 0.9,
  238. - 1.9, - 0.3, 0.6,
  239. - 1.9, 0, 0.6,
  240. - 3, 0.3, 1.35,
  241. - 2.7, 0.3, 1.575,
  242. - 2.65, 0.3, 0.9375,
  243. - 2.5, 0.3, 1.125,
  244. - 1.9, 0.3, 0.6,
  245. - 2, 0.3, 0.9,
  246. 1.7, 0, 1.425,
  247. 1.7, - 0.66, 1.425,
  248. 1.7, - 0.66, 0.6,
  249. 1.7, 0, 0.6,
  250. 2.6, 0, 1.425,
  251. 2.6, - 0.66, 1.425,
  252. 3.1, - 0.66, 0.825,
  253. 3.1, 0, 0.825,
  254. 2.3, 0, 2.1,
  255. 2.3, - 0.25, 2.1,
  256. 2.4, - 0.25, 2.025,
  257. 2.4, 0, 2.025,
  258. 2.7, 0, 2.4,
  259. 2.7, - 0.25, 2.4,
  260. 3.3, - 0.25, 2.4,
  261. 3.3, 0, 2.4,
  262. 1.7, 0.66, 0.6,
  263. 1.7, 0.66, 1.425,
  264. 3.1, 0.66, 0.825,
  265. 2.6, 0.66, 1.425,
  266. 2.4, 0.25, 2.025,
  267. 2.3, 0.25, 2.1,
  268. 3.3, 0.25, 2.4,
  269. 2.7, 0.25, 2.4,
  270. 2.8, 0, 2.475,
  271. 2.8, - 0.25, 2.475,
  272. 3.525, - 0.25, 2.49375,
  273. 3.525, 0, 2.49375,
  274. 2.9, 0, 2.475,
  275. 2.9, - 0.15, 2.475,
  276. 3.45, - 0.15, 2.5125,
  277. 3.45, 0, 2.5125,
  278. 2.8, 0, 2.4,
  279. 2.8, - 0.15, 2.4,
  280. 3.2, - 0.15, 2.4,
  281. 3.2, 0, 2.4,
  282. 3.525, 0.25, 2.49375,
  283. 2.8, 0.25, 2.475,
  284. 3.45, 0.15, 2.5125,
  285. 2.9, 0.15, 2.475,
  286. 3.2, 0.15, 2.4,
  287. 2.8, 0.15, 2.4,
  288. 0, 0, 3.15,
  289. 0.8, 0, 3.15,
  290. 0.8, - 0.45, 3.15,
  291. 0.45, - 0.8, 3.15,
  292. 0, - 0.8, 3.15,
  293. 0, 0, 2.85,
  294. 0.2, 0, 2.7,
  295. 0.2, - 0.112, 2.7,
  296. 0.112, - 0.2, 2.7,
  297. 0, - 0.2, 2.7,
  298. - 0.45, - 0.8, 3.15,
  299. - 0.8, - 0.45, 3.15,
  300. - 0.8, 0, 3.15,
  301. - 0.112, - 0.2, 2.7,
  302. - 0.2, - 0.112, 2.7,
  303. - 0.2, 0, 2.7,
  304. - 0.8, 0.45, 3.15,
  305. - 0.45, 0.8, 3.15,
  306. 0, 0.8, 3.15,
  307. - 0.2, 0.112, 2.7,
  308. - 0.112, 0.2, 2.7,
  309. 0, 0.2, 2.7,
  310. 0.45, 0.8, 3.15,
  311. 0.8, 0.45, 3.15,
  312. 0.112, 0.2, 2.7,
  313. 0.2, 0.112, 2.7,
  314. 0.4, 0, 2.55,
  315. 0.4, - 0.224, 2.55,
  316. 0.224, - 0.4, 2.55,
  317. 0, - 0.4, 2.55,
  318. 1.3, 0, 2.55,
  319. 1.3, - 0.728, 2.55,
  320. 0.728, - 1.3, 2.55,
  321. 0, - 1.3, 2.55,
  322. 1.3, 0, 2.4,
  323. 1.3, - 0.728, 2.4,
  324. 0.728, - 1.3, 2.4,
  325. 0, - 1.3, 2.4,
  326. - 0.224, - 0.4, 2.55,
  327. - 0.4, - 0.224, 2.55,
  328. - 0.4, 0, 2.55,
  329. - 0.728, - 1.3, 2.55,
  330. - 1.3, - 0.728, 2.55,
  331. - 1.3, 0, 2.55,
  332. - 0.728, - 1.3, 2.4,
  333. - 1.3, - 0.728, 2.4,
  334. - 1.3, 0, 2.4,
  335. - 0.4, 0.224, 2.55,
  336. - 0.224, 0.4, 2.55,
  337. 0, 0.4, 2.55,
  338. - 1.3, 0.728, 2.55,
  339. - 0.728, 1.3, 2.55,
  340. 0, 1.3, 2.55,
  341. - 1.3, 0.728, 2.4,
  342. - 0.728, 1.3, 2.4,
  343. 0, 1.3, 2.4,
  344. 0.224, 0.4, 2.55,
  345. 0.4, 0.224, 2.55,
  346. 0.728, 1.3, 2.55,
  347. 1.3, 0.728, 2.55,
  348. 0.728, 1.3, 2.4,
  349. 1.3, 0.728, 2.4,
  350. 0, 0, 0,
  351. 1.425, 0, 0,
  352. 1.425, 0.798, 0,
  353. 0.798, 1.425, 0,
  354. 0, 1.425, 0,
  355. 1.5, 0, 0.075,
  356. 1.5, 0.84, 0.075,
  357. 0.84, 1.5, 0.075,
  358. 0, 1.5, 0.075,
  359. - 0.798, 1.425, 0,
  360. - 1.425, 0.798, 0,
  361. - 1.425, 0, 0,
  362. - 0.84, 1.5, 0.075,
  363. - 1.5, 0.84, 0.075,
  364. - 1.5, 0, 0.075,
  365. - 1.425, - 0.798, 0,
  366. - 0.798, - 1.425, 0,
  367. 0, - 1.425, 0,
  368. - 1.5, - 0.84, 0.075,
  369. - 0.84, - 1.5, 0.075,
  370. 0, - 1.5, 0.075,
  371. 0.798, - 1.425, 0,
  372. 1.425, - 0.798, 0,
  373. 0.84, - 1.5, 0.075,
  374. 1.5, - 0.84, 0.075
  375. ];
  376. super();
  377. // number of segments per patch
  378. segments = Math.max( 2, Math.floor( segments ) );
  379. // Jim Blinn scaled the teapot down in size by about 1.3 for
  380. // some rendering tests. He liked the new proportions that he kept
  381. // the data in this form. The model was distributed with these new
  382. // proportions and became the norm. Trivia: comparing images of the
  383. // real teapot and the computer model, the ratio for the bowl of the
  384. // real teapot is more like 1.25, but since 1.3 is the traditional
  385. // value given, we use it here.
  386. const blinnScale = 1.3;
  387. // scale the size to be the real scaling factor
  388. const maxHeight = 3.15 * ( blinn ? 1 : blinnScale );
  389. const maxHeight2 = maxHeight / 2;
  390. const trueSize = size / maxHeight2;
  391. // Number of elements depends on what is needed. Subtract degenerate
  392. // triangles at tip of bottom and lid out in advance.
  393. let numTriangles = bottom ? ( 8 * segments - 4 ) * segments : 0;
  394. numTriangles += lid ? ( 16 * segments - 4 ) * segments : 0;
  395. numTriangles += body ? 40 * segments * segments : 0;
  396. const indices = new Uint32Array( numTriangles * 3 );
  397. let numVertices = bottom ? 4 : 0;
  398. numVertices += lid ? 8 : 0;
  399. numVertices += body ? 20 : 0;
  400. numVertices *= ( segments + 1 ) * ( segments + 1 );
  401. const vertices = new Float32Array( numVertices * 3 );
  402. const normals = new Float32Array( numVertices * 3 );
  403. const uvs = new Float32Array( numVertices * 2 );
  404. // Bezier form
  405. const ms = new Matrix4();
  406. ms.set(
  407. - 1.0, 3.0, - 3.0, 1.0,
  408. 3.0, - 6.0, 3.0, 0.0,
  409. - 3.0, 3.0, 0.0, 0.0,
  410. 1.0, 0.0, 0.0, 0.0 );
  411. const g = [];
  412. const sp = [];
  413. const tp = [];
  414. const dsp = [];
  415. const dtp = [];
  416. // M * G * M matrix, sort of see
  417. // http://www.cs.helsinki.fi/group/goa/mallinnus/curves/surfaces.html
  418. const mgm = [];
  419. const vert = [];
  420. const sdir = [];
  421. const tdir = [];
  422. const norm = new Vector3();
  423. let tcoord;
  424. let sval;
  425. let tval;
  426. let p;
  427. let dsval = 0;
  428. let dtval = 0;
  429. const normOut = new Vector3();
  430. const gmx = new Matrix4();
  431. const tmtx = new Matrix4();
  432. const vsp = new Vector4();
  433. const vtp = new Vector4();
  434. const vdsp = new Vector4();
  435. const vdtp = new Vector4();
  436. const vsdir = new Vector3();
  437. const vtdir = new Vector3();
  438. const mst = ms.clone();
  439. mst.transpose();
  440. // internal function: test if triangle has any matching vertices;
  441. // if so, don't save triangle, since it won't display anything.
  442. const notDegenerate = ( vtx1, vtx2, vtx3 ) => // if any vertex matches, return false
  443. ! ( ( ( vertices[ vtx1 * 3 ] === vertices[ vtx2 * 3 ] ) &&
  444. ( vertices[ vtx1 * 3 + 1 ] === vertices[ vtx2 * 3 + 1 ] ) &&
  445. ( vertices[ vtx1 * 3 + 2 ] === vertices[ vtx2 * 3 + 2 ] ) ) ||
  446. ( ( vertices[ vtx1 * 3 ] === vertices[ vtx3 * 3 ] ) &&
  447. ( vertices[ vtx1 * 3 + 1 ] === vertices[ vtx3 * 3 + 1 ] ) &&
  448. ( vertices[ vtx1 * 3 + 2 ] === vertices[ vtx3 * 3 + 2 ] ) ) || ( vertices[ vtx2 * 3 ] === vertices[ vtx3 * 3 ] ) &&
  449. ( vertices[ vtx2 * 3 + 1 ] === vertices[ vtx3 * 3 + 1 ] ) &&
  450. ( vertices[ vtx2 * 3 + 2 ] === vertices[ vtx3 * 3 + 2 ] ) );
  451. for ( let i = 0; i < 3; i ++ ) {
  452. mgm[ i ] = new Matrix4();
  453. }
  454. const minPatches = body ? 0 : 20;
  455. const maxPatches = bottom ? 32 : 28;
  456. const vertPerRow = segments + 1;
  457. let surfCount = 0;
  458. let vertCount = 0;
  459. let normCount = 0;
  460. let uvCount = 0;
  461. let indexCount = 0;
  462. for ( let surf = minPatches; surf < maxPatches; surf ++ ) {
  463. // lid is in the middle of the data, patches 20-27,
  464. // so ignore it for this part of the loop if the lid is not desired
  465. if ( lid || ( surf < 20 || surf >= 28 ) ) {
  466. // get M * G * M matrix for x,y,z
  467. for ( let i = 0; i < 3; i ++ ) {
  468. // get control patches
  469. for ( let r = 0; r < 4; r ++ ) {
  470. for ( let c = 0; c < 4; c ++ ) {
  471. // transposed
  472. g[ c * 4 + r ] = teapotVertices[ teapotPatches[ surf * 16 + r * 4 + c ] * 3 + i ];
  473. // is the lid to be made larger, and is this a point on the lid
  474. // that is X or Y?
  475. if ( fitLid && ( surf >= 20 && surf < 28 ) && ( i !== 2 ) ) {
  476. // increase XY size by 7.7%, found empirically. I don't
  477. // increase Z so that the teapot will continue to fit in the
  478. // space -1 to 1 for Y (Y is up for the final model).
  479. g[ c * 4 + r ] *= 1.077;
  480. }
  481. // Blinn "fixed" the teapot by dividing Z by blinnScale, and that's the
  482. // data we now use. The original teapot is taller. Fix it:
  483. if ( ! blinn && ( i === 2 ) ) {
  484. g[ c * 4 + r ] *= blinnScale;
  485. }
  486. }
  487. }
  488. gmx.set( g[ 0 ], g[ 1 ], g[ 2 ], g[ 3 ], g[ 4 ], g[ 5 ], g[ 6 ], g[ 7 ], g[ 8 ], g[ 9 ], g[ 10 ], g[ 11 ], g[ 12 ], g[ 13 ], g[ 14 ], g[ 15 ] );
  489. tmtx.multiplyMatrices( gmx, ms );
  490. mgm[ i ].multiplyMatrices( mst, tmtx );
  491. }
  492. // step along, get points, and output
  493. for ( let sstep = 0; sstep <= segments; sstep ++ ) {
  494. const s = sstep / segments;
  495. for ( let tstep = 0; tstep <= segments; tstep ++ ) {
  496. const t = tstep / segments;
  497. // point from basis
  498. // get power vectors and their derivatives
  499. for ( p = 4, sval = tval = 1.0; p --; ) {
  500. sp[ p ] = sval;
  501. tp[ p ] = tval;
  502. sval *= s;
  503. tval *= t;
  504. if ( p === 3 ) {
  505. dsp[ p ] = dtp[ p ] = 0.0;
  506. dsval = dtval = 1.0;
  507. } else {
  508. dsp[ p ] = dsval * ( 3 - p );
  509. dtp[ p ] = dtval * ( 3 - p );
  510. dsval *= s;
  511. dtval *= t;
  512. }
  513. }
  514. vsp.fromArray( sp );
  515. vtp.fromArray( tp );
  516. vdsp.fromArray( dsp );
  517. vdtp.fromArray( dtp );
  518. // do for x,y,z
  519. for ( let i = 0; i < 3; i ++ ) {
  520. // multiply power vectors times matrix to get value
  521. tcoord = vsp.clone();
  522. tcoord.applyMatrix4( mgm[ i ] );
  523. vert[ i ] = tcoord.dot( vtp );
  524. // get s and t tangent vectors
  525. tcoord = vdsp.clone();
  526. tcoord.applyMatrix4( mgm[ i ] );
  527. sdir[ i ] = tcoord.dot( vtp );
  528. tcoord = vsp.clone();
  529. tcoord.applyMatrix4( mgm[ i ] );
  530. tdir[ i ] = tcoord.dot( vdtp );
  531. }
  532. // find normal
  533. vsdir.fromArray( sdir );
  534. vtdir.fromArray( tdir );
  535. norm.crossVectors( vtdir, vsdir );
  536. norm.normalize();
  537. // if X and Z length is 0, at the cusp, so point the normal up or down, depending on patch number
  538. if ( vert[ 0 ] === 0 && vert[ 1 ] === 0 ) {
  539. // if above the middle of the teapot, normal points up, else down
  540. normOut.set( 0, vert[ 2 ] > maxHeight2 ? 1 : - 1, 0 );
  541. } else {
  542. // standard output: rotate on X axis
  543. normOut.set( norm.x, norm.z, - norm.y );
  544. }
  545. // store it all
  546. vertices[ vertCount ++ ] = trueSize * vert[ 0 ];
  547. vertices[ vertCount ++ ] = trueSize * ( vert[ 2 ] - maxHeight2 );
  548. vertices[ vertCount ++ ] = - trueSize * vert[ 1 ];
  549. normals[ normCount ++ ] = normOut.x;
  550. normals[ normCount ++ ] = normOut.y;
  551. normals[ normCount ++ ] = normOut.z;
  552. uvs[ uvCount ++ ] = 1 - t;
  553. uvs[ uvCount ++ ] = 1 - s;
  554. }
  555. }
  556. // save the faces
  557. for ( let sstep = 0; sstep < segments; sstep ++ ) {
  558. for ( let tstep = 0; tstep < segments; tstep ++ ) {
  559. const v1 = surfCount * vertPerRow * vertPerRow + sstep * vertPerRow + tstep;
  560. const v2 = v1 + 1;
  561. const v3 = v2 + vertPerRow;
  562. const v4 = v1 + vertPerRow;
  563. // Normals and UVs cannot be shared. Without clone(), you can see the consequences
  564. // of sharing if you call geometry.applyMatrix4( matrix ).
  565. if ( notDegenerate( v1, v2, v3 ) ) {
  566. indices[ indexCount ++ ] = v1;
  567. indices[ indexCount ++ ] = v2;
  568. indices[ indexCount ++ ] = v3;
  569. }
  570. if ( notDegenerate( v1, v3, v4 ) ) {
  571. indices[ indexCount ++ ] = v1;
  572. indices[ indexCount ++ ] = v3;
  573. indices[ indexCount ++ ] = v4;
  574. }
  575. }
  576. }
  577. // increment only if a surface was used
  578. surfCount ++;
  579. }
  580. }
  581. this.setIndex( new BufferAttribute( indices, 1 ) );
  582. this.setAttribute( 'position', new BufferAttribute( vertices, 3 ) );
  583. this.setAttribute( 'normal', new BufferAttribute( normals, 3 ) );
  584. this.setAttribute( 'uv', new BufferAttribute( uvs, 2 ) );
  585. this.computeBoundingSphere();
  586. }
  587. }
  588. export { TeapotGeometry };