svg_sandbox.html 6.5 KB

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