webgl_multiple_canvases_complex.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. border: 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. <script type="module">
  45. import * as THREE from '../build/three.module.js';
  46. var views = [];
  47. var scene, renderer;
  48. var mouseX = 0, mouseY = 0;
  49. var windowHalfX = window.innerWidth / 2;
  50. var windowHalfY = window.innerHeight / 2;
  51. init();
  52. animate();
  53. //
  54. function View( canvas, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  55. canvas.width = viewWidth * window.devicePixelRatio;
  56. canvas.height = viewHeight * window.devicePixelRatio;
  57. var context = canvas.getContext( '2d' );
  58. var camera = new THREE.PerspectiveCamera( 20, viewWidth / viewHeight, 1, 10000 );
  59. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  60. camera.position.z = 1800;
  61. this.render = function () {
  62. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  63. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  64. camera.lookAt( scene.position );
  65. renderer.setViewport( 0, fullHeight - viewHeight, viewWidth, viewHeight );
  66. renderer.render( scene, camera );
  67. context.drawImage( renderer.domElement, 0, 0 );
  68. };
  69. }
  70. //
  71. function init() {
  72. var canvas1 = document.getElementById( 'canvas1' );
  73. var canvas2 = document.getElementById( 'canvas2' );
  74. var canvas3 = document.getElementById( 'canvas3' );
  75. var fullWidth = 550;
  76. var fullHeight = 600;
  77. views.push( new View( canvas1, fullWidth, fullHeight, 0, 0, canvas1.clientWidth, canvas1.clientHeight ) );
  78. views.push( new View( canvas2, fullWidth, fullHeight, 150, 200, canvas2.clientWidth, canvas2.clientHeight ) );
  79. views.push( new View( canvas3, fullWidth, fullHeight, 75, 300, canvas3.clientWidth, canvas3.clientHeight ) );
  80. //
  81. scene = new THREE.Scene();
  82. scene.background = new THREE.Color( 0xffffff );
  83. var light = new THREE.DirectionalLight( 0xffffff );
  84. light.position.set( 0, 0, 1 ).normalize();
  85. scene.add( light );
  86. // shadow
  87. var canvas = document.createElement( 'canvas' );
  88. canvas.width = 128;
  89. canvas.height = 128;
  90. var context = canvas.getContext( '2d' );
  91. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  92. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  93. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  94. context.fillStyle = gradient;
  95. context.fillRect( 0, 0, canvas.width, canvas.height );
  96. var shadowTexture = new THREE.CanvasTexture( canvas );
  97. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  98. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  99. var shadowMesh;
  100. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  101. shadowMesh.position.y = - 250;
  102. shadowMesh.rotation.x = - Math.PI / 2;
  103. scene.add( shadowMesh );
  104. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  105. shadowMesh.position.x = - 400;
  106. shadowMesh.position.y = - 250;
  107. shadowMesh.rotation.x = - Math.PI / 2;
  108. scene.add( shadowMesh );
  109. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  110. shadowMesh.position.x = 400;
  111. shadowMesh.position.y = - 250;
  112. shadowMesh.rotation.x = - Math.PI / 2;
  113. scene.add( shadowMesh );
  114. var radius = 200;
  115. var geometry1 = new THREE.IcosahedronBufferGeometry( radius, 1 );
  116. var count = geometry1.attributes.position.count;
  117. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  118. var geometry2 = geometry1.clone();
  119. var geometry3 = geometry1.clone();
  120. var color = new THREE.Color();
  121. var positions1 = geometry1.attributes.position;
  122. var positions2 = geometry2.attributes.position;
  123. var positions3 = geometry3.attributes.position;
  124. var colors1 = geometry1.attributes.color;
  125. var colors2 = geometry2.attributes.color;
  126. var colors3 = geometry3.attributes.color;
  127. for ( var i = 0; i < count; i ++ ) {
  128. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  129. colors1.setXYZ( i, color.r, color.g, color.b );
  130. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  131. colors2.setXYZ( i, color.r, color.g, color.b );
  132. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  133. colors3.setXYZ( i, color.r, color.g, color.b );
  134. }
  135. var material = new THREE.MeshPhongMaterial( {
  136. color: 0xffffff,
  137. flatShading: true,
  138. vertexColors: THREE.VertexColors,
  139. shininess: 0
  140. } );
  141. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  142. var mesh = new THREE.Mesh( geometry1, material );
  143. var wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  144. mesh.add( wireframe );
  145. mesh.position.x = - 400;
  146. mesh.rotation.x = - 1.87;
  147. scene.add( mesh );
  148. var mesh = new THREE.Mesh( geometry2, material );
  149. var wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  150. mesh.add( wireframe );
  151. mesh.position.x = 400;
  152. scene.add( mesh );
  153. var mesh = new THREE.Mesh( geometry3, material );
  154. var wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  155. mesh.add( wireframe );
  156. scene.add( mesh );
  157. renderer = new THREE.WebGLRenderer( { antialias: true } );
  158. renderer.setPixelRatio( window.devicePixelRatio );
  159. renderer.setSize( fullWidth, fullHeight );
  160. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  161. }
  162. function onDocumentMouseMove( event ) {
  163. mouseX = event.clientX - windowHalfX;
  164. mouseY = event.clientY - windowHalfY;
  165. }
  166. function animate() {
  167. for ( var i = 0; i < views.length; ++ i ) {
  168. views[ i ].render();
  169. }
  170. requestAnimationFrame( animate );
  171. }
  172. </script>
  173. </body>
  174. </html>