Skip to content

Commit

Permalink
Merge pull request #42 from maxxfrazer/unlit-color-enhancement
Browse files Browse the repository at this point in the history
Brilliant Colours
  • Loading branch information
maxxfrazer authored Jun 14, 2023
2 parents e7ca067 + 88f8ae1 commit 68d404d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
26 changes: 12 additions & 14 deletions Sources/FocusEntity/FocusEntity+Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,19 @@ extension FocusEntity {
guard let (camPos, camDir) = self.getCamVector() else {
return nil
}
let rcQuery = ARRaycastQuery(
origin: camPos, direction: camDir,
allowing: self.allowedRaycast, alignment: .any
)
let results = self.arView?.session.raycast(rcQuery) ?? []

// 1. Check for a result on an existing plane using geometry.
if let existingPlaneUsingGeometryResult = results.first(
where: { $0.target == .existingPlaneGeometry }
) {
return existingPlaneUsingGeometryResult
for target in self.allowedRaycasts {
let rcQuery = ARRaycastQuery(
origin: camPos, direction: camDir,
allowing: target, alignment: .any
)
let results = self.arView?.session.raycast(rcQuery) ?? []

// Check for a result matching target
if let result = results.first(
where: { $0.target == target }
) { return result }
}

// 2. As a fallback, check for a result on estimated planes.
return results.first(where: { $0.target == .estimatedPlane })
return nil
}
#endif

Expand Down
17 changes: 11 additions & 6 deletions Sources/FocusEntity/FocusEntity+Colored.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ public extension FocusEntity {
if self.fillPlane?.model?.materials.count == 0 {
self.fillPlane?.model?.materials = [SimpleMaterial()]
}
var modelMaterial = UnlitMaterial(color: .clear)
var modelMaterial: Material!
if #available(iOS 15, macOS 12, *) {
var mat = PhysicallyBasedMaterial()
switch endColor {
case .color(let uikitColour):
modelMaterial.color = .init(tint: uikitColour, texture: nil)
mat.baseColor = .init(tint: .black.withAlphaComponent(uikitColour.cgColor.alpha))
mat.emissiveColor = .init(color: uikitColour)
mat.emissiveIntensity = 2
case .texture(let tex):
modelMaterial.color = .init(tint: .white.withAlphaComponent(0.9999), texture: .init(tex))
mat.baseColor = .init(tint: .white.withAlphaComponent(0.9999), texture: .init(tex))
@unknown default: break
}
modelMaterial = mat
} else {
modelMaterial.baseColor = endColor
// Necessary for transparency.
modelMaterial.tintColor = Material.Color.white.withAlphaComponent(0.995)
var mat = UnlitMaterial(color: .clear)
mat.baseColor = endColor
mat.tintColor = .white.withAlphaComponent(0.9999)
modelMaterial = mat
}
self.fillPlane?.model?.materials[0] = modelMaterial
}
Expand Down
31 changes: 12 additions & 19 deletions Sources/FocusEntity/FocusEntity+Segment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ internal extension FocusEntity {
self.corner = corner
self.alignment = alignment

switch alignment {
case .vertical:
plane = ModelComponent(
mesh: .generatePlane(width: 1, depth: 1),
materials: [UnlitMaterial(color: color)]
)
case .horizontal:
plane = ModelComponent(
mesh: .generatePlane(width: 1, depth: 1),
materials: [UnlitMaterial(color: color)]
)
var mat: Material!
if #available(iOS 15.0, *) {
var phMat = PhysicallyBasedMaterial()
phMat.baseColor = .init(tint: .black)
phMat.emissiveColor = .init(color: color)
phMat.emissiveIntensity = 2
mat = phMat
} else {
// Fallback on earlier versions
mat = UnlitMaterial(color: color)
}
plane = ModelComponent(mesh: .generatePlane(width: 1, depth: 1), materials: [mat])

super.init()

switch alignment {
Expand All @@ -86,15 +87,7 @@ internal extension FocusEntity {
case .horizontal:
self.scale = [Segment.length, 1, Segment.thickness]
}
// self.orientation = .init(angle: .pi / 2, axis: [1, 0, 0])
self.name = name

// let material = plane.firstMaterial!
// material.diffuse.contents = FocusSquare.primaryColor
// material.isDoubleSided = true
// material.ambient.contents = UIColor.black
// material.lightingModel = .constant
// material.emission.contents = FocusSquare.primaryColor
model = plane
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/FocusEntity/FocusEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public extension HasFocusEntity {
set { self.focus.segments = newValue }
}
#if canImport(ARKit)
var allowedRaycast: ARRaycastQuery.Target {
get { self.focus.allowedRaycast }
set { self.focus.allowedRaycast = newValue }
var allowedRaycasts: [ARRaycastQuery.Target] {
get { self.focus.allowedRaycasts }
set { self.focus.allowedRaycasts = newValue }
}
#endif
}
Expand Down
10 changes: 1 addition & 9 deletions Sources/FocusEntity/FocusEntityComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public struct FocusEntityComponent: Component {
public internal(set) var isOpen = true
internal var segments: [FocusEntity.Segment] = []
#if !os(macOS)
public var allowedRaycast: ARRaycastQuery.Target = .estimatedPlane
public var allowedRaycasts: [ARRaycastQuery.Target] = [.existingPlaneGeometry, .estimatedPlane]
#endif

static var defaultPlane = MeshResource.generatePlane(
Expand All @@ -95,13 +95,5 @@ public struct FocusEntityComponent: Component {
/// - Parameter style: FocusEntityComponent Style, dictating how the FocusEntity will appear in different states.
public init(style: Style) {
self.style = style
// If the device has LiDAR, then default behaviour is to only allow
// existing detected planes
#if !os(macOS)
if #available(iOS 13.4, *),
ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
self.allowedRaycast = .existingPlaneGeometry
}
#endif
}
}

0 comments on commit 68d404d

Please sign in to comment.