|
| 1 | +// |
| 2 | +// Axis.swift |
| 3 | +// ShapeScript Viewer |
| 4 | +// |
| 5 | +// Created by Nick Lockwood on 23/08/2021. |
| 6 | +// Copyright © 2021 Nick Lockwood. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Euclid |
| 10 | +import SceneKit |
| 11 | +import ShapeScript |
| 12 | + |
| 13 | +struct Axes { |
| 14 | + let geometry: Geometry |
| 15 | + |
| 16 | + init(scale: Double, background: MaterialProperty?) { |
| 17 | + let textScale = 0.1 |
| 18 | + let color = Color(.underPageBackgroundColor) |
| 19 | + let brightness = background?.brightness(over: color) ?? color.brightness |
| 20 | + let lineColor = brightness > 0.5 ? Color.black : .white |
| 21 | + var material = Material.default |
| 22 | + material.color = lineColor |
| 23 | + geometry = Geometry(transform: Transform(scale: Vector(size: [scale])), children: [ |
| 24 | + Geometry(type: .path(.line(-.x, .x))), |
| 25 | + Geometry(type: .fill(Path.text("+X")), transform: Transform( |
| 26 | + offset: .x - Vector(-0.5, 0.25, 0) * textScale, |
| 27 | + scale: Vector(size: [textScale]) |
| 28 | + ), material: material), |
| 29 | + Geometry(type: .fill(Path.text("-X")), transform: Transform( |
| 30 | + offset: -.x - Vector(1.5, 0.25, 0) * textScale, |
| 31 | + scale: Vector(size: [textScale]) |
| 32 | + ), material: material), |
| 33 | + Geometry(type: .path(.line(-.y, .y))), |
| 34 | + Geometry(type: .fill(Path.text("+Y")), transform: Transform( |
| 35 | + offset: .y - Vector(0.6, -0.5, 0) * textScale, |
| 36 | + scale: Vector(size: [textScale]) |
| 37 | + ), material: material), |
| 38 | + Geometry(type: .fill(Path.text("-Y")), transform: Transform( |
| 39 | + offset: -.y - Vector(0.5, 1.25, 0) * textScale, |
| 40 | + scale: Vector(size: [textScale]) |
| 41 | + ), material: material), |
| 42 | + Geometry(type: .path(.line(-.z, .z))), |
| 43 | + Geometry(type: .fill(Path.text("+Z")), transform: Transform( |
| 44 | + offset: .z - Vector(0.6, 0.25, -0.5) * textScale, |
| 45 | + scale: Vector(size: [textScale]) |
| 46 | + ), material: material), |
| 47 | + Geometry(type: .fill(Path.text("-Z")), transform: Transform( |
| 48 | + offset: -.z - Vector(0.5, 0.25, 0.5) * textScale, |
| 49 | + scale: Vector(size: [textScale]) |
| 50 | + ), material: material), |
| 51 | + ]) |
| 52 | + _ = geometry.build { true } |
| 53 | + var options = Scene.OutputOptions.default |
| 54 | + options.lineColor = lineColor |
| 55 | + geometry.scnBuild(with: options) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +extension SCNNode { |
| 60 | + convenience init(_ axes: Axes) { |
| 61 | + self.init(axes.geometry) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +private extension Vector { |
| 66 | + static let x = Vector(1, 0, 0) |
| 67 | + static let y = Vector(0, 1, 0) |
| 68 | + static let z = Vector(0, 0, 1) |
| 69 | +} |
| 70 | + |
| 71 | +private extension Geometry { |
| 72 | + convenience init( |
| 73 | + type: GeometryType = .group, |
| 74 | + transform: Transform = .identity, |
| 75 | + material: Material = .default, |
| 76 | + children: [Geometry] = [] |
| 77 | + ) { |
| 78 | + self.init( |
| 79 | + type: type, |
| 80 | + name: nil, |
| 81 | + transform: transform, |
| 82 | + material: material, |
| 83 | + children: children, |
| 84 | + sourceLocation: nil |
| 85 | + ) |
| 86 | + } |
| 87 | +} |
0 commit comments