Skip to content

Commit

Permalink
-Added render layer filters to the render pipeline! You can now filte…
Browse files Browse the repository at this point in the history
…r out objects per-render.

-Also expanded render_to to include clear controls, and viewport options. #137
  • Loading branch information
maluoi committed Apr 18, 2021
1 parent 20b465a commit e260670
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 133 deletions.
12 changes: 9 additions & 3 deletions StereoKit/Assets/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ public bool Intersect(Ray modelSpaceRay, out Vec3 modelSpaceAt)
/// tint. If you're adventurous and don't need per-instance colors,
/// this is a great spot to pack in extra per-instance data for the
/// shader!</param>
public void Draw(Material material, Matrix transform, Color colorLinear)
=> NativeAPI.render_add_mesh(_inst, material._inst, transform, colorLinear);
/// <param name="layer">All visuals are rendered using a layer
/// bit-flag. By default, all layers are rendered, but this can be
/// useful for filtering out objects for different rendering
/// purposes! For example: rendering a mesh over the user's head from
/// a 3rd person perspective, but filtering it out from the 1st
/// person perspective.</param>
public void Draw(Material material, Matrix transform, Color colorLinear, RenderLayer layer = RenderLayer.Layer0)
=> NativeAPI.render_add_mesh(_inst, material._inst, transform, colorLinear, layer);

/// <summary>Adds a mesh to the render queue for this frame! If the
/// Hierarchy has a transform on it, that transform is combined with
Expand All @@ -146,7 +152,7 @@ public void Draw(Material material, Matrix transform, Color colorLinear)
/// <param name="transform">A Matrix that will transform the mesh
/// from Model Space into the current Hierarchy Space.</param>
public void Draw(Material material, Matrix transform)
=> NativeAPI.render_add_mesh(_inst, material._inst, transform, Color.White);
=> NativeAPI.render_add_mesh(_inst, material._inst, transform, Color.White, RenderLayer.Layer0);

/// <summary>Generates a plane on the XZ axis facing up that is
/// optionally subdivided, pre-sized to the given dimensions. UV
Expand Down
12 changes: 9 additions & 3 deletions StereoKit/Assets/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,22 @@ public void RecalculateBounds()
/// tint. If you're adventurous and don't need per-instance colors,
/// this is a great spot to pack in extra per-instance data for the
/// shader!</param>
public void Draw(Matrix transform, Color colorLinear)
=> NativeAPI.render_add_model(_inst, transform, colorLinear);
/// <param name="layer">All visuals are rendered using a layer
/// bit-flag. By default, all layers are rendered, but this can be
/// useful for filtering out objects for different rendering
/// purposes! For example: rendering a mesh over the user's head from
/// a 3rd person perspective, but filtering it out from the 1st
/// person perspective.</param>
public void Draw(Matrix transform, Color colorLinear, RenderLayer layer = RenderLayer.Layer0)
=> NativeAPI.render_add_model(_inst, transform, colorLinear, layer);

/// <summary>Adds this Model to the render queue for this frame! If
/// the Hierarchy has a transform on it, that transform is combined
/// with the Matrix provided here.</summary>
/// <param name="transform">A Matrix that will transform the Model
/// from Model Space into the current Hierarchy Space.</param>
public void Draw(Matrix transform)
=> NativeAPI.render_add_model(_inst, transform, Color.White);
=> NativeAPI.render_add_model(_inst, transform, Color.White, RenderLayer.Layer0);
#endregion

/// <summary>Looks for a Model asset that's already loaded, matching
Expand Down
34 changes: 18 additions & 16 deletions StereoKit/Native/NativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,24 @@ internal static class NativeAPI

///////////////////////////////////////////

