|
@@ -1,191 +0,0 @@
|
|
|
-/*
|
|
|
- * Copyright (c) 2009-2015 jMonkeyEngine
|
|
|
- * All rights reserved.
|
|
|
- *
|
|
|
- * Redistribution and use in source and binary forms, with or without
|
|
|
- * modification, are permitted provided that the following conditions are
|
|
|
- * met:
|
|
|
- *
|
|
|
- * * Redistributions of source code must retain the above copyright
|
|
|
- * notice, this list of conditions and the following disclaimer.
|
|
|
- *
|
|
|
- * * Redistributions in binary form must reproduce the above copyright
|
|
|
- * notice, this list of conditions and the following disclaimer in the
|
|
|
- * documentation and/or other materials provided with the distribution.
|
|
|
- *
|
|
|
- * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
|
|
- * may be used to endorse or promote products derived from this software
|
|
|
- * without specific prior written permission.
|
|
|
- *
|
|
|
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
- */
|
|
|
-package com.jme3.material.logic;
|
|
|
-
|
|
|
-import com.jme3.asset.AssetManager;
|
|
|
-import com.jme3.light.DirectionalLight;
|
|
|
-import com.jme3.light.Light;
|
|
|
-import com.jme3.light.LightList;
|
|
|
-import com.jme3.light.PointLight;
|
|
|
-import com.jme3.light.SpotLight;
|
|
|
-import com.jme3.material.TechniqueDef;
|
|
|
-import com.jme3.math.ColorRGBA;
|
|
|
-import com.jme3.math.Matrix4f;
|
|
|
-import com.jme3.math.Vector3f;
|
|
|
-import com.jme3.renderer.Caps;
|
|
|
-import com.jme3.renderer.RenderManager;
|
|
|
-import com.jme3.renderer.Renderer;
|
|
|
-import com.jme3.scene.Geometry;
|
|
|
-import com.jme3.shader.DefineList;
|
|
|
-import com.jme3.shader.Shader;
|
|
|
-import com.jme3.shader.Uniform;
|
|
|
-import com.jme3.shader.VarType;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.EnumSet;
|
|
|
-
|
|
|
-/**
|
|
|
- * Rendering logic for static pass.
|
|
|
- *
|
|
|
- * @author Kirill Vainer
|
|
|
- */
|
|
|
-public class StaticPassLightingLogic extends DefaultTechniqueDefLogic {
|
|
|
-
|
|
|
- protected static final String DEFINE_NUM_DIR_LIGHTS = "NUM_DIR_LIGHTS";
|
|
|
- protected static final String DEFINE_NUM_POINT_LIGHTS = "NUM_POINT_LIGHTS";
|
|
|
- protected static final String DEFINE_NUM_SPOT_LIGHTS = "NUM_SPOT_LIGHTS";
|
|
|
-
|
|
|
- protected final int numDirLightsDefineId;
|
|
|
- protected final int numPointLightsDefineId;
|
|
|
- protected final int numSpotLightsDefineId;
|
|
|
-
|
|
|
- protected final ColorRGBA ambientLightColor = new ColorRGBA(0, 0, 0, 1);
|
|
|
- protected final Vector3f tempPosition = new Vector3f();
|
|
|
- protected final Vector3f tempDirection = new Vector3f();
|
|
|
-
|
|
|
- protected final ArrayList<DirectionalLight> tempDirLights = new ArrayList<>();
|
|
|
- protected final ArrayList<PointLight> tempPointLights = new ArrayList<>();
|
|
|
- protected final ArrayList<SpotLight> tempSpotLights = new ArrayList<>();
|
|
|
-
|
|
|
- public StaticPassLightingLogic(TechniqueDef techniqueDef) {
|
|
|
- super(techniqueDef);
|
|
|
-
|
|
|
- numDirLightsDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NUM_DIR_LIGHTS, VarType.Int);
|
|
|
- numPointLightsDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NUM_POINT_LIGHTS, VarType.Int);
|
|
|
- numSpotLightsDefineId = techniqueDef.addShaderUnmappedDefine(DEFINE_NUM_SPOT_LIGHTS, VarType.Int);
|
|
|
- }
|
|
|
-
|
|
|
- protected void makeCurrentBase(AssetManager assetManager, RenderManager renderManager,
|
|
|
- EnumSet<Caps> rendererCaps, LightList lights, DefineList defines) {
|
|
|
-
|
|
|
- // TODO: if it ever changes that render isn't called
|
|
|
- // right away with the same geometry after makeCurrent, it would be
|
|
|
- // a problem.
|
|
|
- // Do a radix sort.
|
|
|
- tempDirLights.clear();
|
|
|
- tempPointLights.clear();
|
|
|
- tempSpotLights.clear();
|
|
|
- ambientLightColor.set(0, 0, 0, 1);
|
|
|
-
|
|
|
- for (Light light : lights) {
|
|
|
- switch (light.getType()) {
|
|
|
- case Directional:
|
|
|
- tempDirLights.add((DirectionalLight) light);
|
|
|
- break;
|
|
|
- case Point:
|
|
|
- tempPointLights.add((PointLight) light);
|
|
|
- break;
|
|
|
- case Spot:
|
|
|
- tempSpotLights.add((SpotLight) light);
|
|
|
- break;
|
|
|
- case Ambient:
|
|
|
- ambientLightColor.addLocal(light.getColor());
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- ambientLightColor.a = 1.0f;
|
|
|
-
|
|
|
- defines.set(numDirLightsDefineId, tempDirLights.size());
|
|
|
- defines.set(numPointLightsDefineId, tempPointLights.size());
|
|
|
- defines.set(numSpotLightsDefineId, tempSpotLights.size());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Shader makeCurrent(AssetManager assetManager, RenderManager renderManager,
|
|
|
- EnumSet<Caps> rendererCaps, Geometry geometry, DefineList defines) {
|
|
|
- LightList lights = getFilteredLightList(renderManager, geometry);
|
|
|
- makeCurrentBase(assetManager, renderManager, rendererCaps, lights, defines);
|
|
|
- return techniqueDef.getShader(assetManager, rendererCaps, defines);
|
|
|
- }
|
|
|
-
|
|
|
- protected void updateLightListUniforms(Matrix4f viewMatrix, Shader shader) {
|
|
|
- Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
|
|
|
- ambientColor.setValue(VarType.Vector4, ambientLightColor);
|
|
|
-
|
|
|
- Uniform lightData = shader.getUniform("g_LightData");
|
|
|
-
|
|
|
- int totalSize = tempDirLights.size() * 2
|
|
|
- + tempPointLights.size() * 2
|
|
|
- + tempSpotLights.size() * 3;
|
|
|
- lightData.setVector4Length(totalSize);
|
|
|
-
|
|
|
- int index = 0;
|
|
|
-
|
|
|
- for (SpotLight light : tempSpotLights) {
|
|
|
- ColorRGBA color = light.getColor();
|
|
|
- float lightTypeAndShadowMap = encodeLightTypeAndShadowMapIndex(light);
|
|
|
- lightData.setVector4InArray(color.r, color.g, color.b, lightTypeAndShadowMap, index++);
|
|
|
-
|
|
|
- tempPosition.set(light.getPosition());
|
|
|
- float invRange = light.getInvSpotRange();
|
|
|
- lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRange, index++);
|
|
|
-
|
|
|
- tempDirection.set(light.getDirection());
|
|
|
- float spotAngleCos = light.getPackedAngleCos();
|
|
|
- lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, spotAngleCos, index++);
|
|
|
- }
|
|
|
-
|
|
|
- for (DirectionalLight light : tempDirLights) {
|
|
|
- ColorRGBA color = light.getColor();
|
|
|
- float lightTypeAndShadowMap = encodeLightTypeAndShadowMapIndex(light);
|
|
|
- lightData.setVector4InArray(color.r, color.g, color.b, lightTypeAndShadowMap, index++);
|
|
|
-
|
|
|
- tempDirection.set(light.getDirection());
|
|
|
- lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, 1f, index++);
|
|
|
- }
|
|
|
-
|
|
|
- for (PointLight light : tempPointLights) {
|
|
|
- ColorRGBA color = light.getColor();
|
|
|
- float lightTypeAndShadowMap = encodeLightTypeAndShadowMapIndex(light);
|
|
|
- lightData.setVector4InArray(color.r, color.g, color.b, lightTypeAndShadowMap, index++);
|
|
|
-
|
|
|
- tempPosition.set(light.getPosition());
|
|
|
- float invRadius = light.getInvRadius();
|
|
|
- lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRadius, index++);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- protected void updateShadowUniforms(Renderer renderer, Shader shader, int nextTextureUnit) {
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void render(RenderManager renderManager, Shader shader, Geometry geometry, int nextTextureUnit) {
|
|
|
- Renderer renderer = renderManager.getRenderer();
|
|
|
- Matrix4f viewMatrix = renderManager.getCurrentCamera().getViewMatrix();
|
|
|
- updateLightListUniforms(viewMatrix, shader);
|
|
|
- updateShadowUniforms(renderer, shader, nextTextureUnit);
|
|
|
- renderer.setShader(shader);
|
|
|
- renderMeshFromGeometry(renderer, geometry);
|
|
|
- }
|
|
|
-
|
|
|
-}
|