├── .gitignore ├── LICENSE ├── README.md ├── SceneKitOffscreen.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── lachlan.xcuserdatad │ └── xcschemes │ ├── SceneKitOffscreen.xcscheme │ └── xcschememanagement.plist ├── SceneKitOffscreen ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist └── ViewController.swift └── doc ├── SceneKitOffscreen.gif └── ScenekitOffscreen.PNG /.gitignore: -------------------------------------------------------------------------------- 1 | ### Xcode ### 2 | build/ 3 | *.pbxuser 4 | !default.pbxuser 5 | *.mode1v3 6 | !default.mode1v3 7 | *.mode2v3 8 | !default.mode2v3 9 | *.perspectivev3 10 | !default.perspectivev3 11 | xcuserdata 12 | *.xccheckout 13 | *.moved-aside 14 | DerivedData 15 | *.xcuserstate 16 | 17 | # Workspace file - autogenerated by cocoa pods 18 | *.xcworkspace 19 | 20 | *.dae 21 | 22 | # CocoaPods 23 | Pods/ 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Lachlan Hurst 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SceneKitOffscreenRendering 2 | Quick example using a `SCNRenderer` and Metal to render a scene offscreen to a `MTLTexture` that's then displayed in another scene. Much like Portal (the game), well kind of. 3 | 4 | The rendered image displayed as a texture in the bottom `SCNView` is quite pixelated due to using a small texture size, there's no problem bumping this up. 5 | 6 | Interesting parts are found in the `doRender` function, with boilerplate code in the `setupMetal` and `setupTexture` functions. As the code shows, this was rather trivial to setup. 7 | 8 | ![screenshot](doc/SceneKitOffscreen.gif) 9 | 10 | ![screenshot](doc/ScenekitOffscreen.PNG) 11 | 12 | ## License 13 | MIT 14 | -------------------------------------------------------------------------------- /SceneKitOffscreen.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 7304BE891BDB0F0800733159 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7304BE881BDB0F0800733159 /* AppDelegate.swift */; }; 11 | 7304BE8B1BDB0F0800733159 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7304BE8A1BDB0F0800733159 /* ViewController.swift */; }; 12 | 7304BE8E1BDB0F0800733159 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7304BE8C1BDB0F0800733159 /* Main.storyboard */; }; 13 | 7304BE901BDB0F0800733159 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7304BE8F1BDB0F0800733159 /* Assets.xcassets */; }; 14 | 7304BE931BDB0F0900733159 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7304BE911BDB0F0900733159 /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | 7304BE851BDB0F0800733159 /* SceneKitOffscreen.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SceneKitOffscreen.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | 7304BE881BDB0F0800733159 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | 7304BE8A1BDB0F0800733159 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | 7304BE8D1BDB0F0800733159 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | 7304BE8F1BDB0F0800733159 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 7304BE921BDB0F0900733159 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | 7304BE941BDB0F0900733159 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | 7304BE821BDB0F0800733159 /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | 7304BE7C1BDB0F0800733159 = { 39 | isa = PBXGroup; 40 | children = ( 41 | 7304BE871BDB0F0800733159 /* SceneKitOffscreen */, 42 | 7304BE861BDB0F0800733159 /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | 7304BE861BDB0F0800733159 /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | 7304BE851BDB0F0800733159 /* SceneKitOffscreen.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | 7304BE871BDB0F0800733159 /* SceneKitOffscreen */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 7304BE881BDB0F0800733159 /* AppDelegate.swift */, 58 | 7304BE8A1BDB0F0800733159 /* ViewController.swift */, 59 | 7304BE8C1BDB0F0800733159 /* Main.storyboard */, 60 | 7304BE8F1BDB0F0800733159 /* Assets.xcassets */, 61 | 7304BE911BDB0F0900733159 /* LaunchScreen.storyboard */, 62 | 7304BE941BDB0F0900733159 /* Info.plist */, 63 | ); 64 | path = SceneKitOffscreen; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | 7304BE841BDB0F0800733159 /* SceneKitOffscreen */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = 7304BE971BDB0F0900733159 /* Build configuration list for PBXNativeTarget "SceneKitOffscreen" */; 73 | buildPhases = ( 74 | 7304BE811BDB0F0800733159 /* Sources */, 75 | 7304BE821BDB0F0800733159 /* Frameworks */, 76 | 7304BE831BDB0F0800733159 /* Resources */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = SceneKitOffscreen; 83 | productName = SceneKitOffscreen; 84 | productReference = 7304BE851BDB0F0800733159 /* SceneKitOffscreen.app */; 85 | productType = "com.apple.product-type.application"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | 7304BE7D1BDB0F0800733159 /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastSwiftUpdateCheck = 0710; 94 | LastUpgradeCheck = 1010; 95 | ORGANIZATIONNAME = "Lachlan Hurst"; 96 | TargetAttributes = { 97 | 7304BE841BDB0F0800733159 = { 98 | CreatedOnToolsVersion = 7.1; 99 | DevelopmentTeam = 4F7DKRYKP6; 100 | LastSwiftMigration = 1010; 101 | }; 102 | }; 103 | }; 104 | buildConfigurationList = 7304BE801BDB0F0800733159 /* Build configuration list for PBXProject "SceneKitOffscreen" */; 105 | compatibilityVersion = "Xcode 3.2"; 106 | developmentRegion = English; 107 | hasScannedForEncodings = 0; 108 | knownRegions = ( 109 | en, 110 | Base, 111 | ); 112 | mainGroup = 7304BE7C1BDB0F0800733159; 113 | productRefGroup = 7304BE861BDB0F0800733159 /* Products */; 114 | projectDirPath = ""; 115 | projectRoot = ""; 116 | targets = ( 117 | 7304BE841BDB0F0800733159 /* SceneKitOffscreen */, 118 | ); 119 | }; 120 | /* End PBXProject section */ 121 | 122 | /* Begin PBXResourcesBuildPhase section */ 123 | 7304BE831BDB0F0800733159 /* Resources */ = { 124 | isa = PBXResourcesBuildPhase; 125 | buildActionMask = 2147483647; 126 | files = ( 127 | 7304BE931BDB0F0900733159 /* LaunchScreen.storyboard in Resources */, 128 | 7304BE901BDB0F0800733159 /* Assets.xcassets in Resources */, 129 | 7304BE8E1BDB0F0800733159 /* Main.storyboard in Resources */, 130 | ); 131 | runOnlyForDeploymentPostprocessing = 0; 132 | }; 133 | /* End PBXResourcesBuildPhase section */ 134 | 135 | /* Begin PBXSourcesBuildPhase section */ 136 | 7304BE811BDB0F0800733159 /* Sources */ = { 137 | isa = PBXSourcesBuildPhase; 138 | buildActionMask = 2147483647; 139 | files = ( 140 | 7304BE8B1BDB0F0800733159 /* ViewController.swift in Sources */, 141 | 7304BE891BDB0F0800733159 /* AppDelegate.swift in Sources */, 142 | ); 143 | runOnlyForDeploymentPostprocessing = 0; 144 | }; 145 | /* End PBXSourcesBuildPhase section */ 146 | 147 | /* Begin PBXVariantGroup section */ 148 | 7304BE8C1BDB0F0800733159 /* Main.storyboard */ = { 149 | isa = PBXVariantGroup; 150 | children = ( 151 | 7304BE8D1BDB0F0800733159 /* Base */, 152 | ); 153 | name = Main.storyboard; 154 | sourceTree = ""; 155 | }; 156 | 7304BE911BDB0F0900733159 /* LaunchScreen.storyboard */ = { 157 | isa = PBXVariantGroup; 158 | children = ( 159 | 7304BE921BDB0F0900733159 /* Base */, 160 | ); 161 | name = LaunchScreen.storyboard; 162 | sourceTree = ""; 163 | }; 164 | /* End PBXVariantGroup section */ 165 | 166 | /* Begin XCBuildConfiguration section */ 167 | 7304BE951BDB0F0900733159 /* Debug */ = { 168 | isa = XCBuildConfiguration; 169 | buildSettings = { 170 | ALWAYS_SEARCH_USER_PATHS = NO; 171 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 172 | CLANG_CXX_LIBRARY = "libc++"; 173 | CLANG_ENABLE_MODULES = YES; 174 | CLANG_ENABLE_OBJC_ARC = YES; 175 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 176 | CLANG_WARN_BOOL_CONVERSION = YES; 177 | CLANG_WARN_COMMA = YES; 178 | CLANG_WARN_CONSTANT_CONVERSION = YES; 179 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 180 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 181 | CLANG_WARN_EMPTY_BODY = YES; 182 | CLANG_WARN_ENUM_CONVERSION = YES; 183 | CLANG_WARN_INFINITE_RECURSION = YES; 184 | CLANG_WARN_INT_CONVERSION = YES; 185 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 186 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 187 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 188 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 189 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 190 | CLANG_WARN_STRICT_PROTOTYPES = YES; 191 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 192 | CLANG_WARN_UNREACHABLE_CODE = YES; 193 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 194 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 195 | COPY_PHASE_STRIP = NO; 196 | DEBUG_INFORMATION_FORMAT = dwarf; 197 | ENABLE_STRICT_OBJC_MSGSEND = YES; 198 | ENABLE_TESTABILITY = YES; 199 | GCC_C_LANGUAGE_STANDARD = gnu99; 200 | GCC_DYNAMIC_NO_PIC = NO; 201 | GCC_NO_COMMON_BLOCKS = YES; 202 | GCC_OPTIMIZATION_LEVEL = 0; 203 | GCC_PREPROCESSOR_DEFINITIONS = ( 204 | "DEBUG=1", 205 | "$(inherited)", 206 | ); 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 214 | MTL_ENABLE_DEBUG_INFO = YES; 215 | ONLY_ACTIVE_ARCH = YES; 216 | SDKROOT = iphoneos; 217 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 218 | TARGETED_DEVICE_FAMILY = "1,2"; 219 | }; 220 | name = Debug; 221 | }; 222 | 7304BE961BDB0F0900733159 /* Release */ = { 223 | isa = XCBuildConfiguration; 224 | buildSettings = { 225 | ALWAYS_SEARCH_USER_PATHS = NO; 226 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 227 | CLANG_CXX_LIBRARY = "libc++"; 228 | CLANG_ENABLE_MODULES = YES; 229 | CLANG_ENABLE_OBJC_ARC = YES; 230 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 231 | CLANG_WARN_BOOL_CONVERSION = YES; 232 | CLANG_WARN_COMMA = YES; 233 | CLANG_WARN_CONSTANT_CONVERSION = YES; 234 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 235 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 236 | CLANG_WARN_EMPTY_BODY = YES; 237 | CLANG_WARN_ENUM_CONVERSION = YES; 238 | CLANG_WARN_INFINITE_RECURSION = YES; 239 | CLANG_WARN_INT_CONVERSION = YES; 240 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 241 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 242 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 243 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 244 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 245 | CLANG_WARN_STRICT_PROTOTYPES = YES; 246 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 247 | CLANG_WARN_UNREACHABLE_CODE = YES; 248 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 249 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 250 | COPY_PHASE_STRIP = NO; 251 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 252 | ENABLE_NS_ASSERTIONS = NO; 253 | ENABLE_STRICT_OBJC_MSGSEND = YES; 254 | GCC_C_LANGUAGE_STANDARD = gnu99; 255 | GCC_NO_COMMON_BLOCKS = YES; 256 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 257 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 258 | GCC_WARN_UNDECLARED_SELECTOR = YES; 259 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 260 | GCC_WARN_UNUSED_FUNCTION = YES; 261 | GCC_WARN_UNUSED_VARIABLE = YES; 262 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 263 | MTL_ENABLE_DEBUG_INFO = NO; 264 | SDKROOT = iphoneos; 265 | SWIFT_COMPILATION_MODE = wholemodule; 266 | TARGETED_DEVICE_FAMILY = "1,2"; 267 | VALIDATE_PRODUCT = YES; 268 | }; 269 | name = Release; 270 | }; 271 | 7304BE981BDB0F0900733159 /* Debug */ = { 272 | isa = XCBuildConfiguration; 273 | buildSettings = { 274 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 275 | DEVELOPMENT_TEAM = 4F7DKRYKP6; 276 | INFOPLIST_FILE = SceneKitOffscreen/Info.plist; 277 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 278 | PRODUCT_BUNDLE_IDENTIFIER = com.lachlanhurst.SceneKitOffscreen; 279 | PRODUCT_NAME = "$(TARGET_NAME)"; 280 | SWIFT_SWIFT3_OBJC_INFERENCE = Default; 281 | SWIFT_VERSION = 4.2; 282 | }; 283 | name = Debug; 284 | }; 285 | 7304BE991BDB0F0900733159 /* Release */ = { 286 | isa = XCBuildConfiguration; 287 | buildSettings = { 288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 289 | DEVELOPMENT_TEAM = 4F7DKRYKP6; 290 | INFOPLIST_FILE = SceneKitOffscreen/Info.plist; 291 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 292 | PRODUCT_BUNDLE_IDENTIFIER = com.lachlanhurst.SceneKitOffscreen; 293 | PRODUCT_NAME = "$(TARGET_NAME)"; 294 | SWIFT_SWIFT3_OBJC_INFERENCE = Default; 295 | SWIFT_VERSION = 4.2; 296 | }; 297 | name = Release; 298 | }; 299 | /* End XCBuildConfiguration section */ 300 | 301 | /* Begin XCConfigurationList section */ 302 | 7304BE801BDB0F0800733159 /* Build configuration list for PBXProject "SceneKitOffscreen" */ = { 303 | isa = XCConfigurationList; 304 | buildConfigurations = ( 305 | 7304BE951BDB0F0900733159 /* Debug */, 306 | 7304BE961BDB0F0900733159 /* Release */, 307 | ); 308 | defaultConfigurationIsVisible = 0; 309 | defaultConfigurationName = Release; 310 | }; 311 | 7304BE971BDB0F0900733159 /* Build configuration list for PBXNativeTarget "SceneKitOffscreen" */ = { 312 | isa = XCConfigurationList; 313 | buildConfigurations = ( 314 | 7304BE981BDB0F0900733159 /* Debug */, 315 | 7304BE991BDB0F0900733159 /* Release */, 316 | ); 317 | defaultConfigurationIsVisible = 0; 318 | defaultConfigurationName = Release; 319 | }; 320 | /* End XCConfigurationList section */ 321 | }; 322 | rootObject = 7304BE7D1BDB0F0800733159 /* Project object */; 323 | } 324 | -------------------------------------------------------------------------------- /SceneKitOffscreen.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SceneKitOffscreen.xcodeproj/xcuserdata/lachlan.xcuserdatad/xcschemes/SceneKitOffscreen.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /SceneKitOffscreen.xcodeproj/xcuserdata/lachlan.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SceneKitOffscreen.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 7304BE841BDB0F0800733159 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /SceneKitOffscreen/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SceneKitOffscreen 4 | // 5 | // Created by Lachlan Hurst on 24/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /SceneKitOffscreen/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /SceneKitOffscreen/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /SceneKitOffscreen/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /SceneKitOffscreen/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /SceneKitOffscreen/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // SceneKitOffscreen 4 | // 5 | // Created by Lachlan Hurst on 24/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SceneKit 11 | import Metal 12 | 13 | class ViewController: UIViewController, SCNSceneRendererDelegate { 14 | @IBOutlet var scnView1: SCNView! 15 | @IBOutlet var scnView2: SCNView! 16 | 17 | var scene1:SCNScene! 18 | var scene2:SCNScene! 19 | 20 | var plane:SCNGeometry! 21 | 22 | var device:MTLDevice! 23 | var commandQueue: MTLCommandQueue! 24 | var renderer: SCNRenderer! 25 | 26 | var offscreenTexture:MTLTexture! 27 | let rgbColorSpace = CGColorSpaceCreateDeviceRGB() 28 | let bytesPerPixel = Int(4) 29 | let bitsPerComponent = Int(8) 30 | let bitsPerPixel:Int = 32 31 | var textureSizeX:Int = 50 32 | var textureSizeY:Int = 50 33 | 34 | override func viewDidLoad() { 35 | super.viewDidLoad() 36 | 37 | scene1 = SCNScene() 38 | 39 | let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0) 40 | box.materials.first?.diffuse.contents = UIColor.red 41 | let boxNode = SCNNode(geometry: box) 42 | scene1.rootNode.addChildNode(boxNode) 43 | 44 | let sphere = SCNSphere(radius: 1) 45 | sphere.materials.first?.diffuse.contents = UIColor.yellow 46 | let sphereNode = SCNNode(geometry: sphere) 47 | sphereNode.position = SCNVector3Make(2, 0, 0) 48 | scene1.rootNode.addChildNode(sphereNode) 49 | 50 | scnView1.scene = scene1 51 | 52 | 53 | scene2 = SCNScene() 54 | 55 | plane = SCNPlane(width: 10, height: 10) 56 | let planeNode = SCNNode(geometry: plane) 57 | scene2.rootNode.addChildNode(planeNode) 58 | 59 | scnView2.scene = scene2 60 | 61 | 62 | 63 | scnView1.autoenablesDefaultLighting = true 64 | scnView2.autoenablesDefaultLighting = true 65 | scnView1.allowsCameraControl = true 66 | scnView2.allowsCameraControl = true 67 | //scnView1.showsStatistics = true 68 | //scnView2.showsStatistics = true 69 | scnView1.isPlaying = true 70 | scnView2.isPlaying = true 71 | 72 | scnView1.delegate = self 73 | 74 | setupMetal() 75 | setupTexture() 76 | 77 | plane.materials.first?.diffuse.contents = offscreenTexture 78 | } 79 | 80 | func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) { 81 | doRender() 82 | } 83 | 84 | func doRender() { 85 | //rendering to a MTLTexture, so the viewport is the size of this texture 86 | let viewport = CGRect(x: 0, y: 0, width: CGFloat(textureSizeX), height: CGFloat(textureSizeY)) 87 | 88 | //write to offscreenTexture, clear the texture before rendering using green, store the result 89 | let renderPassDescriptor = MTLRenderPassDescriptor() 90 | renderPassDescriptor.colorAttachments[0].texture = offscreenTexture 91 | renderPassDescriptor.colorAttachments[0].loadAction = .clear 92 | renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 1, 0, 1.0); //green 93 | renderPassDescriptor.colorAttachments[0].storeAction = .store 94 | 95 | let commandBuffer = commandQueue.makeCommandBuffer()! 96 | 97 | // reuse scene1 and the current point of view 98 | renderer.scene = scene1 99 | renderer.pointOfView = scnView1.pointOfView 100 | renderer.render(atTime: 0, viewport: viewport, commandBuffer: commandBuffer, passDescriptor: renderPassDescriptor) 101 | 102 | commandBuffer.commit() 103 | } 104 | 105 | func setupMetal() { 106 | if let defaultMtlDevice = MTLCreateSystemDefaultDevice() { 107 | device = defaultMtlDevice 108 | commandQueue = device.makeCommandQueue() 109 | renderer = SCNRenderer(device: device, options: nil) 110 | } else { 111 | fatalError("iOS simulator does not support Metal, this example can only be run on a real device.") 112 | } 113 | } 114 | 115 | func setupTexture() { 116 | 117 | var rawData0 = [UInt8](repeating: 0, count: Int(textureSizeX) * Int(textureSizeY) * 4) 118 | 119 | let bytesPerRow = 4 * Int(textureSizeX) 120 | let bitmapInfo = CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue 121 | 122 | let context = CGContext(data: &rawData0, width: Int(textureSizeX), height: Int(textureSizeY), bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: rgbColorSpace, bitmapInfo: bitmapInfo)! 123 | context.setFillColor(UIColor.green.cgColor) 124 | context.fill(CGRect(x: 0, y: 0, width: CGFloat(textureSizeX), height: CGFloat(textureSizeY))) 125 | 126 | let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.rgba8Unorm, width: Int(textureSizeX), height: Int(textureSizeY), mipmapped: false) 127 | 128 | textureDescriptor.usage = MTLTextureUsage(rawValue: MTLTextureUsage.renderTarget.rawValue | MTLTextureUsage.shaderRead.rawValue) 129 | 130 | let textureA = device.makeTexture(descriptor: textureDescriptor)! 131 | 132 | let region = MTLRegionMake2D(0, 0, Int(textureSizeX), Int(textureSizeY)) 133 | textureA.replace(region: region, mipmapLevel: 0, withBytes: &rawData0, bytesPerRow: Int(bytesPerRow)) 134 | 135 | offscreenTexture = textureA 136 | } 137 | 138 | override func didReceiveMemoryWarning() { 139 | super.didReceiveMemoryWarning() 140 | // Dispose of any resources that can be recreated. 141 | } 142 | 143 | 144 | } 145 | 146 | -------------------------------------------------------------------------------- /doc/SceneKitOffscreen.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lachlanhurst/SceneKitOffscreenRendering/a4ebe0c5acb482c06d5fdf710a5ef11bc37db322/doc/SceneKitOffscreen.gif -------------------------------------------------------------------------------- /doc/ScenekitOffscreen.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lachlanhurst/SceneKitOffscreenRendering/a4ebe0c5acb482c06d5fdf710a5ef11bc37db322/doc/ScenekitOffscreen.PNG --------------------------------------------------------------------------------