Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jme3.asset.AssetManager;
- import com.jme3.export.InputCapsule;
- import com.jme3.export.JmeExporter;
- import com.jme3.export.JmeImporter;
- import com.jme3.export.OutputCapsule;
- import com.jme3.light.Light;
- import com.jme3.light.LightList;
- import com.jme3.light.PointLight;
- import com.jme3.material.MatParam;
- import com.jme3.material.Material;
- import com.jme3.material.Technique;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.Camera;
- import com.jme3.renderer.Caps;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.renderer.queue.GeometryList;
- import com.jme3.renderer.queue.RenderQueue.ShadowMode;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.shader.Shader;
- import com.jme3.shader.Uniform;
- import com.jme3.shader.VarType;
- import com.jme3.shadow.AbstractShadowRenderer;
- import com.jme3.shadow.CompareMode;
- import com.jme3.shadow.PointLightShadowRenderer;
- import com.jme3.shadow.ShadowUtil;
- import com.jme3.texture.FrameBuffer;
- import java.io.IOException;
- import java.util.ArrayList;
- public class MultiPointLightShadowRenderer extends MyAbstractShadowRenderer
- {
- public static final int CAM_NUMBER = 6;
- protected PointLight light;
- protected Camera[] shadowCams;
- private Geometry[] frustums = null;
- private Node _lightNode;
- /**
- * Used for serialization use
- * PointLightShadowRenderer"PointLightShadowRenderer(AssetManager
- * assetManager, int shadowMapSize)
- */
- public MultiPointLightShadowRenderer()
- {
- super();
- //_lights = new ArrayList<PointLight>();
- }
- /**
- * Creates a PointLightShadowRenderer
- *
- * @param assetManager the application asset manager
- * @param shadowMapSize the size of the rendered shadowmaps (512,1024,2048,
- * etc...)
- */
- public MultiPointLightShadowRenderer(AssetManager assetManager, int shadowMapSize, Node lightNode)
- {
- super(assetManager, shadowMapSize, CAM_NUMBER);
- //_lights = new ArrayList<PointLight>();
- //Material mat = new Material(assetManager, "MatDefs/Shadow/PostShadowMS.j3md");
- //setPostShadowMaterial(mat);
- _lightNode = lightNode;
- init(shadowMapSize);
- }
- private void init(int shadowMapSize)
- {
- shadowCams = new Camera[CAM_NUMBER];
- for (int i = 0; i < CAM_NUMBER; i++) {
- shadowCams[i] = new Camera(shadowMapSize, shadowMapSize);
- }
- }
- @Override
- protected void updateShadowCams(Camera viewCam) {
- if (light == null)
- {
- throw new IllegalStateException("The light can't be null for a " + this.getClass().getName());
- }
- //bottom
- shadowCams[0].setAxes(Vector3f.UNIT_X.mult(-1f), Vector3f.UNIT_Z.mult(-1f), Vector3f.UNIT_Y.mult(-1f));
- //top
- shadowCams[1].setAxes(Vector3f.UNIT_X.mult(-1f), Vector3f.UNIT_Z, Vector3f.UNIT_Y);
- //forward
- shadowCams[2].setAxes(Vector3f.UNIT_X.mult(-1f), Vector3f.UNIT_Y, Vector3f.UNIT_Z.mult(-1f));
- //backward
- shadowCams[3].setAxes(Vector3f.UNIT_X, Vector3f.UNIT_Y, Vector3f.UNIT_Z);
- //left
- shadowCams[4].setAxes(Vector3f.UNIT_Z, Vector3f.UNIT_Y, Vector3f.UNIT_X.mult(-1f));
- //right
- shadowCams[5].setAxes(Vector3f.UNIT_Z.mult(-1f), Vector3f.UNIT_Y, Vector3f.UNIT_X);
- for (int i = 0; i < CAM_NUMBER; i++)
- {
- shadowCams[i].setFrustumPerspective(90f, 1f, 0.1f, light.getRadius());
- shadowCams[i].setLocation(light.getPosition());
- shadowCams[i].update();
- shadowCams[i].updateViewProjection();
- }
- }
- @Override
- protected GeometryList getOccludersToRender(int shadowMapIndex, GeometryList sceneOccluders, GeometryList sceneReceivers, GeometryList shadowMapOccluders) {
- ShadowUtil.getGeometriesInCamFrustum(sceneOccluders, shadowCams[shadowMapIndex], shadowMapOccluders);
- return shadowMapOccluders;
- }
- @Override
- GeometryList getReceivers(GeometryList sceneReceivers, GeometryList lightReceivers) {
- lightReceivers.clear();
- ShadowUtil.getGeometriesInLightRadius(sceneReceivers, shadowCams, lightReceivers);
- return lightReceivers;
- }
- @Override
- protected Camera getShadowCam(int shadowMapIndex) {
- return shadowCams[shadowMapIndex];
- }
- @Override
- protected void doDisplayFrustumDebug(int shadowMapIndex) {
- if (frustums == null) {
- frustums = new Geometry[CAM_NUMBER];
- Vector3f[] points = new Vector3f[8];
- for (int i = 0; i < 8; i++) {
- points[i] = new Vector3f();
- }
- for (int i = 0; i < CAM_NUMBER; i++) {
- ShadowUtil.updateFrustumPoints2(shadowCams[i], points);
- frustums[i] = createFrustum(points, i);
- }
- }
- if (frustums[shadowMapIndex].getParent() == null) {
- ((Node) viewPort.getScenes().get(0)).attachChild(frustums[shadowMapIndex]);
- }
- }
- @Override
- protected void setMaterialParameters(Material material)
- {
- material.setVector3("LightPos", light.getPosition());
- updatePostShadowMatParams(material);
- }
- protected void updatePostShadowMatParams(Material material)
- {
- Technique technique = material.getActiveTechnique();
- if (technique == null)
- return;
- LightList list = _lightNode.getLocalLightList(); // Node with lights.
- ArrayList<PointLight> arr = new ArrayList<PointLight>(list.size());
- Light l;
- float dist = 0;
- for (int i = 0; i < list.size(); i++)
- {
- l = list.get(i);
- if (l == light) continue;
- if (!(l instanceof PointLight)) continue;
- PointLight p = (PointLight)l;
- dist = p.getPosition().distance(light.getPosition());
- if (dist > p.getRadius() * 2) continue;
- arr.add(p);
- }
- if (arr.size() == 0) return;
- Uniform lightPos = null;
- Vector3f pos = null;
- PointLight pl = null;
- try
- {
- material.getMaterialDef().addMaterialParam(VarType.Int, "LightCount", arr.size(), null);
- material.setInt("LightCount", arr.size());
- material.getMaterialDef().addMaterialParam(VarType.Float, "LightRadius", light.getRadius(), null);
- material.setFloat("LightRadius", light.getRadius());
- //material.getMaterialDef().addMaterialParam(VarType.Vector3Array, "LightsPositions", null, null);
- Shader shader = technique.getShader();
- lightPos = shader.getUniform("g_LightsPositions");
- lightPos.setVector4Length(arr.size());
- //if (lightPos.getValue() == null) return;
- for (int i = 0; i < arr.size(); i++)
- {
- pl = arr.get(i);
- pos = pl.getPosition();
- float radius = pl.getInvRadius();
- lightPos.setVector4InArray(pos.getX(), pos.getY(), pos.getZ(), radius, i);
- }
- //Uniform lightPos = shader.getUniform("g_LightPosition");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- * gets the point light used to cast shadows with this processor
- *
- * @return the point light
- */
- public PointLight getLight() {
- return light;
- }
- /**
- * sets the light to use for casting shadows with this processor
- *
- * @param light the point light
- */
- public void setLight(PointLight light) {
- this.light = light;
- }
- @Override
- public void read(JmeImporter im) throws IOException {
- super.read(im);
- InputCapsule ic = (InputCapsule) im.getCapsule(this);
- light = (PointLight) ic.readSavable("light", null);
- init((int) shadowMapSize);
- }
- @Override
- public void write(JmeExporter ex) throws IOException {
- super.write(ex);
- OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
- oc.write(light, "light", null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement