Skip to content

Commit a70418e

Browse files
committed
merge shader type
1 parent 88005f9 commit a70418e

File tree

6 files changed

+39
-38
lines changed

6 files changed

+39
-38
lines changed

SwiftArche.xcodeproj/project.pbxproj

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
043E2CA029212E8C0057BDD1 /* Light.swift in Sources */ = {isa = PBXBuildFile; fileRef = 043E2C9F29212E8C0057BDD1 /* Light.swift */; };
1212
04ED07DA29226E9C00DD55AB /* normal_shading.metal in Sources */ = {isa = PBXBuildFile; fileRef = 04ED07D929226E9C00DD55AB /* normal_shading.metal */; };
1313
04ED07DE292284FD00DD55AB /* shadow_shading.metal in Sources */ = {isa = PBXBuildFile; fileRef = 04ED07DD292284FD00DD55AB /* shadow_shading.metal */; };
14-
3E0F7B0C2921F63B00C4A843 /* skybox.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E0F7B0B2921F63B00C4A843 /* skybox.metal */; };
14+
3E0F7B0C2921F63B00C4A843 /* skybox_shading.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E0F7B0B2921F63B00C4A843 /* skybox_shading.metal */; };
1515
3E0F7B1429220F8C00C4A843 /* shadowmap.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E0F7B1329220F8C00C4A843 /* shadowmap.metal */; };
1616
3E0F7B1C2922374400C4A843 /* pbr_shading.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E0F7B1B2922374400C4A843 /* pbr_shading.metal */; };
1717
3E67EDC9291B2F7600ED60C8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67EDC8291B2F7600ED60C8 /* AppDelegate.swift */; };
@@ -60,7 +60,7 @@
6060
3E67EEC4291DF16D00ED60C8 /* RenderTargetBlendState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8C1E5621B240E0DF2CAC8 /* RenderTargetBlendState.swift */; };
6161
3E67EEC5291DF16D00ED60C8 /* BlendState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8C050955DD1855EEBFE8E /* BlendState.swift */; };
6262
3E67EEC6291DF54F00ED60C8 /* DepthState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8C518C7B03C3787F9676C /* DepthState.swift */; };
63-
3E67EED12921D9C800ED60C8 /* unlit.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E67EED02921D9C800ED60C8 /* unlit.metal */; };
63+
3E67EED12921D9C800ED60C8 /* unlit_shading.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E67EED02921D9C800ED60C8 /* unlit_shading.metal */; };
6464
3E67EED42921DB4A00ED60C8 /* function_common.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3E67EED32921DB4A00ED60C8 /* function_common.metal */; };
6565
43D8C028EAA2732208634F87 /* EngineObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8C860A954154FCBE6973D /* EngineObject.swift */; };
6666
43D8C14B77E8239FBF9BBE3B /* RenderState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8CC2F6FD53A7EB63662AF /* RenderState.swift */; };
@@ -202,7 +202,7 @@
202202
04ED07DC292284A100DD55AB /* shadow_shading.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = shadow_shading.h; sourceTree = "<group>"; };
203203
04ED07DD292284FD00DD55AB /* shadow_shading.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = shadow_shading.metal; sourceTree = "<group>"; };
204204
3E0F7B0A2921E3CE00C4A843 /* shader_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = shader_common.h; sourceTree = "<group>"; };
205-
3E0F7B0B2921F63B00C4A843 /* skybox.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = skybox.metal; sourceTree = "<group>"; };
205+
3E0F7B0B2921F63B00C4A843 /* skybox_shading.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = skybox_shading.metal; sourceTree = "<group>"; };
206206
3E0F7B1329220F8C00C4A843 /* shadowmap.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = shadowmap.metal; sourceTree = "<group>"; };
207207
3E0F7B1B2922374400C4A843 /* pbr_shading.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = pbr_shading.metal; sourceTree = "<group>"; };
208208
3E0F7B1D2922375800C4A843 /* pbr_shading.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pbr_shading.h; sourceTree = "<group>"; };
@@ -286,7 +286,7 @@
286286
3E67EEAD291CEC9D00ED60C8 /* UpdateFlagManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateFlagManager.swift; sourceTree = "<group>"; };
287287
3E67EEAF291CECA500ED60C8 /* UpdateFlag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateFlag.swift; sourceTree = "<group>"; };
288288
3E67EEC7291F978900ED60C8 /* function_constant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = function_constant.h; sourceTree = "<group>"; };
289-
3E67EED02921D9C800ED60C8 /* unlit.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = unlit.metal; sourceTree = "<group>"; };
289+
3E67EED02921D9C800ED60C8 /* unlit_shading.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = unlit_shading.metal; sourceTree = "<group>"; };
290290
3E67EED22921DB3F00ED60C8 /* function_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = function_common.h; sourceTree = "<group>"; };
291291
3E67EED32921DB4A00ED60C8 /* function_common.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = function_common.metal; sourceTree = "<group>"; };
292292
43D8C050955DD1855EEBFE8E /* BlendState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlendState.swift; sourceTree = "<group>"; };
@@ -536,8 +536,8 @@
536536
3E67EED22921DB3F00ED60C8 /* function_common.h */,
537537
3E67EED32921DB4A00ED60C8 /* function_common.metal */,
538538
3E0F7B0A2921E3CE00C4A843 /* shader_common.h */,
539-
3E67EED02921D9C800ED60C8 /* unlit.metal */,
540-
3E0F7B0B2921F63B00C4A843 /* skybox.metal */,
539+
3E67EED02921D9C800ED60C8 /* unlit_shading.metal */,
540+
3E0F7B0B2921F63B00C4A843 /* skybox_shading.metal */,
541541
04ED07DB29226EA800DD55AB /* normal_shading.h */,
542542
04ED07D929226E9C00DD55AB /* normal_shading.metal */,
543543
3E0F7B1D2922375800C4A843 /* pbr_shading.h */,
@@ -1079,9 +1079,9 @@
10791079
3E0F7B1429220F8C00C4A843 /* shadowmap.metal in Sources */,
10801080
04ED07DA29226E9C00DD55AB /* normal_shading.metal in Sources */,
10811081
04ED07DE292284FD00DD55AB /* shadow_shading.metal in Sources */,
1082-
3E67EED12921D9C800ED60C8 /* unlit.metal in Sources */,
1082+
3E67EED12921D9C800ED60C8 /* unlit_shading.metal in Sources */,
10831083
3E0F7B1C2922374400C4A843 /* pbr_shading.metal in Sources */,
1084-
3E0F7B0C2921F63B00C4A843 /* skybox.metal in Sources */,
1084+
3E0F7B0C2921F63B00C4A843 /* skybox_shading.metal in Sources */,
10851085
);
10861086
runOnlyForDeploymentPostprocessing = 0;
10871087
};

