webgl_mirror_nodes.html 9.0 KB

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