webgl_mirror_nodes.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. <style>
  8. body {
  9. color: #888888;
  10. font-family:Monospace;
  11. font-size:13px;
  12. background-color: #000;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px;
  19. width: 400px;
  20. left: calc(50% - 200px);
  21. text-align: center;
  22. }
  23. a {
  24. color: #00f;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - mirror node-based
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/libs/dat.gui.min.js"></script>
  34. <script src="js/objects/Reflector.js"></script>
  35. <script src="js/objects/ReflectorRTT.js"></script>
  36. <script src="js/controls/OrbitControls.js"></script>
  37. <!-- NodeLibrary -->
  38. <script src="js/nodes/GLNode.js"></script>
  39. <script src="js/nodes/RawNode.js"></script>
  40. <script src="js/nodes/TempNode.js"></script>
  41. <script src="js/nodes/InputNode.js"></script>
  42. <script src="js/nodes/ConstNode.js"></script>
  43. <script src="js/nodes/FunctionNode.js"></script>
  44. <script src="js/nodes/FunctionCallNode.js"></script>
  45. <script src="js/nodes/NodeBuilder.js"></script>
  46. <script src="js/nodes/NodeLib.js"></script>
  47. <script src="js/nodes/NodeFrame.js"></script>
  48. <script src="js/nodes/NodeMaterial.js"></script>
  49. <!-- Accessors -->
  50. <script src="js/nodes/accessors/PositionNode.js"></script>
  51. <script src="js/nodes/accessors/NormalNode.js"></script>
  52. <script src="js/nodes/accessors/UVNode.js"></script>
  53. <script src="js/nodes/accessors/ScreenUVNode.js"></script>
  54. <script src="js/nodes/accessors/ColorsNode.js"></script>
  55. <script src="js/nodes/accessors/CameraNode.js"></script>
  56. <script src="js/nodes/accessors/ReflectNode.js"></script>
  57. <script src="js/nodes/accessors/LightNode.js"></script>
  58. <!-- Inputs -->
  59. <script src="js/nodes/inputs/IntNode.js"></script>
  60. <script src="js/nodes/inputs/FloatNode.js"></script>
  61. <script src="js/nodes/inputs/ColorNode.js"></script>
  62. <script src="js/nodes/inputs/Vector2Node.js"></script>
  63. <script src="js/nodes/inputs/Vector3Node.js"></script>
  64. <script src="js/nodes/inputs/Vector4Node.js"></script>
  65. <script src="js/nodes/inputs/TextureNode.js"></script>
  66. <script src="js/nodes/inputs/CubeTextureNode.js"></script>
  67. <script src="js/nodes/inputs/Matrix4Node.js"></script>
  68. <script src="js/nodes/inputs/ReflectorNode.js"></script>
  69. <!-- Math -->
  70. <script src="js/nodes/math/Math1Node.js"></script>
  71. <script src="js/nodes/math/Math2Node.js"></script>
  72. <script src="js/nodes/math/Math3Node.js"></script>
  73. <script src="js/nodes/math/OperatorNode.js"></script>
  74. <!-- Utils -->
  75. <script src="js/nodes/utils/SwitchNode.js"></script>
  76. <script src="js/nodes/utils/JoinNode.js"></script>
  77. <script src="js/nodes/utils/TimerNode.js"></script>
  78. <script src="js/nodes/utils/RoughnessToBlinnExponentNode.js"></script>
  79. <script src="js/nodes/utils/VelocityNode.js"></script>
  80. <script src="js/nodes/utils/LuminanceNode.js"></script>
  81. <script src="js/nodes/utils/ColorAdjustmentNode.js"></script>
  82. <script src="js/nodes/utils/NoiseNode.js"></script>
  83. <script src="js/nodes/utils/ResolutionNode.js"></script>
  84. <script src="js/nodes/utils/BlurNode.js"></script>
  85. <!-- Phong Material -->
  86. <script src="js/nodes/materials/PhongNode.js"></script>
  87. <script src="js/nodes/materials/PhongNodeMaterial.js"></script>
  88. <!-- NodeMaterial Loader -->
  89. <script src="js/loaders/NodeMaterialLoader.js"></script>
  90. <script>
  91. // scene size
  92. var WIDTH = window.innerWidth;
  93. var HEIGHT = window.innerHeight;
  94. // camera
  95. var VIEW_ANGLE = 45;
  96. var ASPECT = WIDTH / HEIGHT;
  97. var NEAR = 1;
  98. var FAR = 500;
  99. var decalNormal = new THREE.TextureLoader().load( 'textures/decal/decal-normal.jpg' );
  100. var decalDiffuse = new THREE.TextureLoader().load( 'textures/decal/decal-diffuse.png' );
  101. decalDiffuse.wrapS = decalDiffuse.wrapT = THREE.RepeatWrapping;
  102. var camera, scene, renderer;
  103. var clock = new THREE.Clock();
  104. var cameraControls;
  105. var gui = new dat.GUI();
  106. var sphereGroup, smallSphere;
  107. var groundMirrorMaterial;
  108. var frame = new THREE.NodeFrame();
  109. function init() {
  110. // renderer
  111. renderer = new THREE.WebGLRenderer();
  112. renderer.setPixelRatio( window.devicePixelRatio );
  113. renderer.setSize( WIDTH, HEIGHT );
  114. // scene
  115. scene = new THREE.Scene();
  116. // camera
  117. camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
  118. camera.position.set( 0, 75, 160 );
  119. cameraControls = new THREE.OrbitControls( camera, renderer.domElement );
  120. cameraControls.target.set( 0, 40, 0 );
  121. cameraControls.maxDistance = 400;
  122. cameraControls.minDistance = 10;
  123. cameraControls.update();
  124. var container = document.getElementById( 'container' );
  125. container.appendChild( renderer.domElement );
  126. }
  127. function fillScene() {
  128. var planeGeo = new THREE.PlaneBufferGeometry( 100.1, 100.1 );
  129. // reflector/mirror plane
  130. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  131. var groundMirror = new THREE.ReflectorRTT( geometry, { clipBias: 0.003, textureWidth: WIDTH, textureHeight: HEIGHT } );
  132. var mask = new THREE.SwitchNode( new THREE.TextureNode( decalDiffuse ), 'w' );
  133. var maskFlip = new THREE.Math1Node( mask, THREE.Math1Node.INVERT );
  134. var mirror = new THREE.ReflectorNode( groundMirror );
  135. var normal = new THREE.TextureNode( decalNormal );
  136. var normalXY = new THREE.SwitchNode( normal, 'xy' );
  137. var normalXYFlip = new THREE.Math1Node(
  138. normalXY,
  139. THREE.Math1Node.INVERT
  140. );
  141. var offsetNormal = new THREE.OperatorNode(
  142. normalXYFlip,
  143. new THREE.FloatNode( .5 ),
  144. THREE.OperatorNode.SUB
  145. );
  146. mirror.offset = new THREE.OperatorNode(
  147. offsetNormal, // normal
  148. new THREE.FloatNode( 6 ), // scale
  149. THREE.OperatorNode.MUL
  150. );
  151. var clr = new THREE.Math3Node(
  152. mirror,
  153. new THREE.ColorNode( 0xFFFFFF ),
  154. mask,
  155. THREE.Math3Node.MIX
  156. );
  157. var blurMirror = new THREE.BlurNode( mirror );
  158. blurMirror.size = new THREE.Vector2( WIDTH, HEIGHT );
  159. blurMirror.coord = new THREE.FunctionNode( "projCoord.xyz / projCoord.q", "vec3" );
  160. blurMirror.coord.keywords[ "projCoord" ] = new THREE.OperatorNode( mirror.offset, mirror.coord, THREE.OperatorNode.ADD );
  161. blurMirror.radius.x = blurMirror.radius.y = 0;
  162. gui.add( { blur: blurMirror.radius.x }, "blur", 0, 25 ).onChange( function ( v ) {
  163. blurMirror.radius.x = blurMirror.radius.y = v;
  164. } );
  165. groundMirrorMaterial = new THREE.PhongNodeMaterial();
  166. groundMirrorMaterial.environment = blurMirror; // or add "mirror" variable to disable blur
  167. groundMirrorMaterial.environmentAlpha = mask;
  168. groundMirrorMaterial.normal = normal;
  169. //groundMirrorMaterial.normalScale = new THREE.FloatNode( 1 );
  170. groundMirrorMaterial.build();
  171. // test serialization
  172. /*
  173. var library = {};
  174. library[ groundMirror.uuid ] = groundMirror;
  175. library[ decalDiffuse.uuid ] = decalDiffuse;
  176. library[ decalNormal.uuid ] = decalNormal;
  177. library[ mirror.textureMatrix.uuid ] = mirror.textureMatrix; // use textureMatrix to projection
  178. var json = groundMirrorMaterial.toJSON();
  179. groundMirrorMaterial = new THREE.NodeMaterialLoader( null, library ).parse( json );
  180. */
  181. //--
  182. var mirrorMesh = new THREE.Mesh( planeGeo, groundMirrorMaterial );
  183. // add all alternative mirror materials inside the ReflectorRTT to prevent:
  184. // glDrawElements: Source and destination textures of the draw are the same.
  185. groundMirror.add( mirrorMesh );
  186. groundMirror.rotateX( - Math.PI / 2 );
  187. scene.add( groundMirror );
  188. sphereGroup = new THREE.Object3D();
  189. scene.add( sphereGroup );
  190. var geometry = new THREE.CylinderBufferGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  191. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  192. var sphereCap = new THREE.Mesh( geometry, material );
  193. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  194. sphereCap.rotateX( - Math.PI );
  195. var geometry = new THREE.SphereBufferGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  196. var halfSphere = new THREE.Mesh( geometry, material );
  197. halfSphere.add( sphereCap );
  198. halfSphere.rotateX( - Math.PI / 180 * 135 );
  199. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  200. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  201. sphereGroup.add( halfSphere );
  202. var geometry = new THREE.IcosahedronBufferGeometry( 5, 0 );
  203. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  204. smallSphere = new THREE.Mesh( geometry, material );
  205. scene.add( smallSphere );
  206. // walls
  207. var planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  208. planeTop.position.y = 100;
  209. planeTop.rotateX( Math.PI / 2 );
  210. scene.add( planeTop );
  211. var planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  212. planeBack.position.z = - 50;
  213. planeBack.position.y = 50;
  214. scene.add( planeBack );
  215. var planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  216. planeFront.position.z = 50;
  217. planeFront.position.y = 50;
  218. planeFront.rotateY( Math.PI );
  219. scene.add( planeFront );
  220. var planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  221. planeRight.position.x = 50;
  222. planeRight.position.y = 50;
  223. planeRight.rotateY( - Math.PI / 2 );
  224. scene.add( planeRight );
  225. var planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  226. planeLeft.position.x = - 50;
  227. planeLeft.position.y = 50;
  228. planeLeft.rotateY( Math.PI / 2 );
  229. scene.add( planeLeft );
  230. // lights
  231. var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  232. mainLight.position.y = 60;
  233. scene.add( mainLight );
  234. var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  235. greenLight.position.set( 550, 50, 0 );
  236. scene.add( greenLight );
  237. var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  238. redLight.position.set( - 550, 50, 0 );
  239. scene.add( redLight );
  240. var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  241. blueLight.position.set( 0, 50, 550 );
  242. scene.add( blueLight );
  243. }
  244. function render() {
  245. renderer.render( scene, camera );
  246. }
  247. function update() {
  248. requestAnimationFrame( update );
  249. var delta = clock.getDelta();
  250. var timer = Date.now() * 0.01;
  251. sphereGroup.rotation.y -= 0.002;
  252. smallSphere.position.set(
  253. Math.cos( timer * 0.1 ) * 30,
  254. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  255. Math.sin( timer * 0.1 ) * 30
  256. );
  257. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  258. smallSphere.rotation.z = timer * 0.8;
  259. frame.update( delta ).updateNode( groundMirrorMaterial );
  260. render();
  261. }
  262. init();
  263. fillScene();
  264. update();
  265. </script>
  266. </body>
  267. </html>