webgl_mirror_nodes.html 8.1 KB

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