webgl_multiple_renderers.html 6.3 KB

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