This is the 19th day of my participation in the August More Text Challenge

Spark AR is Facebook’s free AR creation platform that enables users to create interactive AR experiences for Facebook and Instagram. More than 400,000 creators in 190 countries have used Spark AR to create their own AR creations

Since the software requires no coding knowledge to use, anyone can now lead the world by creating the next crazy viral Instagram AR effects with little experience in the AR world.

Specialized AR filter designers can cost anywhere from $1,000 to $30,000.

Optional type

Any type in SparkSL can be declared optional. A variable of an optional type does not require its value to appear in the shader.

Instead, the value of the optional variable must be accessed through the valueOr() function, which takes a parameter and uses a fallback value if the variable cannot be found.

This allows you to write a single function to handle both the explicit provision of a value and the absence of a value.

A common use case for optional types is materials with optional normal, diffuse, or specular textures.

In the example below, the function samples an optional normal map and returns the fragment normals if the texture is missing.

// Samples a normal or returns the fragment normal
 vec4 getNormal(optional<std::Texture2d> normalMap, vec2 uv) {
     return normalMap(uv).valueOr(std::get FragmentNormal());
 }   
Copy the code

The valurOr() function can take an optional argument as its argument, which in turn causes the result of the function call itself to be optional.

 vec4 main(optional<vec4> v0, optional<vec4> v1) {
     return v0.valueOr(v1).valueOr(vec4(1.0));
 }     
Copy the code

Overloading of binary and unary operators is also provided.

float main(optional<float> f0, optional<float> f1) {
    optional<float> res = f0 + f1;
    return res.valueOr(0.0);
}                           
Copy the code

Note that binary operators are also used for combinations of optional and non-optional operands. In these cases, the outcome is also optional.

Function annotation

SparkSL allows you to set the entry point of a shader snippet with the @main annotation

// @main
 void main(a) { 
     // ...
 }
Copy the code

The @param annotation allows you to specify a default, minimum, and maximum value for each function argument, and to provide a name and description for each argument’s inspector entry.

In the following example, the range boundaries of f are set by minimum and maximum values, and the default is to specify the value of f if the port on the code fragment is not connected.

 // @param[default=0.5, min=0.0, Max =1.0] f
 void main(float f) {
     // ...
 }
Copy the code

Vec4 variables can use hexadecimal color values in their parameter annotations, for example: @param[default=#AABB00FF] color.

In addition, an optional @return annotation can be used to provide a description of the return value of the function.

// This function description will appear in the inspector
 // @param[default=0.5, min=0.0, Max =1.0] p The description of p's port
 // @param[default=vec2(0.0)] v The description of v's port
 // @return The description of the return value
 vec2 main(float p, vec2 v) { 
     return v * p;
 }
Copy the code