webgl_multiple_renderers.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple renderers</div>
  20. <script src="../build/three.js"></script>
  21. <script src="js/WebGL.js"></script>
  22. <script src="js/libs/stats.min.js"></script>
  23. <script>
  24. if ( WEBGL.isWebGLAvailable() === false ) {
  25. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  26. }
  27. var camera, scene, renderer1, renderer2;
  28. var mesh1, mesh2, mesh3;
  29. var color = new THREE.Color();
  30. init();
  31. animate();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 20, window.innerWidth / ( window.innerHeight / 2 ), 1, 10000 );
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xffffff );
  36. var light = new THREE.DirectionalLight( 0xffffff );
  37. light.position.set( 0, 0, 1 );
  38. scene.add( light );
  39. var light = new THREE.DirectionalLight( 0xffff00, 0.75 );
  40. light.position.set( 0, 0, - 1 );
  41. scene.add( light );
  42. // shadow
  43. var canvas = document.createElement( 'canvas' );
  44. canvas.width = 128;
  45. canvas.height = 128;
  46. var context = canvas.getContext( '2d' );
  47. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  48. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  49. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  50. context.fillStyle = gradient;
  51. context.fillRect( 0, 0, canvas.width, canvas.height );
  52. var shadowTexture = new THREE.CanvasTexture( canvas );
  53. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  54. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  55. var shadowMesh;
  56. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  57. shadowMesh.position.y = - 250;
  58. shadowMesh.rotation.x = - Math.PI / 2;
  59. scene.add( shadowMesh );
  60. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  61. shadowMesh.position.x = - 400;
  62. shadowMesh.position.y = - 250;
  63. shadowMesh.rotation.x = - Math.PI / 2;
  64. scene.add( shadowMesh );
  65. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  66. shadowMesh.position.x = 400;
  67. shadowMesh.position.y = - 250;
  68. shadowMesh.rotation.x = - Math.PI / 2;
  69. scene.add( shadowMesh );
  70. var radius = 200;
  71. var geometry1 = new THREE.IcosahedronBufferGeometry( radius, 1 );
  72. var count = geometry1.attributes.position.count;
  73. geometry1.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  74. var geometry2 = geometry1.clone();
  75. var geometry3 = geometry1.clone();
  76. var positions1 = geometry1.attributes.position;
  77. var positions2 = geometry2.attributes.position;
  78. var positions3 = geometry3.attributes.position;
  79. var colors1 = geometry1.attributes.color;
  80. var colors2 = geometry2.attributes.color;
  81. var colors3 = geometry3.attributes.color;
  82. for ( var i = 0; i < count; i ++ ) {
  83. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  84. colors1.setXYZ( i, color.r, color.g, color.b );
  85. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  86. colors2.setXYZ( i, color.r, color.g, color.b );
  87. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  88. colors3.setXYZ( i, color.r, color.g, color.b );
  89. }
  90. var material = new THREE.MeshPhongMaterial( {
  91. color: 0xffffff,
  92. flatShading: true,
  93. vertexColors: THREE.VertexColors,
  94. shininess: 0
  95. } );
  96. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  97. mesh1 = new THREE.Mesh( geometry1, material );
  98. mesh1.position.x = - 400;
  99. mesh1.rotation.x = - 1.87;
  100. scene.add( mesh1 );
  101. var wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  102. mesh1.add( wireframe );
  103. mesh2 = new THREE.Mesh( geometry2, material );
  104. mesh2.position.x = 400;
  105. scene.add( mesh2 );
  106. var wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  107. mesh2.add( wireframe );
  108. mesh3 = new THREE.Mesh( geometry3, material );
  109. scene.add( mesh3 );
  110. var wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  111. mesh3.add( wireframe );
  112. //
  113. renderer1 = new THREE.WebGLRenderer( { antialias: true } );
  114. renderer1.setPixelRatio( window.devicePixelRatio );
  115. renderer1.setSize( window.innerWidth, window.innerHeight / 2 );
  116. document.body.appendChild( renderer1.domElement );
  117. renderer2 = new THREE.WebGLRenderer();
  118. renderer2.setPixelRatio( window.devicePixelRatio );
  119. renderer2.setSize( window.innerWidth, window.innerHeight / 2 );
  120. document.body.appendChild( renderer2.domElement );
  121. }
  122. function animate() {
  123. requestAnimationFrame( animate );
  124. // update scene
  125. mesh1.rotation.z += Math.PI / 500;
  126. mesh2.rotation.z += Math.PI / 500;
  127. mesh3.rotation.z += Math.PI / 500;
  128. var position = new THREE.Vector3();
  129. var color = new THREE.Color();
  130. var time = performance.now() / 500;
  131. var positions = mesh3.geometry.attributes.position;
  132. var colors = mesh3.geometry.attributes.color;
  133. for ( var i = 0, l = positions.count; i < l; i ++ ) {
  134. position.fromArray( positions.array, i * 3 );
  135. color.setRGB( 1, Math.sin( time + position.x ), Math.cos( time * 2.123 + position.x ) );
  136. colors.setXYZ( i, color.r, color.g, color.b );
  137. }
  138. colors.needsUpdate = true;
  139. //
  140. var time = performance.now() / 2000;
  141. camera.position.x = Math.sin( time ) * 1800;
  142. camera.position.z = Math.cos( time ) * 1800;
  143. camera.lookAt( scene.position );
  144. renderer1.render( scene, camera );
  145. renderer2.render( scene, camera );
  146. }
  147. </script>
  148. </body>
  149. </html>