Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions GameController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// GameController.swift
// Map Generator
//
// Created by v.prusakov on 10/5/20.
// Copyright © 2020 Carl Wieland. All rights reserved.
//

import SceneKit
import ConvexHull

class GameController {

let scene: SCNScene
let sceneRenderer: SCNSceneRenderer

init(scnView: SCNView, map: Map) {
// create a new scene
let scene = SCNScene()
self.scene = scene

let mapNode = SCNNode()
let elevationScale = 2.0;
for c in map.centers {
var points = [Vector3]()
if c.elevation.isNormal {
points.append(Vector3(c.point.x, c.point.y, c.elevation * elevationScale))
}
for edge in c.borders {
if let v0 = edge.v0 , let v1 = edge.v1{
if v1.elevation.isNaN || v0.elevation.isNaN || v1.elevation.isInfinite || v0.elevation.isInfinite {
continue;
}
let vec1 = Vector3(v0.point.x, v0.point.y, v0.elevation * elevationScale)
let vec2 = Vector3(v1.point.x, v1.point.y, v1.elevation * elevationScale)

if vec1.z.isNaN || vec2.z.isNaN {
continue;
}

points.append(vec1)
points.append(vec2)
}

}
let filtered = points.filter{ $0.z != 0 && $0.z.isNormal }

if filtered.count > 4 {
let iscosphere = MeshUtils.convexHullOfPoints(points)
mapNode.addChildNode(iscosphere)
}
}
scene.rootNode.addChildNode(mapNode)

let sphere = SCNNode()
sphere.geometry = SCNSphere(radius: 1)
sphere.simdPosition = .zero
let mat = SCNMaterial()
mat.diffuse.contents = SCNColor.red
sphere.geometry!.materials = [mat]
scene.rootNode.addChildNode(sphere)

// set the scene to the view
scnView.scene = scene

scnView.isPlaying = true
scnView.loops = true

// allows the user to manipulate the camera
scnView.allowsCameraControl = true

// show statistics such as fps and timing information
scnView.showsStatistics = true
scnView.autoenablesDefaultLighting = true

scnView.debugOptions = [.showWireframe]


// configure the view
scnView.backgroundColor = SCNColor.black.withAlphaComponent(0.3)

self.sceneRenderer = scnView
}
}
38 changes: 23 additions & 15 deletions Map Generator iOS/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14E11f" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="blJ-jd-sPh">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="blJ-jd-sPh">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
<scene sceneID="q65-Vi-x1T">
<objects>
<navigationController id="blJ-jd-sPh" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="APy-4G-gX0">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<rect key="frame" x="0.0" y="44" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
Expand All @@ -30,23 +32,23 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="4ek-3g-J3E">
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4ek-3g-J3E">
<rect key="frame" x="0.0" y="44" width="600" height="556"/>
</scrollView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9U6-Uz-WxI">
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9U6-Uz-WxI">
<rect key="frame" x="263" y="72" width="73" height="30"/>
<state key="normal" title="Visualize...">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<segue destination="DtP-Do-J0H" kind="show" id="vz0-YK-3tc"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="4ek-3g-J3E" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="-16" id="8Ka-Yq-3Gp"/>
<constraint firstAttribute="trailingMargin" secondItem="4ek-3g-J3E" secondAttribute="trailing" constant="-16" id="9XS-TV-zmS"/>
Expand All @@ -56,7 +58,13 @@
<constraint firstAttribute="centerX" secondItem="9U6-Uz-WxI" secondAttribute="centerX" id="sck-af-Fye"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="saQ-OE-eQf"/>
<navigationItem key="navigationItem" id="saQ-OE-eQf">
<barButtonItem key="rightBarButtonItem" systemItem="refresh" id="BeS-9J-Ni5">
<connections>
<action selector="onRefreshButtonPressed" destination="BYZ-38-t0r" id="349-Xo-3Dc"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="scrollView" destination="4ek-3g-J3E" id="WJM-1t-DMh"/>
</connections>
Expand All @@ -74,15 +82,15 @@
<viewControllerLayoutGuide type="bottom" id="1yR-HB-aJW"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="hEt-9c-wS6">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<sceneKitView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" allowsCameraControl="YES" autoenablesDefaultLighting="YES" translatesAutoresizingMaskIntoConstraints="NO" id="HCO-6e-qfS" customClass="GameView" customModule="Map_Generator_iOS" customModuleProvider="target">
<sceneKitView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" misplaced="YES" allowsCameraControl="YES" autoenablesDefaultLighting="YES" translatesAutoresizingMaskIntoConstraints="NO" id="HCO-6e-qfS" customClass="GameView" customModule="Map_Generator_iOS" customModuleProvider="target">
<rect key="frame" x="-1" y="64" width="602" height="536"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</sceneKitView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="HCO-6e-qfS" firstAttribute="top" secondItem="de6-Wd-2V8" secondAttribute="bottom" id="BwZ-BD-MPs"/>
<constraint firstItem="1yR-HB-aJW" firstAttribute="top" secondItem="HCO-6e-qfS" secondAttribute="bottom" id="Ruu-bc-fbA"/>
Expand Down
17 changes: 10 additions & 7 deletions Map Generator iOS/DelaunayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import UIKit

