Skip to content

Commit b9984c2

Browse files
committed
Update rendering
1 parent f9194b1 commit b9984c2

File tree

3 files changed

+41
-61
lines changed

3 files changed

+41
-61
lines changed

Sources/GateEngine/System/Rendering/Drawables/Canvas.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@
424424
preconditionFailure("Must set size during `Canvas.init` to use \(#function).")
425425
}
426426

427-
let matricies = camera.matricies(withAspectRatio: size.aspectRatio)
427+
let matricies = camera.matricies(withViewportSize: size)
428428
var position = position * matricies.viewProjection()
429429
position.x /= position.z
430430
position.y /= position.z
@@ -446,28 +446,33 @@
446446
preconditionFailure("Must set size during `Canvas.init` to use \(#function).")
447447
}
448448

449-
let halfSize = size / 2
450-
let aspectRatio = size.aspectRatio
449+
switch camera.fieldOfView {
450+
case .perspective(let fov):
451+
let halfSize = size / 2
452+
let aspectRatio = size.aspectRatio
451453

452-
let inverseView = camera.matricies(withAspectRatio: aspectRatio).view.inverse
453-
let halfFOV = tan(camera.fieldOfViewAsRadians.rawValue * 0.5)
454-
let near = camera.clippingPlane.near
455-
let far = camera.clippingPlane.far
454+
let inverseView = camera.matricies(withViewportSize: size).view.inverse
455+
let halfFOV = tan(fov.rawValueAsRadians * 0.5)
456+
let near = camera.clippingPlane.near
457+
let far = camera.clippingPlane.far
456458

457-
let dx = halfFOV * (position.x / halfSize.width - 1.0) * aspectRatio
458-
let dy = halfFOV * (1.0 - position.y / halfSize.height)
459+
let dx = halfFOV * (position.x / halfSize.width - 1.0) * aspectRatio
460+
let dy = halfFOV * (1.0 - position.y / halfSize.height)
459461

460-
let p1 = Position3(dx * near, dy * near, near) * inverseView
461-
let p2 = Position3(dx * far, dy * far, far) * inverseView
462+
let p1 = Position3(dx * near, dy * near, near) * inverseView
463+
let p2 = Position3(dx * far, dy * far, far) * inverseView
462464

463-
return Ray3D(from: p1, toward: p2)
465+
return Ray3D(from: p1, toward: p2)
466+
case .orthographic(_):
467+
fatalError("Not implemented")
468+
}
464469
}
465470

466471
/**
467472
Create a canvas.
468473

469474
- parameter camera: An optional Scene camera, which is required for 3D space conversions.
470-
- parameter size: The exact size of the canvas, this should be the saizxe of your renderTarget.
475+
- parameter size: The exact size of the canvas, this should be the size of your renderTarget.
471476
- parameter interfaceScale: The userInterface scale. Sometimes called HiDPI. This setting changes how some drawable items are laid out. Use `1` for traditional gaming style drawing.
472477
- parameter estimatedCommandCount: A performance hint of how many commands will be added.
473478
*/

Sources/GateEngine/System/Rendering/Drawables/Scene.swift

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* http://stregasgate.com
66
*/
77

8+
import Shaders
9+
810
/// A Scene is a drawing space with 3 dimensions and a perspective camera.
911
@MainActor public struct Scene: Drawable {
1012
@usableFromInline internal var camera: Camera
@@ -65,9 +67,10 @@
6567
_ geometry: Geometry,
6668
withMaterial material: Material,
6769
at transform: Transform3,
70+
blendMode: DrawCommand.Flags.BlendMode = .normal,
6871
flags: SceneElementFlags = .default
6972
) {
70-
self.insert(geometry, withMaterial: material, at: [transform], flags: flags)
73+
self.insert(geometry, withMaterial: material, at: [transform], blendMode: blendMode, flags: flags)
7174
}
7275

