webgl_multiple_canvases_circle.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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="http://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 {
  121. BufferAttribute,
  122. Camera,
  123. CanvasTexture,
  124. Color,
  125. DirectionalLight,
  126. IcosahedronBufferGeometry,
  127. Math as _Math,
  128. Mesh,
  129. MeshBasicMaterial,
  130. MeshPhongMaterial,
  131. PerspectiveCamera,
  132. PlaneBufferGeometry,
  133. Scene,
  134. VertexColors,
  135. WebGLRenderer,
  136. } from "../build/three.module.js";
  137. var views = [];
  138. var scene, renderer;
  139. var mouseX = 0, mouseY = 0;
  140. var windowHalfX = window.innerWidth / 2;
  141. var windowHalfY = window.innerHeight / 2;
  142. init();
  143. animate();
  144. //
  145. function View( canvas, rotateY ) {
  146. canvas.width = canvas.clientWidth * window.devicePixelRatio;
  147. canvas.height = canvas.clientHeight * window.devicePixelRatio;
  148. var context = canvas.getContext( '2d' );
  149. var camera = new PerspectiveCamera( 20, canvas.clientWidth / canvas.clientHeight, 1, 20000 );
  150. camera.rotation.y = rotateY;
  151. // Think of the virtual camera as a post with 5 cameras on it (even though those cameras happen to live in difference scenes)
  152. // You need to move the post (ie, the virtualCamera) to move all 5 cameras together.
  153. var virtualCamera = new Camera();
  154. virtualCamera.add( camera );
  155. this.render = function () {
  156. virtualCamera.position.x = - mouseX * 4;
  157. virtualCamera.position.y = - mouseY * 4;
  158. virtualCamera.position.z = 1800;
  159. virtualCamera.lookAt( scene.position );
  160. virtualCamera.updateMatrixWorld( true );
  161. renderer.render( scene, camera );
  162. context.drawImage( renderer.domElement, 0, 0 );
  163. };
  164. }
  165. function init() {
  166. var canvas1 = document.getElementById( 'canvas1' );
  167. var canvas2 = document.getElementById( 'canvas2' );
  168. var canvas3 = document.getElementById( 'canvas3' );
  169. var canvas4 = document.getElementById( 'canvas4' );
  170. var canvas5 = document.getElementById( 'canvas5' );
  171. var fudge = 0.45; // I don't know why this is needed :-(
  172. var rot = 30 * _Math.DEG2RAD;
  173. views.push( new View( canvas1, rot * - 2 * fudge ) );
  174. views.push( new View( canvas2, rot * - 1 * fudge ) );
  175. views.push( new View( canvas3, rot * 0 * fudge ) );
  176. views.push( new View( canvas4, rot * 1 * fudge ) );
  177. views.push( new View( canvas5, rot * 2 * fudge ) );
  178. //
  179. scene = new Scene();
  180. scene.background = new Color( 0xffffff );
  181. var light = new DirectionalLight( 0xffffff );
  182. light.position.set( 0, 0, 1 ).normalize();
  183. scene.add( light );
  184. var noof_balls = 51;
  185. // shadow
  186. var canvas = document.createElement( 'canvas' );
  187. canvas.width = 128;
  188. canvas.height = 128;
  189. var context = canvas.getContext( '2d' );
  190. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  191. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  192. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  193. context.fillStyle = gradient;
  194. context.fillRect( 0, 0, canvas.width, canvas.height );
  195. var shadowTexture = new CanvasTexture( canvas );
  196. var shadowMaterial = new MeshBasicMaterial( { map: shadowTexture } );
  197. var shadowGeo = new PlaneBufferGeometry( 300, 300, 1, 1 );
  198. for ( var i = 0; i < noof_balls; i ++ ) { // create shadows
  199. var shadowMesh = new Mesh( shadowGeo, shadowMaterial );
  200. shadowMesh.position.x = - ( noof_balls - 1 ) / 2 * 400 + i * 400;
  201. shadowMesh.position.y = - 250;
  202. shadowMesh.rotation.x = - Math.PI / 2;
  203. scene.add( shadowMesh );
  204. }
  205. var radius = 200;
  206. var geometry1 = new IcosahedronBufferGeometry( radius, 1 );
  207. var count = geometry1.attributes.position.count;
  208. geometry1.addAttribute( 'color', new BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  209. var color = new Color();
  210. var positions = geometry1.attributes.position;
  211. var colors = geometry1.attributes.color;
  212. for ( var i = 0; i < count; i ++ ) {
  213. color.setHSL( ( positions.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  214. colors.setXYZ( i, color.r, color.g, color.b );
  215. }
  216. var material = new MeshPhongMaterial( {
  217. color: 0xffffff,
  218. flatShading: true,
  219. vertexColors: VertexColors,
  220. shininess: 0
  221. } );
  222. var wireframeMaterial = new MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  223. for ( var i = 0; i < noof_balls; i ++ ) { // create balls
  224. var mesh = new Mesh( geometry1, material );
  225. var wireframe = new Mesh( geometry1, wireframeMaterial );
  226. mesh.add( wireframe );
  227. mesh.position.x = - ( noof_balls - 1 ) / 2 * 400 + i * 400;
  228. mesh.rotation.x = i * 0.5;
  229. scene.add( mesh );
  230. }
  231. renderer = new WebGLRenderer( { antialias: true } );
  232. renderer.setPixelRatio( window.devicePixelRatio );
  233. renderer.setSize( 200, 300 );
  234. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  235. }
  236. function onDocumentMouseMove( event ) {
  237. mouseX = event.clientX - windowHalfX;
  238. mouseY = event.clientY - windowHalfY;
  239. }
  240. function animate() {
  241. for ( var i = 0; i < views.length; ++ i ) {
  242. views[ i ].render();
  243. }
  244. requestAnimationFrame( animate );
  245. }
  246. </script>
  247. </body>
  248. </html>