webgl_multiple_renderers.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple renderers</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. box-shadow: 0px 0px 100px 0px #000;
  43. }
  44. a {
  45. color: #0080ff;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="container">
  51. <div id="container1"></div>
  52. <div id="container2"></div>
  53. <div id="container3"></div>
  54. </div>
  55. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - multiple renderers</div>
  56. <script src="../build/three.min.js"></script>
  57. <script src="js/Detector.js"></script>
  58. <script src="js/libs/stats.min.js"></script>
  59. <script>
  60. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  61. var apps = [];
  62. var scene = createScene();
  63. init();
  64. animate();
  65. function init() {
  66. var container1 = document.getElementById( 'container1' );
  67. var container2 = document.getElementById( 'container2' );
  68. var container3 = document.getElementById( 'container3' );
  69. var WIDTH = 800;
  70. var HEIGHT = 600;
  71. apps.push( new App( container1, WIDTH, HEIGHT ) );
  72. apps.push( new App( container2, WIDTH, HEIGHT ) );
  73. apps.push( new App( container3, WIDTH, HEIGHT ) );
  74. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  75. }
  76. function animate() {
  77. updateScene();
  78. for ( var i = 0; i < apps.length; ++i ) {
  79. apps[ i ].animate();
  80. }
  81. requestAnimationFrame( animate );
  82. }
  83. function onDocumentMouseMove (e) {
  84. var container3 = document.getElementById( 'container3' );
  85. container3.style.left = e.pageX - container3.clientWidth / 2 + "px";
  86. container3.style.top = e.pageY - container3.clientHeight / 2 + "px";
  87. }
  88. function createScene () {
  89. var mesh, group1, group2, group3, light;
  90. scene = new THREE.Scene();
  91. light = new THREE.DirectionalLight( 0xffffff );
  92. light.position.set( 0, 0, 1 ).normalize();
  93. scene.add( light );
  94. // shadow
  95. var canvas = document.createElement( 'canvas' );
  96. canvas.width = 128;
  97. canvas.height = 128;
  98. var context = canvas.getContext( '2d' );
  99. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  100. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  101. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  102. context.fillStyle = gradient;
  103. context.fillRect( 0, 0, canvas.width, canvas.height );
  104. var shadowTexture = new THREE.Texture( canvas );
  105. shadowTexture.needsUpdate = true;
  106. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  107. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  108. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  109. mesh.position.y = - 250;
  110. mesh.rotation.x = - Math.PI / 2;
  111. scene.add( mesh );
  112. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  113. mesh.position.x = - 400;
  114. mesh.position.y = - 250;
  115. mesh.rotation.x = - Math.PI / 2;
  116. scene.add( mesh );
  117. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  118. mesh.position.x = 400;
  119. mesh.position.y = - 250;
  120. mesh.rotation.x = - Math.PI / 2;
  121. scene.add( mesh );
  122. var faceIndices = [ 'a', 'b', 'c' ];
  123. var color, f1, f2, f3, p, vertexIndex,
  124. radius = 200,
  125. geometry1 = new THREE.IcosahedronGeometry( radius, 1 ),
  126. geometry2 = new THREE.IcosahedronGeometry( radius, 1 ),
  127. geometry3 = new THREE.IcosahedronGeometry( radius, 1 );
  128. for ( var i = 0; i < geometry1.faces.length; i ++ ) {
  129. f1 = geometry1.faces[ i ];
  130. f2 = geometry2.faces[ i ];
  131. f3 = geometry3.faces[ i ];
  132. for( var j = 0; j < 3; 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.name = 'rotating ball';
  160. group3.position.x = 0;
  161. group3.rotation.x = 0;
  162. scene.add( group3 );
  163. return scene;
  164. }
  165. function updateScene () {
  166. var group = scene.getObjectByName( 'rotating ball' );
  167. group.rotation.x += Math.PI / 600;
  168. var geometry = group.children[0].geometry;
  169. for (var i = 0; i < geometry.faces.length; i ++) {
  170. var f = geometry.faces[ i ];
  171. for( var j = 0; j < 3; j ++ ) {
  172. var color = f.vertexColors[ j ];
  173. color.setHex( ( color.getHex() + 0xfdfdfd ) % 0xffffff );
  174. }
  175. }
  176. geometry.colorsNeedUpdate = true;
  177. }
  178. function App( container, fullWidth, fullHeight ) {
  179. var stats;
  180. var camera, renderer;
  181. var mouseX = 0, mouseY = 0;
  182. var windowHalfX = window.innerWidth / 2;
  183. var windowHalfY = window.innerHeight / 2;
  184. init();
  185. function init() {
  186. camera = new THREE.PerspectiveCamera( 20, container.clientWidth / container.clientHeight, 1, 10000 );
  187. camera.setViewOffset( fullWidth, fullHeight, container.offsetLeft, container.offsetTop, container.clientWidth, container.clientHeight );
  188. camera.position.z = 1800;
  189. renderer = new THREE.WebGLRenderer( { antialias: true } );
  190. renderer.setClearColor( 0xffffff );
  191. renderer.setPixelRatio( window.devicePixelRatio );
  192. renderer.setSize( container.clientWidth, container.clientHeight );
  193. container.appendChild( renderer.domElement );
  194. stats = new Stats();
  195. stats.domElement.style.position = 'absolute';
  196. stats.domElement.style.top = '0px';
  197. container.appendChild( stats.domElement );
  198. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  199. }
  200. function onDocumentMouseMove( event ) {
  201. mouseX = ( event.clientX - windowHalfX );
  202. mouseY = ( event.clientY - windowHalfY );
  203. }
  204. //
  205. this.animate = function() {
  206. render();
  207. stats.update();
  208. };
  209. function render() {
  210. camera.setViewOffset( fullWidth, fullHeight, container.offsetLeft, container.offsetTop, container.clientWidth, container.clientHeight );
  211. camera.position.x += ( -mouseX - camera.position.x ) * 0.5;
  212. camera.position.y += ( - mouseY - camera.position.y ) * 0.5;
  213. camera.lookAt( scene.position );
  214. renderer.render( scene, camera );
  215. }
  216. }
  217. </script>
  218. </body>
  219. </html>