webgl_mirror_nodes.html 8.3 KB

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