[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_clip (float near_plane, float far_plane);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_fov (float field_of_view_degrees);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern Matrix render_get_cam_root ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_cam_root (Matrix cam_root);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_skytex (IntPtr sky_texture);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern IntPtr render_get_skytex ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_skylight (in SphericalHarmonics lighting_info);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern SphericalHarmonics render_get_skylight();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_clear_color(Color color);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_enable_skytex (bool show_sky);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern bool render_enabled_skytex();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_add_mesh (IntPtr mesh, IntPtr material, in Matrix transform, Color color);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_add_model (IntPtr model, in Matrix transform, Color color);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_blit (IntPtr to_rendertarget, IntPtr material);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_screenshot (Vec3 from_viewpt, Vec3 at, int width, int height, string file);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_to (IntPtr to_rendertarget, in Matrix camera, in Matrix projection);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_clip (float near_plane, float far_plane);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_fov (float field_of_view_degrees);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern Matrix render_get_cam_root ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_cam_root (Matrix cam_root);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_skytex (IntPtr sky_texture);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern IntPtr render_get_skytex ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_skylight (in SphericalHarmonics lighting_info);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern RenderLayer render_get_filter ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_filter (RenderLayer layer_filter);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern SphericalHarmonics render_get_skylight ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_set_clear_color(Color color);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_enable_skytex (bool show_sky);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern bool render_enabled_skytex ();
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_add_mesh (IntPtr mesh, IntPtr material, in Matrix transform, Color color, RenderLayer layer);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_add_model (IntPtr model, in Matrix transform, Color color, RenderLayer layer);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_blit (IntPtr to_rendertarget, IntPtr material);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_screenshot (Vec3 from_viewpt, Vec3 at, int width, int height, string file);
[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_to (IntPtr to_rendertarget, in Matrix camera, in Matrix projection, RenderLayer layer_filter, RenderClear clear, Rect viewport);
//[DllImport(dll, CharSet = cSet, CallingConvention = call)] public static extern void render_get_device (void **device, void **context);

///////////////////////////////////////////
Expand Down
32 changes: 32 additions & 0 deletions StereoKit/Native/NativeTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ public Vertex(Vec3 position, Vec3 normal, Vec2 textureCoordinates, Color32 color
public struct Rect
{
public float x, y, width, height;
public Rect(float x, float y, float width, float height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

/// <summary>Textures come in various types and flavors! These are bit-flags
Expand Down Expand Up @@ -564,6 +571,31 @@ public LinePoint(Vec3 point, Color32 color, float thickness)
}
}

public enum RenderLayer
{
Layer0 = 1 << 0,
Layer1 = 1 << 1,
Layer2 = 1 << 2,
Layer3 = 1 << 3,
Layer4 = 1 << 4,
Layer5 = 1 << 5,
Layer6 = 1 << 6,
Layer7 = 1 << 7,
Layer8 = 1 << 8,
Layer9 = 1 << 9,
Vfx = 1 << 10,
All = 0xFFFF,
AllRegular = Layer0 | Layer1 | Layer2 | Layer3 | Layer4 | Layer5 | Layer6 | Layer7 | Layer8 | Layer9,
}

public enum RenderClear
{
None = 0,
Color = 1 << 0,
Depth = 1 << 1,
All = Color|Depth
}

/// <summary>What type of device is the source of the pointer? This is a bit-flag that can
/// contain some input source family information.</summary>
[Flags]
Expand Down
49 changes: 41 additions & 8 deletions StereoKit/Systems/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public static bool EnableSky
set => NativeAPI.render_enable_skytex(value);
}

/// <summary>By default, StereoKit renders all layers. This is a bit
/// flag that allows you to change which layers StereoKit renders for
/// the primary viewpoint. To change what layers a visual is on, use
/// a Draw method that includes a RenderLayer as a parameter.</summary>
public static RenderLayer LayerFilter {
set => NativeAPI.render_set_filter(value);
get => NativeAPI.render_get_filter();
}

/// <summary>This is the gamma space color the renderer will clear
/// the screen to when beginning to draw a new frame. This is ignored
/// on displays with transparent screens</summary>
Expand Down Expand Up @@ -62,7 +71,7 @@ public static Matrix CameraRoot
/// <param name="transform">A Matrix that will transform the mesh
/// from Model Space into the current Hierarchy Space.</param>
public static void Add(Mesh mesh, Material material, Matrix transform)
=> NativeAPI.render_add_mesh(mesh._inst, material._inst, transform, Color.White);
=> NativeAPI.render_add_mesh(mesh._inst, material._inst, transform, Color.White, RenderLayer.Layer0);
/// <summary>Adds a mesh to the render queue for this frame! If the
/// Hierarchy has a transform on it, that transform is combined with
/// the Matrix provided here.</summary>
Expand All @@ -75,8 +84,14 @@ public static void Add(Mesh mesh, Material material, Matrix transform)
/// tint. If you're adventurous and don't need per-instance colors,
/// this is a great spot to pack in extra per-instance data for the
/// shader!</param>
public static void Add(Mesh mesh, Material material, Matrix transform, Color colorLinear)
=> NativeAPI.render_add_mesh(mesh._inst, material._inst, transform, colorLinear);
/// <param name="layer">All visuals are rendered using a layer
/// bit-flag. By default, all layers are rendered, but this can be
/// useful for filtering out objects for different rendering
/// purposes! For example: rendering a mesh over the user's head from
/// a 3rd person perspective, but filtering it out from the 1st
/// person perspective.</param>
public static void Add(Mesh mesh, Material material, Matrix transform, Color colorLinear, RenderLayer layer = RenderLayer.Layer0)
=> NativeAPI.render_add_mesh(mesh._inst, material._inst, transform, colorLinear, layer);

/// <summary>Adds a Model to the render queue for this frame! If the
/// Hierarchy has a transform on it, that transform is combined with
Expand All @@ -85,7 +100,7 @@ public static void Add(Mesh mesh, Material material, Matrix transform, Color col
/// <param name="transform">A Matrix that will transform the Model
/// from Model Space into the current Hierarchy Space.</param>
public static void Add(Model model, Matrix transform)
=> NativeAPI.render_add_model(model._inst, transform, Color.White);
=> NativeAPI.render_add_model(model._inst, transform, Color.White, RenderLayer.Layer0);
/// <summary>Adds a Model to the render queue for this frame! If the
/// Hierarchy has a transform on it, that transform is combined with
/// the Matrix provided here.</summary>
Expand All @@ -97,8 +112,14 @@ public static void Add(Model model, Matrix transform)
/// tint. If you're adventurous and don't need per-instance colors,
/// this is a great spot to pack in extra per-instance data for the
/// shader!</param>
public static void Add(Model model, Matrix transform, Color colorLinear)
=> NativeAPI.render_add_model(model._inst, transform, colorLinear);
/// <param name="layer">All visuals are rendered using a layer
/// bit-flag. By default, all layers are rendered, but this can be
/// useful for filtering out objects for different rendering
/// purposes! For example: rendering a mesh over the user's head from
/// a 3rd person perspective, but filtering it out from the 1st
/// person perspective.</param>
public static void Add(Model model, Matrix transform, Color colorLinear, RenderLayer layer = RenderLayer.Layer0)
=> NativeAPI.render_add_model(model._inst, transform, colorLinear, layer);

/// <summary>Set the near and far clipping planes of the camera!
/// These are important to z-buffer quality, especially when using
Expand Down Expand Up @@ -160,8 +181,20 @@ public static void Screenshot(Vec3 from, Vec3 at, int width, int height, string
/// geometry is flattened onto the draw surface. Normally, you'd use
/// Matrix.Perspective, and occasionally Matrix.Orthographic might be
/// helpful as well.</param>
public static void RenderTo(Tex toRendertarget, Matrix camera, Matrix projection)
=> NativeAPI.render_to(toRendertarget._inst, camera, projection);
/// <param name="layerFilter">This is a bit flag that allows you to
/// change which layers StereoKit renders for this particular render
/// viewpoint. To change what layers a visual is on, use a Draw
/// method that includes a RenderLayer as a parameter.</param>
/// <param name="clear">Describes if an how the rendertarget should
/// be cleared before rendering. Note that clearing the target is
/// unaffected by the viewport, so this will clean the entire
/// surface!</param>
/// <param name="viewport">Allows you to specify a region of the
/// rendertarget to draw to! This is in normalized coordinates, 0-1.
/// If the width of this value is zero, then this will render to the
/// entire texture.</param>
public static void RenderTo(Tex toRendertarget, Matrix camera, Matrix projection, RenderLayer layerFilter = RenderLayer.All, RenderClear clear = RenderClear.All, Rect viewport = default(Rect))
=> NativeAPI.render_to(toRendertarget._inst, camera, projection, layerFilter, clear, viewport);

}
}
Loading

0 comments on commit e260670

Please sign in to comment.