webgl_mirror_nodes.html 8.1 KB

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