webgl_mirror_nodes.html 11 KB

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