svg_sandbox.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js svg - sandbox</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. svg {
  10. display: block;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { SVGRenderer, SVGObject } from 'three/addons/renderers/SVGRenderer.js';
  30. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  31. let camera, scene, renderer, stats;
  32. let group;
  33. init();
  34. animate();
  35. function init() {
  36. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.z = 500;
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0xf0f0f0 );
  40. // QRCODE
  41. const loader = new THREE.BufferGeometryLoader();
  42. loader.load( 'models/json/QRCode_buffergeometry.json', function ( geometry ) {
  43. mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { vertexColors: true } ) );
  44. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  45. scene.add( mesh );
  46. } );
  47. // CUBES
  48. const boxGeometry = new THREE.BoxGeometry( 100, 100, 100 );
  49. let mesh = new THREE.Mesh( boxGeometry, new THREE.MeshBasicMaterial( { color: 0x0000ff, opacity: 0.5, transparent: true } ) );
  50. mesh.position.x = 500;
  51. mesh.rotation.x = Math.random();
  52. mesh.rotation.y = Math.random();
  53. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  54. scene.add( mesh );
  55. mesh = new THREE.Mesh( boxGeometry, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
  56. mesh.position.x = 500;
  57. mesh.position.y = 500;
  58. mesh.rotation.x = Math.random();
  59. mesh.rotation.y = Math.random();
  60. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  61. scene.add( mesh );
  62. // PLANE
  63. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.DoubleSide } ) );
  64. mesh.position.y = - 500;
  65. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  66. scene.add( mesh );
  67. // CYLINDER
  68. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 20, 100, 200, 10 ), new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
  69. mesh.position.x = - 500;
  70. mesh.rotation.x = - Math.PI / 2;
  71. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  72. scene.add( mesh );
  73. // POLYFIELD
  74. const geometry = new THREE.BufferGeometry();
  75. const material = new THREE.MeshBasicMaterial( { vertexColors: true, side: THREE.DoubleSide } );
  76. const v = new THREE.Vector3();
  77. const v0 = new THREE.Vector3();
  78. const v1 = new THREE.Vector3();
  79. const v2 = new THREE.Vector3();
  80. const color = new THREE.Color();
  81. const vertices = [];
  82. const colors = [];
  83. for ( let i = 0; i < 100; i ++ ) {
  84. v.set(
  85. Math.random() * 1000 - 500,
  86. Math.random() * 1000 - 500,
  87. Math.random() * 1000 - 500
  88. );
  89. v0.set(
  90. Math.random() * 100 - 50,
  91. Math.random() * 100 - 50,
  92. Math.random() * 100 - 50
  93. );
  94. v1.set(
  95. Math.random() * 100 - 50,
  96. Math.random() * 100 - 50,
  97. Math.random() * 100 - 50
  98. );
  99. v2.set(
  100. Math.random() * 100 - 50,
  101. Math.random() * 100 - 50,
  102. Math.random() * 100 - 50
  103. );
  104. v0.add( v );
  105. v1.add( v );
  106. v2.add( v );
  107. color.setHex( Math.random() * 0xffffff );
  108. // create a single triangle
  109. vertices.push( v0.x, v0.y, v0.z );
  110. vertices.push( v1.x, v1.y, v1.z );
  111. vertices.push( v2.x, v2.y, v2.z );
  112. colors.push( color.r, color.g, color.b );
  113. colors.push( color.r, color.g, color.b );
  114. colors.push( color.r, color.g, color.b );
  115. }
  116. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  117. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  118. group = new THREE.Mesh( geometry, material );
  119. group.scale.set( 2, 2, 2 );
  120. scene.add( group );
  121. // SPRITES
  122. for ( let i = 0; i < 50; i ++ ) {
  123. const material = new THREE.SpriteMaterial( { color: Math.random() * 0xffffff } );
  124. const sprite = new THREE.Sprite( material );
  125. sprite.position.x = Math.random() * 1000 - 500;
  126. sprite.position.y = Math.random() * 1000 - 500;
  127. sprite.position.z = Math.random() * 1000 - 500;
  128. sprite.scale.set( 64, 64, 1 );
  129. scene.add( sprite );
  130. }
  131. // CUSTOM
  132. const node = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  133. node.setAttribute( 'stroke', 'black' );
  134. node.setAttribute( 'fill', 'red' );
  135. node.setAttribute( 'r', '40' );
  136. for ( let i = 0; i < 50; i ++ ) {
  137. const object = new SVGObject( node.cloneNode() );
  138. object.position.x = Math.random() * 1000 - 500;
  139. object.position.y = Math.random() * 1000 - 500;
  140. object.position.z = Math.random() * 1000 - 500;
  141. scene.add( object );
  142. }
  143. // CUSTOM FROM FILE
  144. const fileLoader = new THREE.FileLoader();
  145. fileLoader.load( 'models/svg/hexagon.svg', function ( svg ) {
  146. const node = document.createElementNS( 'http://www.w3.org/2000/svg', 'g' );
  147. const parser = new DOMParser();
  148. const doc = parser.parseFromString( svg, 'image/svg+xml' );
  149. node.appendChild( doc.documentElement );
  150. const object = new SVGObject( node );
  151. object.position.x = 500;
  152. scene.add( object );
  153. } );
  154. // LIGHTS
  155. const ambient = new THREE.AmbientLight( 0x80ffff );
  156. scene.add( ambient );
  157. const directional = new THREE.DirectionalLight( 0xffff00 );
  158. directional.position.set( - 1, 0.5, 0 );
  159. scene.add( directional );
  160. renderer = new SVGRenderer();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. renderer.setQuality( 'low' );
  163. document.body.appendChild( renderer.domElement );
  164. stats = new Stats();
  165. document.body.appendChild( stats.dom );
  166. //
  167. window.addEventListener( 'resize', onWindowResize );
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. //
  175. function animate() {
  176. requestAnimationFrame( animate );
  177. render();
  178. stats.update();
  179. }
  180. function render() {
  181. const time = Date.now() * 0.0002;
  182. camera.position.x = Math.sin( time ) * 500;
  183. camera.position.z = Math.cos( time ) * 500;
  184. camera.lookAt( scene.position );
  185. group.rotation.x += 0.01;
  186. renderer.render( scene, camera );
  187. }
  188. </script>
  189. </body>
  190. </html>