TeapotGeometry.js 19 KB

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