webgl_multiple_canvases_complex.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple canvases - complex</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: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. #canvas1, #canvas2, #canvas3 {
  17. position: relative;
  18. display: block;
  19. outline: 1px solid red;
  20. }
  21. #canvas1 {
  22. width: 300px;
  23. height: 200px;
  24. }
  25. #canvas2 {
  26. width: 400px;
  27. height: 100px;
  28. left: 150px;
  29. }
  30. #canvas3 {
  31. width: 200px;
  32. height: 300px;
  33. left: 75px;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="container">
  39. <canvas id="canvas1"></canvas>
  40. <canvas id="canvas2"></canvas>
  41. <canvas id="canvas3"></canvas>
  42. </div>
  43. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple canvases - complex</div>
  44. <!-- Import maps polyfill -->
  45. <!-- Remove this when import maps will be widely supported -->
  46. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  47. <script type="importmap">
  48. {
  49. "imports": {
  50. "three": "../build/three.module.js",
  51. "three/addons/": "./jsm/"
  52. }
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from 'three';
  57. const views = [];
  58. let scene, renderer;
  59. let mouseX = 0, mouseY = 0;
  60. const windowHalfX = window.innerWidth / 2;
  61. const windowHalfY = window.innerHeight / 2;
  62. init();
  63. animate();
  64. //
  65. function View( canvas, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  66. canvas.width = viewWidth * window.devicePixelRatio;
  67. canvas.height = viewHeight * window.devicePixelRatio;
  68. const context = canvas.getContext( '2d' );
  69. const camera = new THREE.PerspectiveCamera( 20, viewWidth / viewHeight, 1, 10000 );
  70. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  71. camera.position.z = 1800;
  72. this.render = function () {
  73. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  74. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  75. camera.lookAt( scene.position );
  76. renderer.setViewport( 0, fullHeight - viewHeight, viewWidth, viewHeight );
  77. renderer.render( scene, camera );
  78. context.drawImage( renderer.domElement, 0, 0 );
  79. };
  80. }
  81. //
  82. function init() {
  83. const canvas1 = document.getElementById( 'canvas1' );
  84. const canvas2 = document.getElementById( 'canvas2' );
  85. const canvas3 = document.getElementById( 'canvas3' );
  86. const fullWidth = 550;
  87. const fullHeight = 600;
  88. views.push( new View( canvas1, fullWidth, fullHeight, 0, 0, canvas1.clientWidth, canvas1.clientHeight ) );
  89. views.push( new View( canvas2, fullWidth, fullHeight, 150, 200, canvas2.clientWidth, canvas2.clientHeight ) );
  90. views.push( new View( canvas3, fullWidth, fullHeight, 75, 300, canvas3.clientWidth, canvas3.clientHeight ) );
  91. //
  92. scene = new THREE.Scene();
  93. scene.background = new THREE.Color( 0xffffff );
  94. const light = new THREE.DirectionalLight( 0xffffff );
  95. light.position.set( 0, 0, 1 ).normalize();
  96. scene.add( light );
  97. // shadow
  98. const canvas = document.createElement( 'canvas' );
  99. canvas.width = 128;
  100. canvas.height = 128;
  101. const context = canvas.getContext( '2d' );
  102. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  103. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  104. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  105. context.fillStyle = gradient;
  106. context.fillRect( 0, 0, canvas.width, canvas.height );
  107. const shadowTexture = new THREE.CanvasTexture( canvas );
  108. shadowTexture.colorSpace = THREE.SRGBColorSpace;
  109. const shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  110. const shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  111. let shadowMesh;
  112. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  113. shadowMesh.position.y = - 250;
  114. shadowMesh.rotation.x = - Math.PI / 2;
  115. scene.add( shadowMesh );
  116. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  117. shadowMesh.position.x = - 400;
  118. shadowMesh.position.y = - 250;
  119. shadowMesh.rotation.x = - Math.PI / 2;
  120. scene.add( shadowMesh );
  121. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  122. shadowMesh.position.x = 400;
  123. shadowMesh.position.y = - 250;
  124. shadowMesh.rotation.x = - Math.PI / 2;
  125. scene.add( shadowMesh );
  126. const radius = 200;
  127. const geometry1 = new THREE.IcosahedronGeometry( radius, 1 );
  128. const count = geometry1.attributes.position.count;
  129. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  130. const geometry2 = geometry1.clone();
  131. const geometry3 = geometry1.clone();
  132. const color = new THREE.Color();
  133. const positions1 = geometry1.attributes.position;
  134. const positions2 = geometry2.attributes.position;
  135. const positions3 = geometry3.attributes.position;
  136. const colors1 = geometry1.attributes.color;
  137. const colors2 = geometry2.attributes.color;
  138. const colors3 = geometry3.attributes.color;
  139. for ( let i = 0; i < count; i ++ ) {
  140. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5, THREE.SRGBColorSpace );
  141. colors1.setXYZ( i, color.r, color.g, color.b );
  142. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5, THREE.SRGBColorSpace );
  143. colors2.setXYZ( i, color.r, color.g, color.b );
  144. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0, THREE.SRGBColorSpace );
  145. colors3.setXYZ( i, color.r, color.g, color.b );
  146. }
  147. const material = new THREE.MeshPhongMaterial( {
  148. color: 0xffffff,
  149. flatShading: true,
  150. vertexColors: true,
  151. shininess: 0
  152. } );
  153. const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  154. let mesh = new THREE.Mesh( geometry1, material );
  155. let wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  156. mesh.add( wireframe );
  157. mesh.position.x = - 400;
  158. mesh.rotation.x = - 1.87;
  159. scene.add( mesh );
  160. mesh = new THREE.Mesh( geometry2, material );
  161. wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  162. mesh.add( wireframe );
  163. mesh.position.x = 400;
  164. scene.add( mesh );
  165. mesh = new THREE.Mesh( geometry3, material );
  166. wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  167. mesh.add( wireframe );
  168. scene.add( mesh );
  169. renderer = new THREE.WebGLRenderer( { antialias: true } );
  170. renderer.setPixelRatio( window.devicePixelRatio );
  171. renderer.setSize( fullWidth, fullHeight );
  172. document.addEventListener( 'mousemove', onDocumentMouseMove );
  173. }
  174. function onDocumentMouseMove( event ) {
  175. mouseX = event.clientX - windowHalfX;
  176. mouseY = event.clientY - windowHalfY;
  177. }
  178. function animate() {
  179. for ( let i = 0; i < views.length; ++ i ) {
  180. views[ i ].render();
  181. }
  182. requestAnimationFrame( animate );
  183. }
  184. </script>
  185. </body>
  186. </html>