Skip to content

Instantly share code, notes, and snippets.

@rockarts
Created September 3, 2018 04:50
Show Gist options
  • Select an option

  • Save rockarts/6cbf6d08918cc984c114802aa3ce2ad6 to your computer and use it in GitHub Desktop.

Select an option

Save rockarts/6cbf6d08918cc984c114802aa3ce2ad6 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// ExploreOutdoors
//
// Created by Steven Rockarts on 2018-09-01.
// Copyright © 2018 Figure4Software. All rights reserved.
//
import UIKit
import Mapbox
import SceneKit
import MapKit
import MapboxSceneKit
class ViewController: UIViewController {
//Valley of the 10 Peaks
//NE & SW Corners
var minLat = 51.20
var minLon = -116.28
var maxLat = 51.32
var maxLon = -116.12
var sceneView: SCNView = SCNView()
var scene: SCNScene = SCNScene()
var terrainNode: TerrainNode?
var terrainNodeScale = SCNVector3(0.0003, 0.0003, 0.0003) // Scale down map (otherwise it's far too big)
var trackpoints:[CLLocationCoordinate2D]?
let cameraNode = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
setupSceneView()
createTerrain()
}
func setupSceneView(){
self.view.addSubview(sceneView)
sceneView.frame = self.view.bounds
sceneView.scene = scene
sceneView.allowsCameraControl = true
sceneView.isPlaying = true
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 3)
scene.rootNode.addChildNode(cameraNode)
let lightNode = SCNNode()
let light = SCNLight()
light.type = .ambient
light.intensity = 1000
lightNode.light = light
scene.rootNode.addChildNode(lightNode)
}
func createTerrain() {
terrainNode = TerrainNode(minLat: minLat, maxLat: maxLat,
minLon: minLon, maxLon: maxLon)
if let terrainNode = terrainNode {
terrainNode.scale = terrainNodeScale // Scale down map
terrainNode.geometry?.materials = defaultMaterials()
scene.rootNode.addChildNode(terrainNode)
//create terrain geometry based on mapbox elevation data
terrainNode.fetchTerrainHeights(minWallHeight: 100.0, enableDynamicShadows: true, progress: { progress, total in }, completion: {
NSLog("Terrain load complete")
self.addValleyofTheTenPeaksPOIMarkers()
})
terrainNode.fetchTerrainTexture("mapbox/satellite-v9", progress: { progress, total in }, completion: { image in
NSLog("Texture load complete")
//apply the satellite image to the terrain mesh
terrainNode.geometry?.materials[4].diffuse.contents = image
})
}
}
func addValleyofTheTenPeaksPOIMarkers() {
let locations : [String: (CLLocationDegrees, CLLocationDegrees)] = [
"Moraine Lake": (51.320143, -116.185173),
"Mt. Fay": (51.298343, -116.163270),
"Mt Little": (51.295878, -116.183472),
"Mt Bowlen": (51.299772, -116.188203),
"Mt Tonsa": (51.296674, -116.201430),
"Mt Perren": (51.296779, -116.208143),
"Mt Allen": (51.291698, -116.219711),
"Mt Tuzo": (51.301836, -116.227780),
"Deltaform":(51.301585, -116.245475),
"Neptuak": (51.307604, -116.257772),
"Wenkchemna Peak": (51.328359, -116.275496)
]
if let terrainNode = terrainNode {
for location in locations {
let markerHeight = CGFloat(50.0)
let text = SCNText(string: location.key, extrusionDepth: 1)
let material = SCNMaterial()
material.diffuse.contents = UIColor.red
text.materials = [material]
let sphereMarker = SCNNode(geometry: text)
sphereMarker.geometry?.firstMaterial?.diffuse.contents = UIColor.red
let tempLocation = CLLocation(latitude: location.value.0, longitude: location.value.1)
sphereMarker.position = terrainNode.positionForLocation(tempLocation)
sphereMarker.position.y += Float(markerHeight / 2.0) + 200
sphereMarker.scale = SCNVector3(x: 11, y: 11, z: 11)
terrainNode.addChildNode(sphereMarker)
}
}
}
// Create default materials for each side of the terrain node
func defaultMaterials() -> [SCNMaterial] {
let groundImage = SCNMaterial()
groundImage.diffuse.contents = UIColor.darkGray
groundImage.name = "Ground texture"
let sideMaterial = SCNMaterial()
sideMaterial.diffuse.contents = UIColor.darkGray
sideMaterial.isDoubleSided = true
sideMaterial.name = "Side"
let bottomMaterial = SCNMaterial()
bottomMaterial.diffuse.contents = UIColor.black
bottomMaterial.name = "Bottom"
return [sideMaterial, sideMaterial, sideMaterial, sideMaterial, groundImage, bottomMaterial]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment