threejs-primitives.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. 'use strict';
  2. /* global threejsLessonUtils */
  3. {
  4. const diagrams = {
  5. BoxBufferGeometry: {
  6. create() {
  7. const width = 8;
  8. const height = 8;
  9. const depth = 8;
  10. return new THREE.BoxBufferGeometry(width, height, depth);
  11. },
  12. },
  13. CircleBufferGeometry: {
  14. create() {
  15. const radius = 7;
  16. const segments = 24;
  17. return new THREE.CircleBufferGeometry(radius, segments);
  18. },
  19. },
  20. ConeBufferGeometry: {
  21. create() {
  22. const radius = 6;
  23. const height = 8;
  24. const segments = 16;
  25. return new THREE.ConeBufferGeometry(radius, height, segments);
  26. },
  27. },
  28. CylinderBufferGeometry: {
  29. create() {
  30. const radiusTop = 4;
  31. const radiusBottom = 4;
  32. const height = 8;
  33. const radialSegments = 12;
  34. return new THREE.CylinderBufferGeometry(radiusTop, radiusBottom, height, radialSegments);
  35. },
  36. },
  37. DodecahedronBufferGeometry: {
  38. create() {
  39. const radius = 7;
  40. return new THREE.DodecahedronBufferGeometry(radius);
  41. },
  42. },
  43. ExtrudeBufferGeometry: {
  44. create() {
  45. const shape = new THREE.Shape();
  46. const x = -2.5;
  47. const y = -5;
  48. shape.moveTo(x + 2.5, y + 2.5);
  49. shape.bezierCurveTo(x + 2.5, y + 2.5, x + 2, y, x, y);
  50. shape.bezierCurveTo(x - 3, y, x - 3, y + 3.5, x - 3, y + 3.5);
  51. shape.bezierCurveTo(x - 3, y + 5.5, x - 1.5, y + 7.7, x + 2.5, y + 9.5);
  52. shape.bezierCurveTo(x + 6, y + 7.7, x + 8, y + 4.5, x + 8, y + 3.5);
  53. shape.bezierCurveTo(x + 8, y + 3.5, x + 8, y, x + 5, y);
  54. shape.bezierCurveTo(x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5);
  55. const extrudeSettings = {
  56. steps: 2,
  57. depth: 2,
  58. bevelEnabled: true,
  59. bevelThickness: 1,
  60. bevelSize: 1,
  61. bevelSegments: 2,
  62. };
  63. return new THREE.ExtrudeBufferGeometry(shape, extrudeSettings);
  64. },
  65. },
  66. IcosahedronBufferGeometry: {
  67. create() {
  68. const radius = 7;
  69. return new THREE.IcosahedronBufferGeometry(radius);
  70. },
  71. },
  72. LatheBufferGeometry: {
  73. create() {
  74. const points = [];
  75. for (let i = 0; i < 10; ++i) {
  76. points.push(new THREE.Vector2(Math.sin(i * 0.2) * 3 + 3, (i - 5) * .8));
  77. }
  78. return new THREE.LatheBufferGeometry(points);
  79. },
  80. },
  81. OctahedronBufferGeometry: {
  82. create() {
  83. const radius = 7;
  84. return new THREE.OctahedronBufferGeometry(radius);
  85. },
  86. },
  87. ParametricBufferGeometry: {
  88. create() {
  89. /*
  90. from: https://github.com/mrdoob/three.js/blob/b8d8a8625465bd634aa68e5846354d69f34d2ff5/examples/js/ParametricGeometries.js
  91. The MIT License
  92. Copyright © 2010-2018 three.js authors
  93. Permission is hereby granted, free of charge, to any person obtaining a copy
  94. of this software and associated documentation files (the "Software"), to deal
  95. in the Software without restriction, including without limitation the rights
  96. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  97. copies of the Software, and to permit persons to whom the Software is
  98. furnished to do so, subject to the following conditions:
  99. The above copyright notice and this permission notice shall be included in
  100. all copies or substantial portions of the Software.
  101. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  102. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  103. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  104. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  105. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  106. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  107. THE SOFTWARE.
  108. */
  109. function klein(v, u, target) {
  110. u *= Math.PI;
  111. v *= 2 * Math.PI;
  112. u = u * 2;
  113. let x;
  114. let z;
  115. if (u < Math.PI) {
  116. x = 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(u) * Math.cos(v);
  117. z = -8 * Math.sin(u) - 2 * (1 - Math.cos(u) / 2) * Math.sin(u) * Math.cos(v);
  118. } else {
  119. x = 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(v + Math.PI);
  120. z = -8 * Math.sin(u);
  121. }
  122. const y = -2 * (1 - Math.cos(u) / 2) * Math.sin(v);
  123. target.set(x, y, z).multiplyScalar(0.75);
  124. }
  125. const slices = 25;
  126. const stacks = 25;
  127. return new THREE.ParametricBufferGeometry(klein, slices, stacks);
  128. },
  129. },
  130. PlaneBufferGeometry: {
  131. create() {
  132. const width = 9;
  133. const height = 9;
  134. const widthSegments = 2;
  135. const heightSegments = 2;
  136. return new THREE.PlaneBufferGeometry(width, height, widthSegments, heightSegments);
  137. },
  138. },
  139. PolyhedronBufferGeometry: {
  140. create() {
  141. const verticesOfCube = [
  142. -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1,
  143. -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1,
  144. ];
  145. const indicesOfFaces = [
  146. 2, 1, 0, 0, 3, 2,
  147. 0, 4, 7, 7, 3, 0,
  148. 0, 1, 5, 5, 4, 0,
  149. 1, 2, 6, 6, 5, 1,
  150. 2, 3, 7, 7, 6, 2,
  151. 4, 5, 6, 6, 7, 4,
  152. ];
  153. const radius = 7;
  154. const detail = 2;
  155. return new THREE.PolyhedronBufferGeometry(verticesOfCube, indicesOfFaces, radius, detail);
  156. },
  157. },
  158. RingBufferGeometry: {
  159. create() {
  160. const innerRadius = 2;
  161. const outerRadius = 7;
  162. const segments = 18;
  163. return new THREE.RingBufferGeometry(innerRadius, outerRadius, segments);
  164. },
  165. },
  166. ShapeBufferGeometry: {
  167. create() {
  168. const shape = new THREE.Shape();
  169. const x = -2.5;
  170. const y = -5;
  171. shape.moveTo(x + 2.5, y + 2.5);
  172. shape.bezierCurveTo(x + 2.5, y + 2.5, x + 2, y, x, y);
  173. shape.bezierCurveTo(x - 3, y, x - 3, y + 3.5, x - 3, y + 3.5);
  174. shape.bezierCurveTo(x - 3, y + 5.5, x - 1.5, y + 7.7, x + 2.5, y + 9.5);
  175. shape.bezierCurveTo(x + 6, y + 7.7, x + 8, y + 4.5, x + 8, y + 3.5);
  176. shape.bezierCurveTo(x + 8, y + 3.5, x + 8, y, x + 5, y);
  177. shape.bezierCurveTo(x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5);
  178. return new THREE.ShapeBufferGeometry(shape);
  179. },
  180. },
  181. SphereBufferGeometry: {
  182. create() {
  183. const radius = 7;
  184. const widthSegments = 12;
  185. const heightSegments = 8;
  186. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  187. },
  188. },
  189. TetrahedronBufferGeometry: {
  190. create() {
  191. const radius = 7;
  192. return new THREE.TetrahedronBufferGeometry(radius);
  193. },
  194. },
  195. TextBufferGeometry: {
  196. create() {
  197. return new Promise((resolve) => {
  198. const loader = new THREE.FontLoader();
  199. loader.load('../resources/threejs/fonts/helvetiker_regular.typeface.json', (font) => {
  200. resolve(new THREE.TextBufferGeometry('three.js', {
  201. font: font,
  202. size: 3.0,
  203. height: .2,
  204. curveSegments: 12,
  205. bevelEnabled: true,
  206. bevelThickness: 0.15,
  207. bevelSize: .3,
  208. bevelSegments: 5,
  209. }));
  210. });
  211. });
  212. },
  213. },
  214. TorusBufferGeometry: {
  215. create() {
  216. const radius = 5;
  217. const tubeRadius = 2;
  218. const radialSegments = 8;
  219. const tubularSegments = 24;
  220. return new THREE.TorusBufferGeometry(radius, tubeRadius, radialSegments, tubularSegments);
  221. },
  222. },
  223. TorusKnotBufferGeometry: {
  224. create() {
  225. const radius = 3.5;
  226. const tube = 1.5;
  227. const radialSegments = 8;
  228. const tubularSegments = 64;
  229. const p = 2;
  230. const q = 3;
  231. return new THREE.TorusKnotBufferGeometry(radius, tube, tubularSegments, radialSegments, p, q);
  232. },
  233. },
  234. TubeBufferGeometry: {
  235. create() {
  236. class CustomSinCurve extends THREE.Curve {
  237. constructor(scale) {
  238. super();
  239. this.scale = scale;
  240. }
  241. getPoint(t) {
  242. const tx = t * 3 - 1.5;
  243. const ty = Math.sin(2 * Math.PI * t);
  244. const tz = 0;
  245. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  246. }
  247. }
  248. const path = new CustomSinCurve(4);
  249. const tubularSegments = 20;
  250. const radius = 1;
  251. const radialSegments = 8;
  252. const closed = false;
  253. return new THREE.TubeBufferGeometry(path, tubularSegments, radius, radialSegments, closed);
  254. },
  255. },
  256. EdgesGeometry: {
  257. create() {
  258. const width = 8;
  259. const height = 8;
  260. const depth = 8;
  261. return {
  262. lineGeometry: new THREE.EdgesGeometry(new THREE.BoxBufferGeometry(width, height, depth)),
  263. };
  264. },
  265. nonBuffer: false,
  266. },
  267. WireframeGeometry: {
  268. create() {
  269. const width = 8;
  270. const height = 8;
  271. const depth = 8;
  272. return {
  273. lineGeometry: new THREE.WireframeGeometry(new THREE.BoxBufferGeometry(width, height, depth)),
  274. };
  275. },
  276. nonBuffer: false,
  277. },
  278. SphereBufferGeometryLow: {
  279. create() {
  280. const radius = 7;
  281. const widthSegments = 5;
  282. const heightSegments = 3;
  283. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  284. },
  285. },
  286. SphereBufferGeometryMedium: {
  287. create() {
  288. const radius = 7;
  289. const widthSegments = 24;
  290. const heightSegments = 10;
  291. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  292. },
  293. },
  294. SphereBufferGeometryHigh: {
  295. create() {
  296. const radius = 7;
  297. const widthSegments = 50;
  298. const heightSegments = 50;
  299. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  300. },
  301. },
  302. SphereBufferGeometryLowSmooth: {
  303. create() {
  304. const radius = 7;
  305. const widthSegments = 5;
  306. const heightSegments = 3;
  307. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  308. },
  309. showLines: false,
  310. flatShading: false,
  311. },
  312. SphereBufferGeometryMediumSmooth: {
  313. create() {
  314. const radius = 7;
  315. const widthSegments = 24;
  316. const heightSegments = 10;
  317. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  318. },
  319. showLines: false,
  320. flatShading: false,
  321. },
  322. SphereBufferGeometryHighSmooth: {
  323. create() {
  324. const radius = 7;
  325. const widthSegments = 50;
  326. const heightSegments = 50;
  327. return new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
  328. },
  329. showLines: false,
  330. flatShading: false,
  331. },
  332. PlaneBufferGeometryLow: {
  333. create() {
  334. const width = 9;
  335. const height = 9;
  336. const widthSegments = 1;
  337. const heightSegments = 1;
  338. return new THREE.PlaneBufferGeometry(width, height, widthSegments, heightSegments);
  339. },
  340. },
  341. PlaneBufferGeometryHigh: {
  342. create() {
  343. const width = 9;
  344. const height = 9;
  345. const widthSegments = 10;
  346. const heightSegments = 10;
  347. return new THREE.PlaneBufferGeometry(width, height, widthSegments, heightSegments);
  348. },
  349. },
  350. };
  351. function addLink(parent, name) {
  352. const a = document.createElement('a');
  353. a.href = `https://threejs.org/docs/#api/geometries/${name}`;
  354. const code = document.createElement('code');
  355. code.textContent = name;
  356. a.appendChild(code);
  357. parent.appendChild(a);
  358. return a;
  359. }
  360. function addElem(parent, type, className, text) {
  361. const elem = document.createElement(type);
  362. elem.className = className;
  363. if (text) {
  364. elem.textContent = text;
  365. }
  366. parent.appendChild(elem);
  367. return elem;
  368. }
  369. function addDiv(parent, className) {
  370. return addElem(parent, 'div', className);
  371. }
  372. [...document.querySelectorAll('[data-diagram]')].forEach(createDiagram);
  373. [...document.querySelectorAll('[data-primitive]')].forEach(createPrimitiveDOM);
  374. function createPrimitiveDOM(base) {
  375. const name = base.dataset.primitive;
  376. const info = diagrams[name];
  377. if (!info) {
  378. throw new Error(`no primitive ${name}`);
  379. }
  380. const text = base.innerHTML;
  381. base.innerHTML = '';
  382. const elem = addDiv(base, 'shape');
  383. const right = addDiv(base, 'desc');
  384. addLink(right, name);
  385. if (info.nonBuffer !== false) {
  386. addElem(right, 'span', '', ', ');
  387. addLink(right, name.replace('Buffer', ''));
  388. }
  389. addDiv(right, '.note').innerHTML = text;
  390. return createLiveImage(elem, info);
  391. }
  392. function createDiagram(base) {
  393. const name = base.dataset.diagram;
  394. const info = diagrams[name];
  395. if (!info) {
  396. throw new Error(`no primitive ${name}`);
  397. }
  398. return createLiveImage(base, info);
  399. }
  400. function createLiveImage(elem, info) {
  401. const geometry = info.create();
  402. const promise = (geometry instanceof Promise) ? geometry : Promise.resolve(geometry);
  403. promise.then((geometryInfo) => {
  404. if (geometryInfo instanceof THREE.BufferGeometry ||
  405. geometryInfo instanceof THREE.Geometry) {
  406. const geometry = geometryInfo;
  407. geometryInfo = {
  408. geometry,
  409. };
  410. }
  411. const root = new THREE.Object3D();
  412. const boxGeometry = geometryInfo.geometry || geometryInfo.lineGeometry;
  413. boxGeometry.computeBoundingBox();
  414. const centerOffset = new THREE.Vector3();
  415. boxGeometry.boundingBox.getCenter(centerOffset).multiplyScalar(-1);
  416. if (geometryInfo.geometry) {
  417. const material = new THREE.MeshPhongMaterial({
  418. flatShading: info.flatShading === false ? false : true,
  419. side: THREE.DoubleSide,
  420. });
  421. material.color.setHSL(Math.random(), .5, .5);
  422. const mesh = new THREE.Mesh(geometryInfo.geometry, material);
  423. mesh.position.copy(centerOffset);
  424. root.add(mesh);
  425. }
  426. if (info.showLines !== false) {
  427. const lineMesh = new THREE.LineSegments(
  428. geometryInfo.lineGeometry || geometryInfo.geometry,
  429. new THREE.LineBasicMaterial({
  430. color: geometryInfo.geometry ? 0xffffff : 0x000000,
  431. transparent: true,
  432. opacity: 0.5,
  433. }));
  434. lineMesh.position.copy(centerOffset);
  435. root.add(lineMesh);
  436. }
  437. threejsLessonUtils.addDiagram(elem, {create: () => root});
  438. });
  439. }
  440. }