class DelaunayView: UIView {

var regionPoints = [[CGPoint]]()
var positions = [CGPoint]()
var triangles = [[CGPoint]]()
Expand All @@ -20,25 +21,27 @@ class DelaunayView: UIView {

override func draw(_ dirtyRect: CGRect) {
super.draw(dirtyRect)

let context = UIGraphicsGetCurrentContext()
pointColor.setFill()
for p in positions{

for p in positions {
context!.fillEllipse(in: DelaunayView.rectForPoint(point: p))
}
lineColor.set()
for region in regionPoints{
if region.count == 2{

for region in regionPoints {
if region.count == 2 {
var lines = region
context?.move(to: lines[0])
context?.addLine(to: lines[1])
context?.drawPath(using: .stroke)

}
}

}
static let radius:CGFloat = 3;
static func rectForPoint(point:CGPoint)->CGRect{

static let radius: CGFloat = 3;
static func rectForPoint(point: CGPoint) -> CGRect {
return CGRect(x: point.x - radius, y: point.y - radius, width: 2 * radius, height: 2 * radius)
}
}
93 changes: 4 additions & 89 deletions Map Generator iOS/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,104 +7,19 @@
//

import SceneKit
import QuartzCore
import QHull
import ConvexHull

let NUMPOINTS = 64
let radius:Float = 5.0

class GameViewController: UIViewController {

@IBOutlet weak var gameView: GameView!
var map:Map!
var map: Map!
var gameController: GameController!

override func viewDidLoad() {
// create a new scene
let scene = SCNScene()



let mapNode = SCNNode()
let elevationScale = 2.0;
for c in map.centers {
var points = [Vector3]()
if Float(c.elevation).isNormal{
points.append(Vector3(Float(c.point.x), Float(c.point.y), Float(c.elevation * elevationScale)))
}
for edge in c.borders{
if let v0 = edge.v0 , let v1 = edge.v1{
if Float(v1.elevation).isNaN || Float(v0.elevation).isNaN || Float(v1.elevation).isInfinite || Float(v0.elevation).isInfinite{
continue;
}
let vec1 = Vector3(Float(v0.point.x), Float(v0.point.y), Float(v0.elevation * elevationScale))
let vec2 = Vector3(Float(v1.point.x), Float(v1.point.y), Float(v1.elevation * elevationScale))
if vec1.z.isNaN || vec2.z.isNaN{
continue;
}
points.append(vec1)
points.append(vec2)
}


}
let filtered = points.filter{$0.z != 0 && $0.z.isNormal}

if filtered.count > 4{
let iscosphere = MeshUtils.convexHullOfPoints(points)
mapNode.addChildNode(iscosphere)
}
// let region = voronoi.region(p);
}
scene.rootNode.addChildNode(mapNode)




// var points = [Vector3]()
//
//
//
// for i in 0..<50{
// points.append(randomPointOnSphere() * radius/2)
// }
//
//
//
// let iscosphere = MeshUtils.convexHullOfPoints(points)
// scene.rootNode.addChildNode(iscosphere)


let sphere = SCNNode()
sphere.geometry = SCNSphere(radius: 1)
let mat = SCNMaterial()
mat.diffuse.contents = UIColor.red
sphere.geometry!.materials = [mat]
scene.rootNode.addChildNode(sphere);

// // animate the 3d object
// let animation = CABasicAnimation(keyPath: "rotation")
// animation.toValue = NSValue(SCNVector4: SCNVector4(x: CGFloat(0), y: CGFloat(1), z: CGFloat(0), w: CGFloat(M_PI)*2))
// animation.duration = 10
// animation.repeatCount = MAXFLOAT //repeat forever
//// iscosphere.addAnimation(animation, forKey: nil)

// set the scene to the view
self.gameView!.scene = scene

// allows the user to manipulate the camera
self.gameView!.allowsCameraControl = true
// if let camera = scene.rootNode.camera{
// camera.automaticallyAdjustsZRange = true
// }
// show statistics such as fps and timing information
self.gameView!.showsStatistics = true
self.gameView.autoenablesDefaultLighting = true


// configure the view
// self.gameView!.backgroundColor = UIColor.whiteColor()


self.gameController = GameController(scnView: gameView, map: map)
}

}
Loading