threejs-primitives.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Primitives</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script src="resources/threejs/r102/three.min.js"></script>
  23. <script>
  24. 'use strict';
  25. /* global THREE */
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  29. const fov = 40;
  30. const aspect = 2; // the canvas default
  31. const near = 0.1;
  32. const far = 1000;
  33. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  34. camera.position.z = 120;
  35. const scene = new THREE.Scene();
  36. scene.background = new THREE.Color(0xAAAAAA);
  37. {
  38. const color = 0xFFFFFF;
  39. const intensity = 1;
  40. const light = new THREE.DirectionalLight(color, intensity);
  41. light.position.set(-1, 2, 4);
  42. scene.add(light);
  43. }
  44. {
  45. const color = 0xFFFFFF;
  46. const intensity = 1;
  47. const light = new THREE.DirectionalLight(color, intensity);
  48. light.position.set(1, -2, -4);
  49. scene.add(light);
  50. }
  51. const objects = [];
  52. const spread = 15;
  53. function addObject(x, y, obj) {
  54. obj.position.x = x * spread;
  55. obj.position.y = y * spread;
  56. scene.add(obj);
  57. objects.push(obj);
  58. }
  59. function createMaterial() {
  60. const material = new THREE.MeshPhongMaterial({
  61. side: THREE.DoubleSide,
  62. });
  63. const hue = Math.random();
  64. const saturation = 1;
  65. const luminance = .5;
  66. material.color.setHSL(hue, saturation, luminance);
  67. return material;
  68. }
  69. function addSolidGeometry(x, y, geometry) {
  70. const mesh = new THREE.Mesh(geometry, createMaterial());
  71. addObject(x, y, mesh);
  72. }
  73. function addLineGeometry(x, y, geometry) {
  74. const material = new THREE.LineBasicMaterial({color: 0x000000});
  75. const mesh = new THREE.LineSegments(geometry, material);
  76. addObject(x, y, mesh);
  77. }
  78. {
  79. const width = 8;
  80. const height = 8;
  81. const depth = 8;
  82. addSolidGeometry(-2, 2, new THREE.BoxBufferGeometry(width, height, depth));
  83. }
  84. {
  85. const radius = 7;
  86. const segments = 24;
  87. addSolidGeometry(-1, 2, new THREE.CircleBufferGeometry(radius, segments));
  88. }
  89. {
  90. const radius = 6;
  91. const height = 8;
  92. const segments = 16;
  93. addSolidGeometry(0, 2, new THREE.ConeBufferGeometry(radius, height, segments));
  94. }
  95. {
  96. const radiusTop = 4;
  97. const radiusBottom = 4;
  98. const height = 8;
  99. const radialSegments = 12;
  100. addSolidGeometry(1, 2, new THREE.CylinderBufferGeometry(radiusTop, radiusBottom, height, radialSegments));
  101. }
  102. {
  103. const radius = 7;
  104. addSolidGeometry(2, 2, new THREE.DodecahedronBufferGeometry(radius));
  105. }
  106. {
  107. const shape = new THREE.Shape();
  108. const x = -2.5;
  109. const y = -5;
  110. shape.moveTo(x + 2.5, y + 2.5);
  111. shape.bezierCurveTo(x + 2.5, y + 2.5, x + 2, y, x, y);
  112. shape.bezierCurveTo(x - 3, y, x - 3, y + 3.5, x - 3, y + 3.5);
  113. shape.bezierCurveTo(x - 3, y + 5.5, x - 1.5, y + 7.7, x + 2.5, y + 9.5);
  114. shape.bezierCurveTo(x + 6, y + 7.7, x + 8, y + 4.5, x + 8, y + 3.5);
  115. shape.bezierCurveTo(x + 8, y + 3.5, x + 8, y, x + 5, y);
  116. shape.bezierCurveTo(x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5);
  117. const extrudeSettings = {
  118. steps: 2,
  119. depth: 2,
  120. bevelEnabled: true,
  121. bevelThickness: 1,
  122. bevelSize: 1,
  123. bevelSegments: 2,
  124. };
  125. addSolidGeometry(-2, 1, new THREE.ExtrudeBufferGeometry(shape, extrudeSettings));
  126. }
  127. {
  128. const radius = 7;
  129. addSolidGeometry(-1, 1, new THREE.IcosahedronBufferGeometry(radius));
  130. }
  131. {
  132. const points = [];
  133. for (let i = 0; i < 10; ++i) {
  134. points.push(new THREE.Vector2(Math.sin(i * 0.2) * 3 + 3, (i - 5) * .8));
  135. }
  136. addSolidGeometry(0, 1, new THREE.LatheBufferGeometry(points));
  137. }
  138. {
  139. const radius = 7;
  140. addSolidGeometry(1, 1, new THREE.OctahedronBufferGeometry(radius));
  141. }
  142. {
  143. /*
  144. from: https://github.com/mrdoob/three.js/blob/b8d8a8625465bd634aa68e5846354d69f34d2ff5/examples/js/ParametricGeometries.js
  145. The MIT License
  146. Copyright © 2010-2018 three.js authors
  147. Permission is hereby granted, free of charge, to any person obtaining a copy
  148. of this software and associated documentation files (the "Software"), to deal
  149. in the Software without restriction, including without limitation the rights
  150. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  151. copies of the Software, and to permit persons to whom the Software is
  152. furnished to do so, subject to the following conditions:
  153. The above copyright notice and this permission notice shall be included in
  154. all copies or substantial portions of the Software.
  155. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  156. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  157. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  158. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  159. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  160. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  161. THE SOFTWARE.
  162. */
  163. function klein(v, u, target) {
  164. u *= Math.PI;
  165. v *= 2 * Math.PI;
  166. u = u * 2;
  167. let x;
  168. let z;
  169. if (u < Math.PI) {
  170. x = 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(u) * Math.cos(v);
  171. z = -8 * Math.sin(u) - 2 * (1 - Math.cos(u) / 2) * Math.sin(u) * Math.cos(v);
  172. } else {
  173. x = 3 * Math.cos(u) * (1 + Math.sin(u)) + (2 * (1 - Math.cos(u) / 2)) * Math.cos(v + Math.PI);
  174. z = -8 * Math.sin(u);
  175. }
  176. const y = -2 * (1 - Math.cos(u) / 2) * Math.sin(v);
  177. target.set(x, y, z).multiplyScalar(0.75);
  178. }
  179. const slices = 25;
  180. const stacks = 25;
  181. addSolidGeometry(2, 1, new THREE.ParametricBufferGeometry(klein, slices, stacks));
  182. }
  183. {
  184. const width = 9;
  185. const height = 9;
  186. const widthSegments = 2;
  187. const heightSegments = 2;
  188. addSolidGeometry(-2, 0, new THREE.PlaneBufferGeometry(width, height, widthSegments, heightSegments));
  189. }
  190. {
  191. const verticesOfCube = [
  192. -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1,
  193. -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1,
  194. ];
  195. const indicesOfFaces = [
  196. 2, 1, 0, 0, 3, 2,
  197. 0, 4, 7, 7, 3, 0,
  198. 0, 1, 5, 5, 4, 0,
  199. 1, 2, 6, 6, 5, 1,
  200. 2, 3, 7, 7, 6, 2,
  201. 4, 5, 6, 6, 7, 4,
  202. ];
  203. const radius = 7;
  204. const detail = 2;
  205. addSolidGeometry(-1, 0, new THREE.PolyhedronBufferGeometry(verticesOfCube, indicesOfFaces, radius, detail));
  206. }
  207. {
  208. const innerRadius = 2;
  209. const outerRadius = 7;
  210. const segments = 18;
  211. addSolidGeometry(0, 0, new THREE.RingBufferGeometry(innerRadius, outerRadius, segments));
  212. }
  213. {
  214. const shape = new THREE.Shape();
  215. const x = -2.5;
  216. const y = -5;
  217. shape.moveTo(x + 2.5, y + 2.5);
  218. shape.bezierCurveTo(x + 2.5, y + 2.5, x + 2, y, x, y);
  219. shape.bezierCurveTo(x - 3, y, x - 3, y + 3.5, x - 3, y + 3.5);
  220. shape.bezierCurveTo(x - 3, y + 5.5, x - 1.5, y + 7.7, x + 2.5, y + 9.5);
  221. shape.bezierCurveTo(x + 6, y + 7.7, x + 8, y + 4.5, x + 8, y + 3.5);
  222. shape.bezierCurveTo(x + 8, y + 3.5, x + 8, y, x + 5, y);
  223. shape.bezierCurveTo(x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5);
  224. addSolidGeometry(1, 0, new THREE.ShapeBufferGeometry(shape));
  225. }
  226. {
  227. const radius = 7;
  228. const widthSegments = 12;
  229. const heightSegments = 8;
  230. addSolidGeometry(2, 0, new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments));
  231. }
  232. {
  233. const radius = 7;
  234. addSolidGeometry(-2, -1, new THREE.TetrahedronBufferGeometry(radius));
  235. }
  236. {
  237. const loader = new THREE.FontLoader();
  238. loader.load('resources/threejs/fonts/helvetiker_regular.typeface.json', (font) => {
  239. const geometry = new THREE.TextBufferGeometry('three.js', {
  240. font: font,
  241. size: 3.0,
  242. height: .2,
  243. curveSegments: 12,
  244. bevelEnabled: true,
  245. bevelThickness: 0.15,
  246. bevelSize: .3,
  247. bevelSegments: 5,
  248. });
  249. const mesh = new THREE.Mesh(geometry, createMaterial());
  250. geometry.computeBoundingBox();
  251. geometry.boundingBox.getCenter(mesh.position).multiplyScalar(-1);
  252. const parent = new THREE.Object3D();
  253. parent.add(mesh);
  254. addObject(-1, -1, parent);
  255. });
  256. }
  257. {
  258. const radius = 5;
  259. const tubeRadius = 2;
  260. const radialSegments = 8;
  261. const tubularSegments = 24;
  262. addSolidGeometry(0, -1, new THREE.TorusBufferGeometry(radius, tubeRadius, radialSegments, tubularSegments));
  263. }
  264. {
  265. const radius = 3.5;
  266. const tube = 1.5;
  267. const radialSegments = 8;
  268. const tubularSegments = 64;
  269. const p = 2;
  270. const q = 3;
  271. addSolidGeometry(1, -1, new THREE.TorusKnotBufferGeometry(radius, tube, tubularSegments, radialSegments, p, q));
  272. }
  273. {
  274. class CustomSinCurve extends THREE.Curve {
  275. constructor(scale) {
  276. super();
  277. this.scale = scale;
  278. }
  279. getPoint(t) {
  280. const tx = t * 3 - 1.5;
  281. const ty = Math.sin(2 * Math.PI * t);
  282. const tz = 0;
  283. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  284. }
  285. }
  286. const path = new CustomSinCurve(4);
  287. const tubularSegments = 20;
  288. const radius = 1;
  289. const radialSegments = 8;
  290. const closed = false;
  291. addSolidGeometry(2, -1, new THREE.TubeBufferGeometry(path, tubularSegments, radius, radialSegments, closed));
  292. }
  293. {
  294. const width = 8;
  295. const height = 8;
  296. const depth = 8;
  297. addLineGeometry(-1, -2, new THREE.EdgesGeometry(new THREE.BoxBufferGeometry(width, height, depth)));
  298. }
  299. {
  300. const width = 8;
  301. const height = 8;
  302. const depth = 8;
  303. addLineGeometry(1, -2, new THREE.WireframeGeometry(new THREE.BoxBufferGeometry(width, height, depth)));
  304. }
  305. function resizeRendererToDisplaySize(renderer) {
  306. const canvas = renderer.domElement;
  307. const width = canvas.clientWidth;
  308. const height = canvas.clientHeight;
  309. const needResize = canvas.width !== width || canvas.height !== height;
  310. if (needResize) {
  311. renderer.setSize(width, height, false);
  312. }
  313. return needResize;
  314. }
  315. function render(time) {
  316. time *= 0.001;
  317. if (resizeRendererToDisplaySize(renderer)) {
  318. const canvas = renderer.domElement;
  319. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  320. camera.updateProjectionMatrix();
  321. }
  322. objects.forEach((obj, ndx) => {
  323. const speed = .1 + ndx * .05;
  324. const rot = time * speed;
  325. obj.rotation.x = rot;
  326. obj.rotation.y = rot;
  327. });
  328. renderer.render(scene, camera);
  329. requestAnimationFrame(render);
  330. }
  331. requestAnimationFrame(render);
  332. }
  333. main();
  334. </script>
  335. </html>