-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppState.swift
116 lines (96 loc) · 4.58 KB
/
AppState.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
An extension that holds application state functions related to manually sorting transparent objects to ensure correct rendering.
*/
import ARKit
import Combine
import Foundation
import RealityKit
import SwiftSplashTrackPieces
import UIKit
public extension AppState {
/// Utility function that adds a model sort group component to an entity.
fileprivate func setEntityDrawOrder(_ entity: Entity, _ sortOrder: Int32, _ sortGroup: ModelSortGroup) {
entity.forEachDescendant(withComponent: ModelComponent.self) { modelEntity, model in
logger.info("Setting sort order of \(sortOrder) of \(entity.name), child entity: \(modelEntity.name)")
let component = ModelSortGroupComponent(group: sortGroup, order: sortOrder)
modelEntity.components.set(component)
}
}
/// Manually specifies sort ordering for the transparent start piece meshes.
func handleStartPieceTransparency(_ startPiece: Entity) {
let group = ModelSortGroup()
// Opaque fish parts.
if let entity = startPiece.findEntity(named: fishIdleAnimModelName) {
setEntityDrawOrder(entity, 1, group)
}
if let entity = startPiece.findEntity(named: fishRideAnimModelName) {
setEntityDrawOrder(entity, 2, group)
}
// Transparent fish parts.
if let entity = startPiece.findEntity(named: fishGlassIdleAnimModelName) {
setEntityDrawOrder(entity, 3, group)
}
if let entity = startPiece.findEntity(named: fishGlassRideAnimModelName) {
setEntityDrawOrder(entity, 4, group)
}
// Water.
if let entity = startPiece.findEntity(named: sortOrderWaterName) {
setEntityDrawOrder(entity, 5, group)
}
// Glass globe.
if let entity = startPiece.findEntity(named: sortOrderGlassGlobeName) {
setEntityDrawOrder(entity, 6, group)
}
// Selection glow.
if let entity = startPiece.findEntity(named: startGlowName) {
setEntityDrawOrder(entity, 7, group)
}
}
/// Manually specifies sort ordering for transparent track pieces.
func handleTrackPieceTransparency(_ trackPiece: Entity) {
// Find opaque fish parts and set sort order to 1.
trackPiece.forEachDescendant(withSuffix: sortOrderFishGlassSuffix) { entity in
setEntityDrawOrder(entity, 1, trackPieceSortOrderGroup)
}
// Find transparent fish parts and set sort order to 2.
trackPiece.forEachDescendant(withSuffix: sortOrderFishSuffix) { entity in
setEntityDrawOrder(entity, 2, trackPieceSortOrderGroup)
}
// Find the bottom track piece and set sort order to 3.
trackPiece.forEachDescendant(withSuffix: sortOrderTrackBottomSuffix) { entity in
setEntityDrawOrder(entity, 3, trackPieceSortOrderGroup)
}
// Find water parts and set sort order to 4.
trackPiece.forEachDescendant(withSuffix: sortOrderWaterSuffix) { entity in
setEntityDrawOrder(entity, 4, trackPieceSortOrderGroup)
}
// Find the glass top piece and set sort order to 5.
trackPiece.forEachDescendant(withSuffix: sortOrderTrackTopsuffix) { entity in
setEntityDrawOrder(entity, 5, trackPieceSortOrderGroup)
}
// Find the bottom track piece and set sort order to 6.
trackPiece.forEachDescendant(withSuffix: sortOrderTrackBottomGlowSuffix) { entity in
setEntityDrawOrder(entity, 6, trackPieceSortOrderGroup)
}
// Find the top glow piece and set sort order to 7.
trackPiece.forEachDescendant(withSuffix: sortOrderTrackGlowSuffix) { entity in
setEntityDrawOrder(entity, 7, trackPieceSortOrderGroup)
}
}
/// Manually specifies sort ordering for the transparent goal piece meshes.
func handleEndPieceTransparency(_ endPiece: Entity) {
let group = ModelSortGroup()
if let entity = endPiece.findEntity(named: sortOrderEndWaterName) {
setEntityDrawOrder(entity, 1, group)
}
if let entity = endPiece.findEntity(named: sortOrderEndSlideName) {
let component = ModelSortGroupComponent(group: group, order: 2)
entity.components.set(component)
}
if let entity = endPiece.findEntity(named: sortOrderEndSlideTopName) {
setEntityDrawOrder(entity, 3, group)
}
}
}