金属着色器更改摄像机 Z 值无效。

huangapple go评论84阅读模式
英文:

Metal shader changing camera z does nothing

问题

以下是您提供的Metal Shader代码的翻译部分:

  1. #include <metal_stdlib>
  2. using namespace metal;
  3. // 将角度转换为弧度的函数
  4. float degreesToRadians(float degrees) {
  5. return degrees * (M_PI_F / 180.0);
  6. }
  7. // 生成投影矩阵的函数
  8. float4x4 generateProjectionMatrix(float fov, float aspectRatio, float near, float far) {
  9. float yScale = 1.0 / tan(fov * 0.5);
  10. float xScale = yScale / aspectRatio;
  11. float zRange = near - far;
  12. float4x4 projectionMatrix;
  13. projectionMatrix.columns[0] = float4(xScale, 0.0, 0.0, 0.0);
  14. projectionMatrix.columns[1] = float4(0.0, yScale, 0.0, 0.0);
  15. projectionMatrix.columns[2] = float4(0.0, 0.0, (near + far) / zRange, -1.0);
  16. projectionMatrix.columns[3] = float4(0.0, 0.0, 2.0 * near * far / zRange, 0.0);
  17. return projectionMatrix;
  18. }
  19. // 生成视图矩阵的函数
  20. float4x4 generateViewMatrix(float3 cameraPosition) {
  21. float3 eye = cameraPosition;
  22. float3 target = float3(0.0, 0.0, 0.0); // 假设目标在原点
  23. float3 up = float3(0.0, 1.0, 0.0); // 上方向是正Y轴
  24. float3 zAxis = normalize(eye - target);
  25. float3 xAxis = normalize(cross(up, zAxis));
  26. float3 yAxis = cross(zAxis, xAxis);
  27. float4x4 viewMatrix;
  28. viewMatrix.columns[0] = float4(xAxis.x, yAxis.x, zAxis.x, 0.0);
  29. viewMatrix.columns[1] = float4(xAxis.y, yAxis.y, zAxis.y, 0.0);
  30. viewMatrix.columns[2] = float4(xAxis.z, yAxis.z, zAxis.z, 0.0);
  31. viewMatrix.columns[3] = float4(-dot(xAxis, eye), -dot(yAxis, eye), -dot(zAxis, eye), 1.0);
  32. return viewMatrix;
  33. }
  34. [[ stitchable ]] float2
  35. accordion(float2 position, float amount, float fov_in) {
  36. float4 newPos = float4(position.x, position.y, 0.0, 1.0);
  37. float3 cameraPosition = float3(0.0, 0.0, amount);
  38. float4x4 viewMatrix = generateViewMatrix(cameraPosition);
  39. float nearPlane = 0.1;
  40. float farPlane = 100.0;
  41. float aspectRatio = 1.0;
  42. float fov = degreesToRadians(fov_in);
  43. float4x4 projectionMatrix = generateProjectionMatrix(fov,
  44. aspectRatio,
  45. nearPlane,
  46. farPlane);
  47. float4 final = projectionMatrix * viewMatrix * newPos;
  48. return final.xy;
  49. }

请注意,这只是代码的翻译部分,不包括问题或额外内容。

英文:

I have the following Metal Shader code, if I move the camera X/Y the view updates correctly, however If I move the z position of the camera, it's as if nothing happens, the view renders normally without zooming in/out...I feel like I'm missing something?

