webgl_multiple_canvases_circle.html 8.5 KB

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