webgl_mirror_nodes.html 8.9 KB

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