webgl_mirror_nodes.html 9.0 KB

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