Search in sources :

Example 6 with Directional3DLight

use of com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight in project gdx-graph by MarcinSc.

the class Episode18Scene method createLights.

private Lighting3DEnvironment createLights() {
    float ambientBrightness = 0.8f;
    float directionalBrightness = 0.8f;
    Lighting3DEnvironment lights = new Lighting3DEnvironment();
    lights.setAmbientColor(new Color(ambientBrightness, ambientBrightness, ambientBrightness, 1f));
    DirectionalLight directionalLight = new DirectionalLight();
    directionalLight.setColor(directionalBrightness, directionalBrightness, directionalBrightness, 1f);
    directionalLight.setDirection(-1f, -0.3f, 0);
    lights.addDirectionalLight(new Directional3DLight(directionalLight));
    return lights;
}
Also used : Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) Lighting3DEnvironment(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment) Color(com.badlogic.gdx.graphics.Color) DirectionalLight(com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)

Example 7 with Directional3DLight

use of com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight 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();
            }
        }
    };
}
Also used : ShaderContextImpl(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl) Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) GraphModelsImpl(com.gempukku.libgdx.graph.plugin.models.impl.GraphModelsImpl) ObjectMap(com.badlogic.gdx.utils.ObjectMap) GraphShader(com.gempukku.libgdx.graph.shader.GraphShader) Camera(com.badlogic.gdx.graphics.Camera) TimeProvider(com.gempukku.libgdx.graph.time.TimeProvider) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) JsonValue(com.badlogic.gdx.utils.JsonValue) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) Lighting3DPrivateData(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData) Array(com.badlogic.gdx.utils.Array) Lighting3DEnvironment(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment) RenderOrder(com.gempukku.libgdx.graph.pipeline.RenderOrder) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 8 with Directional3DLight

use of com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight in project gdx-graph by MarcinSc.

the class ShadowShaderTestScene method initializeScene.

@Override
public void initializeScene() {
    camera = new PerspectiveCamera();
    camera.near = 0.1f;
    camera.far = 100;
    updateCamera();
    pipelineRenderer = loadPipelineRenderer();
    float halfSize = 5;
    ModelBuilder modelBuilder = new ModelBuilder();
    Model wall = modelBuilder.createBox(halfSize * 2, halfSize * 2, 0.001f, new Material(), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
    Model sphere = modelBuilder.createSphere(2, 2, 2, 50, 50, new Material(), VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
    disposables.add(wall);
    disposables.add(sphere);
    ModelInstance wallInstance = new ModelInstance(wall);
    wallInstance.transform.translate(0, 0, -3);
    sphereInstance = new ModelInstance(sphere);
    GraphModels graphModels = pipelineRenderer.getPluginData(GraphModels.class);
    CommonPropertiesModelInstanceModelAdapter wallAdapter = new CommonPropertiesModelInstanceModelAdapter(wallInstance, graphModels);
    wallAdapter.getPropertyContainer().setValue("Color", Color.LIGHT_GRAY);
    CommonPropertiesModelInstanceModelAdapter sphereAdapter = new CommonPropertiesModelInstanceModelAdapter(sphereInstance, graphModels);
    sphereAdapter.getPropertyContainer().setValue("Color", Color.RED);
    wallAdapter.addTag("Color");
    wallAdapter.addTag("Color Shadow");
    sphereAdapter.addTag("Color");
    sphereAdapter.addTag("Color Shadow");
    Lighting3DPublicData lighting = pipelineRenderer.getPluginData(Lighting3DPublicData.class);
    environment = new Lighting3DEnvironment(new Vector3(), 20f);
    environment.setAmbientColor(new Color(0.1f, 0.1f, 0.1f, 1f));
    Directional3DLight directionalLight = new Directional3DLight();
    directionalLight.setColor(Color.WHITE);
    directionalLight.setIntensity(0.4f);
    directionalLight.setDirection(0, 0, -1);
    directionalLight.setShadowsEnabled(true);
    directionalLight.setShadowBufferSize(512);
    environment.addDirectionalLight(directionalLight);
    lighting.setEnvironment("Scene", environment);
    stage = createStage();
    disposables.add(stage);
    Gdx.input.setInputProcessor(stage);
    UIPluginPublicData ui = pipelineRenderer.getPluginData(UIPluginPublicData.class);
    ui.setStage("Stage", stage);
}
Also used : CommonPropertiesModelInstanceModelAdapter(com.gempukku.libgdx.graph.util.model.CommonPropertiesModelInstanceModelAdapter) Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) Color(com.badlogic.gdx.graphics.Color) Material(com.badlogic.gdx.graphics.g3d.Material) Vector3(com.badlogic.gdx.math.Vector3) PerspectiveCamera(com.badlogic.gdx.graphics.PerspectiveCamera) ModelInstance(com.badlogic.gdx.graphics.g3d.ModelInstance) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) UIPluginPublicData(com.gempukku.libgdx.graph.plugin.ui.UIPluginPublicData) GraphModels(com.gempukku.libgdx.graph.plugin.models.GraphModels) Lighting3DEnvironment(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment) Model(com.badlogic.gdx.graphics.g3d.Model) Lighting3DPublicData(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPublicData)

