webgl_multiple_canvases_circle.html 8.4 KB

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