7376
/** Adds geometry to the scene for rendering.
@@ -89,8 +92,8 @@
8992
resource: .geometry(geometry),
9093
transforms: transforms,
9194
material: material,
92-
vsh: .standard,
93-
fsh: (material.channels.first?.texture != nil) ? .textureSample : .materialColor,
95+
vsh: (material.channels.first?.texture != nil) ? .standard : material.channels.first?.color == .vertexColors ? .vertexColors : .standard,
96+
fsh: (material.channels.first?.texture != nil) ? .textureSample : material.channels.first?.color == .vertexColors ? .vertexColor : .materialColor,
9497
flags: flags.drawCommandFlags(withPrimitive: .triangle, blendMode: blendMode)
9598
)
9699
self.insert(command)
@@ -203,53 +206,15 @@
203206

204207
/** Adds lines to the scene for rendering.
205208
- parameter lines: The lines to draw.
209+
- parameter color: The color information used to draw the geometry. nil will use vertex color data.
206210
- parameter transform: Describes how the lines instance should be positioned and scaled relative to the scene.
207211
- parameter flags: Options to customize how drawing is handled.
208212
- Explicitly instances the geometry as it's own batch. Use this for known instancing like particles.
209213
*/
210214
@_transparent
211215
public mutating func insert(
212216
_ lines: Lines,
213-
at transform: Transform3,
214-
flags: SceneElementFlags = .default
215-
) {
216-
self.insert(lines, at: [transform], flags: flags)
217-
}
218-
219-
/** Adds lines to the scene for rendering.
220-
- parameter lines: The lines to draw.
221-
- parameter transforms: Describes how each lines instance should be positioned and scaled relative to the scene.
222-
- parameter flags: Options to customize how drawing is handled.
223-
- Explicitly instances the geometry as it's own batch. Use this for known instancing like particles.
224-
*/
225-
@inlinable @inline(__always)
226-
public mutating func insert(
227-
_ lines: Lines,
228-
at transforms: [Transform3],
229-
flags: SceneElementFlags = .default
230-
) {
231-
let command = DrawCommand(
232-
resource: .lines(lines),
233-
transforms: transforms,
234-
material: Material(color: .vertexColors),
235-
vsh: .vertexColors,
236-
fsh: .vertexColor,
237-
flags: flags.drawCommandFlags(withPrimitive: .line)
238-
)
239-
self.insert(command)
240-
}
241-
242-
/** Adds lines to the scene for rendering.
243-
- parameter lines: The lines to draw.
244-
- parameter color: The color information used to draw the geometry.
245-
- parameter transform: Describes how the lines instance should be positioned and scaled relative to the scene.
246-
- parameter flags: Options to customize how drawing is handled.
247-
- Explicitly instances the geometry as it's own batch. Use this for known instancing like particles.
248-
*/
249-
@_transparent
250-
public mutating func insert(
251-
_ lines: Lines,
252-
withColor color: Color,
217+
withColor color: Color? = nil,
253218
at transform: Transform3,
254219
flags: SceneElementFlags = .default
255220
) {
@@ -258,23 +223,24 @@
258223

259224
/** Adds lines to the scene for rendering.
260225
- parameter lines: The lines to draw.
261-
- parameter color: The color information used to draw the geometry.
226+
- parameter color: The color information used to draw the geometry. nil will use vertex color data.
262227
- parameter transforms: Describes how each lines instance should be positioned and scaled relative to the scene.
263228
- parameter flags: Options to customize how drawing is handled.
264229
- Explicitly instances the geometry as it's own batch. Use this for known instancing like particles.
265230
*/
266231
@inlinable @inline(__always)
267232
public mutating func insert(
268233
_ lines: Lines,
269-
withColor color: Color,
234+
withColor color: Color? = nil,
270235
at transforms: [Transform3],
271236
flags: SceneElementFlags = .default
272237
) {
238+
let useVertexColors: Bool = (color == nil)
273239
let command = DrawCommand(
274240
resource: .lines(lines),
275241
transforms: transforms,
276-
material: Material(color: color),
277-
vsh: .standard,
242+
material: Material(color: useVertexColors ? .black : color!),
243+
vsh: useVertexColors ? .vertexColors : .materialColor,
278244
fsh: .materialColor,
279245
flags: flags.drawCommandFlags(withPrimitive: .line)
280246
)
@@ -399,7 +365,7 @@
399365

400366
@_transparent
401367
public func matrices(withSize size: GameMath.Size2) -> Matrices {
402-
self.camera.matricies(withAspectRatio: size.aspectRatio)
368+
self.camera.matricies(withViewportSize: size)
403369
}
404370
}
405371

Sources/GateEngine/System/Rendering/SystemShaders.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ extension VertexShader {
3434
return vsh
3535
}()
3636

37+
public static let materialColor: VertexShader = {
38+
let vsh = VertexShader()
39+
let mvp = vsh.modelViewProjectionMatrix
40+
let vertexPosition = vsh.input.geometry(0).position
41+
vsh.output.position = mvp * Vec4(vertexPosition, 1)
42+
vsh.output["color"] = vsh.channel(0).color
43+
return vsh
44+
}()
45+
3746
public static let skinned: VertexShader = {
3847
let vsh = VertexShader()
3948
let bones = vsh.uniforms.value(named: "bones", as: Mat4Array.self, arrayCapacity: 64)

0 commit comments

Comments
 (0)