Example 9 with Directional3DLight

use of com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight in project gdx-graph by MarcinSc.

the class ShadowShaderTestScene method loadPipelineRenderer.

private PipelineRenderer loadPipelineRenderer() {
    PipelineRenderer pipelineRenderer = PipelineLoader.loadPipelineRenderer(Gdx.files.local("test/shadow-shader-test.json"), timeKeeper);
    pipelineRenderer.setPipelineProperty("Camera", camera);
    pipelineRenderer.getPluginData(RenderCallbackPublicData.class).setRenderCallback("Callback", new RenderCallback() {

        @Override
        public void renderCallback(RenderPipeline renderPipeline, PipelineDataProvider pipelineDataProvider, PipelineRenderingContext pipelineRenderingContext, PipelineNode.PipelineRequirementsCallback pipelineRequirementsCallback) {
            RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
            Directional3DLight firstLight = environment.getDirectionalLights().get(0);
            RenderPipelineBuffer shadowFrameBuffer = firstLight.getShadowFrameBuffer();
            renderPipeline.drawTexture(shadowFrameBuffer, currentBuffer, pipelineRenderingContext, pipelineDataProvider.getFullScreenRender());
        }
    });
    return pipelineRenderer;
}
Also used : PipelineDataProvider(com.gempukku.libgdx.graph.pipeline.producer.node.PipelineDataProvider) Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) RenderCallbackPublicData(com.gempukku.libgdx.graph.plugin.callback.RenderCallbackPublicData) RenderCallback(com.gempukku.libgdx.graph.plugin.callback.RenderCallback) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) PipelineNode(com.gempukku.libgdx.graph.pipeline.producer.node.PipelineNode)

Example 10 with Directional3DLight

use of com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight 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;
}
Also used : Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) Lighting3DPrivateData(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData) DefaultFieldOutput(com.gempukku.libgdx.graph.shader.node.DefaultFieldOutput) Array(com.badlogic.gdx.utils.Array) Lights3DProvider(com.gempukku.libgdx.graph.plugin.lighting3d.provider.Lights3DProvider) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Lighting3DEnvironment(com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment) LightColor(com.gempukku.libgdx.graph.plugin.lighting3d.LightColor)

Aggregations

Directional3DLight (com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight)15 Lighting3DEnvironment (com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DEnvironment)14 Color (com.badlogic.gdx.graphics.Color)12 DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)11 Array (com.badlogic.gdx.utils.Array)2 ObjectMap (com.badlogic.gdx.utils.ObjectMap)2 PipelineRenderingContext (com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext)2 Lighting3DPrivateData (com.gempukku.libgdx.graph.plugin.lighting3d.Lighting3DPrivateData)2 Camera (com.badlogic.gdx.graphics.Camera)1 PerspectiveCamera (com.badlogic.gdx.graphics.PerspectiveCamera)1 Material (com.badlogic.gdx.graphics.g3d.Material)1 Model (com.badlogic.gdx.graphics.g3d.Model)1 ModelInstance (com.badlogic.gdx.graphics.g3d.ModelInstance)1 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)1 Vector3 (com.badlogic.gdx.math.Vector3)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 RenderOrder (com.gempukku.libgdx.graph.pipeline.RenderOrder)1 RenderPipeline (com.gempukku.libgdx.graph.pipeline.RenderPipeline)1 RenderPipelineBuffer (com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer)1 PipelineDataProvider (com.gempukku.libgdx.graph.pipeline.producer.node.PipelineDataProvider)1