Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Components/Components/Containers/ChiselModelComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,19 @@ public sealed class ChiselModelComponent : ChiselNodeComponent


public ChiselGeneratedRenderSettings RenderSettings { get { return renderSettings; } }
public ChiselGeneratedColliderSettings ColliderSettings { get { return colliderSettings; } }
public ChiselGeneratedColliderSettings ColliderSettings { get { return colliderSettings; } }

/// <summary>
/// The operation of a brush drawn out of one of the model's surfaces when none is chosen: pulled out of the surface
/// (a positive height) it adds, pushed into it it cuts. With the subtractive workflow the model's brushes are the
/// empty space, so there it is the other way round.
/// </summary>
public CSGOperationType OperationOutOfSurface(float height)
{
var pushedIn = height < 0;
var subtractiveWorkflow = renderSettings != null && renderSettings.subtractiveWorkflow;
return pushedIn != subtractiveWorkflow ? CSGOperationType.Subtractive : CSGOperationType.Additive;
}
public override CSGTreeNode TopTreeNode { get { return Node; } protected set { Node = (CSGTree)value; } }
public override bool IsContainer { get { return IsActive; } }
public bool IsInitialized { get; private set; } = false;
Expand Down
6 changes: 4 additions & 2 deletions Components/Components/Decals/ChiselDecalComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ public ChiselExtrudedShapeComponent ExtrudeIntoBrush(float height)
}

