SpotLightShadowRenderer.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2009-2012 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.asset.AssetManager;
  34. import com.jme3.export.InputCapsule;
  35. import com.jme3.export.JmeExporter;
  36. import com.jme3.export.JmeImporter;
  37. import com.jme3.export.OutputCapsule;
  38. import com.jme3.light.SpotLight;
  39. import com.jme3.material.Material;
  40. import com.jme3.math.FastMath;
  41. import com.jme3.math.Vector2f;
  42. import com.jme3.math.Vector3f;
  43. import com.jme3.renderer.Camera;
  44. import com.jme3.renderer.queue.GeometryList;
  45. import com.jme3.scene.Node;
  46. import java.io.IOException;
  47. /**
  48. * SpotLightShadowRenderer renderer use Parrallel Split Shadow Mapping technique
  49. * (pssm)<br> It splits the view frustum in several parts and compute a shadow
  50. * map for each one.<br> splits are distributed so that the closer they are from
  51. * the camera, the smaller they are to maximize the resolution used of the
  52. * shadow map.<br> This result in a better quality shadow than standard shadow
  53. * mapping.<br> for more informations on this read this <a
  54. * href="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html">http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html</a><br>
  55. * <p/>
  56. * @author Rémy Bouquet aka Nehon
  57. */
  58. public class SpotLightShadowRenderer extends AbstractShadowRenderer {
  59. protected float zFarOverride = 0;
  60. protected Camera shadowCam;
  61. protected SpotLight light;
  62. protected Vector3f[] points = new Vector3f[8];
  63. //Holding the info for fading shadows in the far distance
  64. protected Vector2f fadeInfo;
  65. protected float fadeLength;
  66. /**
  67. * Used for serialization use SpotLightShadowRenderer#SpotLightShadowRenderer(AssetManager assetManager, int shadowMapSize)
  68. */
  69. public SpotLightShadowRenderer() {
  70. super();
  71. }
  72. /**
  73. * Create a SpotLightShadowRenderer This use standard shadow mapping
  74. *
  75. * @param assetManager the application asset manager
  76. * @param shadowMapSize the size of the rendered shadowmaps (512,1024,2048,
  77. * etc...) the more quality, the less fps).
  78. */
  79. public SpotLightShadowRenderer(AssetManager assetManager, int shadowMapSize) {
  80. super(assetManager, shadowMapSize, 1);
  81. init(shadowMapSize);
  82. }
  83. private void init(int shadowMapSize) {
  84. shadowCam = new Camera(shadowMapSize, shadowMapSize);
  85. for (int i = 0; i < points.length; i++) {
  86. points[i] = new Vector3f();
  87. }
  88. }
  89. /**
  90. * return the light used to cast shadows
  91. *
  92. * @return the SpotLight
  93. */
  94. public SpotLight getLight() {
  95. return light;
  96. }
  97. /**
  98. * Sets the light to use to cast shadows
  99. *
  100. * @param light a SpotLight
  101. */
  102. public void setLight(SpotLight light) {
  103. this.light = light;
  104. }
  105. @Override
  106. protected void updateShadowCams(Camera viewCam) {
  107. float zFar = zFarOverride;
  108. if (zFar == 0) {
  109. zFar = viewCam.getFrustumFar();
  110. }
  111. //We prevent computing the frustum points and splits with zeroed or negative near clip value
  112. float frustumNear = Math.max(viewCam.getFrustumNear(), 0.001f);
  113. ShadowUtil.updateFrustumPoints(viewCam, frustumNear, zFar, 1.0f, points);
  114. //shadowCam.setDirection(direction);
  115. shadowCam.setFrustumPerspective(light.getSpotOuterAngle() * FastMath.RAD_TO_DEG * 2.0f, 1, 1f, light.getSpotRange());
  116. shadowCam.getRotation().lookAt(light.getDirection(), shadowCam.getUp());
  117. shadowCam.setLocation(light.getPosition());
  118. shadowCam.update();
  119. shadowCam.updateViewProjection();
  120. }
  121. @Override
  122. protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
  123. ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCam, shadowMapOccluders);
  124. return shadowMapOccluders;
  125. }
  126. @Override
  127. GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
  128. lightReceivers.clear();
  129. ShadowUtil.getGeometriesInCamFrustum(sceneReceivers, shadowCam, lightReceivers);
  130. return lightReceivers;
  131. }
  132. @Override
  133. protected Camera getShadowCam(int shadowMapIndex) {
  134. return shadowCam;
  135. }
  136. @Override
  137. protected void doDisplayFrustumDebug(int shadowMapIndex) {
  138. Vector3f[] points2 = points.clone();
  139. ((Node) viewPort.getScenes().get(0)).attachChild(createFrustum(points, shadowMapIndex));
  140. ShadowUtil.updateFrustumPoints2(shadowCam, points2);
  141. ((Node) viewPort.getScenes().get(0)).attachChild(createFrustum(points2, shadowMapIndex));
  142. }
  143. @Override
  144. protected void setMaterialParameters(Material material) {
  145. }
  146. /**
  147. * How far the shadows are rendered in the view
  148. *
  149. * @see #setShadowZExtend(float zFar)
  150. * @return shadowZExtend
  151. */
  152. public float getShadowZExtend() {
  153. return zFarOverride;
  154. }
  155. /**
  156. * Set the distance from the eye where the shadows will be rendered default
  157. * value is dynamicaly computed to the shadow casters/receivers union bound
  158. * zFar, capped to view frustum far value.
  159. *
  160. * @param zFar the zFar values that override the computed one
  161. */
  162. public void setShadowZExtend(float zFar) {
  163. if (fadeInfo != null) {
  164. fadeInfo.set(zFar - fadeLength, 1f / fadeLength);
  165. }
  166. this.zFarOverride = zFar;
  167. }
  168. /**
  169. * Define the length over which the shadow will fade out when using a
  170. * shadowZextend This is useful to make dynamic shadows fade into baked
  171. * shadows in the distance.
  172. *
  173. * @param length the fade length in world units
  174. */
  175. public void setShadowZFadeLength(float length) {
  176. if (length == 0) {
  177. fadeInfo = null;
  178. fadeLength = 0;
  179. postshadowMat.clearParam("FadeInfo");
  180. } else {
  181. if (zFarOverride == 0) {
  182. fadeInfo = new Vector2f(0, 0);
  183. } else {
  184. fadeInfo = new Vector2f(zFarOverride - length, 1.0f / length);
  185. }
  186. fadeLength = length;
  187. postshadowMat.setVector2("FadeInfo", fadeInfo);
  188. }
  189. }
  190. /**
  191. * get the length over which the shadow will fade out when using a
  192. * shadowZextend
  193. *
  194. * @return the fade length in world units
  195. */
  196. public float getShadowZFadeLength() {
  197. if (fadeInfo != null) {
  198. return zFarOverride - fadeInfo.x;
  199. }
  200. return 0f;
  201. }
  202. @Override
  203. public void read(JmeImporter im) throws IOException {
  204. super.read(im);
  205. InputCapsule ic = (InputCapsule) im.getCapsule(this);
  206. zFarOverride = ic.readInt("zFarOverride", 0);
  207. light = (SpotLight) ic.readSavable("light", null);
  208. fadeInfo = (Vector2f) ic.readSavable("fadeInfo", null);
  209. fadeLength = ic.readFloat("fadeLength", 0f);
  210. init((int) shadowMapSize);
  211. }
  212. @Override
  213. public void write(JmeExporter ex) throws IOException {
  214. super.write(ex);
  215. OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
  216. oc.write(zFarOverride, "zFarOverride", 0);
  217. oc.write(light, "light", null);
  218. oc.write(fadeInfo, "fadeInfo", null);
  219. oc.write(fadeLength, "fadeLength", 0f);
  220. }
  221. }