1
0

primitives.html 11 KB

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