杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SCNVector3Extensions.swift - Copyright 2024 SwifterSwift
 
#if os(OSX)
/// SwifterSwift: CGFloat.
public typealias SceneKitFloat = CGFloat
#else
/// SwifterSwift: Float.
public typealias SceneKitFloat = Float
#endif
 
#if canImport(SceneKit)
import SceneKit
 
// MARK: - Methods
 
public extension SCNVector3 {
    /// SwifterSwift: Returns the absolute values of the vector's components.
    ///
    ///         SCNVector3(2, -3, -6).abs -> SCNVector3(2, 3, 6)
    ///
    var absolute: SCNVector3 {
        return SCNVector3(abs(x), abs(y), abs(z))
    }
 
    /// SwifterSwift: Returns the length of the vector.
    ///
    ///         SCNVector3(2, 3, 6).length -> 7
    ///
    var length: SceneKitFloat {
        return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))
    }
 
    /// SwifterSwift: Returns the unit or normalized vector where `length = 1`.
    ///
    ///     SCNVector3(2, 3, 6).normalized  -> SCNVector3(2/7, 3/7, 6/7)
    ///
    var normalized: SCNVector3 {
        let length = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))
        return SCNVector3(x / length, y / length, z / length)
    }
}
 
// MARK: - Operators
 
public extension SCNVector3 {
    /// SwifterSwift: Add two SCNVector3s.
    ///
    ///     SCNVector3(10, 10, 10) + SCNVector3(10, 20, -30) -> SCNVector3(20, 30, -20)
    ///
    /// - Parameters:
    ///   - lhs: SCNVector3 to add to.
    ///   - rhs: SCNVector3 to add.
    /// - Returns: result of addition of the two given SCNVector3s.
    static func + (lhs: SCNVector3, rhs: SCNVector3) -> SCNVector3 {
        return SCNVector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z)
    }
 
    /// SwifterSwift: Add a SCNVector3 to self.
    ///
    ///     SCNVector3(10, 10, 10) += SCNVector3(10, 20, -30) -> SCNVector3(20, 30, -20)
    ///
    /// - Parameters:
    ///   - lhs: `self`.
    ///   - rhs: SCNVector3 to add.
    static func += (lhs: inout SCNVector3, rhs: SCNVector3) {
        lhs.x += rhs.x
        lhs.y += rhs.y
        lhs.z += rhs.z
    }
 
    /// SwifterSwift: Subtract two SCNVector3s.
    ///
    ///     SCNVector3(10, 10, 10) - SCNVector3(10, 20, -30) -> SCNVector3(0, -10, 40)
    ///
    /// - Parameters:
    ///   - lhs: SCNVector3 to subtract from.
    ///   - rhs: SCNVector3 to subtract.
    /// - Returns: result of subtract of the two given SCNVector3s.
    static func - (lhs: SCNVector3, rhs: SCNVector3) -> SCNVector3 {
        return SCNVector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z)
    }
 
    /// SwifterSwift: Subtract a SCNVector3 from self.
    ///
    ///     SCNVector3(10, 10, 10) -= SCNVector3(10, 20, -30) -> SCNVector3(0, -10, 40)
    ///
    /// - Parameters:
    ///   - lhs: `self`.
    ///   - rhs: SCNVector3 to subtract.
    static func -= (lhs: inout SCNVector3, rhs: SCNVector3) {
        lhs.x -= rhs.x
        lhs.y -= rhs.y
        lhs.z -= rhs.z
    }
 
    /// SwifterSwift: Multiply a SCNVector3 with a scalar
    ///
    ///     SCNVector3(10, 20, -30) * 3 -> SCNVector3(30, 60, -90)
    ///
    /// - Parameters:
    ///   - vector: SCNVector3 to multiply.
    ///   - scalar: scalar value.
    /// - Returns: result of multiplication of the given SCNVector3 with the scalar.
    static func * (vector: SCNVector3, scalar: SceneKitFloat) -> SCNVector3 {
        return SCNVector3(vector.x * scalar, vector.y * scalar, vector.z * scalar)
    }
 
    /// SwifterSwift: Multiply self with a scalar
    ///
    ///     SCNVector3(10, 20, -30) *= 3 -> SCNVector3(30, 60, -90)
    ///
    /// - Parameters:
    ///   - vector: `self`.
    ///   - scalar: scalar value.
    /// - Returns: result of multiplication of the given CGPoint with the scalar.
    static func *= (vector: inout SCNVector3, scalar: SceneKitFloat) {
        vector.x *= scalar
        vector.y *= scalar
        vector.z *= scalar
    }
 
    /// SwifterSwift: Multiply a scalar with a SCNVector3.
    ///
    ///     3 * SCNVector3(10, 20, -30) -> SCNVector3(30, 60, -90)
    ///
    /// - Parameters:
    ///   - scalar: scalar value.
    ///   - vector: SCNVector3 to multiply.
    /// - Returns: result of multiplication of the given CGPoint with the scalar.
    static func * (scalar: SceneKitFloat, vector: SCNVector3) -> SCNVector3 {
        return SCNVector3(vector.x * scalar, vector.y * scalar, vector.z * scalar)
    }
 
    /// SwifterSwift: Divide a SCNVector3 with a scalar.
    ///
    ///     SCNVector3(10, 20, -30) / 3 -> SCNVector3(3/10, 0.15, -30)
    ///
    /// - Parameters:
    ///   - vector: SCNVector3 to divide.
    ///   - scalar: scalar value.
    /// - Returns: result of division of the given SCNVector3 with the scalar.
    static func / (vector: SCNVector3, scalar: SceneKitFloat) -> SCNVector3 {
        return SCNVector3(vector.x / scalar, vector.y / scalar, vector.z / scalar)
    }
 
    /// SwifterSwift: Divide self with a scalar.
    ///
    ///     SCNVector3(10, 20, -30) /= 3 -> SCNVector3(3/10, 0.15, -30)
    ///
    /// - Parameters:
    ///   - vector: `self`.
    ///   - scalar: scalar value.
    /// - Returns: result of division of the given CGPoint with the scalar.
    static func /= (vector: inout SCNVector3, scalar: SceneKitFloat) {
        vector = SCNVector3(vector.x / scalar, vector.y / scalar, vector.z / scalar)
    }
}
 
#endif