svg_sandbox.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. let camera, scene, renderer, stats;
  31. let group;
  32. init();
  33. animate();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
  36. camera.position.z = 500;
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xf0f0f0 );
  39. // QRCODE
  40. const loader = new THREE.BufferGeometryLoader();
  41. loader.load( 'models/json/QRCode_buffergeometry.json', function ( geometry ) {
  42. mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { vertexColors: true } ) );
  43. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  44. scene.add( mesh );
  45. } );
  46. // CUBES
  47. const boxGeometry = new THREE.BoxGeometry( 100, 100, 100 );
  48. let mesh = new THREE.Mesh( boxGeometry, new THREE.MeshBasicMaterial( { color: 0x0000ff, opacity: 0.5, transparent: true } ) );
  49. mesh.position.x = 500;
  50. mesh.rotation.x = Math.random();
  51. mesh.rotation.y = Math.random();
  52. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  53. scene.add( mesh );
  54. mesh = new THREE.Mesh( boxGeometry, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
  55. mesh.position.x = 500;
  56. mesh.position.y = 500;
  57. mesh.rotation.x = Math.random();
  58. mesh.rotation.y = Math.random();
  59. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  60. scene.add( mesh );
  61. // PLANE
  62. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.DoubleSide } ) );
  63. mesh.position.y = - 500;
  64. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  65. scene.add( mesh );
  66. // CYLINDER
  67. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 20, 100, 200, 10 ), new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
  68. mesh.position.x = - 500;
  69. mesh.rotation.x = - Math.PI / 2;
  70. mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
  71. scene.add( mesh );
  72. // POLYFIELD
  73. const geometry = new THREE.BufferGeometry();
  74. const material = new THREE.MeshBasicMaterial( { vertexColors: true, side: THREE.DoubleSide } );
  75. const v = new THREE.Vector3();
  76. const v0 = new THREE.Vector3();
  77. const v1 = new THREE.Vector3();
  78. const v2 = new THREE.Vector3();
  79. const color = new THREE.Color();
  80. const vertices = [];
  81. const colors = [];
  82. for ( let i = 0; i < 100; i ++ ) {
  83. v.set(
  84. Math.random() * 1000 - 500,
  85. Math.random() * 1000 - 500,
  86. Math.random() * 1000 - 500
  87. );
  88. v0.set(
  89. Math.random() * 100 - 50,
  90. Math.random() * 100 - 50,
  91. Math.random() * 100 - 50
  92. );
  93. v1.set(
  94. Math.random() * 100 - 50,
  95. Math.random() * 100 - 50,
  96. Math.random() * 100 - 50
  97. );
  98. v2.set(
  99. Math.random() * 100 - 50,
  100. Math.random() * 100 - 50,
  101. Math.random() * 100 - 50
  102. );
  103. v0.add( v );
  104. v1.add( v );
  105. v2.add( v );
  106. color.setHex( Math.random() * 0xffffff );
  107. // create a single triangle
  108. vertices.push( v0.x, v0.y, v0.z );
  109. vertices.push( v1.x, v1.y, v1.z );
  110. vertices.push( v2.x, v2.y, v2.z );
  111. colors.push( color.r, color.g, color.b );
  112. colors.push( color.r, color.g, color.b );
  113. colors.push( color.r, color.g, color.b );
  114. }
  115. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  116. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  117. group = new THREE.Mesh( geometry, material );
  118. group.scale.set( 2, 2, 2 );
  119. scene.add( group );
  120. // SPRITES
  121. for ( let i = 0; i < 50; i ++ ) {
  122. const material = new THREE.SpriteMaterial( { color: Math.random() * 0xffffff } );
  123. const sprite = new THREE.Sprite( material );
  124. sprite.position.x = Math.random() * 1000 - 500;
  125. sprite.position.y = Math.random() * 1000 - 500;
  126. sprite.position.z = Math.random() * 1000 - 500;
  127. sprite.scale.set( 64, 64, 1 );
  128. scene.add( sprite );
  129. }
  130. // CUSTOM
  131. const node = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
  132. node.setAttribute( 'stroke', 'black' );
  133. node.setAttribute( 'fill', 'red' );
  134. node.setAttribute( 'r', '40' );
  135. for ( let i = 0; i < 50; i ++ ) {
  136. const object = new SVGObject( node.cloneNode() );
  137. object.position.x = Math.random() * 1000 - 500;
  138. object.position.y = Math.random() * 1000 - 500;
  139. object.position.z = Math.random() * 1000 - 500;
  140. scene.add( object );
  141. }
  142. // CUSTOM FROM FILE
  143. const fileLoader = new THREE.FileLoader();
  144. fileLoader.load( 'models/svg/hexagon.svg', function ( svg ) {
  145. const node = document.createElementNS( 'http://www.w3.org/2000/svg', 'g' );
  146. const parser = new DOMParser();
  147. const doc = parser.parseFromString( svg, 'image/svg+xml' );
  148. node.appendChild( doc.documentElement );
  149. const object = new SVGObject( node );
  150. object.position.x = 500;
  151. scene.add( object );
  152. } );
  153. // LIGHTS
  154. const ambient = new THREE.AmbientLight( 0x80ffff );
  155. scene.add( ambient );
  156. const directional = new THREE.DirectionalLight( 0xffff00 );
  157. directional.position.set( - 1, 0.5, 0 );
  158. scene.add( directional );
  159. renderer = new SVGRenderer();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. renderer.setQuality( 'low' );
  162. document.body.appendChild( renderer.domElement );
  163. stats = new Stats();
  164. document.body.appendChild( stats.dom );
  165. //
  166. window.addEventListener( 'resize', onWindowResize );
  167. }
  168. function onWindowResize() {
  169. camera.aspect = window.innerWidth / window.innerHeight;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. }
  173. //
  174. function animate() {
  175. requestAnimationFrame( animate );
  176. render();
  177. stats.update();
  178. }
  179. function render() {
  180. const time = Date.now() * 0.0002;
  181. camera.position.x = Math.sin( time ) * 500;
  182. camera.position.z = Math.cos( time ) * 500;
  183. camera.lookAt( scene.position );
  184. group.rotation.x += 0.01;
  185. renderer.render( scene, camera );
  186. }
  187. </script>
  188. </body>
  189. </html>