我一直在学习Metal for iOS/OSX,我开始遵循Ray Wenderlich教程(https://www.raywenderlich.com/146414/metal-tutorial-swift-3-part-1-getting-started).本教程工作正常,但没有提及MTLVertexAttributeDescriptors.
现在我正在开发自己的应用程序,我遇到了奇怪的故障,我想知道我不使用MTLVertexAttributeDescriptors的事实是否与问题有关.
他们有什么不同?我已经能够制作各种具有不同顶点结构的着色器,我甚至都不知道这些东西.
我知道你用它们来描述在着色器中使用的顶点组件的布局.例如,着色器可能将此结构用于顶点,并且它将在下面函数的顶点描述符中设置.
typedef struct
{
float3 position [[attribute(T_VertexAttributePosition)]];
float2 texCoord [[attribute(T_VertexAttributeTexcoord)]];
} Vertex;
class func buildMetalVertexDescriptor() -> MTLVertexDescriptor {
let mtlVertexDescriptor = MTLVertexDescriptor()
mtlVertexDescriptor.attributes[T_VertexAttribute.position.rawValue].format = MTLVertexFormat.float3
mtlVertexDescriptor.attributes[T_VertexAttribute.position.rawValue].offset = 0
mtlVertexDescriptor.attributes[T_VertexAttribute.position.rawValue].bufferIndex = T_BufferIndex.meshPositions.rawValue
mtlVertexDescriptor.attributes[T_VertexAttribute.texcoord.rawValue].format = MTLVertexFormat.float2
mtlVertexDescriptor.attributes[T_VertexAttribute.texcoord.rawValue].offset = 0
mtlVertexDescriptor.attributes[T_VertexAttribute.texcoord.rawValue].bufferIndex = T_BufferIndex.meshGenerics.rawValue
mtlVertexDescriptor.layouts[T_BufferIndex.meshPositions.rawValue].stride = 12
mtlVertexDescriptor.layouts[T_BufferIndex.meshPositions.rawValue].stepRate = 1
mtlVertexDescriptor.layouts[T_BufferIndex.meshPositions.rawValue].stepFunction = MTLVertexStepFunction.perVertex
mtlVertexDescriptor.layouts[T_BufferIndex.meshGenerics.rawValue].stride = 8
mtlVertexDescriptor.layouts[T_BufferIndex.meshGenerics.rawValue].stepRate = 1
mtlVertexDescriptor.layouts[T_BufferIndex.meshGenerics.rawValue].stepFunction = MTLVertexStepFunction.perVertex
return mtlVertexDescriptor
}
Run Code Online (Sandbox Code Playgroud)
但即使没有MTLVertexDescriptor设置,着色器也可以访问顶点缓冲区和数组中顶点的位置/ texCoord组件.只需设置顶点缓冲区,着色器就可以访问所有组件.这个描述符有什么好处呢?
在配备独立显卡的 Mac 上,应使用托管缓冲区而不是共享缓冲区,但是使用[MTLBuffer:didModifyRange:]保持同步还有其他要求。
然而,在 Apple Silicon 上,如果我通过假装[MTLDevice hasUnifiedMemory]返回NO
并删除对 的调用来强制使用托管缓冲区didModifyRange:
,则渲染工作正常。
在 GPU 内存统一的 Apple Silicon 上测试托管缓冲区的最佳方法是什么,以便我可以确保我的代码可以在较旧的 Mac 上运行?
我只是想提前感谢任何可以提供帮助的人,我们目前完全陷入困境。
项目场景:-> 带有简单 2d 精灵的 2D 横向滚动游戏-> 相机是正交的并保持原位(相机不动)-> 我的角色(骆驼)也是静态的,不动。-> 精灵(平台)从右向左移动(由脚本完成的移动)-> 项目视频在这里上传(在 iphone5 上运行)。它清楚地显示了小的打嗝和抽搐。
问题:该游戏在 Android 设备和 Unity 编辑器上运行非常流畅(在 Unity 分析器上也没有尖峰)但是,当我在 iOS 设备(iphone5、iphon5s)上运行它时,我的精灵的运动是生涩的。就像它会平稳移动大约 1 秒钟,然后会发生一瞬间的小打嗝/抽搐。这会导致游戏的滞后和滞后运动,这非常烦人,因为我无法为 iOS 获得正确的质量。
我尝试过的事情:-> 我在各种线程和论坛上研究了许多可能的解决方案,但似乎没有任何问题对我有用:-(-> 我尝试了不同的脚本技术,例如使用 Lerp 函数进行移动,尝试将代码放入LateUpdate 和 FixedUpdate。-> 我也在项目质量设置中关闭了 vSync,但没有运气:-(-> 我还尝试了没有脚本的运动,通过制作精灵的动画从右向左移动。-> 我试过了也从最新的 Unity 5.1 制作项目,但仍然存在相同的延迟:-( -> 我尝试将精灵尺寸减小到非常低(低质量像素),但问题仍然存在。-> 我还尝试将相机更改为透视查看它是否减少了混蛋
我相信周围的许多其他人在制作此类 2D 游戏时一定遇到过这个问题。我希望有人一定已经克服了这个问题,并且可以帮助我解决这个问题。这个问题困扰我很久了...
我真的很感激任何帮助......统一中的任何新技术或在 Unity 中制作此类 2D 游戏的任何更好的优化逻辑。谢谢。
期待大家的投入。
供参考的 YouTube 视频链接 - https://www.youtube.com/watch?v=cnST1QzoDw4
我正在按照本教程将 Unity 项目嵌入到 iOS 项目中。
我在这个Precompile Prefix.pch
过程中遇到了错误
错误:无法生成进程(参数列表太长)
我的Prefix.pch
包含
#include "Preprocessor.h"
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
#include "UnityTrampolineConfigure.h"
#include "UnityInterface.h"
#ifndef __OBJC__
#if USE_IL2CPP_PCH
#include "il2cpp_precompiled_header.h"
#endif
#endif
#ifndef TARGET_IPHONE_SIMULATOR
#define TARGET_IPHONE_SIMULATOR 0
#endif
#define printf_console printf
Run Code Online (Sandbox Code Playgroud)
我尝试过的解决方案:
/Users/myproject
,但仍然无法正常工作。我没有太多的 C 知识,所以我不确定我应该详细说明我的项目的哪些部分,如果我需要显示更多细节,请发表评论。
对于我的 Unity 游戏项目的Firebase设置,我从一开始就遵循本教程:
Unity 中的 Firebase 入门 (2019) - Firecast
视频中有一段用于初始化Firebase SDK 的代码:
void Start()
{
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(continuationAction: task =>
{
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
});
}
Run Code Online (Sandbox Code Playgroud)
现在,当我在 Unity 编辑器中按下播放按钮时,我立即收到此错误对话框:
在不同的对话框上多次按下取消按钮后,我在控制台中收到这些错误。
我在这个过程中做错了什么,我无法理解!所以请指导我解决这个问题。
在WWDC20 中,苹果引入了PHPicker - UIImagePickerController 的现代替代品。
我想知道是否可以使用新的照片选择器检索 PHAsset?
这是我的代码:
private func presentPicker(filter: PHPickerFilter) {
var configuration = PHPickerConfiguration()
configuration.filter = filter
configuration.selectionLimit = 0
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
}
Run Code Online (Sandbox Code Playgroud) 我在自己的应用程序中遇到了一个有趣的 Metal 性能问题,我只需对此示例项目进行少量调整即可重现该问题。我的视图大小约为 1600x900,如下所示:
\n\n每帧有两次绘制调用,一次用于背景,一次用于线条。背景由4个顶点组成,线条大约有2000个顶点。当场景像上面一样绘制时,Xcode 的 GPU 帧捕获告诉我整个帧需要大约 4 毫秒(!)。一些观察结果:
\n这对我来说没有意义。为什么上述变化会对帧时间产生如此巨大的影响?这是 100 倍的差异。
\n我正在 2018 Mac mini(配备 Intel UHD Graphics 630 1536 MB)上运行代码,以防万一这很重要。
\n以下是对演示项目所做的更改:
\nMTLBuffer
在初始化期间创建两个sAAPLVertex quadVertices[] = { ... 4 vertices omitted ... };\nquadBuffer = [_device newBufferWithBytes:quadVertices length:4 * sizeof(AAPLVertex) MTLResourceStorageModeManaged];\n\nAAPLVertex dataVertices[] = { ... ~2000 vertices …
Run Code Online (Sandbox Code Playgroud) 我想知道如何SmoothDamp
统一工作。我正在尝试在统一之外重新创建该函数,但问题是我不知道它是如何工作的。
我到处都看到使用Camera.main
是一种不好的做法,我们不应该使用它。那么获取主相机对象的好方法是什么?
我想在屏幕上显示硬币数量,但我的 VScode 出现错误:
命名空间“UnityEngine”中不存在类型或命名空间名称“UI”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Main : MonoBehaviour {
public Player player;
public Text coinText;
public Image[] hearts;
public Sprite isLife, noneLife;
void Start() {
}
void Update() {
coinText.text = player.getCoins().ToString();
Lose();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在创建一个在场景中查找文本字段的模块。如果安装了 TextMeshPro,它将查找它,如果没有,则查找常规文本字段/文本网格
问题是:我怎样才能条件(伪代码)
if(textMeshProExists)
Look for A
else
Look for B
Run Code Online (Sandbox Code Playgroud) 如标题中所述,我正在尝试使用unity3d创建一个android动态壁纸。不幸的是,我找不到有关该操作的任何信息。
在金属着色器下,为什么这两行的工作原理不同:
float b = (color.rgb * float3(1,1.1,0.9)).x;
Run Code Online (Sandbox Code Playgroud)
和
float b = dot(color.rgb, float3(1,1.1,0.9));
Run Code Online (Sandbox Code Playgroud)