webgl_multiple_renderers.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple renderers</div>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. let camera, scene, renderer1, renderer2;
  23. let mesh1, mesh2, mesh3;
  24. const color = new THREE.Color();
  25. init();
  26. animate();
  27. function init() {
  28. camera = new THREE.PerspectiveCamera( 20, window.innerWidth / ( window.innerHeight / 2 ), 1, 10000 );
  29. scene = new THREE.Scene();
  30. scene.background = new THREE.Color( 0xffffff );
  31. const light1 = new THREE.DirectionalLight( 0xffffff );
  32. light1.position.set( 0, 0, 1 );
  33. scene.add( light1 );
  34. const light2 = new THREE.DirectionalLight( 0xffff00, 0.75 );
  35. light2.position.set( 0, 0, - 1 );
  36. scene.add( light2 );
  37. // shadow
  38. const canvas = document.createElement( 'canvas' );
  39. canvas.width = 128;
  40. canvas.height = 128;
  41. const context = canvas.getContext( '2d' );
  42. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  43. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  44. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  45. context.fillStyle = gradient;
  46. context.fillRect( 0, 0, canvas.width, canvas.height );
  47. const shadowTexture = new THREE.CanvasTexture( canvas );
  48. const shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  49. const shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  50. let shadowMesh;
  51. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  52. shadowMesh.position.y = - 250;
  53. shadowMesh.rotation.x = - Math.PI / 2;
  54. scene.add( shadowMesh );
  55. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  56. shadowMesh.position.x = - 400;
  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. const radius = 200;
  66. const geometry1 = new THREE.IcosahedronGeometry( radius, 1 );
  67. const count = geometry1.attributes.position.count;
  68. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  69. const geometry2 = geometry1.clone();
  70. const geometry3 = geometry1.clone();
  71. const positions1 = geometry1.attributes.position;
  72. const positions2 = geometry2.attributes.position;
  73. const positions3 = geometry3.attributes.position;
  74. const colors1 = geometry1.attributes.color;
  75. const colors2 = geometry2.attributes.color;
  76. const colors3 = geometry3.attributes.color;
  77. for ( let i = 0; i < count; i ++ ) {
  78. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  79. colors1.setXYZ( i, color.r, color.g, color.b );
  80. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  81. colors2.setXYZ( i, color.r, color.g, color.b );
  82. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  83. colors3.setXYZ( i, color.r, color.g, color.b );
  84. }
  85. const material = new THREE.MeshPhongMaterial( {
  86. color: 0xffffff,
  87. flatShading: true,
  88. vertexColors: true,
  89. shininess: 0
  90. } );
  91. const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  92. mesh1 = new THREE.Mesh( geometry1, material );
  93. mesh1.position.x = - 400;
  94. mesh1.rotation.x = - 1.87;
  95. scene.add( mesh1 );
  96. const wireframe1 = new THREE.Mesh( geometry1, wireframeMaterial );
  97. mesh1.add( wireframe1 );
  98. mesh2 = new THREE.Mesh( geometry2, material );
  99. mesh2.position.x = 400;
  100. scene.add( mesh2 );
  101. const wireframe2 = new THREE.Mesh( geometry2, wireframeMaterial );
  102. mesh2.add( wireframe2 );
  103. mesh3 = new THREE.Mesh( geometry3, material );
  104. scene.add( mesh3 );
  105. const wireframe3 = new THREE.Mesh( geometry3, wireframeMaterial );
  106. mesh3.add( wireframe3 );
  107. //
  108. renderer1 = new THREE.WebGLRenderer( { antialias: true } );
  109. renderer1.setPixelRatio( window.devicePixelRatio );
  110. renderer1.setSize( window.innerWidth, window.innerHeight / 2 );
  111. document.body.appendChild( renderer1.domElement );
  112. renderer2 = new THREE.WebGLRenderer();
  113. renderer2.setPixelRatio( window.devicePixelRatio );
  114. renderer2.setSize( window.innerWidth, window.innerHeight / 2 );
  115. document.body.appendChild( renderer2.domElement );
  116. }
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. // update scene
  120. mesh1.rotation.z += Math.PI / 500;
  121. mesh2.rotation.z += Math.PI / 500;
  122. mesh3.rotation.z += Math.PI / 500;
  123. const position = new THREE.Vector3();
  124. const color = new THREE.Color();
  125. let time = performance.now() / 500;
  126. const positions = mesh3.geometry.attributes.position;
  127. const colors = mesh3.geometry.attributes.color;
  128. for ( let i = 0, l = positions.count; i < l; i ++ ) {
  129. position.fromArray( positions.array, i * 3 );
  130. color.setRGB( 1, Math.sin( time + position.x ), Math.cos( time * 2.123 + position.x ) );
  131. colors.setXYZ( i, color.r, color.g, color.b );
  132. }
  133. colors.needsUpdate = true;
  134. //
  135. time = performance.now() / 2000;
  136. camera.position.x = Math.sin( time ) * 1800;
  137. camera.position.z = Math.cos( time ) * 1800;
  138. camera.lookAt( scene.position );
  139. renderer1.render( scene, camera );
  140. renderer2.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>