Skip to content

Commit

Permalink
Merge pull request #12 from maxxfrazer/color-fix
Browse files Browse the repository at this point in the history
- Fix colored style not working when applied
  • Loading branch information
maxxfrazer authored Nov 19, 2020
2 parents cf36b43 + 2b54027 commit 88112e6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
swift package generate-xcodeproj
xcodebuild clean build -project $PROJECT -scheme $SCHEME -sdk $SDK CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO ONLY_ACTIVE_ARCH=NO
env:
DEVELOPER_DIR: /Applications/Xcode_11.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_12.app/Contents/Developer
PROJECT: FocusEntity.xcodeproj
SCHEME: FocusEntity-Package
SDK: iphoneos
3 changes: 2 additions & 1 deletion FocusEntity-Example/FocusEntity-Example/FocusARView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class FocusARView: ARView {
required init(frame frameRect: CGRect) {
super.init(frame: frameRect)
self.setupConfig()
self.focusEntity = FocusEntity(on: self, style: .classic)
self.focusEntity = FocusEntity(on: self, focus: .classic)
// self.focusEntity = FocusEntity(on: self, style: .colored(onColor: .red, offColor: .blue, nonTrackingColor: .orange))
}

func setupConfig() {
Expand Down
7 changes: 5 additions & 2 deletions Sources/FocusEntity/FocusEntity+Colored.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ import RealityKit
public extension FocusEntity {

internal func coloredStateChanged() {
guard let coloredStyle = self.focusEntity.coloredStyle else {
guard let coloredStyle = self.focus.coloredStyle else {
return
}
var endColor: Material.Color = .clear
if self.state == .initializing {
endColor = coloredStyle.otherColor
endColor = coloredStyle.nonTrackingColor
} else {
endColor = self.onPlane ? coloredStyle.onColor : coloredStyle.offColor
}
if self.fillPlane?.model?.materials.count == 0 {
self.fillPlane?.model?.materials = [SimpleMaterial()]
}
self.fillPlane?.model?.materials[0] = SimpleMaterial(
color: endColor, isMetallic: false
)
Expand Down
22 changes: 12 additions & 10 deletions Sources/FocusEntity/FocusEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import Combine
public protocol HasFocusEntity: Entity {}

public extension HasFocusEntity {
var focusEntity: FocusEntityComponent {
var focus: FocusEntityComponent {
get { self.components[FocusEntityComponent.self] ?? .classic }
set { self.components[FocusEntityComponent.self] = newValue }
}
var isOpen: Bool {
get { self.focusEntity.isOpen }
set { self.focusEntity.isOpen = newValue }
get { self.focus.isOpen }
set { self.focus.isOpen = newValue }
}
internal var segments: [FocusEntity.Segment] {
get { self.focusEntity.segments }
set { self.focusEntity.segments = newValue }
get { self.focus.segments }
set { self.focus.segments = newValue }
}
}

Expand Down Expand Up @@ -186,11 +186,12 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity {
// MARK: - Initialization

public convenience init(on arView: ARView, style: FocusEntityComponent.Style) {
self.init(on: arView, style: FocusEntityComponent(style: style))
self.init(on: arView, focus: FocusEntityComponent(style: style))
}
public required init(on arView: ARView, style: FocusEntityComponent) {
public required init(on arView: ARView, focus: FocusEntityComponent) {
self.arView = arView
super.init()
self.focus = focus
self.name = "FocusEntity"
self.orientation = simd_quatf(angle: .pi / 2, axis: [1, 0, 0])

Expand All @@ -201,13 +202,14 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity {
self.delegate?.toInitializingState?()
arView.scene.addAnchor(self)
self.setAutoUpdate(to: true)
switch self.focusEntity.style {
switch self.focus.style {
case .colored(_, _, _, let mesh):
let fillPlane = ModelEntity(mesh: mesh)
self.positioningEntity.addChild(fillPlane)
self.fillPlane = fillPlane
self.coloredStateChanged()
case .classic:
guard let classicStyle = self.focusEntity.classicStyle else {
guard let classicStyle = self.focus.classicStyle else {
return
}
self.setupClassic(classicStyle)
Expand Down Expand Up @@ -260,7 +262,7 @@ open class FocusEntity: Entity, HasAnchoring, HasFocusEntity {
///
/// - Parameter newPlane: If the entity is directly on a plane, is it a new plane to track
public func stateChanged(newPlane: Bool = false) {
switch self.focusEntity.style {
switch self.focus.style {
case .colored:
self.coloredStateChanged()
case .classic:
Expand Down
23 changes: 18 additions & 5 deletions Sources/FocusEntity/FocusEntityComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ public struct FocusEntityComponent: Component {
case classic(color: Material.Color)
case colored(
onColor: Material.Color, offColor: Material.Color,
otherColor: Material.Color, mesh: MeshResource
nonTrackingColor: Material.Color,
mesh: MeshResource = MeshResource.generatePlane(width: 0.1, depth: 0.1)
)

internal struct Classic {
var color: Material.Color
}

/// When using colored style, first material of a mesh will be replaced with the chosen color
internal struct Colored {
/// Color when tracking the surface of a known plane
var onColor: Material.Color
/// Color when tracking an estimated plane
var offColor: Material.Color
var otherColor: Material.Color
/// Color when no surface tracking is achieved
var nonTrackingColor: Material.Color
var mesh: MeshResource
}
}
Expand All @@ -39,14 +45,21 @@ public struct FocusEntityComponent: Component {

var coloredStyle: Style.Colored? {
switch self.style {
case .colored(let onColor, let offColor, let otherColor, let mesh):
return Style.Colored(onColor: onColor, offColor: offColor, otherColor: otherColor, mesh: mesh)
case .colored(let onColor, let offColor, let nonTrackingColor, let mesh):
return Style.Colored(onColor: onColor, offColor: offColor, nonTrackingColor: nonTrackingColor, mesh: mesh)
default:
return nil
}
}
public static let classic = FocusEntityComponent(style: .classic(color: #colorLiteral(red: 1, green: 0.8, blue: 0, alpha: 1)))
public static let plane = FocusEntityComponent(style: .colored(onColor: .green, offColor: .red, otherColor: Material.Color.orange.withAlphaComponent(0.2), mesh: FocusEntityComponent.defaultPlane))
public static let plane = FocusEntityComponent(
style: .colored(
onColor: .green,
offColor: .orange,
nonTrackingColor: Material.Color.red.withAlphaComponent(0.2),
mesh: FocusEntityComponent.defaultPlane
)
)
internal var isOpen = true
internal var segments: [FocusEntity.Segment] = []

Expand Down

0 comments on commit 88112e6

Please sign in to comment.