TeapotBufferGeometry.js 19 KB

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