/// <summary>How far a brush <see cref="ExtrudeIntoBrush"/> made reaches out of its surface (into it below zero),
/// adding or cutting to match</summary>
/// adding or cutting to match (<see cref="ChiselModelComponent.OperationOutOfSurface"/>)</summary>
public static void SetExtrusionHeight(ChiselExtrudedShapeComponent extruded, float height)
{
if (extruded == null)
return;
extruded.Operation = height > 0 ? CSGOperationType.Additive : CSGOperationType.Subtractive;
var model = extruded.GetComponentInParent<ChiselModelComponent>();
extruded.Operation = model ? model.OperationOutOfSurface(height) :
height > 0 ? CSGOperationType.Additive : CSGOperationType.Subtractive;
extruded.definition.path.segments[1].position = ChiselPathPoint.DefaultDirection * height;
extruded.OnValidate();
// as the placement tools do while dragging, so it is built again
Expand Down
11 changes: 6 additions & 5 deletions Components/Components/Decals/ChiselSurfaceShapes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public static bool NearestOnOutline(IReadOnlyList<Vector3> outline, Vector3 poin
}

/// <summary>
/// A polygon decal as its surface has it now: its frame laid on the surface's plane (world space, Z into the surface),
/// and its corners in that frame's XY, the anchored ones where their edges are now. False when it has no surface
/// to be on: then it is where its transform and outline put it.
/// A polygon decal as its surface has it now: its frame laid on the surface's plane (world space, Z into the surface
/// from the side the decal is on), and its corners in that frame's XY, the anchored ones where their edges are now.
/// False when it has no surface to be on: then it is where its transform and outline put it.
/// </summary>
public static bool Resolve(ChiselDecalComponent decal, out Matrix4x4 frame, List<Vector2> corners)
{
Expand All @@ -165,9 +165,10 @@ public static bool Resolve(ChiselDecalComponent decal, out Matrix4x4 frame, List
!SurfaceOutline(targets[0].brush, targets[0].surfaceIndex, outline, out var plane))
return false;

// the decal's frame turned onto the surface's plane, and moved onto it
// the decal's frame turned onto the surface's plane, and moved onto it. It stays on the side it was drawn on: a
// surface drawn turned round (a room's wall) is seen, and drawn on, from behind its brush's plane
var transform = decal.transform;
var forward = -plane.normal;
var forward = Vector3.Dot(transform.forward, plane.normal) > 0 ? plane.normal : -plane.normal;
var turn = Quaternion.FromToRotation(transform.forward, forward);
var rotation = turn * transform.rotation;
var position = plane.ClosestPointOnPlane(transform.position);
Expand Down
21 changes: 14 additions & 7 deletions Components/Query/ChiselHotspotRepeats.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Chisel.Core;
using Unity.Entities;
using Unity.Mathematics;

namespace Chisel.Components
{
// A hotspot surface's repeats as its last build laid them
public static class ChiselHotspotRepeats
{
// The section input a surface was last built with; false when it has none
public static bool TryGetSectionInput(SurfaceReference surface, out HotspotSectionInput input)
// What the last build of the surface's brush drew; false when it drew nothing yet or the surface isn't in it
internal static bool TryGetBuilt(SurfaceReference surface, out BlobAssetReference<ChiselBrushRenderBuffer> buffer)
{
input = default;
buffer = default;
if (surface == null || !surface.node || !surface.brush.Valid)
return false;
var model = surface.node.hierarchyItem.Model;
Expand All @@ -21,12 +22,18 @@ public static bool TryGetSectionInput(SurfaceReference surface, out HotspotSecti
var data = lookups[model.Node];
data.lastJobHandle.Complete();
if (!data.brushRenderBufferLookup.IsCreated ||
!data.brushRenderBufferLookup.TryGetValue(CompactHierarchyManager.GetCompactNodeID(surface.brush), out var buffer) || !buffer.IsCreated)
!data.brushRenderBufferLookup.TryGetValue(CompactHierarchyManager.GetCompactNodeID(surface.brush), out buffer) || !buffer.IsCreated)
return false;
ref var surfaces = ref buffer.Value.surfaces;
if (surface.surfaceIndex < 0 || surface.surfaceIndex >= surfaces.Length)
return surface.surfaceIndex >= 0 && surface.surfaceIndex < buffer.Value.surfaces.Length;
}

// The section input a surface was last built with; false when it has none
public static bool TryGetSectionInput(SurfaceReference surface, out HotspotSectionInput input)
{
input = default;
if (!TryGetBuilt(surface, out var buffer))
return false;
input = surfaces[surface.surfaceIndex].sectionInput;
input = buffer.Value.surfaces[surface.surfaceIndex].sectionInput;
return input.count > 0;
}

Expand Down
28 changes: 25 additions & 3 deletions Components/Query/ChiselTextureLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,31 @@ public static SurfaceDetails Toggled(SurfaceReference surface, SurfaceDetails de
var to = toWorld ? worldToPlane : objectToPlane;
details.UV0 = (UVMatrix)math.mul((float4x4)details.UV0, math.mul(from, math.inverse(to)));

details.hotspot = ToggledHotspot(surface, details.hotspot, toWorld, treePlane.xyz, math.normalizesafe(plane.xyz), nodeToTree, treeToNode);
// a hotspot tile is laid out as seen from the side the surface is drawn on
var localNormal = math.normalizesafe(plane.xyz);
var turned = IsDrawnTurned(surface, treePlane.xyz);
details.hotspot = ToggledHotspot(surface, details.hotspot, toWorld, turned ? -treePlane.xyz : treePlane.xyz, localNormal,
turned ? -localNormal : localNormal, nodeToTree, treeToNode);
return Flip(details, toWorld);
}

// Whether the surface was last drawn turned round, seen from behind its brush's plane (treeNormal): every vertex
// of it facing the other way, as GenerateSurfaceTrianglesJob decides it
static bool IsDrawnTurned(SurfaceReference surface, float3 treeNormal)
{
if (!ChiselHotspotRepeats.TryGetBuilt(surface, out var buffer))
return false;
ref var vertices = ref buffer.Value.surfaces[surface.surfaceIndex].renderVertices;
if (vertices.Length == 0)
return false;
for (int v = 0; v < vertices.Length; v++)
{
if (math.dot(vertices[v].normal, treeNormal) >= 0)
return false;
}
return true;
}

static SurfaceDetails Flip(SurfaceDetails details, bool toWorld)
{
if (toWorld) details.detailFlags |= SurfaceDetailFlags.TextureIsInWorldSpace;
Expand All @@ -46,8 +67,9 @@ static SurfaceDetails Flip(SurfaceDetails details, bool toWorld)
// A hotspot surface's settings in the other space: the turn between the object's frame and the world's, its
// density in the brush's own units or the tree's, and its repeats' offset from the inset corner or the frame's
// origin (from its last build)
// (normal: the drawn side's, in the tree; localNormal: the brush's plane's; ownNormal: that facing the drawn side)
static HotspotSurface ToggledHotspot(SurfaceReference surface, HotspotSurface hotspot, bool toWorld, float3 normal, float3 localNormal,
float4x4 nodeToTree, float4x4 treeToNode)
float3 ownNormal, float4x4 nodeToTree, float4x4 treeToNode)
{
HotspotMatcher.Frame(normal, localNormal, nodeToTree, 0, false, out _, out var objectUp);
HotspotMatcher.Frame(normal, 0, false, out var worldRight, out var worldUp);
Expand All @@ -56,7 +78,7 @@ static HotspotSurface ToggledHotspot(SurfaceReference surface, HotspotSurface ho
var rotation = hotspot.rotation + (toWorld ? delta : -delta);
rotation -= 360 * math.floor(rotation / 360);
// the brush's own units along the frame as locked (its rotation there)
var units = HotspotMatcher.OwnUnits(normal, localNormal, treeToNode, toWorld ? hotspot.rotation : rotation, hotspot.mirror);
var units = HotspotMatcher.OwnUnits(normal, ownNormal, treeToNode, toWorld ? hotspot.rotation : rotation, hotspot.mirror);
hotspot.rotation = rotation;
var density = hotspot.Density * (toWorld ? units : 1 / units);
hotspot.density = density == 1 ? 0 : density;
Expand Down
50 changes: 50 additions & 0 deletions Components/Tests/ChiselHotspotMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,32 @@ public void TogglingTheLock_ChangesNothingOnScreen()
AssertSameLook(plainBefore, plainModel, plainFront, "plain, to the world");
}

// A room's wall, seen from inside, looks the same after its lock is toggled too
[Test]
public void TogglingTheLockOfARoomsWall_ChangesNothingOnScreen([Values] bool subtractiveWorkflow)
{
BorderOnB1(32);
var model = Model();
if (subtractiveWorkflow)
{
model.renderSettings.subtractiveWorkflow = true;
model.SyncModelSettingsStore();
} else
Box(model, new float3(-8, -6, -4), new float3(10, 8, 4));
var room = Box(model, new float3(-2, 0, -0.125f), new float3(6, 2, 0.125f), d => { d.hotspot.rotation = 90; return d; });
if (!subtractiveWorkflow)
room.Operation = CSGOperationType.Subtractive;
Place(room, new Vector3(0.3f, 0.2f, 0), Quaternion.Euler(0, 0, 20), new Vector3(0.75f, 0.75f, 0.75f));
// the wall the box's front is, seen from inside
var inside = math.normalize((float3)(room.transform.localRotation * Vector3.forward));
var before = Drawn(model, inside);
ToggleLock(room);
Assert.That(ChiselTextureLock.IsInWorldSpace(room.SurfaceDefinition.surfaces[0].surfaceDetails), Is.True);
AssertSameLook(before, model, inside, "to the world");
ToggleLock(room);
AssertSameLook(before, model, inside, "back to the object");
}

// Every uv drawn at a position on a face with this normal
static List<float2> UVsAt(ChiselModelComponent model, float3 position, float3 normal)
{
Expand Down Expand Up @@ -1464,6 +1490,30 @@ public void AMirroredWall_TakesItsTileMirrored()
AreEqual(new float2(1.0f, 1.0f), UVAt(model, new float3(-2, 2, -0.125f), kFront), "top left");
}

// The back wall of a room, seen from inside, takes its tile as a wall of a solid does from outside: not mirrored.
// The room is carved out of a solid by a subtractive brush, or is a brush of a model with the subtractive workflow
[Test]
public void ARoomsWall_TakesItsTileUnmirrored([Values] bool subtractiveWorkflow)
{
var model = Model();
if (subtractiveWorkflow)
{
model.renderSettings.subtractiveWorkflow = true;
model.SyncModelSettingsStore();
Box(model, new float3(-2, 0, -1.875f), new float3(2, 2, 0.125f));
} else
{
Box(model, new float3(-3, -1, -3), new float3(3, 3, 1));
var room = Box(model, new float3(-2, 0, -1.875f), new float3(2, 2, 0.125f));
room.Operation = CSGOperationType.Subtractive;
Update();
}
// B1 as on the 4 x 2 wall seen from the front (AWall4x2_TakesThe2To1Tile_UpStaysUp)
AreEqual(new float2(0.5f, 0.75f), UVAt(model, new float3(-2, 0, 0.125f), kFront), "bottom left");
AreEqual(new float2(1.0f, 0.75f), UVAt(model, new float3(2, 0, 0.125f), kFront), "bottom right");
AreEqual(new float2(0.5f, 1.0f), UVAt(model, new float3(-2, 2, 0.125f), kFront), "top left");
}

[Test]
public void ADenserWall_HasANarrowerBorder()
{
Expand Down
Loading
Loading