Skip to content

Commit

Permalink
Simplification. Review comments fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
astojilj committed Mar 11, 2021
1 parent 4c58ba6 commit 5c204bc
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions Examples/Examples/All Examples/SceneKitExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ public class SceneKitExample: UIViewController, ExampleProtocol, CustomLayerHost

override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// The below line is used for internal testing purposes only.
self.finish()
}

func addModelAndTerrain() {
try! mapView.__map.addStyleCustomLayer(forLayerId: "Custom",
layerHost: self,
layerPosition: nil)
layerPosition: LayerPosition(above: nil, below: "waterway-label", at: nil))

var demSource = RasterDemSource()
demSource.url = "mapbox://mapbox.mapbox-terrain-dem-v1"
demSource.tileSize = 512
demSource.tileSize = 514
demSource.maxzoom = 14.0
mapView.style.addSource(source: demSource, identifier: "mapbox-dem")
let terrain = Terrain(sourceId: "mapbox-dem")
Expand All @@ -73,18 +71,16 @@ public class SceneKitExample: UIViewController, ExampleProtocol, CustomLayerHost
"hillshade-illumination-anchor": "map"
] as [ String: Any ]

let insertHillshadeBelow = try! map.styleLayerExists(forLayerId: "water") ?
LayerPosition(above: nil, below: "water", at: nil) : try! map.styleLayerExists(forLayerId: "hillshade") ?
LayerPosition(above: nil, below: "hillshade", at: nil) : nil
try! map.addStyleLayer(forProperties: properties, layerPosition: insertHillshadeBelow)
try! map.addStyleLayer(forProperties: properties,
layerPosition: LayerPosition(above: nil, below: "water", at: nil))
}

public func renderingWillStart(_ metalDevice: MTLDevice, colorPixelFormat: UInt, depthStencilPixelFormat: UInt) {
renderer = SCNRenderer(device: metalDevice)
scene = SCNScene()
renderer.scene = scene

modelNode = SCNScene(named: "34M_17")?.rootNode
modelNode = SCNScene(named: "34M_17")?.rootNode.clone()
scene.rootNode.addChildNode(modelNode)

cameraNode = SCNNode()
Expand Down Expand Up @@ -173,34 +169,19 @@ public class SceneKitExample: UIViewController, ExampleProtocol, CustomLayerHost
transformSimd[3, 2] = m[14].doubleValue
transformSimd[3, 3] = m[15].doubleValue

// Projection.project(for: modelOrigin, zoomScale: 1.0 / 512.0) corresponds to gl-js's
// mapboxgl.MercatorCoordinate.fromLngLat(). origin is in spherical mercator normalized to
// 0..1 for the width of the world. In other words, (x,y) E [0..1) is used to represent
// coordinates in one world copy, values of x +/- 1 represent wrap.
let origin = try! Projection.project(for: modelOrigin, zoomScale: 1.0 / 512.0)
let meterInMercatorCoordinateUnits = try! 1.0 / (512.0 * Projection.getMetersPerPixelAtLatitude(forLatitude: modelOrigin.latitude, zoom: 0))
let metersPerPixel = try! Projection.getMetersPerPixelAtLatitude(forLatitude: modelOrigin.latitude, zoom: parameters.zoom)
// Model is using metric unit system: scale x and y from meters to mercator and keep z is in meters.
let meterInMercatorCoordinateUnits = try! 1.0 / (Projection.getMetersPerPixelAtLatitude(forLatitude: modelOrigin.latitude, zoom: parameters.zoom))
let modelScale = makeScaleMatrix(xScale: meterInMercatorCoordinateUnits, yScale: -meterInMercatorCoordinateUnits, zScale: 1)

// Translate scaled model to model origin (in web mercator coordinates) and elevate to model origin's altitude (in meters).
let origin = try! Projection.project(for: modelOrigin, zoomScale: pow(2, parameters.zoom))
var elevation = 0.0
if let elevationData = parameters.elevationData, let elevationValue = elevationData.getElevationFor(self.modelOrigin) {
elevation = elevationValue.doubleValue
}
let translateModel = makeTranslationMatrix(tx: origin.x, ty: origin.y, tz: elevation)

// origin is in normalized MercatorCoordinates. Normalized refers to the
// world copy represented with (x, y) values in range [0..1], and corresponds
// to modelAsMercatorCoordinate in https://docs.mapbox.com/mapbox-gl-js/example/add-3d-model/
let transformModel = makeTranslationMatrix(tx: origin.x, ty: origin.y, tz: elevation * meterInMercatorCoordinateUnits)

// the same scale as in gl-js example, scale from meters to mercator.
let modelScale = makeScaleMatrix(xScale: meterInMercatorCoordinateUnits, yScale: -meterInMercatorCoordinateUnits, zScale: meterInMercatorCoordinateUnits)

// mercator scale is specific to gl-native example because gl-js's customLayerMatrix computes this
// internaly: https://github.com/mapbox/mapbox-gl-js/blob/main/src/geo/transform.js#L1316
// The mercatorMatrix can be used to transform points from mercator coordinates
// ([0, 0] nw, [1, 1] se) to GL coordinates.
let worldSize = pow(2, parameters.zoom) * 512.0
let mercatorMatrix = transformSimd * makeScaleMatrix(xScale: worldSize, yScale: worldSize, zScale: worldSize * metersPerPixel)

let transform = mercatorMatrix * transformModel * modelScale
let transform = transformSimd * translateModel * modelScale

var scnMat = SCNMatrix4()
scnMat.m11 = Float(transform[0, 0])
Expand Down Expand Up @@ -241,6 +222,7 @@ public class SceneKitExample: UIViewController, ExampleProtocol, CustomLayerHost
}

public func renderingWillEnd() {
// Unimplemented
// The below line is used for internal testing purposes only.
self.finish()
}
}

0 comments on commit 5c204bc

Please sign in to comment.