webgl_mirror_nodes.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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: 200px;
  20. left: calc(50% - 100px);
  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">three.js</a> - mirror
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/Mirror.js"></script>
  34. <script src="js/controls/OrbitControls.js"></script>
  35. <!-- NodeLibrary -->
  36. <script src="js/nodes/GLNode.js"></script>
  37. <script src="js/nodes/RawNode.js"></script>
  38. <script src="js/nodes/TempNode.js"></script>
  39. <script src="js/nodes/InputNode.js"></script>
  40. <script src="js/nodes/ConstNode.js"></script>
  41. <script src="js/nodes/FunctionNode.js"></script>
  42. <script src="js/nodes/FunctionCallNode.js"></script>
  43. <script src="js/nodes/BuilderNode.js"></script>
  44. <script src="js/nodes/NodeLib.js"></script>
  45. <script src="js/nodes/NodeMaterial.js"></script>
  46. <!-- Accessors -->
  47. <script src="js/nodes/accessors/PositionNode.js"></script>
  48. <script src="js/nodes/accessors/NormalNode.js"></script>
  49. <script src="js/nodes/accessors/UVNode.js"></script>
  50. <script src="js/nodes/accessors/ScreenUVNode.js"></script>
  51. <script src="js/nodes/accessors/ColorsNode.js"></script>
  52. <script src="js/nodes/accessors/CameraNode.js"></script>
  53. <script src="js/nodes/accessors/ReflectNode.js"></script>
  54. <script src="js/nodes/accessors/LightNode.js"></script>
  55. <!-- Inputs -->
  56. <script src="js/nodes/inputs/IntNode.js"></script>
  57. <script src="js/nodes/inputs/FloatNode.js"></script>
  58. <script src="js/nodes/inputs/ColorNode.js"></script>
  59. <script src="js/nodes/inputs/Vector2Node.js"></script>
  60. <script src="js/nodes/inputs/Vector3Node.js"></script>
  61. <script src="js/nodes/inputs/Vector4Node.js"></script>
  62. <script src="js/nodes/inputs/TextureNode.js"></script>
  63. <script src="js/nodes/inputs/CubeTextureNode.js"></script>
  64. <script src="js/nodes/inputs/Matrix4Node.js"></script>
  65. <script src="js/nodes/inputs/MirrorNode.js"></script>
  66. <!-- Math -->
  67. <script src="js/nodes/math/Math1Node.js"></script>
  68. <script src="js/nodes/math/Math2Node.js"></script>
  69. <script src="js/nodes/math/Math3Node.js"></script>
  70. <script src="js/nodes/math/OperatorNode.js"></script>
  71. <!-- Utils -->
  72. <script src="js/nodes/utils/SwitchNode.js"></script>
  73. <script src="js/nodes/utils/JoinNode.js"></script>
  74. <script src="js/nodes/utils/TimerNode.js"></script>
  75. <script src="js/nodes/utils/RoughnessToBlinnExponentNode.js"></script>
  76. <script src="js/nodes/utils/VelocityNode.js"></script>
  77. <script src="js/nodes/utils/LuminanceNode.js"></script>
  78. <script src="js/nodes/utils/ColorAdjustmentNode.js"></script>
  79. <script src="js/nodes/utils/NoiseNode.js"></script>
  80. <script src="js/nodes/utils/ResolutionNode.js"></script>
  81. <!-- Phong Material -->
  82. <script src="js/nodes/materials/PhongNode.js"></script>
  83. <script src="js/nodes/materials/PhongNodeMaterial.js"></script>
  84. <script>
  85. // scene size
  86. var WIDTH = window.innerWidth;
  87. var HEIGHT = window.innerHeight;
  88. // camera
  89. var VIEW_ANGLE = 45;
  90. var ASPECT = WIDTH / HEIGHT;
  91. var NEAR = 1;
  92. var FAR = 500;
  93. var decalNormal = new THREE.TextureLoader().load( 'textures/decal/decal-normal.jpg' );
  94. var decalDiffuse = new THREE.TextureLoader().load( 'textures/decal/decal-diffuse.png' );
  95. decalDiffuse.wrapS = decalDiffuse.wrapT = THREE.RepeatWrapping;
  96. var camera, scene, renderer;
  97. var cameraControls;
  98. var groundMirror;
  99. var sphereGroup, smallSphere;
  100. var groundMirrorMaterial;
  101. function init() {
  102. // renderer
  103. renderer = new THREE.WebGLRenderer();
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( WIDTH, HEIGHT );
  106. // scene
  107. scene = new THREE.Scene();
  108. // camera
  109. camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
  110. camera.position.set( 0, 75, 160 );
  111. cameraControls = new THREE.OrbitControls(camera, renderer.domElement);
  112. cameraControls.target.set( 0, 40, 0);
  113. cameraControls.maxDistance = 400;
  114. cameraControls.minDistance = 10;
  115. cameraControls.update();
  116. var container = document.getElementById( 'container' );
  117. container.appendChild( renderer.domElement );
  118. }
  119. function fillScene() {
  120. var planeGeo = new THREE.PlaneBufferGeometry( 100.1, 100.1 );
  121. // MIRROR planes
  122. groundMirror = new THREE.Mirror( renderer, camera, { clipBias: 0.003, textureWidth: WIDTH, textureHeight: HEIGHT, color: 0x777777 } );
  123. var mask = new THREE.SwitchNode( new THREE.TextureNode( decalDiffuse ), 'w' );
  124. var maskFlip = new THREE.Math1Node( mask, THREE.Math1Node.INVERT );
  125. var mirror = new THREE.MirrorNode( groundMirror );
  126. var normal = new THREE.TextureNode( decalNormal );
  127. var normalXY = new THREE.SwitchNode( normal, 'xy' );
  128. var normalXYFlip = new THREE.Math1Node(
  129. normalXY,
  130. THREE.Math1Node.INVERT
  131. );
  132. var offsetNormal = new THREE.OperatorNode(
  133. normalXYFlip,
  134. new THREE.FloatNode( .5 ),
  135. THREE.OperatorNode.SUB
  136. );
  137. mirror.offset = new THREE.OperatorNode(
  138. offsetNormal, // normal
  139. new THREE.FloatNode( 6 ),// scale
  140. THREE.OperatorNode.MUL
  141. );
  142. var clr = new THREE.Math3Node(
  143. mirror,
  144. new THREE.ColorNode( 0xFFFFFF ),
  145. mask,
  146. THREE.Math3Node.MIX
  147. );
  148. groundMirrorMaterial = new THREE.PhongNodeMaterial();
  149. groundMirrorMaterial.environment = mirror;
  150. groundMirrorMaterial.environmentAlpha = mask;
  151. groundMirrorMaterial.normal = normal;
  152. //groundMirrorMaterial.normalScale = new THREE.FloatNode( 1 );
  153. groundMirrorMaterial.build();
  154. var mirrorMesh = new THREE.Mesh( planeGeo, groundMirrorMaterial );
  155. mirrorMesh.add( groundMirror );
  156. mirrorMesh.rotateX( - Math.PI / 2 );
  157. scene.add( mirrorMesh );
  158. sphereGroup = new THREE.Object3D();
  159. scene.add( sphereGroup );
  160. var geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  161. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  162. var sphereCap = new THREE.Mesh( geometry, material );
  163. sphereCap.position.y = -15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  164. sphereCap.rotateX(-Math.PI);
  165. var geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  166. var halfSphere = new THREE.Mesh( geometry, material );
  167. halfSphere.add( sphereCap );
  168. halfSphere.rotateX( - Math.PI / 180 * 135 );
  169. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  170. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  171. sphereGroup.add( halfSphere );
  172. var geometry = new THREE.IcosahedronGeometry( 5, 0 );
  173. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, shading: THREE.FlatShading } );
  174. smallSphere = new THREE.Mesh( geometry, material );
  175. scene.add(smallSphere);
  176. // walls
  177. var planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  178. planeTop.position.y = 100;
  179. planeTop.rotateX( Math.PI / 2 );
  180. scene.add( planeTop );
  181. var planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  182. planeBack.position.z = -50;
  183. planeBack.position.y = 50;
  184. scene.add( planeBack );
  185. var planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  186. planeFront.position.z = 50;
  187. planeFront.position.y = 50;
  188. planeFront.rotateY( Math.PI );
  189. scene.add( planeFront );
  190. var planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  191. planeRight.position.x = 50;
  192. planeRight.position.y = 50;
  193. planeRight.rotateY( - Math.PI / 2 );
  194. scene.add( planeRight );
  195. var planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  196. planeLeft.position.x = -50;
  197. planeLeft.position.y = 50;
  198. planeLeft.rotateY( Math.PI / 2 );
  199. scene.add( planeLeft );
  200. // lights
  201. var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  202. mainLight.position.y = 60;
  203. scene.add( mainLight );
  204. var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  205. greenLight.position.set( 550, 50, 0 );
  206. scene.add( greenLight );
  207. var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  208. redLight.position.set( - 550, 50, 0 );
  209. scene.add( redLight );
  210. var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  211. blueLight.position.set( 0, 50, 550 );
  212. scene.add( blueLight );
  213. }
  214. function render() {
  215. // render (update) the mirrors
  216. groundMirrorMaterial.visible = false;
  217. groundMirror.render();
  218. groundMirrorMaterial.visible = true;
  219. renderer.render(scene, camera);
  220. }
  221. function update() {
  222. requestAnimationFrame( update );
  223. var timer = Date.now() * 0.01;
  224. sphereGroup.rotation.y -= 0.002;
  225. smallSphere.position.set(
  226. Math.cos( timer * 0.1 ) * 30,
  227. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  228. Math.sin( timer * 0.1 ) * 30
  229. );
  230. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  231. smallSphere.rotation.z = timer * 0.8;
  232. cameraControls.update();
  233. render();
  234. }
  235. init();
  236. fillScene();
  237. update();
  238. </script>
  239. </body>
  240. </html>