webgl_multiple_renderers_complex.html 7.2 KB

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