webgl_multiple_canvases_complex.html 6.8 KB

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