Search in sources :

Example 31 with ShaderFieldType

use of com.gempukku.libgdx.graph.shader.field.ShaderFieldType in project gdx-graph by MarcinSc.

the class ModelShaderPreviewWidget method createShader.

private void createShader(final Graph<? extends GraphNode, ? extends GraphConnection, ? extends GraphProperty> graph) {
    try {
        timeKeeper = new DefaultTimeKeeper();
        graphShader = GraphShaderBuilder.buildModelShader("Test", WhitePixel.sharedInstance.texture, graph, true);
        frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
        createModel();
        MapWritablePropertyContainer globalPropertyContainer = new MapWritablePropertyContainer();
        for (GraphProperty property : graph.getProperties()) {
            if (property.getLocation() == PropertyLocation.Global_Uniform) {
                ShaderFieldType propertyType = ShaderFieldTypeRegistry.findShaderFieldType(property.getType());
                globalPropertyContainer.setValue(property.getName(), propertyType.convertFromJson(property.getData()));
            }
        }
        shaderContext.setGlobalPropertyContainer(globalPropertyContainer);
        localPropertyContainer = new MapWritablePropertyContainer();
        for (GraphProperty property : graph.getProperties()) {
            if (property.getLocation() == PropertyLocation.Uniform) {
                ShaderFieldType propertyType = ShaderFieldTypeRegistry.findShaderFieldType(property.getType());
                Object value = propertyType.convertFromJson(property.getData());
                if (propertyType.isTexture()) {
                    if (value != null) {
                        try {
                            Texture texture = new Texture(Gdx.files.absolute((String) value));
                            graphShader.addManagedResource(texture);
                            localPropertyContainer.setValue(property.getName(), new TextureRegion(texture));
                        } catch (Exception exp) {
                            localPropertyContainer.setValue(property.getName(), WhitePixel.sharedInstance.textureRegion);
                        }
                    } else {
                        localPropertyContainer.setValue(property.getName(), WhitePixel.sharedInstance.textureRegion);
                    }
                }
            }
        }
        shaderContext.setTimeProvider(timeKeeper);
        shaderInitialized = true;
    } catch (Exception exp) {
        exp.printStackTrace();
        if (graphShader != null)
            graphShader.dispose();
    }
}
Also used : DefaultTimeKeeper(com.gempukku.libgdx.graph.util.DefaultTimeKeeper) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) GraphProperty(com.gempukku.libgdx.graph.data.GraphProperty) FrameBuffer(com.badlogic.gdx.graphics.glutils.FrameBuffer) MapWritablePropertyContainer(com.gempukku.libgdx.graph.shader.property.MapWritablePropertyContainer) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 32 with ShaderFieldType

use of com.gempukku.libgdx.graph.shader.field.ShaderFieldType in project gdx-graph by MarcinSc.

the class PropertyShaderNodeBuilder method buildFragmentNode.

@Override
public ObjectMap<String, ? extends FieldOutput> buildFragmentNode(boolean designTime, String nodeId, JsonValue data, ObjectMap<String, Array<FieldOutput>> inputs, ObjectSet<String> producedOutputs, VertexShaderBuilder vertexShaderBuilder, FragmentShaderBuilder fragmentShaderBuilder, GraphShaderContext graphShaderContext, GraphShader graphShader) {
    final String name = data.getString("name");
    final String propertyType = data.getString("type");
    ShaderFieldType fieldType = ShaderFieldTypeRegistry.findShaderFieldType(propertyType);
    PropertySource propertySource = graphShaderContext.getPropertySource(name);
    PropertyLocation propertyLocation = propertySource.getPropertyLocation();
    if (propertyLocation == PropertyLocation.Attribute) {
        return LibGDXCollections.singletonMap("value", fieldType.addAsFragmentAttribute(vertexShaderBuilder, fragmentShaderBuilder, data, propertySource));
    } else if (propertyLocation == PropertyLocation.Uniform) {
        return LibGDXCollections.singletonMap("value", fieldType.addAsLocalUniform(fragmentShaderBuilder, data, propertySource));
    } else {
        return LibGDXCollections.singletonMap("value", fieldType.addAsGlobalUniform(fragmentShaderBuilder, data, propertySource));
    }
}
Also used : ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 33 with ShaderFieldType

use of com.gempukku.libgdx.graph.shader.field.ShaderFieldType in project gdx-graph by MarcinSc.

the class GraphShaderBuilder method buildNode.

private static ObjectMap<String, GraphShaderNodeBuilder.FieldOutput> buildNode(boolean designTime, boolean fragmentShader, Graph<? extends GraphNode, ? extends GraphConnection, ? extends GraphProperty> graph, GraphShaderContext context, GraphShader graphShader, String nodeId, ObjectMap<String, ObjectMap<String, GraphShaderNodeBuilder.FieldOutput>> nodeOutputs, VertexShaderBuilder vertexShaderBuilder, FragmentShaderBuilder fragmentShaderBuilder, GraphConfiguration... graphConfigurations) {
    ObjectMap<String, GraphShaderNodeBuilder.FieldOutput> nodeOutput = nodeOutputs.get(nodeId);
    if (nodeOutput == null) {
        GraphNode nodeInfo = graph.getNodeById(nodeId);
        String nodeInfoType = nodeInfo.getConfiguration().getType();
        GraphShaderNodeBuilder nodeBuilder = getNodeBuilder(nodeInfoType, graphConfigurations);
        if (nodeBuilder == null)
            throw new IllegalStateException("Unable to find graph shader node builder for type: " + nodeInfoType);
        ObjectMap<String, Array<GraphShaderNodeBuilder.FieldOutput>> inputFields = new ObjectMap<>();
        for (GraphNodeInput nodeInput : new ObjectMap.Values<>(nodeBuilder.getConfiguration(nodeInfo.getData()).getNodeInputs())) {
            String fieldId = nodeInput.getFieldId();
            Array<GraphConnection> vertexInfos = findInputVertices(graph, nodeId, fieldId);
            if (vertexInfos.size == 0 && nodeInput.isRequired())
                throw new IllegalStateException("Required input not provided");
            Array<String> fieldTypes = new Array<>();
            Array<GraphShaderNodeBuilder.FieldOutput> fieldOutputs = new Array<>();
            for (GraphConnection vertexInfo : vertexInfos) {
                ObjectMap<String, GraphShaderNodeBuilder.FieldOutput> output = buildNode(designTime, fragmentShader, graph, context, graphShader, vertexInfo.getNodeFrom(), nodeOutputs, vertexShaderBuilder, fragmentShaderBuilder, graphConfigurations);
                GraphShaderNodeBuilder.FieldOutput fieldOutput = output.get(vertexInfo.getFieldFrom());
                ShaderFieldType fieldType = fieldOutput.getFieldType();
                fieldTypes.add(fieldType.getName());
                fieldOutputs.add(fieldOutput);
            }
            if (!nodeInput.acceptsInputTypes(fieldTypes))
                throw new IllegalStateException("Producer produces a field of value not compatible with consumer");
            inputFields.put(fieldId, fieldOutputs);
        }
        ObjectSet<String> requiredOutputs = findRequiredOutputs(graph, nodeId);
        if (fragmentShader) {
            nodeOutput = (ObjectMap<String, GraphShaderNodeBuilder.FieldOutput>) nodeBuilder.buildFragmentNode(designTime, nodeId, nodeInfo.getData(), inputFields, requiredOutputs, vertexShaderBuilder, fragmentShaderBuilder, context, graphShader);
        } else {
            nodeOutput = (ObjectMap<String, GraphShaderNodeBuilder.FieldOutput>) nodeBuilder.buildVertexNode(designTime, nodeId, nodeInfo.getData(), inputFields, requiredOutputs, vertexShaderBuilder, context, graphShader);
        }
        nodeOutputs.put(nodeId, nodeOutput);
    }
    return nodeOutput;
}
Also used : Array(com.badlogic.gdx.utils.Array) GraphShaderNodeBuilder(com.gempukku.libgdx.graph.shader.node.GraphShaderNodeBuilder) ObjectMap(com.badlogic.gdx.utils.ObjectMap) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 34 with ShaderFieldType

use of com.gempukku.libgdx.graph.shader.field.ShaderFieldType in project gdx-graph by MarcinSc.

the class DivideShaderNodeBuilder method determineOutputType.

private ShaderFieldType determineOutputType(FieldOutput a, FieldOutput b) {
    ShaderFieldType aType = a.getFieldType();
    ShaderFieldType bType = b.getFieldType();
    if (aType.getName().equals(ShaderFieldType.Float))
        return bType;
    if (bType.getName().equals(ShaderFieldType.Float))
        return aType;
    if (aType != bType)
        throw new IllegalStateException("Invalid mix of input field types");
    return aType;
}
Also used : ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 35 with ShaderFieldType

use of com.gempukku.libgdx.graph.shader.field.ShaderFieldType in project gdx-graph by MarcinSc.

the class SubtractShaderNodeBuilder method buildCommonNode.

@Override
protected ObjectMap<String, ? extends FieldOutput> buildCommonNode(boolean designTime, String nodeId, JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, CommonShaderBuilder commonShaderBuilder, GraphShaderContext graphShaderContext, GraphShader graphShader) {
    FieldOutput aValue = inputs.get("a");
    FieldOutput bValue = inputs.get("b");
    ShaderFieldType resultType = determineOutputType(aValue, bValue);
    commonShaderBuilder.addMainLine("// Subtract node");
    String name = "result_" + nodeId;
    commonShaderBuilder.addMainLine(resultType.getShaderType() + " " + name + " = " + aValue.getRepresentation() + " - " + bValue.getRepresentation() + ";");
    return LibGDXCollections.singletonMap("output", new DefaultFieldOutput(resultType, name));
}
Also used : DefaultFieldOutput(com.gempukku.libgdx.graph.shader.node.DefaultFieldOutput) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType) DefaultFieldOutput(com.gempukku.libgdx.graph.shader.node.DefaultFieldOutput)

Aggregations

ShaderFieldType (com.gempukku.libgdx.graph.shader.field.ShaderFieldType)55 DefaultFieldOutput (com.gempukku.libgdx.graph.shader.node.DefaultFieldOutput)43 ObjectMap (com.badlogic.gdx.utils.ObjectMap)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 FrameBuffer (com.badlogic.gdx.graphics.glutils.FrameBuffer)3 Array (com.badlogic.gdx.utils.Array)3 GraphProperty (com.gempukku.libgdx.graph.data.GraphProperty)3 DefaultTimeKeeper (com.gempukku.libgdx.graph.util.DefaultTimeKeeper)3 PropertyContainer (com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.PropertyContainer)2 PropertySource (com.gempukku.libgdx.graph.shader.property.PropertySource)2 ValuePerVertex (com.gempukku.libgdx.graph.util.ValuePerVertex)2 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 Texture (com.badlogic.gdx.graphics.Texture)1 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)1 VertexAttributes (com.badlogic.gdx.graphics.VertexAttributes)1 TextureDescriptor (com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor)1 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 IntArray (com.badlogic.gdx.utils.IntArray)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 PropertyNodeConfiguration (com.gempukku.libgdx.graph.config.PropertyNodeConfiguration)1