Search in sources :

Example 26 with ShaderFieldType

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

the class ParticlesShaderPreviewWidget method createShader.

private void createShader(final Graph<? extends GraphNode, ? extends GraphConnection, ? extends GraphProperty> graph) {
    try {
        timeKeeper = new DefaultTimeKeeper();
        graphShader = GraphShaderBuilder.buildParticlesShader("Test", WhitePixel.sharedInstance.texture, graph, true);
        frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
        createModel();
        shaderContext.setTimeProvider(timeKeeper);
        shaderContext.setGlobalPropertyContainer(new PropertyContainer() {

            @Override
            public Object getValue(String name) {
                for (GraphProperty property : graph.getProperties()) {
                    if (property.getName().equals(name) && property.getLocation() == PropertyLocation.Global_Uniform) {
                        ShaderFieldType propertyType = ShaderFieldTypeRegistry.findShaderFieldType(property.getType());
                        return propertyType.convertFromJson(property.getData());
                    }
                }
                return null;
            }
        });
        shaderContext.setLocalPropertyContainer(new PropertyContainer() {

            @Override
            public Object getValue(String name) {
                for (GraphProperty property : graph.getProperties()) {
                    if (property.getName().equals(name) && property.getLocation() != PropertyLocation.Global_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);
                                    return new TextureRegion(texture);
                                } catch (Exception exp) {
                                }
                            }
                            return WhitePixel.sharedInstance.textureRegion;
                        } else {
                            return value;
                        }
                    }
                }
                return null;
            }
        });
        shaderInitialized = true;
    } catch (Exception exp) {
        exp.printStackTrace();
        if (graphShader != null)
            graphShader.dispose();
    }
}
Also used : DefaultTimeKeeper(com.gempukku.libgdx.graph.util.DefaultTimeKeeper) PropertyContainer(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.PropertyContainer) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) GraphProperty(com.gempukku.libgdx.graph.data.GraphProperty) FrameBuffer(com.badlogic.gdx.graphics.glutils.FrameBuffer) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 27 with ShaderFieldType

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

the class ScreenShaderPreviewWidget method createShader.

private void createShader(final Graph<? extends GraphNode, ? extends GraphConnection, ? extends GraphProperty> graph) {
    try {
        timeKeeper = new DefaultTimeKeeper();
        graphShader = GraphShaderBuilder.buildScreenShader("Test", WhitePixel.sharedInstance.texture, graph, true);
        frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
        shaderContext.setTimeProvider(timeKeeper);
        shaderContext.setGlobalPropertyContainer(new PropertyContainer() {

            @Override
            public Object getValue(String name) {
                for (GraphProperty property : graph.getProperties()) {
                    if (property.getName().equals(name)) {
                        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);
                                    return new TextureRegion(texture);
                                } catch (Exception exp) {
                                }
                            }
                            return WhitePixel.sharedInstance.textureRegion;
                        } else {
                            return value;
                        }
                    }
                }
                return null;
            }
        });
        shaderInitialized = true;
    } catch (Exception exp) {
        exp.printStackTrace();
        if (graphShader != null)
            graphShader.dispose();
    }
}
Also used : DefaultTimeKeeper(com.gempukku.libgdx.graph.util.DefaultTimeKeeper) PropertyContainer(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.PropertyContainer) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) GraphProperty(com.gempukku.libgdx.graph.data.GraphProperty) FrameBuffer(com.badlogic.gdx.graphics.glutils.FrameBuffer) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 28 with ShaderFieldType

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

the class PropertyShaderNodeBuilder method buildVertexNode.

@Override
public ObjectMap<String, ? extends FieldOutput> buildVertexNode(boolean designTime, String nodeId, JsonValue data, ObjectMap<String, Array<FieldOutput>> inputs, ObjectSet<String> producedOutputs, VertexShaderBuilder vertexShaderBuilder, 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.addAsVertexAttribute(vertexShaderBuilder, data, propertySource));
    } else if (propertyLocation == PropertyLocation.Uniform) {
        return LibGDXCollections.singletonMap("value", fieldType.addAsLocalUniform(vertexShaderBuilder, data, propertySource));
    } else {
        return LibGDXCollections.singletonMap("value", fieldType.addAsGlobalUniform(vertexShaderBuilder, data, propertySource));
    }
}
Also used : ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 29 with ShaderFieldType

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

the class GraphModelUtil method getShaderVertexAttributes.

public static VertexAttributes getShaderVertexAttributes(GraphModels graphModels, String tag) {
    ObjectMap<String, PropertySource> shaderProperties = graphModels.getShaderProperties(tag);
    if (shaderProperties == null)
        throw new GdxRuntimeException("Unable to locate shader with tag: " + tag);
    Array<VertexAttribute> vertexAttributeArray = new Array<>(VertexAttribute.class);
    for (ObjectMap.Entry<String, PropertySource> shaderProperty : shaderProperties) {
        PropertySource propertySource = shaderProperty.value;
        PropertyLocation propertyLocation = propertySource.getPropertyLocation();
        if (propertyLocation == PropertyLocation.Attribute) {
            ShaderFieldType shaderFieldType = propertySource.getShaderFieldType();
            if (shaderFieldType instanceof ArrayShaderFieldType) {
                int arraySize = ((ArrayShaderFieldType) shaderFieldType).getArrayLength();
                for (int i = 0; i < arraySize; i++) {
                    vertexAttributeArray.add(new VertexAttribute(1024, shaderFieldType.getNumberOfComponents(), propertySource.getAttributeName(i)));
                }
            } else {
                vertexAttributeArray.add(new VertexAttribute(1024, shaderFieldType.getNumberOfComponents(), propertySource.getAttributeName()));
            }
        }
    }
    return new VertexAttributes(vertexAttributeArray.toArray());
}
Also used : VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) PropertySource(com.gempukku.libgdx.graph.shader.property.PropertySource) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) IntArray(com.badlogic.gdx.utils.IntArray) ObjectMap(com.badlogic.gdx.utils.ObjectMap) PropertyLocation(com.gempukku.libgdx.graph.shader.property.PropertyLocation) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) ArrayShaderFieldType(com.gempukku.libgdx.graph.shader.field.ArrayShaderFieldType) ArrayShaderFieldType(com.gempukku.libgdx.graph.shader.field.ArrayShaderFieldType) ShaderFieldType(com.gempukku.libgdx.graph.shader.field.ShaderFieldType)

