webgl_rtt.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - render-to-texture</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. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> render-to-texture webgl example</div>
  12. <script id="fragment_shader_screen" type="x-shader/x-fragment">
  13. varying vec2 vUv;
  14. uniform sampler2D tDiffuse;
  15. void main() {
  16. gl_FragColor = texture2D( tDiffuse, vUv );
  17. }
  18. </script>
  19. <script id="fragment_shader_pass_1" type="x-shader/x-fragment">
  20. varying vec2 vUv;
  21. uniform float time;
  22. void main() {
  23. float r = vUv.x;
  24. if( vUv.y < 0.5 ) r = 0.0;
  25. float g = vUv.y;
  26. if( vUv.x < 0.5 ) g = 0.0;
  27. gl_FragColor = vec4( r, g, time, 1.0 );
  28. }
  29. </script>
  30. <script id="vertexShader" type="x-shader/x-vertex">
  31. varying vec2 vUv;
  32. void main() {
  33. vUv = uv;
  34. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  35. }
  36. </script>
  37. <script type="module">
  38. import * as THREE from '../build/three.module.js';
  39. import Stats from './jsm/libs/stats.module.js';
  40. var container, stats;
  41. var cameraRTT, camera, sceneRTT, sceneScreen, scene, renderer, zmesh1, zmesh2;
  42. var mouseX = 0, mouseY = 0;
  43. var windowHalfX = window.innerWidth / 2;
  44. var windowHalfY = window.innerHeight / 2;
  45. var rtTexture, material, quad;
  46. var delta = 0.01;
  47. init();
  48. animate();
  49. function init() {
  50. container = document.getElementById( 'container' );
  51. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 10000 );
  52. camera.position.z = 100;
  53. cameraRTT = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
  54. cameraRTT.position.z = 100;
  55. //
  56. scene = new THREE.Scene();
  57. sceneRTT = new THREE.Scene();
  58. sceneScreen = new THREE.Scene();
  59. var light = new THREE.DirectionalLight( 0xffffff );
  60. light.position.set( 0, 0, 1 ).normalize();
  61. sceneRTT.add( light );
  62. light = new THREE.DirectionalLight( 0xffaaaa, 1.5 );
  63. light.position.set( 0, 0, - 1 ).normalize();
  64. sceneRTT.add( light );
  65. rtTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat } );
  66. material = new THREE.ShaderMaterial( {
  67. uniforms: { time: { value: 0.0 } },
  68. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  69. fragmentShader: document.getElementById( 'fragment_shader_pass_1' ).textContent
  70. } );
  71. var materialScreen = new THREE.ShaderMaterial( {
  72. uniforms: { tDiffuse: { value: rtTexture.texture } },
  73. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  74. fragmentShader: document.getElementById( 'fragment_shader_screen' ).textContent,
  75. depthWrite: false
  76. } );
  77. var plane = new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight );
  78. quad = new THREE.Mesh( plane, material );
  79. quad.position.z = - 100;
  80. sceneRTT.add( quad );
  81. var geometry = new THREE.TorusBufferGeometry( 100, 25, 15, 30 );
  82. var mat1 = new THREE.MeshPhongMaterial( { color: 0x555555, specular: 0xffaa00, shininess: 5 } );
  83. var mat2 = new THREE.MeshPhongMaterial( { color: 0x550000, specular: 0xff2200, shininess: 5 } );
  84. zmesh1 = new THREE.Mesh( geometry, mat1 );
  85. zmesh1.position.set( 0, 0, 100 );
  86. zmesh1.scale.set( 1.5, 1.5, 1.5 );
  87. sceneRTT.add( zmesh1 );
  88. zmesh2 = new THREE.Mesh( geometry, mat2 );
  89. zmesh2.position.set( 0, 150, 100 );
  90. zmesh2.scale.set( 0.75, 0.75, 0.75 );
  91. sceneRTT.add( zmesh2 );
  92. quad = new THREE.Mesh( plane, materialScreen );
  93. quad.position.z = - 100;
  94. sceneScreen.add( quad );
  95. var n = 5,
  96. geometry = new THREE.SphereBufferGeometry( 10, 64, 32 ),
  97. material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: rtTexture.texture } );
  98. for ( var j = 0; j < n; j ++ ) {
  99. for ( var i = 0; i < n; i ++ ) {
  100. var mesh = new THREE.Mesh( geometry, material2 );
  101. mesh.position.x = ( i - ( n - 1 ) / 2 ) * 20;
  102. mesh.position.y = ( j - ( n - 1 ) / 2 ) * 20;
  103. mesh.position.z = 0;
  104. mesh.rotation.y = - Math.PI / 2;
  105. scene.add( mesh );
  106. }
  107. }
  108. renderer = new THREE.WebGLRenderer();
  109. renderer.setPixelRatio( window.devicePixelRatio );
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. renderer.autoClear = false;
  112. container.appendChild( renderer.domElement );
  113. stats = new Stats();
  114. container.appendChild( stats.dom );
  115. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  116. }
  117. function onDocumentMouseMove( event ) {
  118. mouseX = ( event.clientX - windowHalfX );
  119. mouseY = ( event.clientY - windowHalfY );
  120. }
  121. //
  122. function animate() {
  123. requestAnimationFrame( animate );
  124. render();
  125. stats.update();
  126. }
  127. function render() {
  128. var time = Date.now() * 0.0015;
  129. camera.position.x += ( mouseX - camera.position.x ) * .05;
  130. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  131. camera.lookAt( scene.position );
  132. if ( zmesh1 && zmesh2 ) {
  133. zmesh1.rotation.y = - time;
  134. zmesh2.rotation.y = - time + Math.PI / 2;
  135. }
  136. if ( material.uniforms[ "time" ].value > 1 || material.uniforms[ "time" ].value < 0 ) {
  137. delta *= - 1;
  138. }
  139. material.uniforms[ "time" ].value += delta;
  140. // Render first scene into texture
  141. renderer.setRenderTarget( rtTexture );
  142. renderer.clear();
  143. renderer.render( sceneRTT, cameraRTT );
  144. // Render full screen quad with generated texture
  145. renderer.setRenderTarget( null );
  146. renderer.clear();
  147. renderer.render( sceneScreen, cameraRTT );
  148. // Render second scene to screen
  149. // (using first scene as regular texture)
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>