Am I creating the transforms and multiplying them correctly?

  1. #include &lt;metal_stdlib&gt;
  2. using namespace metal;
  3. float degreesToRadians(float degrees) {
  4. return degrees * (M_PI_F / 180.0);
  5. }
  6. // Function to generate a projection matrix
  7. float4x4 generateProjectionMatrix(float fov, float aspectRatio, float near, float far) {
  8. float yScale = 1.0 / tan(fov * 0.5);
  9. float xScale = yScale / aspectRatio;
  10. float zRange = near - far;
  11. float4x4 projectionMatrix;
  12. projectionMatrix.columns[0] = float4(xScale, 0.0, 0.0, 0.0);
  13. projectionMatrix.columns[1] = float4(0.0, yScale, 0.0, 0.0);
  14. projectionMatrix.columns[2] = float4(0.0, 0.0, (near + far) / zRange, -1.0);
  15. projectionMatrix.columns[3] = float4(0.0, 0.0, 2.0 * near * far / zRange, 0.0);
  16. return projectionMatrix;
  17. }
  18. float4x4 generateViewMatrix(float3 cameraPosition) {
  19. float3 eye = cameraPosition;
  20. float3 target = float3(0.0, 0.0, 0.0); // Assuming the target is at the origin
  21. float3 up = float3(0.0, 1.0, 0.0); // Up direction is positive Y-axis
  22. float3 zAxis = normalize(eye - target);
  23. float3 xAxis = normalize(cross(up, zAxis));
  24. float3 yAxis = cross(zAxis, xAxis);
  25. float4x4 viewMatrix;
  26. viewMatrix.columns[0] = float4(xAxis.x, yAxis.x, zAxis.x, 0.0);
  27. viewMatrix.columns[1] = float4(xAxis.y, yAxis.y, zAxis.y, 0.0);
  28. viewMatrix.columns[2] = float4(xAxis.z, yAxis.z, zAxis.z, 0.0);
  29. viewMatrix.columns[3] = float4(-dot(xAxis, eye), -dot(yAxis, eye), -dot(zAxis, eye), 1.0);
  30. return viewMatrix;
  31. }
  32. [[ stitchable ]] float2
  33. accordion(float2 position, float amount, float fov_in) {
  34. float4 newPos = float4(position.x, position.y, 0.0, 1.0);
  35. float3 cameraPosition = float3(0.0, 0.0, amount);
  36. float4x4 viewMatrix = generateViewMatrix(cameraPosition);
  37. float nearPlane = 0.1;
  38. float farPlane = 100.0;
  39. float aspectRatio = 1.0;
  40. float fov = degreesToRadians(fov_in);
  41. float4x4 projectionMatrix = generateProjectionMatrix(fov,
  42. aspectRatio,
  43. nearPlane,
  44. farPlane);
  45. float4 final = projectionMatrix * viewMatrix * newPos;
  46. return final.xy;
  47. }

答案1

得分: 0

需要启用深度并提供深度缓冲。

来自https://metalbyexample.com/modern-metal-2/的代码:

  1. mtkView.colorPixelFormat = .bgra8Unorm_srgb
  2. mtkView.depthStencilPixelFormat = .depth32Float
  3. pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
  4. pipelineDescriptor.depthAttachmentPixelFormat = view.depthStencilPixelFormat
  5. let depthStencilState: MTLDepthStencilState
  6. static func buildDepthStencilState(device: MTLDevice) -> MTLDepthStencilState {
  7. let depthStencilDescriptor = MTLDepthStencilDescriptor()
  8. depthStencilDescriptor.depthCompareFunction = .less
  9. depthStencilDescriptor.isDepthWriteEnabled = true
  10. return device.makeDepthStencilState(descriptor: depthStencilDescriptor)!
  11. }
  12. depthStencilState = Renderer.buildDepthStencilState(device: device)
  13. commandEncoder.setDepthStencilState(depthStencilState)
英文:

You need to enable depth and provide a depth buffer.

Code from https://metalbyexample.com/modern-metal-2/

  1. mtkView.colorPixelFormat = .bgra8Unorm_srgb
  2. mtkView.depthStencilPixelFormat = .depth32Float
  3. pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
  4. pipelineDescriptor.depthAttachmentPixelFormat = view.depthStencilPixelFormat
  5. let depthStencilState: MTLDepthStencilState
  6. static func buildDepthStencilState(device: MTLDevice) -&gt; MTLDepthStencilState {
  7. let depthStencilDescriptor = MTLDepthStencilDescriptor()
  8. depthStencilDescriptor.depthCompareFunction = .less
  9. depthStencilDescriptor.isDepthWriteEnabled = true
  10. return device.makeDepthStencilState(descriptor: depthStencilDescriptor)!
  11. }
  12. depthStencilState = Renderer.buildDepthStencilState(device: device)
  13. commandEncoder.setDepthStencilState(depthStencilState)

huangapple
  • 本文由 发表于 2023年6月12日 03:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定