Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.29 KB | None | 0 0
  1. import com.jme3.asset.AssetManager;
  2. import com.jme3.export.InputCapsule;
  3. import com.jme3.export.JmeExporter;
  4. import com.jme3.export.JmeImporter;
  5. import com.jme3.export.OutputCapsule;
  6. import com.jme3.light.Light;
  7. import com.jme3.light.LightList;
  8. import com.jme3.light.PointLight;
  9. import com.jme3.material.MatParam;
  10. import com.jme3.material.Material;
  11. import com.jme3.material.Technique;
  12. import com.jme3.math.Vector2f;
  13. import com.jme3.math.Vector3f;
  14. import com.jme3.renderer.Camera;
  15. import com.jme3.renderer.Caps;
  16. import com.jme3.renderer.RenderManager;
  17. import com.jme3.renderer.ViewPort;
  18. import com.jme3.renderer.queue.GeometryList;
  19. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  20. import com.jme3.scene.Geometry;
  21. import com.jme3.scene.Node;
  22. import com.jme3.shader.Shader;
  23. import com.jme3.shader.Uniform;
  24. import com.jme3.shader.VarType;
  25. import com.jme3.shadow.AbstractShadowRenderer;
  26. import com.jme3.shadow.CompareMode;
  27. import com.jme3.shadow.PointLightShadowRenderer;
  28. import com.jme3.shadow.ShadowUtil;
  29. import com.jme3.texture.FrameBuffer;
  30.  
  31. import java.io.IOException;
  32. import java.util.ArrayList;
  33.  
  34.  
  35. public class MultiPointLightShadowRenderer extends MyAbstractShadowRenderer
  36. {
  37.  
  38.     public static final int CAM_NUMBER = 6;
  39.     protected PointLight light;
  40.     protected Camera[] shadowCams;
  41.     private Geometry[] frustums = null;
  42.    
  43.     private Node _lightNode;
  44.  
  45.     /**
  46.      * Used for serialization use
  47.      * PointLightShadowRenderer"PointLightShadowRenderer(AssetManager
  48.      * assetManager, int shadowMapSize)
  49.      */
  50.     public MultiPointLightShadowRenderer()
  51.     {
  52.         super();
  53.         //_lights = new ArrayList<PointLight>();
  54.     }
  55.  
  56.     /**
  57.      * Creates a PointLightShadowRenderer
  58.      *
  59.      * @param assetManager the application asset manager
  60.      * @param shadowMapSize the size of the rendered shadowmaps (512,1024,2048,
  61.      * etc...)
  62.      */
  63.     public MultiPointLightShadowRenderer(AssetManager assetManager, int shadowMapSize, Node lightNode)
  64.     {
  65.         super(assetManager, shadowMapSize, CAM_NUMBER);
  66.        
  67.         //_lights = new ArrayList<PointLight>();
  68.         //Material mat = new Material(assetManager, "MatDefs/Shadow/PostShadowMS.j3md");
  69.         //setPostShadowMaterial(mat);
  70.        
  71.         _lightNode = lightNode;
  72.        
  73.         init(shadowMapSize);
  74.        
  75.        
  76.     }
  77.  
  78.     private void init(int shadowMapSize)
  79.     {
  80.         shadowCams = new Camera[CAM_NUMBER];
  81.         for (int i = 0; i < CAM_NUMBER; i++) {
  82.             shadowCams[i] = new Camera(shadowMapSize, shadowMapSize);
  83.         }
  84.     }
  85.  
  86.     @Override
  87.     protected void updateShadowCams(Camera viewCam) {
  88.  
  89.         if (light == null)
  90.         {
  91.             throw new IllegalStateException("The light can't be null for a " + this.getClass().getName());
  92.         }
  93.  
  94.         //bottom
  95.         shadowCams[0].setAxes(Vector3f.UNIT_X.mult(-1f), Vector3f.UNIT_Z.mult(-1f), Vector3f.UNIT_Y.mult(-1f));
  96.  
  97.         //top
  98.         shadowCams[1].setAxes(Vector3f.UNIT_X.mult(-1f), Vector3f.UNIT_Z, Vector3f.UNIT_Y);
  99.  
  100.         //forward
  101.         shadowCams[2].setAxes(Vector3f.UNIT_X.mult(-1f), Vector3f.UNIT_Y, Vector3f.UNIT_Z.mult(-1f));
  102.  
  103.         //backward
  104.         shadowCams[3].setAxes(Vector3f.UNIT_X, Vector3f.UNIT_Y, Vector3f.UNIT_Z);
  105.  
  106.         //left
  107.         shadowCams[4].setAxes(Vector3f.UNIT_Z, Vector3f.UNIT_Y, Vector3f.UNIT_X.mult(-1f));
  108.  
  109.         //right
  110.         shadowCams[5].setAxes(Vector3f.UNIT_Z.mult(-1f), Vector3f.UNIT_Y, Vector3f.UNIT_X);
  111.  
  112.         for (int i = 0; i < CAM_NUMBER; i++)
  113.         {
  114.             shadowCams[i].setFrustumPerspective(90f, 1f, 0.1f, light.getRadius());
  115.             shadowCams[i].setLocation(light.getPosition());
  116.             shadowCams[i].update();
  117.             shadowCams[i].updateViewProjection();
  118.         }
  119.  
  120.     }
  121.  
  122.     @Override
  123.     protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
  124.         ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCams[shadowMapIndex], shadowMapOccluders);
  125.         return shadowMapOccluders;
  126.     }
  127.  
  128.     @Override
  129.     GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
  130.         lightReceivers.clear();
  131.         ShadowUtil.getGeometriesInLightRadius(sceneReceivers, shadowCams, lightReceivers);
  132.         return lightReceivers;
  133.     }
  134.  
  135.     @Override
  136.     protected Camera getShadowCam(int shadowMapIndex) {
  137.         return shadowCams[shadowMapIndex];
  138.     }
  139.  
  140.     @Override
  141.     protected void doDisplayFrustumDebug(int shadowMapIndex) {
  142.         if (frustums == null) {
  143.             frustums = new Geometry[CAM_NUMBER];
  144.             Vector3f[] points = new Vector3f[8];
  145.             for (int i = 0; i < 8; i++) {
  146.                 points[i] = new Vector3f();
  147.             }
  148.             for (int i = 0; i < CAM_NUMBER; i++) {
  149.                 ShadowUtil.updateFrustumPoints2(shadowCams[i], points);
  150.                 frustums[i] = createFrustum(points, i);
  151.             }
  152.         }
  153.         if (frustums[shadowMapIndex].getParent() == null) {
  154.             ((Node) viewPort.getScenes().get(0)).attachChild(frustums[shadowMapIndex]);
  155.         }
  156.     }
  157.  
  158.     @Override
  159.     protected void setMaterialParameters(Material material)
  160.     {
  161.         material.setVector3("LightPos", light.getPosition());
  162.        
  163.         updatePostShadowMatParams(material);
  164.     }
  165.    
  166.     protected void updatePostShadowMatParams(Material material)
  167.     {
  168.         Technique technique = material.getActiveTechnique();
  169.         if (technique == null)
  170.             return;
  171.        
  172.         LightList list = _lightNode.getLocalLightList(); // Node with lights.
  173.         ArrayList<PointLight> arr = new ArrayList<PointLight>(list.size());
  174.        
  175.         Light l;
  176.         float dist = 0;
  177.        
  178.         for (int i = 0; i < list.size(); i++)
  179.         {
  180.             l = list.get(i);
  181.             if (l == light) continue;
  182.             if (!(l instanceof PointLight)) continue;
  183.            
  184.             PointLight p = (PointLight)l;
  185.            
  186.             dist = p.getPosition().distance(light.getPosition());
  187.             if (dist > p.getRadius() * 2) continue;
  188.            
  189.             arr.add(p);
  190.         }
  191.        
  192.         if (arr.size() == 0) return;
  193.        
  194.         Uniform lightPos = null;
  195.         Vector3f pos = null;
  196.         PointLight pl = null;
  197.        
  198.         try
  199.         {
  200.             material.getMaterialDef().addMaterialParam(VarType.Int, "LightCount", arr.size(), null);
  201.             material.setInt("LightCount", arr.size());
  202.             material.getMaterialDef().addMaterialParam(VarType.Float, "LightRadius", light.getRadius(), null);
  203.             material.setFloat("LightRadius", light.getRadius());
  204.             //material.getMaterialDef().addMaterialParam(VarType.Vector3Array, "LightsPositions", null, null);
  205.            
  206.  
  207.             Shader shader = technique.getShader();
  208.            
  209.             lightPos = shader.getUniform("g_LightsPositions");
  210.             lightPos.setVector4Length(arr.size());
  211.             //if (lightPos.getValue() == null) return;
  212.            
  213.             for (int i = 0; i < arr.size(); i++)
  214.             {
  215.                 pl = arr.get(i);
  216.  
  217.                 pos = pl.getPosition();
  218.                 float radius = pl.getInvRadius();
  219.                 lightPos.setVector4InArray(pos.getX(), pos.getY(), pos.getZ(), radius, i);
  220.                
  221.             }  
  222.            
  223.             //Uniform lightPos = shader.getUniform("g_LightPosition");
  224.            
  225.         }
  226.         catch (Exception e)
  227.         {
  228.             e.printStackTrace();
  229.         }
  230.     }
  231.  
  232.     /**
  233.      * gets the point light used to cast shadows with this processor
  234.      *
  235.      * @return the point light
  236.      */
  237.     public PointLight getLight() {
  238.         return light;
  239.     }
  240.  
  241.     /**
  242.      * sets the light to use for casting shadows with this processor
  243.      *
  244.      * @param light the point light
  245.      */
  246.     public void setLight(PointLight light) {
  247.         this.light = light;
  248.     }
  249.  
  250.     @Override
  251.     public void read(JmeImporter im) throws IOException {
  252.         super.read(im);
  253.         InputCapsule ic = (InputCapsule) im.getCapsule(this);
  254.         light = (PointLight) ic.readSavable("light", null);
  255.         init((int) shadowMapSize);
  256.     }
  257.  
  258.     @Override
  259.     public void write(JmeExporter ex) throws IOException {
  260.         super.write(ex);
  261.         OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
  262.         oc.write(light, "light", null);
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement