webgl_mirror_nodes.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 maskFlip = new THREE.Math1Node( mask, THREE.Math1Node.INVERT );
  83. var mirror = new THREE.ReflectorNode( groundMirror );
  84. var normalMap = new THREE.TextureNode( decalNormal );
  85. var normalXY = new THREE.SwitchNode( normalMap, 'xy' );
  86. var normalXYFlip = new THREE.Math1Node(
  87. normalXY,
  88. THREE.Math1Node.INVERT
  89. );
  90. var offsetNormal = new THREE.OperatorNode(
  91. normalXYFlip,
  92. new THREE.FloatNode( .5 ),
  93. THREE.OperatorNode.SUB
  94. );
  95. mirror.offset = new THREE.OperatorNode(
  96. offsetNormal, // normal
  97. new THREE.FloatNode( 6 ), // scale
  98. THREE.OperatorNode.MUL
  99. );
  100. var clr = new THREE.Math3Node(
  101. mirror,
  102. new THREE.ColorNode( 0xFFFFFF ),
  103. mask,
  104. THREE.Math3Node.MIX
  105. );
  106. var blurMirror = new THREE.BlurNode( mirror );
  107. blurMirror.size = new THREE.Vector2( WIDTH, HEIGHT );
  108. blurMirror.uv = new THREE.ExpressionNode( "projCoord.xyz / projCoord.q", "vec3" );
  109. blurMirror.uv.keywords[ "projCoord" ] = new THREE.OperatorNode( mirror.offset, mirror.uv, THREE.OperatorNode.ADD );
  110. blurMirror.radius.x = blurMirror.radius.y = 0;
  111. gui.add( { blur: blurMirror.radius.x }, "blur", 0, 25 ).onChange( function ( v ) {
  112. blurMirror.radius.x = blurMirror.radius.y = v;
  113. } );
  114. groundMirrorMaterial = new THREE.PhongNodeMaterial();
  115. groundMirrorMaterial.environment = blurMirror; // or add "mirror" variable to disable blur
  116. groundMirrorMaterial.environmentAlpha = mask;
  117. groundMirrorMaterial.normal = new THREE.NormalMapNode( normalMap );
  118. //groundMirrorMaterial.normalScale = new THREE.FloatNode( 1 );
  119. // test serialization
  120. /*
  121. var library = {};
  122. library[ groundMirror.uuid ] = groundMirror;
  123. library[ decalDiffuse.uuid ] = decalDiffuse;
  124. library[ decalNormal.uuid ] = decalNormal;
  125. library[ mirror.textureMatrix.uuid ] = mirror.textureMatrix; // use textureMatrix to projection
  126. var json = groundMirrorMaterial.toJSON();
  127. groundMirrorMaterial = new THREE.NodeMaterialLoader( null, library ).parse( json );
  128. */
  129. //--
  130. var mirrorMesh = new THREE.Mesh( planeGeo, groundMirrorMaterial );
  131. // add all alternative mirror materials inside the ReflectorRTT to prevent:
  132. // glDrawElements: Source and destination textures of the draw are the same.
  133. groundMirror.add( mirrorMesh );
  134. groundMirror.rotateX( - Math.PI / 2 );
  135. scene.add( groundMirror );
  136. sphereGroup = new THREE.Object3D();
  137. scene.add( sphereGroup );
  138. var geometry = new THREE.CylinderBufferGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  139. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  140. var sphereCap = new THREE.Mesh( geometry, material );
  141. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  142. sphereCap.rotateX( - Math.PI );
  143. var geometry = new THREE.SphereBufferGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  144. var halfSphere = new THREE.Mesh( geometry, material );
  145. halfSphere.add( sphereCap );
  146. halfSphere.rotateX( - Math.PI / 180 * 135 );
  147. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  148. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  149. sphereGroup.add( halfSphere );
  150. var geometry = new THREE.IcosahedronBufferGeometry( 5, 0 );
  151. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  152. smallSphere = new THREE.Mesh( geometry, material );
  153. scene.add( smallSphere );
  154. // walls
  155. var planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  156. planeTop.position.y = 100;
  157. planeTop.rotateX( Math.PI / 2 );
  158. scene.add( planeTop );
  159. var planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  160. planeBack.position.z = - 50;
  161. planeBack.position.y = 50;
  162. scene.add( planeBack );
  163. var planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  164. planeFront.position.z = 50;
  165. planeFront.position.y = 50;
  166. planeFront.rotateY( Math.PI );
  167. scene.add( planeFront );
  168. var planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  169. planeRight.position.x = 50;
  170. planeRight.position.y = 50;
  171. planeRight.rotateY( - Math.PI / 2 );
  172. scene.add( planeRight );
  173. var planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  174. planeLeft.position.x = - 50;
  175. planeLeft.position.y = 50;
  176. planeLeft.rotateY( Math.PI / 2 );
  177. scene.add( planeLeft );
  178. // lights
  179. var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  180. mainLight.position.y = 60;
  181. scene.add( mainLight );
  182. var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  183. greenLight.position.set( 550, 50, 0 );
  184. scene.add( greenLight );
  185. var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  186. redLight.position.set( - 550, 50, 0 );
  187. scene.add( redLight );
  188. var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  189. blueLight.position.set( 0, 50, 550 );
  190. scene.add( blueLight );
  191. }
  192. function render() {
  193. renderer.render( scene, camera );
  194. }
  195. function update() {
  196. requestAnimationFrame( update );
  197. var delta = clock.getDelta();
  198. var timer = Date.now() * 0.01;
  199. sphereGroup.rotation.y -= 0.002;
  200. smallSphere.position.set(
  201. Math.cos( timer * 0.1 ) * 30,
  202. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  203. Math.sin( timer * 0.1 ) * 30
  204. );
  205. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  206. smallSphere.rotation.z = timer * 0.8;
  207. frame.update( delta ).updateNode( groundMirrorMaterial );
  208. render();
  209. }
  210. init();
  211. fillScene();
  212. update();
  213. </script>
  214. </body>
  215. </html>