webgl_multiple_canvases_complex.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #container1, #container2, #container3 {
  23. position: relative;
  24. border: 1px solid red;
  25. }
  26. #container1 {
  27. width: 300px;
  28. height: 200px;
  29. }
  30. #container2 {
  31. width: 400px;
  32. height: 100px;
  33. left: 150px;
  34. }
  35. #container3 {
  36. width: 200px;
  37. height: 300px;
  38. left: 75px;
  39. }
  40. a {
  41. color: #0080ff;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="container">
  47. <div id="container1"></div>
  48. <div id="container2"></div>
  49. <div id="container3"></div>
  50. </div>
  51. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - multiple canvases - complex</div>
  52. <script src="../build/three.min.js"></script>
  53. <script src="js/Detector.js"></script>
  54. <script src="js/libs/stats.min.js"></script>
  55. <script>
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var apps = [];
  58. init();
  59. animate();
  60. function init() {
  61. var container1 = document.getElementById( 'container1' );
  62. var container2 = document.getElementById( 'container2' );
  63. var container3 = document.getElementById( 'container3' );
  64. var fullWidth = 550;
  65. var fullHeight = 600;
  66. apps.push( new App( 'container1', fullWidth, fullHeight, 0, 0, container1.clientWidth, container1.clientHeight ) );
  67. apps.push( new App( 'container2', fullWidth, fullHeight, 150, 200, container2.clientWidth, container2.clientHeight ) );
  68. apps.push( new App( 'container3', fullWidth, fullHeight, 75, 300, container3.clientWidth, container3.clientHeight ) );
  69. }
  70. function animate() {
  71. for ( var i = 0; i < apps.length; ++i ) {
  72. apps[ i ].animate();
  73. }
  74. requestAnimationFrame( animate );
  75. }
  76. function App( containerId, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  77. var container, stats;
  78. var camera, scene, renderer;
  79. var mesh, group1, group2, group3, light;
  80. var mouseX = 0, mouseY = 0;
  81. var windowHalfX = window.innerWidth / 2;
  82. var windowHalfY = window.innerHeight / 2;
  83. init();
  84. function init() {
  85. container = document.getElementById( containerId );
  86. camera = new THREE.PerspectiveCamera( 20, container.clientWidth / container.clientHeight, 1, 10000 );
  87. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  88. camera.position.z = 1800;
  89. scene = new THREE.Scene();
  90. light = new THREE.DirectionalLight( 0xffffff );
  91. light.position.set( 0, 0, 1 ).normalize();
  92. scene.add( light );
  93. // shadow
  94. var canvas = document.createElement( 'canvas' );
  95. canvas.width = 128;
  96. canvas.height = 128;
  97. var context = canvas.getContext( '2d' );
  98. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  99. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  100. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  101. context.fillStyle = gradient;
  102. context.fillRect( 0, 0, canvas.width, canvas.height );
  103. var shadowTexture = new THREE.Texture( canvas );
  104. shadowTexture.needsUpdate = true;
  105. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  106. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  107. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  108. mesh.position.y = - 250;
  109. mesh.rotation.x = - Math.PI / 2;
  110. scene.add( mesh );
  111. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  112. mesh.position.x = - 400;
  113. mesh.position.y = - 250;
  114. mesh.rotation.x = - Math.PI / 2;
  115. scene.add( mesh );
  116. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  117. mesh.position.x = 400;
  118. mesh.position.y = - 250;
  119. mesh.rotation.x = - Math.PI / 2;
  120. scene.add( mesh );
  121. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  122. var color, f1, f2, f3, p, n, vertexIndex,
  123. radius = 200,
  124. geometry1 = new THREE.IcosahedronGeometry( radius, 1 ),
  125. geometry2 = new THREE.IcosahedronGeometry( radius, 1 ),
  126. geometry3 = new THREE.IcosahedronGeometry( radius, 1 );
  127. for ( var i = 0; i < geometry1.faces.length; i ++ ) {
  128. f1 = geometry1.faces[ i ];
  129. f2 = geometry2.faces[ i ];
  130. f3 = geometry3.faces[ i ];
  131. n = ( f1 instanceof THREE.Face3 ) ? 3 : 4;
  132. for( var j = 0; j < n; j ++ ) {
  133. vertexIndex = f1[ faceIndices[ j ] ];
  134. p = geometry1.vertices[ vertexIndex ];
  135. color = new THREE.Color( 0xffffff );
  136. color.setHSL( ( p.y / radius + 1 ) / 2, 1.0, 0.5 );
  137. f1.vertexColors[ j ] = color;
  138. color = new THREE.Color( 0xffffff );
  139. color.setHSL( 0.0, ( p.y / radius + 1 ) / 2, 0.5 );
  140. f2.vertexColors[ j ] = color;
  141. color = new THREE.Color( 0xffffff );
  142. color.setHSL( 0.125 * vertexIndex / geometry1.vertices.length, 1.0, 0.5 );
  143. f3.vertexColors[ j ] = color;
  144. }
  145. }
  146. var materials = [
  147. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  148. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true, transparent: true } )
  149. ];
  150. group1 = THREE.SceneUtils.createMultiMaterialObject( geometry1, materials );
  151. group1.position.x = -400;
  152. group1.rotation.x = -1.87;
  153. scene.add( group1 );
  154. group2 = THREE.SceneUtils.createMultiMaterialObject( geometry2, materials );
  155. group2.position.x = 400;
  156. group2.rotation.x = 0;
  157. scene.add( group2 );
  158. group3 = THREE.SceneUtils.createMultiMaterialObject( geometry3, materials );
  159. group3.position.x = 0;
  160. group3.rotation.x = 0;
  161. scene.add( group3 );
  162. renderer = new THREE.WebGLRenderer( { antialias: true } );
  163. renderer.setClearColor( 0xffffff );
  164. renderer.setPixelRatio( window.devicePixelRatio );
  165. renderer.setSize( container.clientWidth, container.clientHeight );
  166. container.appendChild( renderer.domElement );
  167. stats = new Stats();
  168. stats.domElement.style.position = 'absolute';
  169. stats.domElement.style.top = '0px';
  170. container.appendChild( stats.domElement );
  171. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  172. }
  173. function onDocumentMouseMove( event ) {
  174. mouseX = ( event.clientX - windowHalfX );
  175. mouseY = ( event.clientY - windowHalfY );
  176. }
  177. //
  178. this.animate = function() {
  179. render();
  180. stats.update();
  181. };
  182. function render() {
  183. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  184. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  185. camera.lookAt( scene.position );
  186. renderer.render( scene, camera );
  187. }
  188. }
  189. </script>
  190. </body>
  191. </html>