webgl_clipping_advanced.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - clipping planes</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. </head>
  9. <body>
  10. <script type="module">
  11. import {
  12. AmbientLight,
  13. BoxBufferGeometry,
  14. Color,
  15. DirectionalLight,
  16. DoubleSide,
  17. Group,
  18. Matrix4,
  19. Mesh,
  20. MeshBasicMaterial,
  21. MeshPhongMaterial,
  22. PerspectiveCamera,
  23. Plane,
  24. PlaneBufferGeometry,
  25. Scene,
  26. SpotLight,
  27. Vector3,
  28. WebGLRenderer
  29. } from "../build/three.module.js";
  30. import Stats from './jsm/libs/stats.module.js';
  31. import { GUI } from './jsm/libs/dat.gui.module.js';
  32. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  33. function planesFromMesh( vertices, indices ) {
  34. // creates a clipping volume from a convex triangular mesh
  35. // specified by the arrays 'vertices' and 'indices'
  36. var n = indices.length / 3,
  37. result = new Array( n );
  38. for ( var i = 0, j = 0; i < n; ++ i, j += 3 ) {
  39. var a = vertices[ indices[ j ] ],
  40. b = vertices[ indices[ j + 1 ] ],
  41. c = vertices[ indices[ j + 2 ] ];
  42. result[ i ] = new Plane().
  43. setFromCoplanarPoints( a, b, c );
  44. }
  45. return result;
  46. }
  47. function createPlanes( n ) {
  48. // creates an array of n uninitialized plane objects
  49. var result = new Array( n );
  50. for ( var i = 0; i !== n; ++ i )
  51. result[ i ] = new Plane();
  52. return result;
  53. }
  54. function assignTransformedPlanes( planesOut, planesIn, matrix ) {
  55. // sets an array of existing planes to transformed 'planesIn'
  56. for ( var i = 0, n = planesIn.length; i !== n; ++ i )
  57. planesOut[ i ].copy( planesIn[ i ] ).applyMatrix4( matrix );
  58. }
  59. function cylindricalPlanes( n, innerRadius ) {
  60. var result = createPlanes( n );
  61. for ( var i = 0; i !== n; ++ i ) {
  62. var plane = result[ i ],
  63. angle = i * Math.PI * 2 / n;
  64. plane.normal.set(
  65. Math.cos( angle ), 0, Math.sin( angle ) );
  66. plane.constant = innerRadius;
  67. }
  68. return result;
  69. }
  70. var planeToMatrix = ( function () {
  71. // creates a matrix that aligns X/Y to a given plane
  72. // temporaries:
  73. var xAxis = new Vector3(),
  74. yAxis = new Vector3(),
  75. trans = new Vector3();
  76. return function planeToMatrix( plane ) {
  77. var zAxis = plane.normal,
  78. matrix = new Matrix4();
  79. // Hughes & Moeller '99
  80. // "Building an Orthonormal Basis from a Unit Vector."
  81. if ( Math.abs( zAxis.x ) > Math.abs( zAxis.z ) ) {
  82. yAxis.set( - zAxis.y, zAxis.x, 0 );
  83. } else {
  84. yAxis.set( 0, - zAxis.z, zAxis.y );
  85. }
  86. xAxis.crossVectors( yAxis.normalize(), zAxis );
  87. plane.coplanarPoint( trans );
  88. return matrix.set(
  89. xAxis.x, yAxis.x, zAxis.x, trans.x,
  90. xAxis.y, yAxis.y, zAxis.y, trans.y,
  91. xAxis.z, yAxis.z, zAxis.z, trans.z,
  92. 0, 0, 0, 1 );
  93. };
  94. } )();
  95. // A regular tetrahedron for the clipping volume:
  96. var Vertices = [
  97. new Vector3( + 1, 0, + Math.SQRT1_2 ),
  98. new Vector3( - 1, 0, + Math.SQRT1_2 ),
  99. new Vector3( 0, + 1, - Math.SQRT1_2 ),
  100. new Vector3( 0, - 1, - Math.SQRT1_2 )
  101. ],
  102. Indices = [
  103. 0, 1, 2, 0, 2, 3, 0, 3, 1, 1, 3, 2
  104. ],
  105. Planes = planesFromMesh( Vertices, Indices ),
  106. PlaneMatrices = Planes.map( planeToMatrix ),
  107. GlobalClippingPlanes = cylindricalPlanes( 5, 3.5 ),
  108. Empty = Object.freeze( [] );
  109. var camera, scene, renderer, startTime, stats,
  110. object, clipMaterial,
  111. volumeVisualization,
  112. globalClippingPlanes;
  113. function init() {
  114. camera = new PerspectiveCamera(
  115. 36, window.innerWidth / window.innerHeight, 0.25, 16 );
  116. camera.position.set( 0, 1.5, 3 );
  117. scene = new Scene();
  118. // Lights
  119. scene.add( new AmbientLight( 0x505050 ) );
  120. var spotLight = new SpotLight( 0xffffff );
  121. spotLight.angle = Math.PI / 5;
  122. spotLight.penumbra = 0.2;
  123. spotLight.position.set( 2, 3, 3 );
  124. spotLight.castShadow = true;
  125. spotLight.shadow.camera.near = 3;
  126. spotLight.shadow.camera.far = 10;
  127. spotLight.shadow.mapSize.width = 1024;
  128. spotLight.shadow.mapSize.height = 1024;
  129. scene.add( spotLight );
  130. var dirLight = new DirectionalLight( 0x55505a, 1 );
  131. dirLight.position.set( 0, 2, 0 );
  132. dirLight.castShadow = true;
  133. dirLight.shadow.camera.near = 1;
  134. dirLight.shadow.camera.far = 10;
  135. dirLight.shadow.camera.right = 1;
  136. dirLight.shadow.camera.left = - 1;
  137. dirLight.shadow.camera.top = 1;
  138. dirLight.shadow.camera.bottom = - 1;
  139. dirLight.shadow.mapSize.width = 1024;
  140. dirLight.shadow.mapSize.height = 1024;
  141. scene.add( dirLight );
  142. // Geometry
  143. clipMaterial = new MeshPhongMaterial( {
  144. color: 0xee0a10,
  145. shininess: 100,
  146. side: DoubleSide,
  147. // Clipping setup:
  148. clippingPlanes: createPlanes( Planes.length ),
  149. clipShadows: true
  150. } );
  151. object = new Group();
  152. var geometry = new BoxBufferGeometry( 0.18, 0.18, 0.18 );
  153. for ( var z = - 2; z <= 2; ++ z )
  154. for ( var y = - 2; y <= 2; ++ y )
  155. for ( var x = - 2; x <= 2; ++ x ) {
  156. var mesh = new Mesh( geometry, clipMaterial );
  157. mesh.position.set( x / 5, y / 5, z / 5 );
  158. mesh.castShadow = true;
  159. object.add( mesh );
  160. }
  161. scene.add( object );
  162. var planeGeometry =
  163. new PlaneBufferGeometry( 3, 3, 1, 1 ),
  164. color = new Color();
  165. volumeVisualization = new Group();
  166. volumeVisualization.visible = false;
  167. for ( var i = 0, n = Planes.length; i !== n; ++ i ) {
  168. var material = new MeshBasicMaterial( {
  169. color: color.setHSL( i / n, 0.5, 0.5 ).getHex(),
  170. side: DoubleSide,
  171. opacity: 0.2,
  172. transparent: true,
  173. // clip to the others to show the volume (wildly
  174. // intersecting transparent planes look bad)
  175. clippingPlanes: clipMaterial.clippingPlanes.
  176. filter( function ( _, j ) {
  177. return j !== i;
  178. } )
  179. // no need to enable shadow clipping - the plane
  180. // visualization does not cast shadows
  181. } );
  182. volumeVisualization.add(
  183. new Mesh( planeGeometry, material ) );
  184. }
  185. scene.add( volumeVisualization );
  186. var ground = new Mesh( planeGeometry,
  187. new MeshPhongMaterial( {
  188. color: 0xa0adaf, shininess: 150 } ) );
  189. ground.rotation.x = - Math.PI / 2;
  190. ground.scale.multiplyScalar( 3 );
  191. ground.receiveShadow = true;
  192. scene.add( ground );
  193. // Renderer
  194. var container = document.body;
  195. renderer = new WebGLRenderer();
  196. renderer.shadowMap.enabled = true;
  197. renderer.setPixelRatio( window.devicePixelRatio );
  198. renderer.setSize( window.innerWidth, window.innerHeight );
  199. window.addEventListener( 'resize', onWindowResize, false );
  200. container.appendChild( renderer.domElement );
  201. // Clipping setup:
  202. globalClippingPlanes = createPlanes( GlobalClippingPlanes.length );
  203. renderer.clippingPlanes = Empty;
  204. renderer.localClippingEnabled = true;
  205. // Stats
  206. stats = new Stats();
  207. container.appendChild( stats.dom );
  208. // Controls
  209. var controls = new OrbitControls( camera, renderer.domElement );
  210. controls.target.set( 0, 1, 0 );
  211. controls.update();
  212. // GUI
  213. var gui = new GUI(),
  214. folder = gui.addFolder( "Local Clipping" ),
  215. props = {
  216. get 'Enabled'() {
  217. return renderer.localClippingEnabled;
  218. },
  219. set 'Enabled'( v ) {
  220. renderer.localClippingEnabled = v;
  221. if ( ! v ) volumeVisualization.visible = false;
  222. },
  223. get 'Shadows'() {
  224. return clipMaterial.clipShadows;
  225. },
  226. set 'Shadows'( v ) {
  227. clipMaterial.clipShadows = v;
  228. },
  229. get 'Visualize'() {
  230. return volumeVisualization.visible;
  231. },
  232. set 'Visualize'( v ) {
  233. if ( renderer.localClippingEnabled )
  234. volumeVisualization.visible = v;
  235. }
  236. };
  237. folder.add( props, 'Enabled' );
  238. folder.add( props, 'Shadows' );
  239. folder.add( props, 'Visualize' ).listen();
  240. gui.addFolder( "Global Clipping" ).
  241. add( {
  242. get 'Enabled'() {
  243. return renderer.clippingPlanes !== Empty;
  244. },
  245. set 'Enabled'( v ) {
  246. renderer.clippingPlanes = v ?
  247. globalClippingPlanes : Empty;
  248. }
  249. }, "Enabled" );
  250. // Start
  251. startTime = Date.now();
  252. }
  253. function onWindowResize() {
  254. camera.aspect = window.innerWidth / window.innerHeight;
  255. camera.updateProjectionMatrix();
  256. renderer.setSize( window.innerWidth, window.innerHeight );
  257. }
  258. function setObjectWorldMatrix( object, matrix ) {
  259. // set the orientation of an object based on a world matrix
  260. var parent = object.parent;
  261. scene.updateMatrixWorld();
  262. object.matrix.getInverse( parent.matrixWorld );
  263. object.applyMatrix( matrix );
  264. }
  265. var transform = new Matrix4(),
  266. tmpMatrix = new Matrix4();
  267. function animate() {
  268. var currentTime = Date.now(),
  269. time = ( currentTime - startTime ) / 1000;
  270. requestAnimationFrame( animate );
  271. object.position.y = 1;
  272. object.rotation.x = time * 0.5;
  273. object.rotation.y = time * 0.2;
  274. object.updateMatrix();
  275. transform.copy( object.matrix );
  276. var bouncy = Math.cos( time * .5 ) * 0.5 + 0.7;
  277. transform.multiply(
  278. tmpMatrix.makeScale( bouncy, bouncy, bouncy ) );
  279. assignTransformedPlanes(
  280. clipMaterial.clippingPlanes, Planes, transform );
  281. var planeMeshes = volumeVisualization.children;
  282. for ( var i = 0, n = planeMeshes.length; i !== n; ++ i ) {
  283. tmpMatrix.multiplyMatrices( transform, PlaneMatrices[ i ] );
  284. setObjectWorldMatrix( planeMeshes[ i ], tmpMatrix );
  285. }
  286. transform.makeRotationY( time * 0.1 );
  287. assignTransformedPlanes(
  288. globalClippingPlanes, GlobalClippingPlanes, transform );
  289. stats.begin();
  290. renderer.render( scene, camera );
  291. stats.end();
  292. }
  293. init();
  294. animate();
  295. </script>
  296. </body>
  297. </html>