ShadowUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.shadow;
  33. import com.jme3.bounding.BoundingBox;
  34. import com.jme3.bounding.BoundingVolume;
  35. import com.jme3.math.Matrix4f;
  36. import com.jme3.math.Transform;
  37. import com.jme3.math.Vector2f;
  38. import com.jme3.math.Vector3f;
  39. import com.jme3.renderer.Camera;
  40. import com.jme3.renderer.queue.GeometryList;
  41. import com.jme3.scene.Geometry;
  42. import static java.lang.Math.max;
  43. import static java.lang.Math.min;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. /**
  47. * Includes various useful shadow mapping functions.
  48. *
  49. * @see
  50. * <ul>
  51. * <li><a href="http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/">http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/</a></li>
  52. * <li><a href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html">http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html</a></li>
  53. * </ul>
  54. * for more info.
  55. */
  56. public class ShadowUtil {
  57. /**
  58. * Updates a points arrays with the frustum corners of the provided camera.
  59. * @param viewCam
  60. * @param points
  61. */
  62. public static void updateFrustumPoints2(Camera viewCam, Vector3f[] points) {
  63. int w = viewCam.getWidth();
  64. int h = viewCam.getHeight();
  65. float n = viewCam.getFrustumNear();
  66. float f = viewCam.getFrustumFar();
  67. points[0].set(viewCam.getWorldCoordinates(new Vector2f(0, 0), n));
  68. points[1].set(viewCam.getWorldCoordinates(new Vector2f(0, h), n));
  69. points[2].set(viewCam.getWorldCoordinates(new Vector2f(w, h), n));
  70. points[3].set(viewCam.getWorldCoordinates(new Vector2f(w, 0), n));
  71. points[4].set(viewCam.getWorldCoordinates(new Vector2f(0, 0), f));
  72. points[5].set(viewCam.getWorldCoordinates(new Vector2f(0, h), f));
  73. points[6].set(viewCam.getWorldCoordinates(new Vector2f(w, h), f));
  74. points[7].set(viewCam.getWorldCoordinates(new Vector2f(w, 0), f));
  75. }
  76. /**
  77. * Updates the points array to contain the frustum corners of the given
  78. * camera. The nearOverride and farOverride variables can be used
  79. * to override the camera's near/far values with own values.
  80. *
  81. * TODO: Reduce creation of new vectors
  82. *
  83. * @param viewCam
  84. * @param nearOverride
  85. * @param farOverride
  86. */
  87. public static void updateFrustumPoints(Camera viewCam,
  88. float nearOverride,
  89. float farOverride,
  90. float scale,
  91. Vector3f[] points) {
  92. Vector3f pos = viewCam.getLocation();
  93. Vector3f dir = viewCam.getDirection();
  94. Vector3f up = viewCam.getUp();
  95. float depthHeightRatio = viewCam.getFrustumTop() / viewCam.getFrustumNear();
  96. float near = nearOverride;
  97. float far = farOverride;
  98. float ftop = viewCam.getFrustumTop();
  99. float fright = viewCam.getFrustumRight();
  100. float ratio = fright / ftop;
  101. float near_height;
  102. float near_width;
  103. float far_height;
  104. float far_width;
  105. if (viewCam.isParallelProjection()) {
  106. near_height = ftop;
  107. near_width = near_height * ratio;
  108. far_height = ftop;
  109. far_width = far_height * ratio;
  110. } else {
  111. near_height = depthHeightRatio * near;
  112. near_width = near_height * ratio;
  113. far_height = depthHeightRatio * far;
  114. far_width = far_height * ratio;
  115. }
  116. Vector3f right = dir.cross(up).normalizeLocal();
  117. Vector3f temp = new Vector3f();
  118. temp.set(dir).multLocal(far).addLocal(pos);
  119. Vector3f farCenter = temp.clone();
  120. temp.set(dir).multLocal(near).addLocal(pos);
  121. Vector3f nearCenter = temp.clone();
  122. Vector3f nearUp = temp.set(up).multLocal(near_height).clone();
  123. Vector3f farUp = temp.set(up).multLocal(far_height).clone();
  124. Vector3f nearRight = temp.set(right).multLocal(near_width).clone();
  125. Vector3f farRight = temp.set(right).multLocal(far_width).clone();
  126. points[0].set(nearCenter).subtractLocal(nearUp).subtractLocal(nearRight);
  127. points[1].set(nearCenter).addLocal(nearUp).subtractLocal(nearRight);
  128. points[2].set(nearCenter).addLocal(nearUp).addLocal(nearRight);
  129. points[3].set(nearCenter).subtractLocal(nearUp).addLocal(nearRight);
  130. points[4].set(farCenter).subtractLocal(farUp).subtractLocal(farRight);
  131. points[5].set(farCenter).addLocal(farUp).subtractLocal(farRight);
  132. points[6].set(farCenter).addLocal(farUp).addLocal(farRight);
  133. points[7].set(farCenter).subtractLocal(farUp).addLocal(farRight);
  134. if (scale != 1.0f) {
  135. // find center of frustum
  136. Vector3f center = new Vector3f();
  137. for (int i = 0; i < 8; i++) {
  138. center.addLocal(points[i]);
  139. }
  140. center.divideLocal(8f);
  141. Vector3f cDir = new Vector3f();
  142. for (int i = 0; i < 8; i++) {
  143. cDir.set(points[i]).subtractLocal(center);
  144. cDir.multLocal(scale - 1.0f);
  145. points[i].addLocal(cDir);
  146. }
  147. }
  148. }
  149. /**
  150. * Compute bounds of a geomList
  151. * @param list
  152. * @param transform
  153. * @return
  154. */
  155. public static BoundingBox computeUnionBound(GeometryList list, Transform transform) {
  156. BoundingBox bbox = new BoundingBox();
  157. for (int i = 0; i < list.size(); i++) {
  158. BoundingVolume vol = list.get(i).getWorldBound();
  159. BoundingVolume newVol = vol.transform(transform);
  160. //Nehon : prevent NaN and infinity values to screw the final bounding box
  161. if (!Float.isNaN(newVol.getCenter().x) && !Float.isInfinite(newVol.getCenter().x)) {
  162. bbox.mergeLocal(newVol);
  163. }
  164. }
  165. return bbox;
  166. }
  167. /**
  168. * Compute bounds of a geomList
  169. * @param list
  170. * @param mat
  171. * @return
  172. */
  173. public static BoundingBox computeUnionBound(GeometryList list, Matrix4f mat) {
  174. BoundingBox bbox = new BoundingBox();
  175. BoundingVolume store = null;
  176. for (int i = 0; i < list.size(); i++) {
  177. BoundingVolume vol = list.get(i).getWorldBound();
  178. store = vol.clone().transform(mat, null);
  179. //Nehon : prevent NaN and infinity values to screw the final bounding box
  180. if (!Float.isNaN(store.getCenter().x) && !Float.isInfinite(store.getCenter().x)) {
  181. bbox.mergeLocal(store);
  182. }
  183. }
  184. return bbox;
  185. }
  186. /**
  187. * Computes the bounds of multiple bounding volumes
  188. * @param bv
  189. * @return
  190. */
  191. public static BoundingBox computeUnionBound(List<BoundingVolume> bv) {
  192. BoundingBox bbox = new BoundingBox();
  193. for (int i = 0; i < bv.size(); i++) {
  194. BoundingVolume vol = bv.get(i);
  195. bbox.mergeLocal(vol);
  196. }
  197. return bbox;
  198. }
  199. /**
  200. * Compute bounds from an array of points
  201. * @param pts
  202. * @param transform
  203. * @return
  204. */
  205. public static BoundingBox computeBoundForPoints(Vector3f[] pts, Transform transform) {
  206. Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
  207. Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
  208. Vector3f temp = new Vector3f();
  209. for (int i = 0; i < pts.length; i++) {
  210. transform.transformVector(pts[i], temp);
  211. min.minLocal(temp);
  212. max.maxLocal(temp);
  213. }
  214. Vector3f center = min.add(max).multLocal(0.5f);
  215. Vector3f extent = max.subtract(min).multLocal(0.5f);
  216. return new BoundingBox(center, extent.x, extent.y, extent.z);
  217. }
  218. /**
  219. * Compute bounds from an array of points
  220. * @param pts
  221. * @param mat
  222. * @return
  223. */
  224. public static BoundingBox computeBoundForPoints(Vector3f[] pts, Matrix4f mat) {
  225. Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
  226. Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
  227. Vector3f temp = new Vector3f();
  228. for (int i = 0; i < pts.length; i++) {
  229. float w = mat.multProj(pts[i], temp);
  230. temp.x /= w;
  231. temp.y /= w;
  232. // Why was this commented out?
  233. temp.z /= w;
  234. min.minLocal(temp);
  235. max.maxLocal(temp);
  236. }
  237. Vector3f center = min.add(max).multLocal(0.5f);
  238. Vector3f extent = max.subtract(min).multLocal(0.5f);
  239. //Nehon 08/18/2010 : Added an offset to the extend to avoid banding artifacts when the frustum are aligned
  240. return new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z + 2.5f);
  241. }
  242. /**
  243. * Updates the shadow camera to properly contain the given
  244. * points (which contain the eye camera frustum corners)
  245. *
  246. * @param occluders
  247. * @param lightCam
  248. * @param points
  249. */
  250. public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
  251. boolean ortho = shadowCam.isParallelProjection();
  252. shadowCam.setProjectionMatrix(null);
  253. if (ortho) {
  254. shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
  255. } else {
  256. shadowCam.setFrustumPerspective(45, 1, 1, 150);
  257. }
  258. Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
  259. Matrix4f projMatrix = shadowCam.getProjectionMatrix();
  260. BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);
  261. Vector3f splitMin = splitBB.getMin(null);
  262. Vector3f splitMax = splitBB.getMax(null);
  263. // splitMin.z = 0;
  264. // Create the crop matrix.
  265. float scaleX, scaleY, scaleZ;
  266. float offsetX, offsetY, offsetZ;
  267. scaleX = 2.0f / (splitMax.x - splitMin.x);
  268. scaleY = 2.0f / (splitMax.y - splitMin.y);
  269. offsetX = -0.5f * (splitMax.x + splitMin.x) * scaleX;
  270. offsetY = -0.5f * (splitMax.y + splitMin.y) * scaleY;
  271. scaleZ = 1.0f / (splitMax.z - splitMin.z);
  272. offsetZ = -splitMin.z * scaleZ;
  273. Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
  274. 0f, scaleY, 0f, offsetY,
  275. 0f, 0f, scaleZ, offsetZ,
  276. 0f, 0f, 0f, 1f);
  277. Matrix4f result = new Matrix4f();
  278. result.set(cropMatrix);
  279. result.multLocal(projMatrix);
  280. shadowCam.setProjectionMatrix(result);
  281. }
  282. /**
  283. * Updates the shadow camera to properly contain the given
  284. * points (which contain the eye camera frustum corners) and the
  285. * shadow occluder objects.
  286. *
  287. * @param occluders
  288. * @param lightCam
  289. * @param points
  290. */
  291. public static void updateShadowCamera(GeometryList occluders,
  292. GeometryList receivers,
  293. Camera shadowCam,
  294. Vector3f[] points) {
  295. updateShadowCamera(occluders, receivers, shadowCam, points, null);
  296. }
  297. /**
  298. * Updates the shadow camera to properly contain the given
  299. * points (which contain the eye camera frustum corners) and the
  300. * shadow occluder objects.
  301. *
  302. * @param occluders
  303. * @param lightCam
  304. * @param points
  305. */
  306. public static void updateShadowCamera(GeometryList occluders,
  307. GeometryList receivers,
  308. Camera shadowCam,
  309. Vector3f[] points,
  310. GeometryList splitOccluders) {
  311. boolean ortho = shadowCam.isParallelProjection();
  312. shadowCam.setProjectionMatrix(null);
  313. if (ortho) {
  314. shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
  315. } else {
  316. shadowCam.setFrustumPerspective(45, 1, 1, 150);
  317. }
  318. // create transform to rotate points to viewspace
  319. Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
  320. BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);
  321. ArrayList<BoundingVolume> visRecvList = new ArrayList<BoundingVolume>();
  322. for (int i = 0; i < receivers.size(); i++) {
  323. // convert bounding box to light's viewproj space
  324. Geometry receiver = receivers.get(i);
  325. BoundingVolume bv = receiver.getWorldBound();
  326. BoundingVolume recvBox = bv.transform(viewProjMatrix, null);
  327. if (splitBB.intersects(recvBox)) {
  328. visRecvList.add(recvBox);
  329. }
  330. }
  331. ArrayList<BoundingVolume> visOccList = new ArrayList<BoundingVolume>();
  332. for (int i = 0; i < occluders.size(); i++) {
  333. // convert bounding box to light's viewproj space
  334. Geometry occluder = occluders.get(i);
  335. BoundingVolume bv = occluder.getWorldBound();
  336. BoundingVolume occBox = bv.transform(viewProjMatrix, null);
  337. boolean intersects = splitBB.intersects(occBox);
  338. if (!intersects && occBox instanceof BoundingBox) {
  339. BoundingBox occBB = (BoundingBox) occBox;
  340. //Kirill 01/10/2011
  341. // Extend the occluder further into the frustum
  342. // This fixes shadow dissapearing issues when
  343. // the caster itself is not in the view camera
  344. // but its shadow is in the camera
  345. // The number is in world units
  346. occBB.setZExtent(occBB.getZExtent() + 50);
  347. occBB.setCenter(occBB.getCenter().addLocal(0, 0, 25));
  348. if (splitBB.intersects(occBB)) {
  349. // To prevent extending the depth range too much
  350. // We return the bound to its former shape
  351. // Before adding it
  352. occBB.setZExtent(occBB.getZExtent() - 50);
  353. occBB.setCenter(occBB.getCenter().subtractLocal(0, 0, 25));
  354. visOccList.add(occBox);
  355. if (splitOccluders != null) {
  356. splitOccluders.add(occluder);
  357. }
  358. }
  359. } else if (intersects) {
  360. visOccList.add(occBox);
  361. if (splitOccluders != null) {
  362. splitOccluders.add(occluder);
  363. }
  364. }
  365. }
  366. BoundingBox casterBB = computeUnionBound(visOccList);
  367. BoundingBox receiverBB = computeUnionBound(visRecvList);
  368. //Nehon 08/18/2010 this is to avoid shadow bleeding when the ground is set to only receive shadows
  369. if (visOccList.size() != visRecvList.size()) {
  370. casterBB.setXExtent(casterBB.getXExtent() + 2.0f);
  371. casterBB.setYExtent(casterBB.getYExtent() + 2.0f);
  372. casterBB.setZExtent(casterBB.getZExtent() + 2.0f);
  373. }
  374. Vector3f casterMin = casterBB.getMin(null);
  375. Vector3f casterMax = casterBB.getMax(null);
  376. Vector3f receiverMin = receiverBB.getMin(null);
  377. Vector3f receiverMax = receiverBB.getMax(null);
  378. Vector3f splitMin = splitBB.getMin(null);
  379. Vector3f splitMax = splitBB.getMax(null);
  380. splitMin.z = 0;
  381. if (!ortho) {
  382. shadowCam.setFrustumPerspective(45, 1, 1, splitMax.z);
  383. }
  384. Matrix4f projMatrix = shadowCam.getProjectionMatrix();
  385. Vector3f cropMin = new Vector3f();
  386. Vector3f cropMax = new Vector3f();
  387. // IMPORTANT: Special handling for Z values
  388. cropMin.x = max(max(casterMin.x, receiverMin.x), splitMin.x);
  389. cropMax.x = min(min(casterMax.x, receiverMax.x), splitMax.x);
  390. cropMin.y = max(max(casterMin.y, receiverMin.y), splitMin.y);
  391. cropMax.y = min(min(casterMax.y, receiverMax.y), splitMax.y);
  392. cropMin.z = min(casterMin.z, splitMin.z);
  393. cropMax.z = min(receiverMax.z, splitMax.z);
  394. // Create the crop matrix.
  395. float scaleX, scaleY, scaleZ;
  396. float offsetX, offsetY, offsetZ;
  397. scaleX = (2.0f) / (cropMax.x - cropMin.x);
  398. scaleY = (2.0f) / (cropMax.y - cropMin.y);
  399. offsetX = -0.5f * (cropMax.x + cropMin.x) * scaleX;
  400. offsetY = -0.5f * (cropMax.y + cropMin.y) * scaleY;
  401. scaleZ = 1.0f / (cropMax.z - cropMin.z);
  402. offsetZ = -cropMin.z * scaleZ;
  403. Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
  404. 0f, scaleY, 0f, offsetY,
  405. 0f, 0f, scaleZ, offsetZ,
  406. 0f, 0f, 0f, 1f);
  407. Matrix4f result = new Matrix4f();
  408. result.set(cropMatrix);
  409. result.multLocal(projMatrix);
  410. shadowCam.setProjectionMatrix(result);
  411. }
  412. }