Skip to content

Commit

Permalink
Material Fix Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dariukas committed May 13, 2017
1 parent 3aaf8e9 commit 2c98991
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 118 deletions.
4 changes: 4 additions & 0 deletions 5DPlannerTask.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
A31CA1771EC73EAE00023460 /* path_6.jpg in Resources */ = {isa = PBXBuildFile; fileRef = A31CA1741EC73EAE00023460 /* path_6.jpg */; };
A31CA1781EC73EAE00023460 /* wallp_0.jpg in Resources */ = {isa = PBXBuildFile; fileRef = A31CA1751EC73EAE00023460 /* wallp_0.jpg */; };
A31CA17B1EC73FCB00023460 /* Other.swift in Sources */ = {isa = PBXBuildFile; fileRef = A31CA17A1EC73FCB00023460 /* Other.swift */; };
A35419FB1EC7B80D00E91DD6 /* environment.jpg in Resources */ = {isa = PBXBuildFile; fileRef = A35419FA1EC7B80D00E91DD6 /* environment.jpg */; };
A36A95601E841AA000BDE440 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36A955F1E841AA000BDE440 /* AppDelegate.swift */; };
A36A95621E841AA000BDE440 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36A95611E841AA000BDE440 /* ViewController.swift */; };
A36A95651E841AA000BDE440 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A36A95631E841AA000BDE440 /* Main.storyboard */; };
Expand Down Expand Up @@ -40,6 +41,7 @@
A31CA1741EC73EAE00023460 /* path_6.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = path_6.jpg; sourceTree = "<group>"; };
A31CA1751EC73EAE00023460 /* wallp_0.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = wallp_0.jpg; sourceTree = "<group>"; };
A31CA17A1EC73FCB00023460 /* Other.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Other.swift; sourceTree = "<group>"; };
A35419FA1EC7B80D00E91DD6 /* environment.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = environment.jpg; sourceTree = "<group>"; };
A36A955C1E841AA000BDE440 /* 5DPlannerTask.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = 5DPlannerTask.app; sourceTree = BUILT_PRODUCTS_DIR; };
A36A955F1E841AA000BDE440 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
A36A95611E841AA000BDE440 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,6 +82,7 @@
isa = PBXGroup;
children = (
A31CA1731EC73EAE00023460 /* laminate_1_2.jpg */,
A35419FA1EC7B80D00E91DD6 /* environment.jpg */,
A31CA1741EC73EAE00023460 /* path_6.jpg */,
A31CA1751EC73EAE00023460 /* wallp_0.jpg */,
);
Expand Down Expand Up @@ -237,6 +240,7 @@
A31CA1761EC73EAE00023460 /* laminate_1_2.jpg in Resources */,
A36A956A1E841AA000BDE440 /* LaunchScreen.storyboard in Resources */,
A31CA1781EC73EAE00023460 /* wallp_0.jpg in Resources */,
A35419FB1EC7B80D00E91DD6 /* environment.jpg in Resources */,
A36A95861E84397900BDE440 /* JSONData.json in Resources */,
A31CA1771EC73EAE00023460 /* path_6.jpg in Resources */,
A36A95671E841AA000BDE440 /* Assets.xcassets in Resources */,
Expand Down
1 change: 0 additions & 1 deletion 5DPlannerTask/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
Expand Down
53 changes: 46 additions & 7 deletions 5DPlannerTask/CustomScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright © 2017 Darius Miliauskas. All rights reserved.
//



import UIKit
import SceneKit

