webgl_clipping_advanced.html 9.8 KB

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