use of com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData in project gdx-graph by MarcinSc.
the class ShadowShaderRendererPipelineNodeProducer method createNodeForSingleInputs.
@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
final ObjectMap<String, GraphShader> shaders = new ObjectMap<>();
final Array<String> allShaderTags = new Array<>();
final JsonValue shaderDefinitions = data.get("shaders");
RenderOrder renderOrder = RenderOrder.valueOf(data.getString("renderOrder", "Shader_Unordered"));
final ModelRenderingStrategy renderingStrategy = createRenderingStrategy(renderOrder);
final String environmentId = data.getString("id", "");
final RenderingStrategyCallback depthStrategyCallback = new RenderingStrategyCallback(shaderContext, new Function<String, GraphShader>() {
@Override
public GraphShader apply(String s) {
return shaders.get(s);
}
});
final Array<RenderPipelineBuffer> createdPipelineBuffers = new Array<>();
final Array<Directional3DLight> shadowDirectionalLights = new Array<>();
final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
final DefaultFieldOutput<RenderPipeline> output = new DefaultFieldOutput<>(PipelineFieldType.RenderPipeline);
result.put("output", output);
return new SingleInputsPipelineNode(result) {
private Lighting3DPrivateData lighting;
private TimeProvider timeProvider;
private GraphModelsImpl models;
private RenderPipeline pipeline;
@Override
public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
lighting = pipelineDataProvider.getPrivatePluginData(Lighting3DPrivateData.class);
timeProvider = pipelineDataProvider.getTimeProvider();
models = pipelineDataProvider.getPrivatePluginData(GraphModelsImpl.class);
for (JsonValue shaderDefinition : shaderDefinitions) {
GraphShader depthGraphShader = ShadowShaderRendererPipelineNodeProducer.createDepthShader(shaderDefinition, pipelineDataProvider.getWhitePixel().texture);
allShaderTags.add(depthGraphShader.getTag());
shaders.put(depthGraphShader.getTag(), depthGraphShader);
}
for (ObjectMap.Entry<String, GraphShader> shaderEntry : shaders.entries()) {
models.registerTag(shaderEntry.key, shaderEntry.value);
}
}
private boolean needsDepth() {
for (GraphShader shader : shaders.values()) {
if (shader.isUsingDepthTexture() && models.hasModelWithTag(shader.getTag()))
return true;
}
return false;
}
private boolean isRequiringSceneColor() {
for (GraphShader shader : shaders.values()) {
if (shader.isUsingColorTexture() && models.hasModelWithTag(shader.getTag()))
return true;
}
return false;
}
@Override
public void processPipelineRequirements(PipelineRequirements pipelineRequirements) {
if (needsDepth())
pipelineRequirements.setRequiringDepthTexture();
}
@Override
public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
boolean enabled = processorEnabled == null || processorEnabled.getValue();
boolean usesDepth = enabled && needsDepth();
RenderPipeline renderPipeline = renderPipelineInput.getValue();
this.pipeline = renderPipeline;
if (enabled) {
boolean needsDrawing = false;
Lighting3DEnvironment environment = lighting.getEnvironment(environmentId);
// Initialize directional light cameras and textures
for (Directional3DLight directionalLight : environment.getDirectionalLights()) {
if (directionalLight.isShadowsEnabled()) {
needsDrawing = true;
directionalLight.updateCamera(environment.getSceneCenter(), environment.getSceneDiameter());
if (directionalLight.getShadowFrameBuffer() == null) {
RenderPipelineBuffer shadowFrameBuffer = renderPipeline.getNewFrameBuffer(directionalLight.getShadowBufferSize(), directionalLight.getShadowBufferSize(), Pixmap.Format.RGB888, Color.WHITE);
directionalLight.setShadowFrameBuffer(shadowFrameBuffer);
createdPipelineBuffers.add(shadowFrameBuffer);
shadowDirectionalLights.add(directionalLight);
}
}
}
if (needsDrawing) {
boolean needsSceneColor = isRequiringSceneColor();
RenderPipelineBuffer drawBuffer = renderPipeline.getDefaultBuffer();
for (Directional3DLight directionalLight : environment.getDirectionalLights()) {
if (directionalLight.isShadowsEnabled()) {
RenderPipelineBuffer shadowBuffer = directionalLight.getShadowFrameBuffer();
Camera camera = directionalLight.getShadowCamera();
shaderContext.setCamera(camera);
shaderContext.setTimeProvider(timeProvider);
shaderContext.setRenderWidth(shadowBuffer.getWidth());
shaderContext.setRenderHeight(shadowBuffer.getHeight());
if (usesDepth) {
renderPipeline.enrichWithDepthBuffer(drawBuffer);
shaderContext.setDepthTexture(drawBuffer.getDepthBufferTexture());
}
if (needsSceneColor)
shaderContext.setColorTexture(drawBuffer.getColorBufferTexture());
// Drawing models on color buffer
depthStrategyCallback.prepare(pipelineRenderingContext, models);
shadowBuffer.beginColor();
renderingStrategy.processModels(models, allShaderTags, camera, depthStrategyCallback);
shadowBuffer.endColor();
}
}
}
}
output.setValue(renderPipeline);
}
@Override
public void endFrame() {
for (RenderPipelineBuffer createdPipelineBuffer : createdPipelineBuffers) {
pipeline.returnFrameBuffer(createdPipelineBuffer);
}
createdPipelineBuffers.clear();
for (Directional3DLight shadowDirectionalLight : shadowDirectionalLights) {
shadowDirectionalLight.setShadowFrameBuffer(null);
}
shadowDirectionalLights.clear();
}
@Override
public void dispose() {
for (GraphShader shader : shaders.values()) {
shader.dispose();
}
}
};
}
use of com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData in project gdx-graph by MarcinSc.
the class AmbientLightShaderNodeBuilder method buildCommonNode.
@Override
protected ObjectMap<String, ? extends FieldOutput> buildCommonNode(boolean designTime, String nodeId, final JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, CommonShaderBuilder commonShaderBuilder, GraphShaderContext graphShaderContext, GraphShader graphShader) {
final String environmentId = data.getString("id", "");
commonShaderBuilder.addUniformVariable("u_ambientLight_" + nodeId, "vec4", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
LightColor ambientColor = lights3DProvider.getAmbientLight(environment, shaderContext.getRenderableModel());
if (ambientColor != null) {
shader.setUniform(location, ambientColor.getRed(), ambientColor.getGreen(), ambientColor.getBlue(), 1f);
} else {
shader.setUniform(location, 0f, 0f, 0f, 1f);
}
}
}, "Ambient light");
return LibGDXCollections.singletonMap("ambient", new DefaultFieldOutput(ShaderFieldType.Vector4, "u_ambientLight_" + nodeId));
}
use of com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData in project gdx-graph by MarcinSc.
the class DirectionalLightShaderNodeBuilder method buildCommonNode.
@Override
protected ObjectMap<String, ? extends FieldOutput> buildCommonNode(boolean designTime, String nodeId, final JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, CommonShaderBuilder commonShaderBuilder, GraphShaderContext graphShaderContext, GraphShader graphShader) {
final int index = data.getInt("index");
final String environmentId = data.getString("id", "");
ObjectMap<String, DefaultFieldOutput> result = new ObjectMap<>();
if (producedOutputs.contains("direction")) {
String name = "u_directionalLightDirection_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec3", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Directional3DLight> directionalLights = lights3DProvider.getDirectionalLights(environment, shaderContext.getRenderableModel(), index + 1);
if (directionalLights != null && directionalLights.size > index && directionalLights.get(index) != null) {
Directional3DLight directionalLight = directionalLights.get(index);
shader.setUniform(location, directionalLight.getDirectionX(), directionalLight.getDirectionY(), directionalLight.getDirectionZ());
} else {
shader.setUniform(location, 0f, 0f, 0f);
}
}
}, "Light direction");
result.put("direction", new DefaultFieldOutput(ShaderFieldType.Vector3, name));
}
if (producedOutputs.contains("color")) {
String name = "u_directionalLightColor_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec4", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Directional3DLight> directionalLights = lights3DProvider.getDirectionalLights(environment, shaderContext.getRenderableModel(), index + 1);
if (directionalLights != null && directionalLights.size > index && directionalLights.get(index) != null) {
Directional3DLight directionalLight = directionalLights.get(index);
LightColor color = directionalLight.getColor();
shader.setUniform(location, color.getRed(), color.getGreen(), color.getBlue(), 1f);
} else {
shader.setUniform(location, 0f, 0f, 0f, 1f);
}
}
}, "Light color");
result.put("color", new DefaultFieldOutput(ShaderFieldType.Vector4, name));
}
return result;
}
use of com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData in project gdx-graph by MarcinSc.
the class PointLightShaderNodeBuilder method buildCommonNode.
@Override
protected ObjectMap<String, ? extends FieldOutput> buildCommonNode(boolean designTime, String nodeId, final JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, CommonShaderBuilder commonShaderBuilder, GraphShaderContext graphShaderContext, GraphShader graphShader) {
final int index = data.getInt("index");
final String environmentId = data.getString("id", "");
ObjectMap<String, DefaultFieldOutput> result = new ObjectMap<>();
if (producedOutputs.contains("position")) {
String name = "u_pointLightPosition_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec3", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Point3DLight> pointLights = lights3DProvider.getPointLights(environment, shaderContext.getRenderableModel(), index + 1);
if (pointLights != null && pointLights.size > index && pointLights.get(index) != null) {
Point3DLight pointLight = pointLights.get(index);
shader.setUniform(location, pointLight.getPosition());
} else {
shader.setUniform(location, 0f, 0f, 0f);
}
}
}, "Point light position");
result.put("position", new DefaultFieldOutput(ShaderFieldType.Vector3, name));
}
if (producedOutputs.contains("color")) {
String name = "u_pointLightColor_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec4", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Point3DLight> pointLights = lights3DProvider.getPointLights(environment, shaderContext.getRenderableModel(), index + 1);
if (pointLights != null && pointLights.size > index && pointLights.get(index) != null) {
Point3DLight pointLight = pointLights.get(index);
LightColor color = pointLight.getColor();
shader.setUniform(location, color.getRed(), color.getGreen(), color.getBlue(), 1f);
} else {
shader.setUniform(location, 0f, 0f, 0f, 1f);
}
}
}, "Point light color");
result.put("color", new DefaultFieldOutput(ShaderFieldType.Vector4, name));
}
if (producedOutputs.contains("intensity")) {
String name = "u_pointLightIntensity_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "float", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Point3DLight> pointLights = lights3DProvider.getPointLights(environment, shaderContext.getRenderableModel(), index + 1);
if (pointLights != null && pointLights.size > index && pointLights.get(index) != null) {
Point3DLight pointLight = pointLights.get(index);
shader.setUniform(location, pointLight.getIntensity());
} else {
shader.setUniform(location, 0f);
}
}
}, "Point light intensity");
result.put("intensity", new DefaultFieldOutput(ShaderFieldType.Float, name));
}
return result;
}
use of com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData in project gdx-graph by MarcinSc.
the class SpotLightShaderNodeBuilder method buildCommonNode.
@Override
protected ObjectMap<String, ? extends FieldOutput> buildCommonNode(boolean designTime, String nodeId, final JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, CommonShaderBuilder commonShaderBuilder, GraphShaderContext graphShaderContext, GraphShader graphShader) {
final int index = data.getInt("index");
final String environmentId = data.getString("id", "");
ObjectMap<String, DefaultFieldOutput> result = new ObjectMap<>();
if (producedOutputs.contains("position")) {
String name = "u_spotLightPosition_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec3", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Spot3DLight> spotLights = lights3DProvider.getSpotLights(environment, shaderContext.getRenderableModel(), index + 1);
if (spotLights != null && spotLights.size > index && spotLights.get(index) != null) {
Spot3DLight spotLight = spotLights.get(index);
shader.setUniform(location, spotLight.getPosition());
} else {
shader.setUniform(location, 0f, 0f, 0f);
}
}
}, "Spot light position");
result.put("position", new DefaultFieldOutput(ShaderFieldType.Vector3, name));
}
if (producedOutputs.contains("direction")) {
String name = "u_spotLightDirection_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec3", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Spot3DLight> spotLights = lights3DProvider.getSpotLights(environment, shaderContext.getRenderableModel(), index + 1);
if (spotLights != null && spotLights.size > index && spotLights.get(index) != null) {
Spot3DLight spotLight = spotLights.get(index);
shader.setUniform(location, spotLight.getDirectionX(), spotLight.getDirectionY(), spotLight.getDirectionZ());
} else {
shader.setUniform(location, 0f, 0f, 0f);
}
}
}, "Spot light direction");
result.put("direction", new DefaultFieldOutput(ShaderFieldType.Vector3, name));
}
if (producedOutputs.contains("color")) {
String name = "u_spotLightColor_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "vec4", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Spot3DLight> spotLights = lights3DProvider.getSpotLights(environment, shaderContext.getRenderableModel(), index + 1);
if (spotLights != null && spotLights.size > index && spotLights.get(index) != null) {
Spot3DLight spotLight = spotLights.get(index);
LightColor color = spotLight.getColor();
shader.setUniform(location, color.getRed(), color.getGreen(), color.getBlue(), 1f);
} else {
shader.setUniform(location, 0f, 0f, 0f, 1f);
}
}
}, "Spot light color");
result.put("color", new DefaultFieldOutput(ShaderFieldType.Vector4, name));
}
if (producedOutputs.contains("intensity")) {
String name = "u_spotLightIntensity_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "float", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Spot3DLight> spotLights = lights3DProvider.getSpotLights(environment, shaderContext.getRenderableModel(), index + 1);
if (spotLights != null && spotLights.size > index && spotLights.get(index) != null) {
Spot3DLight spotLight = spotLights.get(index);
shader.setUniform(location, spotLight.getIntensity());
} else {
shader.setUniform(location, 0f);
}
}
}, "Spot light intensity");
result.put("intensity", new DefaultFieldOutput(ShaderFieldType.Float, name));
}
if (producedOutputs.contains("cutOffAngle")) {
String name = "u_spotLightCutOffAngle_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "float", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Spot3DLight> spotLights = lights3DProvider.getSpotLights(environment, shaderContext.getRenderableModel(), index + 1);
if (spotLights != null && spotLights.size > index && spotLights.get(index) != null) {
Spot3DLight spotLight = spotLights.get(index);
shader.setUniform(location, spotLight.getCutoffAngle());
} else {
shader.setUniform(location, 0f);
}
}
}, "Spot light cut-off angle");
result.put("cutOffAngle", new DefaultFieldOutput(ShaderFieldType.Float, name));
}
if (producedOutputs.contains("exponent")) {
String name = "u_spotLightExponent_" + nodeId + "_" + index;
commonShaderBuilder.addUniformVariable(name, "float", false, new UniformRegistry.UniformSetter() {
@Override
public void set(BasicShader shader, int location, ShaderContext shaderContext) {
Lighting3DPrivateData privatePluginData = shaderContext.getPrivatePluginData(Lighting3DPrivateData.class);
Lighting3DEnvironment environment = privatePluginData.getEnvironment(environmentId);
Lights3DProvider lights3DProvider = privatePluginData.getLights3DProvider();
Array<Spot3DLight> spotLights = lights3DProvider.getSpotLights(environment, shaderContext.getRenderableModel(), index + 1);
if (spotLights != null && spotLights.size > index && spotLights.get(index) != null) {
Spot3DLight spotLight = spotLights.get(index);
shader.setUniform(location, spotLight.getExponent());
} else {
shader.setUniform(location, 0f);
}
}
}, "Spot light exponent");
result.put("exponent", new DefaultFieldOutput(ShaderFieldType.Float, name));
}
return result;
}
Aggregations