1
0

svg_sandbox.html 6.7 KB

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