From e8b5398d8f9a2d4fa9ed339fec774badc270979f Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Wed, 26 Aug 2026 21:37:56 +0100 Subject: All views seem to work ok --- ShedCalc/RenderController.swift | 106 +++++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 19 deletions(-) (limited to 'ShedCalc/RenderController.swift') 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 - let height: Float - let rotation: SIMD3 + let position: SIMD3 + let height: Double + let rotation: SIMD3 } class RenderController { - func createScene(dimensions: SIMD3, rotation: SIMD2) -> Entity { + var beamMaxId = 0 + + func createScene(dimensions: SIMD3, rotation: SIMD2) -> 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 - ) -> (rotation: SIMD2, drag: CGSize) { - let dx = Float(value.translation.width - lastDrag.width) * 0.01 - let dy = Float(value.translation.height - lastDrag.height) * 0.01 + currentRotation: SIMD2 + ) -> (rotation: SIMD2, 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( + let newRotation = SIMD2( currentRotation.x + dy, currentRotation.y + dx ) @@ -94,9 +96,9 @@ class RenderController { return box } - private func createBeam2x4(position: SIMD3, height: Float, rotate: SIMD3, id: String) -> ModelEntity { + private func createBeam2x4(position: SIMD3, height: Double, rotate: SIMD3, 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, height: Float, widthNum: Int, lengthNum: Int, spaceBeem: Float) -> [BeamData] { + func generateShedWall(position: SIMD3, 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, 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, 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(beam.position), height: beam.height, rotate: SIMD3(beam.rotation), id: beam.id) } } -- cgit v1.2.3