svg_sandbox.html 6.8 KB

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