css3d_sandbox.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. <style>
  8. body {
  9. color: #000;
  10. }
  11. a {
  12. color: #080;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - sandbox</div>
  18. <script type="module">
  19. import * as THREE from '../build/three.module.js';
  20. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  21. import { CSS3DRenderer, CSS3DObject } from './jsm/renderers/CSS3DRenderer.js';
  22. var camera, scene, renderer;
  23. var scene2, renderer2;
  24. var controls;
  25. init();
  26. animate();
  27. function init() {
  28. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  29. camera.position.set( 200, 200, 200 );
  30. scene = new THREE.Scene();
  31. scene.background = new THREE.Color( 0xf0f0f0 );
  32. scene2 = new THREE.Scene();
  33. var material = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, wireframeLinewidth: 1, side: THREE.DoubleSide } );
  34. //
  35. for ( var i = 0; i < 10; i ++ ) {
  36. var element = document.createElement( 'div' );
  37. element.style.width = '100px';
  38. element.style.height = '100px';
  39. element.style.opacity = ( i < 5 ) ? 0.5 : 1;
  40. element.style.background = new THREE.Color( Math.random() * 0xffffff ).getStyle();
  41. var object = new CSS3DObject( element );
  42. object.position.x = Math.random() * 200 - 100;
  43. object.position.y = Math.random() * 200 - 100;
  44. object.position.z = Math.random() * 200 - 100;
  45. object.rotation.x = Math.random();
  46. object.rotation.y = Math.random();
  47. object.rotation.z = Math.random();
  48. object.scale.x = Math.random() + 0.5;
  49. object.scale.y = Math.random() + 0.5;
  50. scene2.add( object );
  51. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  52. var mesh = new THREE.Mesh( geometry, material );
  53. mesh.position.copy( object.position );
  54. mesh.rotation.copy( object.rotation );
  55. mesh.scale.copy( object.scale );
  56. scene.add( mesh );
  57. }
  58. //
  59. renderer = new THREE.WebGLRenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. document.body.appendChild( renderer.domElement );
  63. renderer2 = new CSS3DRenderer();
  64. renderer2.setSize( window.innerWidth, window.innerHeight );
  65. renderer2.domElement.style.position = 'absolute';
  66. renderer2.domElement.style.top = 0;
  67. document.body.appendChild( renderer2.domElement );
  68. controls = new TrackballControls( camera, renderer2.domElement );
  69. }
  70. function animate() {
  71. requestAnimationFrame( animate );
  72. controls.update();
  73. renderer.render( scene, camera );
  74. renderer2.render( scene2, camera );
  75. }
  76. </script>
  77. </body>
  78. </html>