The Skung Cave commit

This commit is contained in:
2024-05-09 20:52:01 +10:00
parent 446c444728
commit 9461fe08cf
35 changed files with 11988 additions and 9097 deletions

View File

@ -35,7 +35,7 @@ extension ContentManager
loaders[ext] = loader
}
public mutating func create(mesh: Mesh) throws -> RenderMesh
public mutating func create<V: Vertex>(mesh: Mesh<V>) throws -> RenderMesh<V>
{
let rendMesh = try renderer.createMesh(mesh: mesh)
resources.append(rendMesh)
@ -72,13 +72,18 @@ extension ContentManager
{
let resourceUrl = try getResource(path)
let loader = loaders[resourceUrl.pathExtension]
let loader = loaders[resourceUrl.pathExtension.lowercased()]
guard let resource = loader?.load(url: resourceUrl)
else { throw ContentError.loadFailure }
if T.self == Mesh.self
if T.self == Mesh<VertexPositionNormalTexcoord>.self
{
guard let mesh = resource as? Mesh else { throw ContentError.loadFailure }
guard let mesh = resource as? Mesh<VertexPositionNormalTexcoord> else { throw ContentError.loadFailure }
return mesh as! T
}
if T.self == Mesh<VertexPositionNormalColourTexcoord>.self
{
guard let mesh = resource as? Mesh<VertexPositionNormalTexcoord> else { throw ContentError.loadFailure }
return mesh as! T
}
if T.self == Image.self
@ -100,13 +105,18 @@ extension ContentManager
{
let resourceUrl = try getResource(path)
let loader = loaders[resourceUrl.pathExtension]
let loader = loaders[resourceUrl.pathExtension.lowercased()]
guard let resource = loader?.load(url: resourceUrl)
else { throw ContentError.loadFailure }
if T.self == Mesh.self
if T.self == Mesh<VertexPositionNormalTexcoord>.self
{
guard let mesh = resource as? Mesh else { throw ContentError.loadFailure }
guard let mesh = resource as? Mesh<VertexPositionNormalTexcoord> else { throw ContentError.loadFailure }
return mesh as! T
}
if T.self == Mesh<VertexPositionNormalColourTexcoord>.self
{
guard let mesh = resource as? Mesh<VertexPositionNormalTexcoord> else { throw ContentError.loadFailure }
return mesh as! T
}
if T.self == Image.self
@ -131,9 +141,15 @@ extension ContentManager
guard let resource = loader?.load(url: resourceUrl)
else { throw ContentError.loadFailure }
if T.self == RenderMesh.self
if T.self == RenderMesh<VertexPositionNormalTexcoord>.self
{
guard let mesh = resource as? Mesh else { throw ContentError.loadFailure }
guard let mesh = resource as? Mesh<VertexPositionNormalTexcoord> else { throw ContentError.loadFailure }
let renderResource = try self.create(mesh: mesh)
return renderResource as! T
}
if T.self == RenderMesh<VertexPositionNormalColourTexcoord>.self
{
guard let mesh = resource as? Mesh<VertexPositionNormalColourTexcoord> else { throw ContentError.loadFailure }
let renderResource = try self.create(mesh: mesh)
return renderResource as! T
}
@ -144,9 +160,13 @@ extension ContentManager
{
for resource in resources.reversed()
{
if resource is RenderMesh
if resource is RenderMesh<VertexPositionNormalTexcoord>
{
renderer.deleteMesh(resource as! RenderMesh)
renderer.deleteMesh(resource as! RenderMesh<VertexPositionNormalTexcoord>)
}
else if resource is RenderMesh<VertexPositionNormalColourTexcoord>
{
renderer.deleteMesh(resource as! RenderMesh<VertexPositionNormalColourTexcoord>)
}
else if resource is RenderTexture2D
{

View File

@ -6,4 +6,5 @@ public protocol LoaderProtocol
associatedtype T: Resource
func load(url: URL) -> T?
func load(url: URL, content: inout ContentManager) -> T?
}

View File

@ -1,54 +1,86 @@
import OrderedCollections
public struct Model: Resource
public struct Model<T: Vertex>: Resource
{
public let meshes: [Mesh]
public let meshes: [Mesh<T>]
}
public struct Mesh: Resource
public struct Mesh<T: Vertex>: Resource
{
public typealias Index = UInt16
public struct Vertex: Equatable
{
public let position: Vec3f
public let normal: Vec3f
public let texCoord: Vec2f
public init(position: Vec3f, normal: Vec3f, texCoord: Vec2f)
{
self.position = position
self.normal = normal
self.texCoord = texCoord
}
}
public struct SubMesh
{
public let start, length: Int
public let material: Int //hack
public init(start: Int, length: Int)
public init(start: Int, length: Int, material: Int = -1)
{
self.start = start
self.length = length
self.material = material
}
}
public let vertices: [Vertex]
public let vertices: [T]
public let indices: [Index]
public let subMeshes: OrderedDictionary<String, SubMesh>
public let materials: [Material] // hack
}
public extension Mesh
{
static let empty = Self(vertices: .init(), indices: .init(), subMeshes: .init())
static var empty: Self { Self(vertices: .init(), indices: .init(), subMeshes: .init(), materials: .init()) }
init(vertices: [Vertex], indices: [Index])
init(vertices: [T], indices: [Index])
{
self.init(
vertices: vertices,
indices: indices,
subMeshes: .init())
subMeshes: .init(),
materials: .init())
}
init(vertices: [T], indices: [Index], subMeshes: OrderedDictionary<String, SubMesh>)
{
self.init(
vertices: vertices,
indices: indices,
subMeshes: subMeshes,
materials: .init())
}
}
public protocol Vertex: Equatable {}
public struct VertexPositionNormalTexcoord: Vertex
{
public let position: Vec3f
public let normal: Vec3f
public let texCoord: Vec2f
public init(position: Vec3f, normal: Vec3f, texCoord: Vec2f)
{
self.position = position
self.normal = normal
self.texCoord = texCoord
}
}
public struct VertexPositionNormalColourTexcoord: Vertex
{
public let position: Vec3f
public let normal: Vec3f
public let colour: Colour
public let texCoord: Vec2f
public init(position: Vec3f, colour: Colour, normal: Vec3f, texCoord: Vec2f)
{
self.position = position
self.colour = colour
self.normal = normal
self.texCoord = texCoord
}
}

View File

@ -1,15 +1,18 @@
import Foundation
import OrderedCollections
public struct ObjModel
{
public var positions = [Vec3f]()
public var colours = [Vec3f]()
public var normals = [Vec3f]()
public var texCoords = [Vec2f]()
public var objects = Dictionary<String, Object>()
public struct Object { public var faces = [Face]() }
public var objects = OrderedDictionary<String, Object>()
public var materials = Dictionary<String, ObjMaterial>()
public struct Object { public var meshes = [Mesh]() }
public struct Mesh { public var material = "", faces = [Face]() }
public struct Index { public let p: Int, n: Int, t: Int }
public enum Face
{