css3d_orthographic.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>three.js css3d - orthographic</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. body {
  10. background-color: #f0f0f0;
  11. }
  12. a {
  13. color: #f00;
  14. }
  15. #info {
  16. color: #000000;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - orthographic</div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  25. import { CSS3DRenderer, CSS3DObject } from './jsm/renderers/CSS3DRenderer.js';
  26. var camera, scene, renderer;
  27. var scene2, renderer2;
  28. init();
  29. animate();
  30. function init() {
  31. var frustumSize = 500;
  32. var aspect = window.innerWidth / window.innerHeight;
  33. camera = new THREE.OrthographicCamera( frustumSize * aspect / - 2, frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 1, 1000 );
  34. camera.position.set( - 200, 200, 200 );
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0xf0f0f0 );
  37. scene2 = new THREE.Scene();
  38. var material = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, wireframeLinewidth: 1, side: THREE.DoubleSide } );
  39. // left
  40. createPlane(
  41. 100, 100,
  42. 'chocolate',
  43. new THREE.Vector3( - 50, 0, 0 ),
  44. new THREE.Euler( 0, - 90 * THREE.MathUtils.DEG2RAD, 0 )
  45. );
  46. // right
  47. createPlane(
  48. 100, 100,
  49. 'saddlebrown',
  50. new THREE.Vector3( 0, 0, 50 ),
  51. new THREE.Euler( 0, 0, 0 )
  52. );
  53. // top
  54. createPlane(
  55. 100, 100,
  56. 'yellowgreen',
  57. new THREE.Vector3( 0, 50, 0 ),
  58. new THREE.Euler( - 90 * THREE.MathUtils.DEG2RAD, 0, 0 )
  59. );
  60. // bottom
  61. createPlane(
  62. 300, 300,
  63. 'seagreen',
  64. new THREE.Vector3( 0, - 50, 0 ),
  65. new THREE.Euler( - 90 * THREE.MathUtils.DEG2RAD, 0, 0 )
  66. );
  67. //
  68. renderer = new THREE.WebGLRenderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. document.body.appendChild( renderer.domElement );
  72. renderer2 = new CSS3DRenderer();
  73. renderer2.setSize( window.innerWidth, window.innerHeight );
  74. renderer2.domElement.style.position = 'absolute';
  75. renderer2.domElement.style.top = 0;
  76. document.body.appendChild( renderer2.domElement );
  77. var controls = new OrbitControls( camera, renderer2.domElement );
  78. function createPlane( width, height, cssColor, pos, rot ) {
  79. var element = document.createElement( 'div' );
  80. element.style.width = width + 'px';
  81. element.style.height = height + 'px';
  82. element.style.opacity = 0.75;
  83. element.style.background = cssColor;
  84. var object = new CSS3DObject( element );
  85. object.position.copy( pos );
  86. object.rotation.copy( rot );
  87. scene2.add( object );
  88. var geometry = new THREE.PlaneBufferGeometry( width, height );
  89. var mesh = new THREE.Mesh( geometry, material );
  90. mesh.position.copy( object.position );
  91. mesh.rotation.copy( object.rotation );
  92. scene.add( mesh );
  93. }
  94. }
  95. function animate() {
  96. requestAnimationFrame( animate );
  97. renderer.render( scene, camera );
  98. renderer2.render( scene2, camera );
  99. }
  100. </script>
  101. </body>
  102. </html>