Raymarcher Technical & APi Documentation
Creating Custom SDF
Raymarcher is organized into distinct sections, and one of these sections focuses on Raymarcher objects known as Signed Distance Functions (SDFs).
The Raymarcher library already includes a collection of well-known and common SDFs, containing various primitive shapes, fractals, and volume boxes.

You have the possibility to create your own SDF objects with custom properties and formulas.
To do this, simply create a new C# script and inherit from the RMSdfObjectBase class within the Raymarcher.Objects namespace.
This base class defines the structure for an actual Raymarcher SDF object.

Please note the following guidelines:

- SDF objects can store properties of the following datatypes: float, float2, float3, float4, sampler2D, sampler3D, and float4x4.
- SDF objects are not allowed to use keywords or shader features.

By adhering to these guidelines and extending the RMSdfObjectBase class, you can easily integrate your custom SDFs into the Raymarcher environment.
Example Sphere SDF
See the example reconstructed script for sphere sdf:
using UnityEngine;
using Raymarcher.Objects;

public sealed class CustomSphereSDF : RMSdfObjectBase
{
// Any parameters for your sdf go here
public float sphereRadius = 1.0f;

// The actual body of the sdf method (you can create multiple lines or a single-line method)
public override string SdfMethodBody =>
  $"{VARCONST_RESULT} = length({VARCONST_POSITION}) - {nameof(sphereRadius)};";

// Main method of the sdf that will be used in the Object Buffer
public override string SdfMethodName => "GetCustomSphereSdf";

// All the registered properties & uniforms of the sdf that will be synced between Raymarcher session and the shader
public override ISDFEntity.SDFUniformField[] SdfUniformFields =>
  new ISDFEntity.SDFUniformField[1]
  {
   new ISDFEntity.SDFUniformField(nameof(sphereRadius), ISDFEntity.SDFUniformType.Float)
  };

// Method that is automatically invoked every Raymarcher drawcall. Pick the session material and set the desired property based on the current iteration index (this is set internally as well)
public override void PushSdfEntityToShader(in Material raymarcherSessionMaterial, in string iterationIndex)
 {
  raymarcherSessionMaterial.SetFloat(nameof(sphereRadius) + iterationIndex, Mathf.Abs(sphereRadius));
 }
}
The example script demonstrates the creation of a simple SDF - a sphere.
Once this object inherits from RMSdfObjectBase, the Raymarcher will automatically detect the object upon its creation.
As the object is pushed to the Raymarcher's Object Buffer, you need to recompile the Object Buffer to synchronize the session shader with the current data (further steps will be shown in the editor).
Built-in Raymarcher Parameters & Compiled Format
Raymarcher compiler defines SDF objects in a specific format, allowing you to specify an object's signed distance function and color.
Additionally, a readonly parameter, the current marched position, is available for reference.

As observed in the SdfMethodBody in the example above, specific constants like 'VARCONST_RESULT' or 'VARCONST_POSITION' are used.
These constants represent macros defined by Raymarcher's compiler, included in every SDF method's parameters.
Consequently, when creating a custom SDF object, the compiler will generate it as follows:
float4 ... sdf_method_name ...(float3 p, half3 color, ... sdf_parameters ...)
{
float result;
... sdf_method_body ...
return float4(result, color.rgb);
}
Use the VARCONST_POSITION to access the currently raymarched position.
Use the VARCONST_COLOR to modify or retrieve the current object's color.
Use the VARCONST_RESULT to define the actual SDF formula. This is crucial for creating an SDF, as demonstrated in the example above and for all other built-in SDF objects.

Therefore, the example code provided earlier would be converted to the shader code as follows:
float4 GetCustomSphereSdf(float3 p, half3 color, half sphereRadius)
{
float result;
result = length(p) - sphereRadius;
return float4(result, color.rgb);
}