123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js svg - sandbox</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- svg {
- display: block;
- }
- </style>
- </head>
- <body>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { SVGRenderer, SVGObject } from './jsm/renderers/SVGRenderer.js';
- var camera, scene, renderer, stats;
- var mesh, group;
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 500;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xf0f0f0 );
- // QRCODE
- var loader = new THREE.BufferGeometryLoader();
- loader.load( 'models/json/QRCode_buffergeometry.json', function ( geometry ) {
- mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { vertexColors: THREE.VertexColors } ) );
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
- scene.add( mesh );
- } );
- // CUBES
- var cube = new THREE.BoxBufferGeometry( 100, 100, 100 );
- mesh = new THREE.Mesh( cube, new THREE.MeshBasicMaterial( { color: 0x0000ff, opacity: 0.5, transparent: true } ) );
- mesh.position.x = 500;
- mesh.rotation.x = Math.random();
- mesh.rotation.y = Math.random();
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
- scene.add( mesh );
- mesh = new THREE.Mesh( cube, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
- mesh.position.x = 500;
- mesh.position.y = 500;
- mesh.rotation.x = Math.random();
- mesh.rotation.y = Math.random();
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
- scene.add( mesh );
- // PLANE
- mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 100, 100 ), new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, side: THREE.DoubleSide } ) );
- mesh.position.y = - 500;
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
- scene.add( mesh );
- // CYLINDER
- mesh = new THREE.Mesh( new THREE.CylinderBufferGeometry( 20, 100, 200, 10 ), new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
- mesh.position.x = - 500;
- mesh.rotation.x = - Math.PI / 2;
- mesh.scale.x = mesh.scale.y = mesh.scale.z = 2;
- scene.add( mesh );
- // POLYFIELD
- var geometry = new THREE.BufferGeometry();
- var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors, side: THREE.DoubleSide } );
- var v = new THREE.Vector3();
- var v0 = new THREE.Vector3();
- var v1 = new THREE.Vector3();
- var v2 = new THREE.Vector3();
- var color = new THREE.Color();
- var vertices = [];
- var colors = [];
- for ( var i = 0; i < 100; i ++ ) {
- v.set(
- Math.random() * 1000 - 500,
- Math.random() * 1000 - 500,
- Math.random() * 1000 - 500
- );
- v0.set(
- Math.random() * 100 - 50,
- Math.random() * 100 - 50,
- Math.random() * 100 - 50
- );
- v1.set(
- Math.random() * 100 - 50,
- Math.random() * 100 - 50,
- Math.random() * 100 - 50
- );
- v2.set(
- Math.random() * 100 - 50,
- Math.random() * 100 - 50,
- Math.random() * 100 - 50
- );
- v0.add( v );
- v1.add( v );
- v2.add( v );
- color.setHex( Math.random() * 0xffffff );
- // create a single triangle
- vertices.push( v0.x, v0.y, v0.z );
- vertices.push( v1.x, v1.y, v1.z );
- vertices.push( v2.x, v2.y, v2.z );
- colors.push( color.r, color.g, color.b );
- colors.push( color.r, color.g, color.b );
- colors.push( color.r, color.g, color.b );
- }
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- group = new THREE.Mesh( geometry, material );
- group.scale.set( 2, 2, 2 );
- scene.add( group );
- // SPRITES
- for ( var i = 0; i < 50; i ++ ) {
- var material = new THREE.SpriteMaterial( { color: Math.random() * 0xffffff } );
- var sprite = new THREE.Sprite( material );
- sprite.position.x = Math.random() * 1000 - 500;
- sprite.position.y = Math.random() * 1000 - 500;
- sprite.position.z = Math.random() * 1000 - 500;
- sprite.scale.set( 64, 64, 1 );
- scene.add( sprite );
- }
- // CUSTOM
- var node = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
- node.setAttribute( 'stroke', 'black' );
- node.setAttribute( 'fill', 'red' );
- node.setAttribute( 'r', '40' );
- for ( var i = 0; i < 50; i ++ ) {
- var object = new SVGObject( node.cloneNode() );
- object.position.x = Math.random() * 1000 - 500;
- object.position.y = Math.random() * 1000 - 500;
- object.position.z = Math.random() * 1000 - 500;
- scene.add( object );
- }
- // CUSTOM FROM FILE
- var fileLoader = new THREE.FileLoader();
- fileLoader.load( 'models/svg/hexagon.svg', function ( svg ) {
- var node = document.createElementNS( 'http://www.w3.org/2000/svg', 'g' );
- var parser = new DOMParser();
- var doc = parser.parseFromString( svg, 'image/svg+xml' );
- node.appendChild( doc.documentElement );
- var object = new SVGObject( node );
- object.position.x = 500;
- scene.add( object );
- } );
- // LIGHTS
- var ambient = new THREE.AmbientLight( 0x80ffff );
- scene.add( ambient );
- var directional = new THREE.DirectionalLight( 0xffff00 );
- directional.position.set( - 1, 0.5, 0 );
- scene.add( directional );
- renderer = new SVGRenderer();
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setQuality( 'low' );
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var time = Date.now() * 0.0002;
- camera.position.x = Math.sin( time ) * 500;
- camera.position.z = Math.cos( time ) * 500;
- camera.lookAt( scene.position );
- group.rotation.x += 0.01;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|