svg_sandbox.html 7.0 KB

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