|
|
|
@ -34,12 +34,12 @@ package com.jme3.material.logic; |
|
|
|
|
import com.jme3.asset.AssetManager; |
|
|
|
|
import com.jme3.light.DirectionalLight; |
|
|
|
|
import com.jme3.light.Light; |
|
|
|
|
import com.jme3.light.Light.Type; |
|
|
|
|
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; |
|
|
|
@ -72,6 +72,8 @@ public final class StaticPassLightingLogic extends DefaultTechniqueDefLogic { |
|
|
|
|
private final ArrayList<SpotLight> tempSpotLights = new ArrayList<SpotLight>(); |
|
|
|
|
|
|
|
|
|
private final ColorRGBA ambientLightColor = new ColorRGBA(0, 0, 0, 1); |
|
|
|
|
private final Vector3f tempPosition = new Vector3f(); |
|
|
|
|
private final Vector3f tempDirection = new Vector3f(); |
|
|
|
|
|
|
|
|
|
public StaticPassLightingLogic(TechniqueDef techniqueDef) { |
|
|
|
|
super(techniqueDef); |
|
|
|
@ -113,25 +115,41 @@ public final class StaticPassLightingLogic extends DefaultTechniqueDefLogic { |
|
|
|
|
return techniqueDef.getShader(assetManager, rendererCaps, defines); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void updateLightListUniforms(Shader shader, LightList lights) { |
|
|
|
|
private void transformDirection(Matrix4f viewMatrix, Vector3f direction) { |
|
|
|
|
viewMatrix.multNormal(direction, direction); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void transformPosition(Matrix4f viewMatrix, Vector3f location) { |
|
|
|
|
viewMatrix.mult(location, location); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void updateLightListUniforms(Matrix4f viewMatrix, Shader shader, LightList lights) { |
|
|
|
|
Uniform ambientColor = shader.getUniform("g_AmbientLightColor"); |
|
|
|
|
ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, true, 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 (DirectionalLight light : tempDirLights) { |
|
|
|
|
ColorRGBA color = light.getColor(); |
|
|
|
|
Vector3f dir = light.getDirection(); |
|
|
|
|
tempDirection.set(light.getDirection()); |
|
|
|
|
transformDirection(viewMatrix, tempDirection); |
|
|
|
|
lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++); |
|
|
|
|
lightData.setVector4InArray(dir.x, dir.y, dir.z, 1f, index++); |
|
|
|
|
lightData.setVector4InArray(tempDirection.x, tempDirection.y, tempDirection.z, 1f, index++); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (PointLight light : tempPointLights) { |
|
|
|
|
ColorRGBA color = light.getColor(); |
|
|
|
|
Vector3f pos = light.getPosition(); |
|
|
|
|
tempPosition.set(light.getPosition()); |
|
|
|
|
float invRadius = light.getInvRadius(); |
|
|
|
|
transformPosition(viewMatrix, tempPosition); |
|
|
|
|
lightData.setVector4InArray(color.r, color.g, color.b, 1f, index++); |
|
|
|
|
lightData.setVector4InArray(pos.x, pos.y, pos.z, 1f, index++); |
|
|
|
|
lightData.setVector4InArray(tempPosition.x, tempPosition.y, tempPosition.z, invRadius, index++); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (SpotLight light : tempSpotLights) { |
|
|
|
@ -149,7 +167,8 @@ public final class StaticPassLightingLogic extends DefaultTechniqueDefLogic { |
|
|
|
|
@Override |
|
|
|
|
public void render(RenderManager renderManager, Shader shader, Geometry geometry, LightList lights) { |
|
|
|
|
Renderer renderer = renderManager.getRenderer(); |
|
|
|
|
updateLightListUniforms(shader, lights); |
|
|
|
|
Matrix4f viewMatrix = renderManager.getCurrentCamera().getViewMatrix(); |
|
|
|
|
updateLightListUniforms(viewMatrix, shader, lights); |
|
|
|
|
renderer.setShader(shader); |
|
|
|
|
renderMeshFromGeometry(renderer, geometry); |
|
|
|
|
} |
|
|
|
|