Search in sources :

Example 26 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gdx-graph by MarcinSc.

the class ValueFloatBoxProducer method createValuePart.

private GraphBoxPartImpl createValuePart(float v1) {
    final VisValidatableTextField v1Input = new VisValidatableTextField(Validators.FLOATS) {

        @Override
        public float getPrefWidth() {
            return 50;
        }
    };
    v1Input.setText(String.valueOf(v1));
    v1Input.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            v1Input.fire(new GraphChangedEvent(false, true));
        }
    });
    HorizontalGroup horizontalGroup = new HorizontalGroup();
    horizontalGroup.addActor(new VisLabel("x"));
    horizontalGroup.addActor(v1Input);
    GraphBoxPartImpl colorPart = new GraphBoxPartImpl(horizontalGroup, new GraphBoxPartImpl.Callback() {

        @Override
        public void serialize(JsonValue object) {
            float value;
            try {
                value = Float.parseFloat(v1Input.getText());
            } catch (NumberFormatException exp) {
                value = 0f;
            }
            object.addChild("v1", new JsonValue(value));
        }
    });
    colorPart.setOutputConnector(GraphBoxOutputConnector.Side.Right, configuration.getNodeOutputs().get("value"));
    return colorPart;
}
Also used : Actor(com.badlogic.gdx.scenes.scene2d.Actor) JsonValue(com.badlogic.gdx.utils.JsonValue) VisLabel(com.kotcrab.vis.ui.widget.VisLabel) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) HorizontalGroup(com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup) VisValidatableTextField(com.kotcrab.vis.ui.widget.VisValidatableTextField)

Example 27 with JsonValue

use of com.badlogic.gdx.utils.JsonValue 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 28 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gdx-graph by MarcinSc.

the class ModelShaderRendererPipelineNodeProducer method createColorShader.

private static GraphShader createColorShader(JsonValue shaderDefinition, Texture defaultTexture) {
    JsonValue shaderGraph = shaderDefinition.get("shader");
    String tag = shaderDefinition.getString("tag");
    Gdx.app.debug("Shader", "Building shader with tag: " + tag);
    return GraphLoader.loadGraph(shaderGraph, new ModelShaderLoaderCallback(tag, defaultTexture, false, configurations), PropertyLocation.Uniform);
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) ModelShaderLoaderCallback(com.gempukku.libgdx.graph.plugin.models.ModelShaderLoaderCallback)

Example 29 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gdx-graph by MarcinSc.

the class ModelShaderRendererPipelineNodeProducer method createDepthShader.

private static GraphShader createDepthShader(JsonValue shaderDefinition, Texture defaultTexture) {
    JsonValue shaderGraph = shaderDefinition.get("shader");
    String tag = shaderDefinition.getString("tag");
    Gdx.app.debug("Shader", "Building shader with tag: " + tag);
    return GraphLoader.loadGraph(shaderGraph, new ModelShaderLoaderCallback(tag, defaultTexture, true, configurations), PropertyLocation.Uniform);
}
Also used : JsonValue(com.badlogic.gdx.utils.JsonValue) ModelShaderLoaderCallback(com.gempukku.libgdx.graph.plugin.models.ModelShaderLoaderCallback)

Example 30 with JsonValue

use of com.badlogic.gdx.utils.JsonValue in project gdx-graph by MarcinSc.

the class ParticlesShaderRendererPipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
    final Array<ParticlesGraphShader> particleShaders = new Array<>();
    final JsonValue shaderDefinitions = data.get("shaders");
    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 FullScreenRender fullScreenRender;

        private TimeProvider timeProvider;

        private GraphParticleEffectsImpl particleEffects;

        @Override
        public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
            fullScreenRender = pipelineDataProvider.getFullScreenRender();
            timeProvider = pipelineDataProvider.getTimeProvider();
            particleEffects = pipelineDataProvider.getPrivatePluginData(GraphParticleEffectsImpl.class);
            for (JsonValue shaderDefinition : shaderDefinitions) {
                String tag = shaderDefinition.getString("tag");
                JsonValue shaderGraph = shaderDefinition.get("shader");
                Gdx.app.debug("Shader", "Building shader with tag: " + tag);
                final ParticlesGraphShader graphShader = GraphLoader.loadGraph(shaderGraph, new ParticlesShaderLoaderCallback(tag, pipelineDataProvider.getWhitePixel().texture, configurations), PropertyLocation.Uniform);
                particleShaders.add(graphShader);
            }
            for (ParticlesGraphShader particleShader : particleShaders) {
                particleEffects.registerEffect(particleShader.getTag(), particleShader);
            }
        }

        private boolean usesDepth() {
            for (ParticlesGraphShader particleShader : particleShaders) {
                if (particleShader.isUsingDepthTexture()) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
            final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
            final PipelineNode.FieldOutput<Camera> cameraInput = (PipelineNode.FieldOutput<Camera>) inputs.get("camera");
            final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
            boolean enabled = processorEnabled == null || processorEnabled.getValue();
            RenderPipeline renderPipeline = renderPipelineInput.getValue();
            if (enabled) {
                boolean usesDepth = usesDepth();
                boolean needsSceneColor = false;
                for (ParticlesGraphShader particleShader : particleShaders) {
                    if (particleShader.isUsingColorTexture()) {
                        needsSceneColor = true;
                        break;
                    }
                }
                RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
                if (usesDepth) {
                    renderPipeline.enrichWithDepthBuffer(currentBuffer);
                }
                if (cameraInput != null) {
                    Camera camera = cameraInput.getValue();
                    shaderContext.setCamera(camera);
                }
                shaderContext.setTimeProvider(timeProvider);
                shaderContext.setRenderWidth(currentBuffer.getWidth());
                shaderContext.setRenderHeight(currentBuffer.getHeight());
                RenderPipelineBuffer sceneColorBuffer = null;
                if (needsSceneColor) {
                    sceneColorBuffer = setupColorTexture(renderPipeline, currentBuffer, pipelineRenderingContext);
                }
                currentBuffer.beginColor();
                for (ParticlesGraphShader particleShader : particleShaders) {
                    String tag = particleShader.getTag();
                    if (particleEffects.hasEffects(tag)) {
                        shaderContext.setGlobalPropertyContainer(particleEffects.getGlobalPropertyContainer(tag));
                        particleShader.begin(shaderContext, pipelineRenderingContext.getRenderContext());
                        for (GraphParticleEffectImpl particleEffect : particleEffects.getParticleEffects(tag)) {
                            if (particleEffect.getRenderableParticleEffect().isRendered(shaderContext.getCamera(), tag)) {
                                shaderContext.setLocalPropertyContainer(particleEffect.getPropertyContainer());
                                particleEffect.render(particleShader, shaderContext);
                            }
                        }
                        particleShader.end();
                    }
                }
                currentBuffer.endColor();
                if (sceneColorBuffer != null)
                    renderPipeline.returnFrameBuffer(sceneColorBuffer);
            }
            output.setValue(renderPipeline);
        }

        private RenderPipelineBuffer setupColorTexture(final RenderPipeline renderPipeline, final RenderPipelineBuffer currentBuffer, PipelineRenderingContext pipelineRenderingContext) {
            RenderPipelineBuffer sceneColorBuffer = renderPipeline.getNewFrameBuffer(currentBuffer, Color.BLACK);
            shaderContext.setColorTexture(sceneColorBuffer.getColorBufferTexture());
            renderPipeline.drawTexture(currentBuffer, sceneColorBuffer, pipelineRenderingContext, fullScreenRender);
            return sceneColorBuffer;
        }

        @Override
        public void dispose() {
            for (ParticlesGraphShader particleShader : particleShaders) {
                particleShader.dispose();
            }
        }
    };
}
Also used : ShaderContextImpl(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl) FullScreenRender(com.gempukku.libgdx.graph.pipeline.producer.FullScreenRender) ObjectMap(com.badlogic.gdx.utils.ObjectMap) 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) Array(com.badlogic.gdx.utils.Array) GraphParticleEffectImpl(com.gempukku.libgdx.graph.plugin.particles.impl.GraphParticleEffectImpl) GraphParticleEffectsImpl(com.gempukku.libgdx.graph.plugin.particles.impl.GraphParticleEffectsImpl) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Aggregations

JsonValue (com.badlogic.gdx.utils.JsonValue)148 JsonReader (com.badlogic.gdx.utils.JsonReader)27 IOException (java.io.IOException)21 Array (com.badlogic.gdx.utils.Array)20 Json (com.badlogic.gdx.utils.Json)15 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)14 FileHandle (com.badlogic.gdx.files.FileHandle)11 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)10 BladeJson (com.bladecoder.engine.serialization.BladeJson)9 HashMap (java.util.HashMap)8 Color (com.badlogic.gdx.graphics.Color)7 GraphBoxImpl (com.gempukku.libgdx.graph.ui.graph.GraphBoxImpl)7 ArrayList (java.util.ArrayList)7 Vector2 (com.badlogic.gdx.math.Vector2)6 Actor (com.badlogic.gdx.scenes.scene2d.Actor)6 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)6 Action (com.bladecoder.engine.actions.Action)6 File (java.io.File)6 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 SerializationException (com.badlogic.gdx.utils.SerializationException)5