Advertisement
TiyasAria

CustomSceneViewRepresentable

Oct 23rd, 2023
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.91 KB | None | 0 0
  1. //
  2. //  CustomSceneViewRepresentable.swift
  3. //  keluarga-cemara
  4. //
  5. //  Created by tiyas aria on 20/10/23.
  6. //
  7.  
  8. import SwiftUI
  9. import SceneKit
  10.  
  11. struct CustomSceneViewRepresentable : UIViewRepresentable{
  12.     @Binding var isLoading : Bool
  13.     var lightValue : Float
  14.     let radius : Float = 15.0
  15.    
  16.     func makeUIView(context: Context) -> SCNView {
  17.         let view = SCNView()
  18.         view.allowsCameraControl = true
  19.         view.backgroundColor = .clear
  20.         view.autoenablesDefaultLighting = true
  21. //        let fm = FileManager.default
  22. //        let path = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
  23. //        let fileName = "room.usdz"
  24. //        let modelFilePath  = path.appendingPathComponent(fileName).absoluteString
  25. //        DispatchQueue.main.async {
  26. //            do {
  27. //                let scene = try? SCNScene(url: URL(string: "\(modelFilePath)")!)
  28. //                view.scene = scene
  29. //            }
  30. //        }
  31. //        
  32.         return view
  33.        
  34.     }
  35.    
  36.     func updateUIView(_ uiView: UIViewType, context: Context) {
  37. //        let fm = FileManager.default
  38. //        let path = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
  39. //        let fileName = "room.usdz"
  40. //        let modelFilePath  = path.appendingPathComponent(fileName).absoluteString
  41.         DispatchQueue.main.async {
  42.             do {
  43. //                let scene = try? SCNScene(url: URL(string: "\(modelFilePath)")!)
  44. //                add sceneNode , coba buat shadow pakai asset dummy dulu
  45.                 let scene = SCNScene(named: "scan.usdz")
  46.                 let sceneNode = SCNNode(geometry: scene?.rootNode.geometry)
  47.                 uiView.scene = scene
  48.                
  49.                 let lightNode = setUpLightShadow()
  50.                 let cameraNode = setUpCamera()
  51.                
  52.                 let constraint = SCNLookAtConstraint(target: sceneNode )
  53.                 constraint.isGimbalLockEnabled = true
  54.                 cameraNode.constraints = [constraint]
  55.                 lightNode.constraints = [constraint]
  56.                
  57.                 scene?.rootNode.addChildNode(sceneNode)
  58.                 scene?.rootNode.addChildNode(cameraNode)
  59.                 scene?.rootNode.addChildNode(lightNode)
  60.             }
  61.         }
  62.     }
  63.    
  64.     func setUpCamera() -> SCNNode{
  65.         let camera = SCNCamera()
  66.         let cameraNode = SCNNode()
  67.         cameraNode.camera = camera
  68.         cameraNode.position = SCNVector3(x: -2.0, y: 4.0, z: 4.0)
  69.        
  70.         return cameraNode
  71.     }
  72.    
  73.     func setUpLightShadow() -> SCNNode{
  74.         let light = SCNLight()
  75.         light.type = .directional
  76.         light.castsShadow = true
  77.         light.shadowMode = .modulated
  78.         light.intensity = 3000
  79.        
  80.         let pos = getXYZPosition()
  81.        
  82.         let lightNode = SCNNode()
  83.         lightNode.light = light
  84.         lightNode.light?.shadowColor = UIColor.red
  85.         lightNode.position = SCNVector3(x: pos.x, y: pos.y, z: pos.z)
  86.         lightNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: -.pi / 2)
  87.        
  88.         return lightNode
  89.     }
  90.    
  91. //     func for position
  92.     func getXYZPosition() -> (x: Float, y : Float, z : Float){
  93.         let angle = lightValue
  94.         let x = radius * cos(angle)
  95.         let y : Float = 10.5
  96.         let z = radius * sin(angle)
  97.        
  98.         return (x,y,z)
  99.     }
  100.    
  101. //    func for setup planet
  102. //    func setUpPlanet() -> SCNNode{
  103. //        let planeGeometry = SCNPlane(width: 50, height: 50)
  104. //        let planeNode = SCNNode(geometry: planeGeometry)
  105. //        planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
  106. //        planeNode.position = SCNVector3(x: 0, y: -1.7, z: 0)
  107. //        
  108. //        let material = SCNMaterial()
  109. //        material.diffuse.contents = UIColor.systemPink
  110. //        planeGeometry.materials = [material]
  111. //        return planeNode
  112. //        
  113. //    }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement