summaryrefslogtreecommitdiff
path: root/ShedCalc/RenderController.swift
diff options
context:
space:
mode:
Diffstat (limited to 'ShedCalc/RenderController.swift')
-rw-r--r--ShedCalc/RenderController.swift106
1 files changed, 87 insertions, 19 deletions
diff --git a/ShedCalc/RenderController.swift b/ShedCalc/RenderController.swift
index 1a57cb0..8ceef21 100644
--- a/ShedCalc/RenderController.swift
+++ b/ShedCalc/RenderController.swift
@@ -8,23 +8,25 @@
import SwiftUI
import RealityKit
-let BEAM_2x4_WIDTH: Float = 0.02
-let BEAM_2x4_LENGTH: Float = 0.04
+let BEAM_2x4_WIDTH: Double = 0.02
+let BEAM_2x4_LENGTH: Double = 0.04
struct BeamData {
let id: String
- let position: SIMD3<Float>
- let height: Float
- let rotation: SIMD3<Float>
+ let position: SIMD3<Double>
+ let height: Double
+ let rotation: SIMD3<Double>
}
class RenderController {
- func createScene(dimensions: SIMD3<Float>, rotation: SIMD2<Float>) -> Entity {
+ var beamMaxId = 0
+
+ func createScene(dimensions: SIMD3<Double>, rotation: SIMD2<Double>) -> Entity {
let scene = Entity()
scene.name = "scene"
- let box = createBox(dimensions: [1,1,1])
- scene.addChild(box)
+ //let box = createBox(dimensions: [1,1,1])
+ //scene.addChild(box)
let beam0 = createBeam2x4(position: [1,1,0], height: 2.4, rotate: [90,0,0], id: "beam0")
scene.addChild(beam0)
@@ -69,12 +71,12 @@ class RenderController {
func handleDragGesture(
_ value: DragGesture.Value,
lastDrag: CGSize,
- currentRotation: SIMD2<Float>
- ) -> (rotation: SIMD2<Float>, drag: CGSize) {
- let dx = Float(value.translation.width - lastDrag.width) * 0.01
- let dy = Float(value.translation.height - lastDrag.height) * 0.01
+ currentRotation: SIMD2<Double>
+ ) -> (rotation: SIMD2<Double>, drag: CGSize) {
+ let dx = Double(value.translation.width - lastDrag.width) * 0.01
+ let dy = Double(value.translation.height - lastDrag.height) * 0.01
- let newRotation = SIMD2<Float>(
+ let newRotation = SIMD2<Double>(
currentRotation.x + dy,
currentRotation.y + dx
)
@@ -94,9 +96,9 @@ class RenderController {
return box
}
- private func createBeam2x4(position: SIMD3<Float>, height: Float, rotate: SIMD3<Float>, id: String) -> ModelEntity {
+ private func createBeam2x4(position: SIMD3<Float>, height: Double, rotate: SIMD3<Float>, id: String) -> ModelEntity {
let box = ModelEntity(
- mesh: .generateBox(size: SIMD3(BEAM_2x4_WIDTH,BEAM_2x4_LENGTH,height), cornerRadius: 0.02),
+ mesh: .generateBox(size: SIMD3(Float(BEAM_2x4_WIDTH),Float(BEAM_2x4_LENGTH),Float(height)), cornerRadius: 0.02),
materials: [SimpleMaterial(color: .yellow, isMetallic: false)]
)
//box.look(at: position, from: lookFrom, relativeTo: nil)
@@ -113,24 +115,90 @@ class RenderController {
return box
}
- func generateShedWall(position: SIMD3<Float>, height: Float, widthNum: Int, lengthNum: Int, spaceBeem: Float) -> [BeamData] {
+ func generateShedWall(position: SIMD3<Double>, height: Double, widthNum: Int, lengthNum: Int, spaceBeam: Double) -> [BeamData] {
var beams: [BeamData] = []
var id: Int = 0
//Wall 0
for i in 0...widthNum {
- let x0 = Float(i)*spaceBeem
- beams.append(BeamData(id: "beam\(id)", position: [x0,position.y, position.z], height: height, rotation: [90,0,0]))
+ let x0 = Double(i)*spaceBeam
+ beams.append(BeamData(id: "beam-\(id)", position: [position.x+x0,position.y, position.z], height: height, rotation: [90,0,0]))
id += 1
}
//Wall 1
+ for i in 0...widthNum {
+ let x0 = Double(i)*spaceBeam
+ beams.append(BeamData(id: "beam-\(id)", position: [position.x+x0,position.y, position.z + Double(lengthNum)*spaceBeam], height: height, rotation: [90,0,0]))
+ //print("\(id)")
+ id += 1
+ }
//Wall 2
+ for i in 0...lengthNum {
+ let z0 = Double(i)*spaceBeam
+ beams.append(BeamData(id: "beam-\(id)", position: [position.x,position.y, position.z + z0], height: height, rotation: [90,90,0]))
+ //print("\(id)")
+ id += 1
+ }
//Wall 3
+ for i in 0...lengthNum {
+ let z0 = Double(i)*spaceBeam
+ beams.append(BeamData(id: "beam-\(id)", position: [position.x+Double(widthNum)*spaceBeam,position.y, position.z + z0], height: height, rotation: [90,90,0]))
+ //print("\(id)")
+ id += 1
+ }
+
+ self.beamMaxId = id
+ return beams
+ }
+
+ func generateShedFloor(position: SIMD3<Double>, height: Double, width: Double, widthNum: Int, spaceBeam: Double) -> [BeamData] {
+ var beams: [BeamData] = []
+ var id = self.beamMaxId+1
+
+ //all space
+ for i in 0...widthNum {
+ let x0 = Double(i)*spaceBeam
+ let length = width
+ beams.append(BeamData(id: "beam-\(id)", position:[position.x+x0,position.y-height/2, position.z+length/2], height: length, rotation: [0,0,0]))
+ id += 1
+ }
+
+ //2 beams for length
+ let length = Double(widthNum)*spaceBeam
+ beams.append(BeamData(id: "beam-\(id)", position:[position.x+length/2, position.y-height/2, position.z], height: length, rotation: [0,90,0]))
+ id += 1
+ beams.append(BeamData(id: "beam-\(id)", position:[position.x+length/2,position.y-height/2, position.z+width], height: length, rotation: [0,90,0]))
+ id += 1
+
+ self.beamMaxId = id
+ return beams
+ }
+
+ func generateShedRoof(position: SIMD3<Double>, height: Double, width: Double, widthNum: Int, spaceBeam: Double) -> [BeamData] {
+ var beams: [BeamData] = []
+ var id = self.beamMaxId+1
+
+ //all space
+ for i in 0...widthNum {
+ let x0 = Double(i)*spaceBeam
+ let length = width
+ beams.append(BeamData(id: "beam-\(id)", position:[position.x+x0,position.y+height/2, position.z+length/2], height: length, rotation: [0,0,0]))
+ id += 1
+ }
+
+ //2 beams for length
+ let length = Double(widthNum)*spaceBeam
+ beams.append(BeamData(id: "beam-\(id)", position:[position.x+length/2, position.y+height/2, position.z], height: length, rotation: [0,90,0]))
+ id += 1
+ beams.append(BeamData(id: "beam-\(id)", position:[position.x+length/2,position.y+height/2, position.z+width], height: length, rotation: [0,90,0]))
+ id += 1
+
+ self.beamMaxId = id
return beams
}
func createBeams(_ beams: [BeamData]) -> [ModelEntity] {
return beams.map { beam in
- createBeam2x4(position: beam.position, height: beam.height, rotate: beam.rotation, id: beam.id)
+ createBeam2x4(position: SIMD3<Float>(beam.position), height: beam.height, rotate: SIMD3<Float>(beam.rotation), id: beam.id)
}
}