webgl_multiple_canvases_complex.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. <style>
  8. body {
  9. color: #808080;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. #canvas1, #canvas2, #canvas3 {
  23. position: relative;
  24. display: block;
  25. border: 1px solid red;
  26. }
  27. #canvas1 {
  28. width: 300px;
  29. height: 200px;
  30. }
  31. #canvas2 {
  32. width: 400px;
  33. height: 100px;
  34. left: 150px;
  35. }
  36. #canvas3 {
  37. width: 200px;
  38. height: 300px;
  39. left: 75px;
  40. }
  41. a {
  42. color: #0080ff;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id="container">
  48. <canvas id="canvas1"></canvas>
  49. <canvas id="canvas2"></canvas>
  50. <canvas id="canvas3"></canvas>
  51. </div>
  52. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple canvases - complex</div>
  53. <script src="../build/three.js"></script>
  54. <script src="js/Detector.js"></script>
  55. <script>
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var views = [];
  58. var scene, renderer;
  59. var mouseX = 0, mouseY = 0;
  60. var windowHalfX = window.innerWidth / 2;
  61. var 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. var context = canvas.getContext( '2d' );
  69. var 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, 0, viewWidth, viewHeight );
  77. renderer.render( scene, camera );
  78. context.drawImage( renderer.domElement, 0, 0 );
  79. };
  80. }
  81. //
  82. function init() {
  83. var canvas1 = document.getElementById( 'canvas1' );
  84. var canvas2 = document.getElementById( 'canvas2' );
  85. var canvas3 = document.getElementById( 'canvas3' );
  86. var fullWidth = 550;
  87. var 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. var light = new THREE.DirectionalLight( 0xffffff );
  95. light.position.set( 0, 0, 1 ).normalize();
  96. scene.add( light );
  97. // shadow
  98. var canvas = document.createElement( 'canvas' );
  99. canvas.width = 128;
  100. canvas.height = 128;
  101. var context = canvas.getContext( '2d' );
  102. var 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. var shadowTexture = new THREE.CanvasTexture( canvas );
  108. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  109. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  110. var shadowMesh;
  111. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  112. shadowMesh.position.y = - 250;
  113. shadowMesh.rotation.x = - Math.PI / 2;
  114. scene.add( shadowMesh );
  115. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  116. shadowMesh.position.x = - 400;
  117. shadowMesh.position.y = - 250;
  118. shadowMesh.rotation.x = - Math.PI / 2;
  119. scene.add( shadowMesh );
  120. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  121. shadowMesh.position.x = 400;
  122. shadowMesh.position.y = - 250;
  123. shadowMesh.rotation.x = - Math.PI / 2;
  124. scene.add( shadowMesh );
  125. var faceIndices = [ 'a', 'b', 'c' ];
  126. var radius = 200,
  127. geometry1 = new THREE.IcosahedronGeometry( radius, 1 ),
  128. geometry2 = new THREE.IcosahedronGeometry( radius, 1 ),
  129. geometry3 = new THREE.IcosahedronGeometry( radius, 1 );
  130. for ( var i = 0; i < geometry1.faces.length; i ++ ) {
  131. var f1 = geometry1.faces[ i ];
  132. var f2 = geometry2.faces[ i ];
  133. var f3 = geometry3.faces[ i ];
  134. for ( var j = 0; j < 3; j ++ ) {
  135. var vertexIndex = f1[ faceIndices[ j ] ];
  136. var p = geometry1.vertices[ vertexIndex ];
  137. var color = new THREE.Color( 0xffffff );
  138. color.setHSL( ( p.y / radius + 1 ) / 2, 1.0, 0.5 );
  139. f1.vertexColors[ j ] = color;
  140. var color = new THREE.Color( 0xffffff );
  141. color.setHSL( 0.0, ( p.y / radius + 1 ) / 2, 0.5 );
  142. f2.vertexColors[ j ] = color;
  143. var color = new THREE.Color( 0xffffff );
  144. color.setHSL( 0.125 * vertexIndex / geometry1.vertices.length, 1.0, 0.5 );
  145. f3.vertexColors[ j ] = color;
  146. }
  147. }
  148. var mesh, wireframe;
  149. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors, shininess: 0 } );
  150. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  151. mesh = new THREE.Mesh( geometry1, material );
  152. wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  153. mesh.add( wireframe );
  154. mesh.position.x = - 400;
  155. mesh.rotation.x = - 1.87;
  156. scene.add( mesh );
  157. mesh = new THREE.Mesh( geometry2, material );
  158. wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  159. mesh.add( wireframe );
  160. mesh.position.x = 400;
  161. scene.add( mesh );
  162. mesh = new THREE.Mesh( geometry3, material );
  163. wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  164. mesh.add( wireframe );
  165. scene.add( mesh );
  166. renderer = new THREE.WebGLRenderer( { antialias: true } );
  167. renderer.setPixelRatio( window.devicePixelRatio );
  168. renderer.setSize( fullWidth, fullHeight );
  169. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  170. }
  171. function onDocumentMouseMove( event ) {
  172. mouseX = event.clientX - windowHalfX;
  173. mouseY = event.clientY - windowHalfY;
  174. }
  175. function animate() {
  176. for ( var i = 0; i < views.length; ++i ) {
  177. views[ i ].render();
  178. }
  179. requestAnimationFrame( animate );
  180. }
  181. </script>
  182. </body>
  183. </html>