- To avoid using closures, you could define
terrainas a computed property:To conceal the intermediate steps needed to build objects in your code base, use the Factory pattern 🏭.
var terrain: MTLTexture {
//Do all the necessay configuration
}
Another way to conceal the intermediate variables needed to build objects in your code base, is the Factory pattern 🏭.
- For objects that take an empty initializer, like a
MTLTextureDescriptor, you could use the Builder Pattern. It emphasizes the separation between the phase of building the object to your liking and the phase of actually using it. You could define a custom convenience initializer, but the Builder pattern gives you more freedom in the number of properties to initialize and the order of assigning values to the different properties of the object.For objects that take an empty initializer, like a
MTLTextureDescriptor, you could use the Builder Pattern. It emphasizes the separation between the phase of building the object to your liking and the phase of actually using it. You could define a custom convenience initializer, but the Builder pattern gives you more freedom in the number of properties to initialize and the order of assigning values to the different properties of the object.
extension MTLTextureDescriptor: Buildable {}
varlet terrain: MTLTexture = {
guard let device = MTLCreateSystemDefaultDevice() else {
fatalError("Couldn't get a reference to the preferred default Metal device object.")
}
let descriptor = MTLTextureDescriptor.builder()
.textureType(.type2DArray)
.arrayLength(TERRAIN_TEXTURES_COUNT)
.width(TERRAIN_TEXTURE_SIZE)
.height(TERRAIN_TEXTURE_SIZE)
.mipmapLevelCount(TERRAIN_TEXTURE_MIPMAP_LEVEL_COUNT)
.build()
guard let texture = device.makeTexture(descriptor: descriptor) else {
fatalError("Couldn't make a new texture object.")
}
return texture
}()