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();
}
}
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));
}
}
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;
}
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;
}
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));
}
Aggregations