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