Expand All @@ -16,6 +18,7 @@ class CustomScene: SCNScene {
//let scene = SCNScene(named: "sphere.obj")!
//let scnView = self as! SCNView
setupScene()
setObjects()
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -24,24 +27,60 @@ class CustomScene: SCNScene {
}

func setupScene() {
let omniLight = LightNode(type: .omni)
omniLight.position = SCNVector3(x: 0, y: 10, z: 10)
self.rootNode.addChildNode(omniLight)
// let omniLight = LightNode(type: .omni)
// omniLight.position = SCNVector3(x: 0, y: 500, z: 500)
// self.rootNode.addChildNode(omniLight)

let ambientLight = LightNode(type: .ambient)
ambientLight.color = UIColor.darkGray
ambientLight.light?.color = UIColor.darkGray
self.rootNode.addChildNode(ambientLight)

let camera = CameraNode()
camera.position = SCNVector3(x: 0, y: 0, z: 15)
camera.position = SCNVector3(x: 2000, y: 2000, z: 1500)
self.rootNode.addChildNode(camera)

setupEnviromentLights()
}

func setObjects() {
//let data: [String : AnyObject]=[:]
let input = JSONData.parseJSON(fileName: "JSONData")
let json = JSON(input: input)
let project = Project(dataWithoutMaterial: json.data!)
project.extract(json.data!)

let groundNode = GroundNode(project)
self.rootNode.addChildNode(groundNode)

for item in project.items! {
let floor = Floor(dataWithoutMaterial: item)
floor.extract(item)
for item in floor.items! {
let room = Room(data: item)
room.extract(item)

let roomNode = RoomNode(room)
groundNode.addChildNode(roomNode)

for item in room.items! {
let wall = Wall(data: item)
wall.extract(item)
wall.h = room.h

let wallNode = WallNode(wall)
roomNode.addChildNode(wallNode)
}

if let roomMaterials = room.materials?.materials, let firstWallNode = roomNode.childNodes.first as? WallNode {
let floorNode = FloorNode(side: firstWallNode.width, material: roomMaterials["floor"] as? Material)
roomNode.addChildNode(floorNode)
}
}
}
}

private func setupEnviromentLights() {
let environment = UIImage(named: "IBL.png")
self.lightingEnvironment.contents = environment
self.lightingEnvironment.contents = UIImage(named: "environment.jpg")
self.lightingEnvironment.intensity = 2.0
}
}
Binary file added 5DPlannerTask/Images/environment.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 5DPlannerTask/JSONData.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"hash":"63a807e2624cec7a705c46b74fcbe64b","readOnly":0,"name":"New project","s":0,"data":{"className":"Project","version":2,"width":8000,"height":8000,"ss":[],"sscounter":0,"s":0,"currentFloor":0,"autoinc":14,"ground":{"texture":"path_6.jpg","color":"#767e4c"},"v":1,"readOnly":0,"items":[{"className":"Floor","name":"First floor","h":275,"puid":"p1","items":[{"className":"Room","x":3750,"y":3750,"z":0,"sX":204,"sY":204,"rtype":0,"h":270,"materials":{"floor":{"texture":"laminate_1_2","color":"#FFFFFF","scale":1,"rotate":0},"ceil":{"texture":"linen_1_4","color":"#FFFFFF","scale":1,"rotate":0},"indoor":{"texture":"wallp_0","color":"#FFFFFF","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0","color":"#FFFFFF","scale":1,"rotate":0}},"rhidden":false,"fhidden":false,"puid":"p2","items":[{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0}},"puid":"p3","items":[{"className":"Point","x":0,"y":0,"puid":"p4"},{"className":"Point","x":510,"y":0,"puid":"p5"}]},{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0}},"puid":"p6","items":[{"className":"Point","x":510,"y":0,"puid":"p7"},{"className":"Point","x":510,"y":510,"puid":"p8"}]},{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0}},"puid":"p9","items":[{"className":"Point","x":510,"y":510,"puid":"p10"},{"className":"Point","x":0,"y":510,"puid":"p11"}]},{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0","color":"#ffffff","scale":1,"rotate":0}},"puid":"p12","items":[{"className":"Point","x":0,"y":510,"puid":"p13"},{"className":"Point","x":0,"y":0,"puid":"p14"}]}]}]}]}}
{"hash":"63a807e2624cec7a705c46b74fcbe64b","readOnly":0,"name":"New project","s":0,"data":{"className":"Project","version":2,"width":8000,"height":8000,"ss":[],"sscounter":0,"s":0,"currentFloor":0,"autoinc":14,"ground":{"texture":"path_6.jpg","color":"#767e4c"},"v":1,"readOnly":0,"items":[{"className":"Floor","name":"First floor","h":275,"puid":"p1","items":[{"className":"Room","x":3750,"y":3750,"z":0,"sX":204,"sY":204,"rtype":0,"h":270,"materials":{"floor":{"texture":"laminate_1_2.jpg","color":"#FFFFFF","scale":1,"rotate":0},"ceil":{"texture":"linen_1_4","color":"#FFFFFF","scale":1,"rotate":0},"indoor":{"texture":"wallp_0.jpg","color":"#FFFFFF","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0.jpg","color":"#FFFFFF","scale":1,"rotate":0}},"rhidden":false,"fhidden":false,"puid":"p2","items":[{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0}},"puid":"p3","items":[{"className":"Point","x":0,"y":0,"puid":"p4"},{"className":"Point","x":510,"y":0,"puid":"p5"}]},{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0}},"puid":"p6","items":[{"className":"Point","x":510,"y":0,"puid":"p7"},{"className":"Point","x":510,"y":510,"puid":"p8"}]},{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0}},"puid":"p9","items":[{"className":"Point","x":510,"y":510,"puid":"p10"},{"className":"Point","x":0,"y":510,"puid":"p11"}]},{"className":"Wall","hidden":false,"w":10,"materials":{"indoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0},"outdoor":{"texture":"wallp_0.jpg","color":"#ffffff","scale":1,"rotate":0}},"puid":"p12","items":[{"className":"Point","x":0,"y":510,"puid":"p13"},{"className":"Point","x":0,"y":0,"puid":"p14"}]}]}]}]}}
41 changes: 29 additions & 12 deletions 5DPlannerTask/Models/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class Item: DataExtractProtocol {

class Project: Item {

var width: Float?
var height: Float?
var width: CGFloat?
var height: CGFloat?
var ground: Material?

func extract(_ data: [String: AnyObject]) {
if let width = extractData(input: data, using: "width") as? Float {
if let width = extractData(input: data, using: "width") as? CGFloat {
self.width = width
}
if let height = extractData(input: data, using: "height") as? Float {
if let height = extractData(input: data, using: "height") as? CGFloat {
self.height = height
}
if let ground = extractData(input: data, using: "ground") as? [String: AnyObject] {
Expand All @@ -62,21 +62,21 @@ class Floor: Item {

class Room: Floor {

var x: CGFloat?
var y: CGFloat?
var z: CGFloat?
var x: Float?
var y: Float?
var z: Float?
var sX: CGFloat?
var sY: CGFloat?
var h: CGFloat?

override func extract(_ item: [String : AnyObject]) {
if let value = extractData(input: item, using: "x") as? CGFloat {
if let value = extractData(input: item, using: "x") as? Float {
self.x = value
}
if let value = extractData(input: item, using: "y") as? CGFloat {
if let value = extractData(input: item, using: "y") as? Float {
self.y = value
}
if let value = extractData(input: item, using: "z") as? CGFloat {
if let value = extractData(input: item, using: "z") as? Float {
self.z = value
}
if let value = extractData(input: item, using: "sX") as? CGFloat {
Expand All @@ -96,24 +96,41 @@ class Wall: Room {
var w: CGFloat? //width
var points: [Point] = []

override init(data: [String : AnyObject]) {
super.init(data: data)
for item in self.items! {
let point = Point(item)
self.points.append(point)
}
}

override func extract(_ item: [String : AnyObject]) {
if let value = extractData(input: item, using: "w") as? CGFloat {
self.w = value
}
}
}

class Point: Wall {
class Point: DataExtractProtocol {

var pointX: CGFloat?
var pointY: CGFloat?

override func extract(_ item: [String : AnyObject]) {
init(_ item: [String : AnyObject]) {
if let value = extractData(input: item, using: "x") as? CGFloat {
self.pointX = value
}
if let value = extractData(input: item, using: "y") as? CGFloat {
self.pointY = value
}
}

// override func extract(_ item: [String : AnyObject]) {
// if let value = extractData(input: item, using: "x") as? CGFloat {
// self.pointX = value
// }
// if let value = extractData(input: item, using: "y") as? CGFloat {
// self.pointY = value
// }
// }
}
20 changes: 20 additions & 0 deletions 5DPlannerTask/Models/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,23 @@ class JSONData {
// }
//}

protocol ExtractProtocol {
func extract(_ item: [String : AnyObject])
}

protocol DataExtractProtocol {
func extractData(input: [String: AnyObject], using keyword: String) -> AnyObject?
}

extension DataExtractProtocol {
//The method get the value using keyword (the basic method)
func extractData(input: [String: AnyObject], using keyword: String) -> AnyObject? {
if let value = input[keyword] {
return value
} else {
print("The key \(keyword) does not exist in dictionary.")
return nil
//return input as AnyObject
}
}
}
13 changes: 6 additions & 7 deletions 5DPlannerTask/Models/Material.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import UIKit
import SceneKit


class Materials: DataExtractProtocol {
var materials: [String: AnyObject]?

Expand Down Expand Up @@ -59,8 +58,7 @@ class Material: DataExtractProtocol {

init(_ data: [String: AnyObject]) {
if let color = extractData(input: data, using: "color") as? String {
//self.color = UIColor(hexString: color)
// print("Color: \(color)")
self.color = UIColor(hexString: color)
}
if let texture = extractData(input: data, using: "texture") as? String {
self.imageName = texture
Expand All @@ -70,16 +68,17 @@ class Material: DataExtractProtocol {
}
}

//convert hex color representation to UIColor
extension UIColor {
convenience init(hexString: String) {
var hex: UInt64 = 000000

var hex: UInt64 = 000000 // UIColor.black in the case of other format
if hexString.hasPrefix("#") {
let start = hexString.index(hexString.startIndex, offsetBy: 1)
let hexColor = hexString.substring(from: start)
hex = UInt64(hexColor)!
if let representation = UInt64(hexColor, radix:16) { //strtoul
hex = representation
}
}

let components = (
R: CGFloat((hex >> 16) & 0xff) / 255,
G: CGFloat((hex >> 08) & 0xff) / 255,
Expand Down
Loading

0 comments on commit 2c98991

Please sign in to comment.