Example 30 with ShaderFieldType

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

the class BlinnPhongLightingShaderNodeBuilder method buildFragmentNodeSingleInputs.

@Override
public ObjectMap<String, ? extends FieldOutput> buildFragmentNodeSingleInputs(boolean designTime, String nodeId, final JsonValue data, ObjectMap<String, FieldOutput> inputs, ObjectSet<String> producedOutputs, VertexShaderBuilder vertexShaderBuilder, FragmentShaderBuilder fragmentShaderBuilder, final GraphShaderContext graphShaderContext, GraphShader graphShader) {
    final String environmentId = data.getString("id", "");
    fragmentShaderBuilder.addStructure("Lighting", "  vec3 diffuse;\n" + "  vec3 specular;\n");
    Lighting3DUtils.configureAmbientLighting(fragmentShaderBuilder, nodeId, environmentId);
    ObjectMap<String, String> variables = new ObjectMap<>();
    variables.put("NUM_SPOT_LIGHTS", String.valueOf(maxNumberOfSpotlights));
    variables.put("NUM_POINT_LIGHTS", String.valueOf(maxNumberOfPointLights));
    variables.put("NUM_DIRECTIONAL_LIGHTS", String.valueOf(maxNumberOfDirectionalLights));
    variables.put("NODE_ID", nodeId);
    if (maxNumberOfDirectionalLights > 0)
        passDirectionalLights(environmentId, fragmentShaderBuilder, nodeId, variables);
    if (maxNumberOfPointLights > 0)
        passPointLights(environmentId, fragmentShaderBuilder, nodeId, variables);
    if (maxNumberOfSpotlights > 0)
        passSpotLights(environmentId, fragmentShaderBuilder, nodeId, variables);
    FieldOutput positionValue = inputs.get("position");
    FieldOutput normalValue = inputs.get("normal");
    FieldOutput albedoValue = inputs.get("albedo");
    FieldOutput emissionValue = inputs.get("emission");
    FieldOutput specularValue = inputs.get("specular");
    FieldOutput ambientOcclusionValue = inputs.get("ambientOcclusion");
    FieldOutput shininessValue = inputs.get("shininess");
    String position = positionValue.getRepresentation();
    String normal = normalValue.getRepresentation();
    String albedo = albedoValue != null ? albedoValue.getRepresentation() : "vec3(0.0)";
    String emission = emissionValue != null ? emissionValue.getRepresentation() : "vec3(0.0)";
    String specular = specularValue != null ? specularValue.getRepresentation() : "vec3(1.0)";
    String ambientOcclusion = ambientOcclusionValue != null ? ambientOcclusionValue.getRepresentation() : "1.0";
    String shininess = shininessValue != null ? shininessValue.getRepresentation() : "32.0";
    fragmentShaderBuilder.addMainLine("// Blinn-Phong Lighting node");
    String calculateLightingFunctionName = "calculateBlinnPhongLightingFunction_" + nodeId;
    String calculateLightingFunction = createCalculateLightingFunction(nodeId);
    fragmentShaderBuilder.addFunction(calculateLightingFunctionName, calculateLightingFunction);
    String lightingVariable = "lighting_" + nodeId;
    fragmentShaderBuilder.addMainLine("Lighting " + lightingVariable + " = " + calculateLightingFunctionName + "(" + position + ", " + normal + ", " + shininess + ");");
    ShaderFieldType resultType = ShaderFieldTypeRegistry.findShaderFieldType(ShaderFieldType.Vector3);
    ObjectMap<String, DefaultFieldOutput> result = new ObjectMap<>();
    if (producedOutputs.contains("output")) {
        String name = "color_" + nodeId;
        fragmentShaderBuilder.addMainLine(resultType.getShaderType() + " " + name + " = " + emission + ".rgb + " + ambientOcclusion + " * u_ambientLight_" + nodeId + " * " + albedo + ".rgb;");
        fragmentShaderBuilder.addMainLine(name + " += " + lightingVariable + ".diffuse * " + albedo + ".rgb + " + lightingVariable + ".specular * " + specular + ".rgb;");
        result.put("output", new DefaultFieldOutput(resultType.getName(), name));
    }
    if (producedOutputs.contains("diffuse")) {
        result.put("diffuse", new DefaultFieldOutput(resultType.getName(), lightingVariable + ".diffuse"));
    }
    if (producedOutputs.contains("specularOut")) {
        result.put("specularOut", new DefaultFieldOutput(resultType.getName(), lightingVariable + ".specular"));
    }
    return result;
}
Also used : ObjectMap(com.badlogic.gdx.utils.ObjectMap) 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