webgl_multiple_canvases_circle.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple canvases - circle</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: #555;
  11. }
  12. #container {
  13. width: 100%;
  14. height: 700px;
  15. -webkit-perspective: 800px;
  16. -webkit-perspective-origin: 50% 225px;
  17. -moz-perspective: 800px;
  18. -moz-perspective-origin: 50% 225px;
  19. perspective: 800px;
  20. perspective-origin: 50% 225px;
  21. }
  22. #stage {
  23. width: 100%;
  24. height: 100%;
  25. -webkit-transform-style: preserve-3d;
  26. -moz-transform-style: preserve-3d;
  27. transform-style: preserve-3d;
  28. }
  29. #shape {
  30. position: relative;
  31. top: 160px;
  32. margin: 0 auto;
  33. height: 200px;
  34. width: 200px;
  35. -webkit-transform: translateZ(-0px);
  36. -webkit-transform-style: preserve-3d;
  37. -moz-transform: translateZ(-0px);
  38. -moz-transform-style: preserve-3d;
  39. transform: translateZ(-0px);
  40. transform-style: preserve-3d;
  41. }
  42. .ring {
  43. position: absolute;
  44. display: block;
  45. height: 300px;
  46. width: 200px;
  47. text-align: center;
  48. font-family: Times, serif;
  49. font-size: 124pt;
  50. color: black;
  51. background-color: #fff;
  52. }
  53. #shape {
  54. border: 0px;
  55. background-color: rgba(255, 255, 255, 0);
  56. }
  57. .ring > .r1 {
  58. -webkit-transform: rotateY(300deg) translateZ(-380px);
  59. -moz-transform: rotateY(300deg) translateZ(-380px);
  60. transform: rotateY(300deg) translateZ(-380px);
  61. }
  62. .ring > .r2 {
  63. -webkit-transform: rotateY(330deg) translateZ(-380px);
  64. -moz-transform: rotateY(330deg) translateZ(-380px);
  65. transform: rotateY(330deg) translateZ(-380px);
  66. }
  67. .ring > .r3 {
  68. -webkit-transform: rotateY(0deg) translateZ(-380px);
  69. -moz-transform: rotateY(0deg) translateZ(-380px);
  70. transform: rotateY(0deg) translateZ(-380px);
  71. }
  72. .ring > .r4 {
  73. -webkit-transform: rotateY(30deg) translateZ(-380px);
  74. -moz-transform: rotateY(30deg) translateZ(-380px);
  75. transform: rotateY(30deg) translateZ(-380px);
  76. }
  77. .ring > .r5 {
  78. -webkit-transform: rotateY(60deg) translateZ(-380px);
  79. -moz-transform: rotateY(60deg) translateZ(-380px);
  80. transform: rotateY(60deg) translateZ(-380px);
  81. }
  82. #info {
  83. position: absolute;
  84. top: 0px; width: 100%;
  85. padding: 5px;
  86. }
  87. #help {
  88. position: absolute;
  89. top: 50px; width: 100%;
  90. text-align: center;
  91. }
  92. #help>div {
  93. margin: auto;
  94. padding: 1em;
  95. background-color: rgba(0,0,0,0.3);
  96. width: 50%;
  97. }
  98. </style>
  99. </head>
  100. <body>
  101. <div id="container">
  102. <div id="stage">
  103. <div id="shape" class="ring backfaces">
  104. <canvas id="canvas1" class="ring r1"></canvas>
  105. <canvas id="canvas2" class="ring r2"></canvas>
  106. <canvas id="canvas3" class="ring r3"></canvas>
  107. <canvas id="canvas4" class="ring r4"></canvas>
  108. <canvas id="canvas5" class="ring r5"></canvas>
  109. </div>
  110. </div>
  111. </div>
  112. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple canvases - circle</div>
  113. <div id="help" gstyle="display: none">
  114. <div>
  115. <a href="https://www.google.com/earth/explore/showcase/liquidgalaxy.html">Google's Liquid Galaxy</a> simulator.<br/>
  116. Here 5 monitors are simulated using 3d css. WebGL is then rendered onto each one.
  117. </div>
  118. </div>
  119. <script type="module">
  120. import * as THREE from '../build/three.module.js';
  121. var views = [];
  122. var scene, renderer;
  123. var mouseX = 0, mouseY = 0;
  124. var windowHalfX = window.innerWidth / 2;
  125. var windowHalfY = window.innerHeight / 2;
  126. init();
  127. animate();
  128. //
  129. function View( canvas, rotateY ) {
  130. canvas.width = canvas.clientWidth * window.devicePixelRatio;
  131. canvas.height = canvas.clientHeight * window.devicePixelRatio;
  132. var context = canvas.getContext( '2d' );
  133. var camera = new THREE.PerspectiveCamera( 20, canvas.clientWidth / canvas.clientHeight, 1, 20000 );
  134. camera.rotation.y = rotateY;
  135. // Think of the virtual camera as a post with 5 cameras on it (even though those cameras happen to live in difference scenes)
  136. // You need to move the post (ie, the virtualCamera) to move all 5 cameras together.
  137. var virtualCamera = new THREE.Camera();
  138. virtualCamera.add( camera );
  139. this.render = function () {
  140. virtualCamera.position.x = - mouseX * 4;
  141. virtualCamera.position.y = - mouseY * 4;
  142. virtualCamera.position.z = 1800;
  143. virtualCamera.lookAt( scene.position );
  144. virtualCamera.updateMatrixWorld( true );
  145. renderer.render( scene, camera );
  146. context.drawImage( renderer.domElement, 0, 0 );
  147. };
  148. }
  149. function init() {
  150. var canvas1 = document.getElementById( 'canvas1' );
  151. var canvas2 = document.getElementById( 'canvas2' );
  152. var canvas3 = document.getElementById( 'canvas3' );
  153. var canvas4 = document.getElementById( 'canvas4' );
  154. var canvas5 = document.getElementById( 'canvas5' );
  155. var fudge = 0.45; // I don't know why this is needed :-(
  156. var rot = 30 * THREE.MathUtils.DEG2RAD;
  157. views.push( new View( canvas1, rot * - 2 * fudge ) );
  158. views.push( new View( canvas2, rot * - 1 * fudge ) );
  159. views.push( new View( canvas3, rot * 0 * fudge ) );
  160. views.push( new View( canvas4, rot * 1 * fudge ) );
  161. views.push( new View( canvas5, rot * 2 * fudge ) );
  162. //
  163. scene = new THREE.Scene();
  164. scene.background = new THREE.Color( 0xffffff );
  165. var light = new THREE.DirectionalLight( 0xffffff );
  166. light.position.set( 0, 0, 1 ).normalize();
  167. scene.add( light );
  168. var noof_balls = 51;
  169. // shadow
  170. var canvas = document.createElement( 'canvas' );
  171. canvas.width = 128;
  172. canvas.height = 128;
  173. var context = canvas.getContext( '2d' );
  174. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  175. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  176. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  177. context.fillStyle = gradient;
  178. context.fillRect( 0, 0, canvas.width, canvas.height );
  179. var shadowTexture = new THREE.CanvasTexture( canvas );
  180. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  181. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  182. for ( var i = 0; i < noof_balls; i ++ ) { // create shadows
  183. var shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  184. shadowMesh.position.x = - ( noof_balls - 1 ) / 2 * 400 + i * 400;
  185. shadowMesh.position.y = - 250;
  186. shadowMesh.rotation.x = - Math.PI / 2;
  187. scene.add( shadowMesh );
  188. }
  189. var radius = 200;
  190. var geometry1 = new THREE.IcosahedronBufferGeometry( radius, 1 );
  191. var count = geometry1.attributes.position.count;
  192. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  193. var color = new THREE.Color();
  194. var positions = geometry1.attributes.position;
  195. var colors = geometry1.attributes.color;
  196. for ( var i = 0; i < count; i ++ ) {
  197. color.setHSL( ( positions.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  198. colors.setXYZ( i, color.r, color.g, color.b );
  199. }
  200. var material = new THREE.MeshPhongMaterial( {
  201. color: 0xffffff,
  202. flatShading: true,
  203. vertexColors: THREE.VertexColors,
  204. shininess: 0
  205. } );
  206. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  207. for ( var i = 0; i < noof_balls; i ++ ) { // create balls
  208. var mesh = new THREE.Mesh( geometry1, material );
  209. var wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  210. mesh.add( wireframe );
  211. mesh.position.x = - ( noof_balls - 1 ) / 2 * 400 + i * 400;
  212. mesh.rotation.x = i * 0.5;
  213. scene.add( mesh );
  214. }
  215. renderer = new THREE.WebGLRenderer( { antialias: true } );
  216. renderer.setPixelRatio( window.devicePixelRatio );
  217. renderer.setSize( 200, 300 );
  218. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  219. }
  220. function onDocumentMouseMove( event ) {
  221. mouseX = event.clientX - windowHalfX;
  222. mouseY = event.clientY - windowHalfY;
  223. }
  224. function animate() {
  225. for ( var i = 0; i < views.length; ++ i ) {
  226. views[ i ].render();
  227. }
  228. requestAnimationFrame( animate );
  229. }
  230. </script>
  231. </body>
  232. </html>