webgl_mirror_nodes.html 8.6 KB

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