webgl_mirror_nodes.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - mirror with nodes</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. color: #444;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container"></div>
  19. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - mirror node-based
  20. </div>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import { GUI } from './jsm/libs/dat.gui.module.js';
  24. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  25. import { ReflectorRTT } from './jsm/objects/ReflectorRTT.js';
  26. import {
  27. NodeFrame,
  28. ExpressionNode,
  29. PhongNodeMaterial,
  30. MathNode,
  31. OperatorNode,
  32. TextureNode,
  33. BlurNode,
  34. FloatNode,
  35. ReflectorNode,
  36. SwitchNode,
  37. NormalMapNode,
  38. } from './jsm/nodes/Nodes.js';
  39. const decalNormal = new THREE.TextureLoader().load( 'textures/decal/decal-normal.jpg' );
  40. const decalDiffuse = new THREE.TextureLoader().load( 'textures/decal/decal-diffuse.png' );
  41. decalDiffuse.wrapS = decalDiffuse.wrapT = THREE.RepeatWrapping;
  42. let camera, scene, renderer;
  43. const clock = new THREE.Clock();
  44. let cameraControls;
  45. const gui = new GUI();
  46. let sphereGroup, smallSphere;
  47. let groundMirrorMaterial;
  48. const frame = new NodeFrame();
  49. let groundMirror;
  50. let blurMirror;
  51. function init() {
  52. // renderer
  53. renderer = new THREE.WebGLRenderer();
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. // scene
  57. scene = new THREE.Scene();
  58. // camera
  59. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  60. camera.position.set( 0, 75, 160 );
  61. cameraControls = new OrbitControls( camera, renderer.domElement );
  62. cameraControls.target.set( 0, 40, 0 );
  63. cameraControls.maxDistance = 400;
  64. cameraControls.minDistance = 10;
  65. cameraControls.update();
  66. const container = document.getElementById( 'container' );
  67. container.appendChild( renderer.domElement );
  68. window.addEventListener( 'resize', onWindowResize, false );
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. groundMirror.getRenderTarget().setSize(
  75. window.innerWidth * window.devicePixelRatio,
  76. window.innerHeight * window.devicePixelRatio
  77. );
  78. blurMirror.size.set(
  79. window.innerWidth * window.devicePixelRatio,
  80. window.innerHeight * window.devicePixelRatio
  81. );
  82. blurMirror.updateFrame();
  83. }
  84. function fillScene() {
  85. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  86. let geometry, material;
  87. // reflector/mirror plane
  88. geometry = new THREE.PlaneGeometry( 100, 100 );
  89. groundMirror = new ReflectorRTT( geometry, {
  90. clipBias: 0.003,
  91. textureWidth: window.innerWidth * window.devicePixelRatio,
  92. textureHeight: window.innerHeight * window.devicePixelRatio
  93. } );
  94. const mask = new SwitchNode( new TextureNode( decalDiffuse ), 'w' );
  95. const mirror = new ReflectorNode( groundMirror );
  96. const normalMap = new TextureNode( decalNormal );
  97. const normalXY = new SwitchNode( normalMap, 'xy' );
  98. const normalXYFlip = new MathNode(
  99. normalXY,
  100. MathNode.INVERT
  101. );
  102. const offsetNormal = new OperatorNode(
  103. normalXYFlip,
  104. new FloatNode( .5 ),
  105. OperatorNode.SUB
  106. );
  107. mirror.offset = new OperatorNode(
  108. offsetNormal, // normal
  109. new FloatNode( 6 ), // scale
  110. OperatorNode.MUL
  111. );
  112. blurMirror = new BlurNode( mirror );
  113. blurMirror.size = new THREE.Vector2(
  114. window.innerWidth * window.devicePixelRatio,
  115. window.innerHeight * window.devicePixelRatio
  116. );
  117. blurMirror.uv = new ExpressionNode( 'projCoord.xyz / projCoord.q', 'vec3' );
  118. blurMirror.uv.keywords[ 'projCoord' ] = new OperatorNode( mirror.offset, mirror.uv, OperatorNode.ADD );
  119. blurMirror.radius.x = blurMirror.radius.y = 0;
  120. gui.add( { blur: blurMirror.radius.x }, 'blur', 0, 25 ).onChange( function ( v ) {
  121. blurMirror.radius.x = blurMirror.radius.y = v;
  122. } );
  123. groundMirrorMaterial = new PhongNodeMaterial();
  124. groundMirrorMaterial.environment = blurMirror; // or add "mirror" variable to disable blur
  125. groundMirrorMaterial.environmentAlpha = mask;
  126. groundMirrorMaterial.normal = new NormalMapNode( normalMap );
  127. //groundMirrorMaterial.normalScale = new FloatNode( 1 );
  128. // test serialization
  129. /*
  130. let library = {};
  131. library[ groundMirror.uuid ] = groundMirror;
  132. library[ decalDiffuse.uuid ] = decalDiffuse;
  133. library[ decalNormal.uuid ] = decalNormal;
  134. library[ mirror.textureMatrix.uuid ] = mirror.textureMatrix; // use textureMatrix to projection
  135. let json = groundMirrorMaterial.toJSON();
  136. groundMirrorMaterial = new NodeMaterialLoader( null, library ).parse( json );
  137. */
  138. //--
  139. const mirrorMesh = new THREE.Mesh( planeGeo, groundMirrorMaterial );
  140. // add all alternative mirror materials inside the ReflectorRTT to prevent:
  141. // glDrawElements: Source and destination textures of the draw are the same.
  142. groundMirror.add( mirrorMesh );
  143. groundMirror.rotateX( - Math.PI / 2 );
  144. scene.add( groundMirror );
  145. sphereGroup = new THREE.Object3D();
  146. scene.add( sphereGroup );
  147. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  148. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  149. const sphereCap = new THREE.Mesh( geometry, material );
  150. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  151. sphereCap.rotateX( - Math.PI );
  152. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  153. const halfSphere = new THREE.Mesh( geometry, material );
  154. halfSphere.add( sphereCap );
  155. halfSphere.rotateX( - Math.PI / 180 * 135 );
  156. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  157. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  158. sphereGroup.add( halfSphere );
  159. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  160. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  161. smallSphere = new THREE.Mesh( geometry, material );
  162. scene.add( smallSphere );
  163. // walls
  164. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  165. planeTop.position.y = 100;
  166. planeTop.rotateX( Math.PI / 2 );
  167. scene.add( planeTop );
  168. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  169. planeBack.position.z = - 50;
  170. planeBack.position.y = 50;
  171. scene.add( planeBack );
  172. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  173. planeFront.position.z = 50;
  174. planeFront.position.y = 50;
  175. planeFront.rotateY( Math.PI );
  176. scene.add( planeFront );
  177. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  178. planeRight.position.x = 50;
  179. planeRight.position.y = 50;
  180. planeRight.rotateY( - Math.PI / 2 );
  181. scene.add( planeRight );
  182. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  183. planeLeft.position.x = - 50;
  184. planeLeft.position.y = 50;
  185. planeLeft.rotateY( Math.PI / 2 );
  186. scene.add( planeLeft );
  187. // lights
  188. const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  189. mainLight.position.y = 60;
  190. scene.add( mainLight );
  191. const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  192. greenLight.position.set( 550, 50, 0 );
  193. scene.add( greenLight );
  194. const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  195. redLight.position.set( - 550, 50, 0 );
  196. scene.add( redLight );
  197. const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  198. blueLight.position.set( 0, 50, 550 );
  199. scene.add( blueLight );
  200. }
  201. function render() {
  202. renderer.render( scene, camera );
  203. }
  204. function update() {
  205. requestAnimationFrame( update );
  206. const delta = clock.getDelta();
  207. const timer = Date.now() * 0.01;
  208. sphereGroup.rotation.y -= 0.002;
  209. smallSphere.position.set(
  210. Math.cos( timer * 0.1 ) * 30,
  211. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  212. Math.sin( timer * 0.1 ) * 30
  213. );
  214. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  215. smallSphere.rotation.z = timer * 0.8;
  216. frame.update( delta ).updateNode( groundMirrorMaterial );
  217. render();
  218. }
  219. init();
  220. fillScene();
  221. update();
  222. </script>
  223. </body>
  224. </html>