vox.render/Camera.swift

+9-18
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ import Metal
88
import vox_math
99

1010
public class Camera: Component {
11-
struct CameraData {
12-
var u_viewMat = Matrix()
13-
var u_projMat = Matrix()
14-
var u_VPMat = Matrix()
15-
var u_viewInvMat = Matrix()
16-
var u_projInvMat = Matrix()
17-
var u_cameraPos = Vector3()
18-
}
19-
2011
private var _cameraData = CameraData()
2112
private static var _cameraProperty = "u_camera"
2213

@@ -60,6 +51,7 @@ public class Camera: Component {
6051
private var _viewport: Vector4 = Vector4(0, 0, 1, 1)
6152
private var _lastAspectSize: Vector2 = Vector2(0, 0)
6253
private var _invViewProjMat: Matrix = Matrix();
54+
private var _inverseProjectionMatrix: Matrix = Matrix();
6355

6456
/// Near clip plane - the closest point to the camera when rendering occurs.
6557
public var nearClipPlane: Float {
@@ -287,7 +279,6 @@ extension Camera {
287279
/// - out: Ray
288280
/// - Returns: Ray
289281
public func viewportPointToRay(_ point: Vector2, _ out: Ray) -> Ray {
290-
let invViewProjMat = invViewProjMat
291282
// Use the intersection of the near clipping plane as the origin point.
292283
out.origin = _innerViewportToWorldPoint(point.x, point.y, 0.0, invViewProjMat);
293284
// Use the intersection of the far clipping plane as the origin point.
@@ -411,12 +402,12 @@ extension Camera {
411402
}
412403

413404
private func _updateShaderData() {
414-
_cameraData.u_viewInvMat = _transform.worldMatrix
415-
_cameraData.u_viewInvMat = _transform.worldMatrix
416-
_cameraData.u_viewMat = viewMatrix
417-
_cameraData.u_projInvMat = inverseProjectionMatrix
418-
_cameraData.u_projMat = projectionMatrix
419-
_cameraData.u_VPMat = projectionMatrix * viewMatrix
405+
_cameraData.u_viewInvMat = _transform.worldMatrix.elements
406+
_cameraData.u_projInvMat = inverseProjectionMatrix.elements
407+
_cameraData.u_viewMat = viewMatrix.elements
408+
_cameraData.u_projMat = projectionMatrix.elements
409+
_cameraData.u_VPMat = (projectionMatrix * viewMatrix).elements
410+
_cameraData.u_cameraPos = _transform.worldPosition.internalVector3
420411
shaderData.setData(Camera._cameraProperty, _cameraData)
421412
}
422413

@@ -436,9 +427,9 @@ extension Camera {
436427
get {
437428
if (_isInvProjMatDirty) {
438429
_isInvProjMatDirty = false
439-
_cameraData.u_projInvMat = Matrix.invert(a: projectionMatrix)
430+
_inverseProjectionMatrix = Matrix.invert(a: projectionMatrix)
440431
}
441-
return _cameraData.u_projInvMat
432+
return _inverseProjectionMatrix
442433
}
443434
}
444435
}

vox.render/Renderer.swift

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ import vox_math
99

1010
/// Renderable component.
1111
public class Renderer: Component {
12-
struct RendererData {
13-
var u_localMat = Matrix()
14-
var u_modelMat = Matrix()
15-
var u_normalMat = Matrix()
16-
};
17-
1812
/// ShaderData related to renderer.
1913
public var shaderData: ShaderData
2014
/// Whether it is clipped by the frustum, needs to be turned on camera.enableFrustumCulling.
@@ -34,7 +28,7 @@ public class Renderer: Component {
3428
var _dirtyUpdateFlag: Int = 0
3529
var _receiveShadows: Bool = true
3630

37-
static private let _renderProperty = "u_rendererData"
31+
static private let _renderProperty = "u_renderer"
3832
private var _rendererData = RendererData()
3933

4034
/// Whether receive shadow.
@@ -173,10 +167,10 @@ public class Renderer: Component {
173167
func _updateShaderData(_ cameraInfo: CameraInfo) {
174168
let worldMatrix = entity.transform.worldMatrix
175169

176-
_rendererData.u_modelMat = worldMatrix
177-
_rendererData.u_localMat = entity.transform.localMatrix
170+
_rendererData.u_modelMat = worldMatrix.elements
171+
_rendererData.u_localMat = entity.transform.localMatrix.elements
178172
var normalMatrix = Matrix.invert(a: worldMatrix)
179-
_rendererData.u_normalMat = normalMatrix.transpose()
173+
_rendererData.u_normalMat = normalMatrix.transpose().elements
180174

181175
shaderData.setData(Renderer._renderProperty, _rendererData)
182176
}
File renamed without changes.

vox.shader/type_common.h

+15
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ struct DirectLightData {
5252
vector_float3 color;
5353
vector_float3 direction;
5454
};
55+
56+
struct CameraData {
57+
matrix_float4x4 u_viewMat;
58+
matrix_float4x4 u_projMat;
59+
matrix_float4x4 u_VPMat;
60+
matrix_float4x4 u_viewInvMat;
61+
matrix_float4x4 u_projInvMat;
62+
vector_float3 u_cameraPos;
63+
};
64+
65+
struct RendererData {
66+
matrix_float4x4 u_localMat;
67+
matrix_float4x4 u_modelMat;
68+
matrix_float4x4 u_normalMat;
69+
};

vox.shader/unlit.metal vox.shader/unlit_shading.metal

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ typedef struct {
1717

1818
vertex VertexOut vertex_unlit(const VertexIn in [[stage_in]],
1919
uint v_id [[vertex_id]],
20-
constant matrix_float4x4 &u_MVPMat [[buffer(3)]],
20+
constant CameraData &u_camera [[buffer(2)]],
21+
constant RendererData &u_renderer [[buffer(3)]],
2122
constant float4 &u_tilingOffset [[buffer(4)]],
2223
// skin
2324
texture2d<float> u_jointTexture [[texture(0), function_constant(hasSkinAndHasJointTexture)]],
@@ -71,7 +72,7 @@ vertex VertexOut vertex_unlit(const VertexIn in [[stage_in]],
7172
out.v_uv = out.v_uv * u_tilingOffset.xy + u_tilingOffset.zw;
7273
}
7374

74-
out.position = u_MVPMat * position;
75+
out.position = u_camera.u_VPMat * u_renderer.u_modelMat * position;
7576

7677
return out;
7778
}

0 commit comments

Comments
 (0)