Skip to content

Commit

Permalink
Prevent selection of objects when backface culling enabled
Browse files Browse the repository at this point in the history
Prevent selecting an object if its normal is pointed away from the camera and backface culling is enabled
  • Loading branch information
ItsPepperpot committed Sep 17, 2024
1 parent 2d1e9b3 commit 9b73152
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public override bool ShouldDraw(SharpRenderer renderer)
[Browsable(false)]
public override bool SpecialBlendMode => true;

protected override float? TriangleIntersection(Ray r)
protected override float? TriangleIntersection(Ray r, bool hitBackFaces = true)
{
if (Texture == 0)
return base.TriangleIntersection(r);
Expand Down
22 changes: 19 additions & 3 deletions IndustrialPark/Assets/Shared/EntityAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using SharpDX.Direct3D11;
using static IndustrialPark.ArchiveEditorFunctions;

namespace IndustrialPark
Expand Down Expand Up @@ -325,19 +326,34 @@ public virtual void Draw(SharpRenderer renderer)

public virtual float? GetIntersectionPosition(SharpRenderer renderer, Ray ray)
{
var backfaceCulling = renderer.device.NormalCullMode == CullMode.Back;
// If backface culling is enabled, rays should pass through the back of the faces

if (ShouldDraw(renderer) && ray.Intersects(ref boundingBox))
return triangles == null ? TriangleIntersection(ray, SharpRenderer.cubeTriangles, SharpRenderer.cubeVertices, world) : TriangleIntersection(ray);
return triangles == null ? TriangleIntersection(ray, SharpRenderer.cubeTriangles, SharpRenderer.cubeVertices, world) : TriangleIntersection(ray, !backfaceCulling);
return null;
}

protected virtual float? TriangleIntersection(Ray ray)
protected virtual float? TriangleIntersection(Ray ray, bool hitBackfaces = true)
{
float? smallestDistance = null;

foreach (RenderWareFile.Triangle t in triangles)
if (ray.Intersects(ref vertices[t.vertex1], ref vertices[t.vertex2], ref vertices[t.vertex3], out float distance))
if (ray.Intersects(ref vertices[t.vertex1], ref vertices[t.vertex2], ref vertices[t.vertex3],
out float distance))
{
// If backface culling is enabled, rays should pass through the back of the faces
if (!hitBackfaces)
{
var normal = Vector3.Cross(vertices[t.vertex2] - vertices[t.vertex1], vertices[t.vertex3] - vertices[t.vertex1]);
if (Vector3.Dot(normal, ray.Direction) > 0)
continue;
}

if (smallestDistance == null || distance < smallestDistance)
smallestDistance = distance;
}


return smallestDistance;
}
Expand Down
5 changes: 5 additions & 0 deletions IndustrialPark/SharpDX/SharpDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ public void ApplyRasterState()

private CullMode normalCullMode = CullMode.Back;
private FillMode normalFillMode = FillMode.Solid;

public CullMode NormalCullMode
{
get => normalCullMode;
}

public void SetNormalCullMode(CullMode cullMode)
{
Expand Down

0 comments on commit 9b73152

Please sign in to comment.