当前位置: 首页 > news >正文

自由学习记录(95)

Unity 在 Android Player Settings 里只允许你配置 GLES / Vulkan。这里的列表决定:打包到手机时用哪个 API

这是打包给安卓的API设置

  • OpenGLES2 (兼容低端机)

  • OpenGLES3 (主流)

  • Vulkan (高端机,性能好)

标题的则是Editor的使用的API

Unity Editor 本身运行在 Windows 桌面环境,所以 Editor 的渲染窗口(Scene / Game 视图)默认用 DirectX 11 来绘制。

标题栏看到的 <DX11> 只是 Editor 自己的渲染后端,不会影响你打包到 Android 的结果。

  • Unity 早期版本支持 Edit → Graphics Emulation 功能,例如模拟 OpenGL ES 2/3 等。(但新版本有时隐藏或废弃)Unity Documentation

  • Unity 提供 Rendering Debugger 工具(Window → Analysis → Rendering Debugger)可以可视化一些渲染过程,但它并不是切换 Graphics API 的工具。Unity Documentation+1

  • 最真实还是需要在 手机真机上运行 Build,来准确查看 GLES/Vulkan 下的效果。

用 NVIDIA 显卡完全可以不用 DirectX(DX),而是切换到 OpenGLCoreVulkan

怎么在 Unity 切换?

  1. 打开 Unity → Edit → Project Settings → Player

  2. PC, Mac & Linux Standalone 平台下(注意不是 Android)

  3. 展开 Other Settings → Rendering

  4. 取消勾选 Auto Graphics API for Windows

  5. 在下面的 Graphics APIs 列表里添加或删除:

    • 可以选 Direct3D11 / Direct3D12 / OpenGLCore / Vulkan

    • 把 OpenGLCore 或 Vulkan拖到最上面 → Unity Editor 启动时就会优先用它

启动 Unity 后,标题栏就会从 <DX11> 变成 <Vulkan><OpenGLCore>

  • OpenGLCore:稳定,但在 Windows 上性能一般,主要用来测试跨平台兼容性

  • Vulkan:更现代,Unity 支持度高,可以模拟 Android Vulkan 的效果,但在 Editor 上可能会遇到一些 bug。

  • DirectX:在 Windows 下最优化,几乎所有显卡驱动都会优先保证 DX 的表现。

Unity 官方文档明确指出,你可以在 Project Settings → Player → Other Settings → Rendering 中自行覆盖默认设置:

  • 取消勾选 “Auto Graphics API for Windows/Mac/Linux”,这样 Unity 就不会再自动选择了。

  • 在出现的 Graphics APIs 列表里,你可以手动添加或调整顺序,比如将 OpenGLCoreVulkan 上移至首位。

  • 一旦设置完成,Unity Editor 就会使用你首先指定的 API。Unity DocumentationUnity Support

社区经验分享

虽然能够切换 API,但也存在一些注意事项:

在 Windows 上运行 Vulkan 在性能上和 DX11 差不多,但是在 Unity 中仍被视为实验性 API
—— Reddit 用户讨论Unity Answers

也有用户反馈说:

强制让 Editor 使用 Vulkan 或 DX12 会导致崩溃,具体取决于 Unity 版本与配置
—— Unity 官方论坛Unity Answers

Select a rendering engine

We recommend using the Vulkan Graphics API to render Android XR apps. To select Vulkan as your Graphics API, follow these steps:

  1. In Unity, go to Edit > Project Settings > Player.
  2. Select the Android tab and navigate to Other Settings > Rendering.
  3. If Auto Graphics API is enabled, disable this setting to reveal the Graphics APIs section.
  4. In the Graphics APIs section, select the Add (+) button and select Vulkan from the dropdown

  1. Re-order the Graphics APIs using the handles (=) so that Vulkan is listed first.

  2. Optionally, select any other Graphics APIs and click the Remove (-) button to remove them.

什么是 Passthrough?

XR (Extended Reality) 里,passthrough = 把现实世界的相机画面“透传”到头显里显示

  • 普通的 VR 头显 → 默认显示的是一个完全虚拟的 3D 世界。

  • 开启 passthrough → 你能“透过”头显,看到外部真实环境(通过头显上的摄像头捕捉)。

  • 然后 Unity 可以把虚拟物体叠加在这个现实画面上。

👉 简单说:Passthrough = 把现实世界当作背景,虚拟物体叠在上面

为什么和 URP 有关?

Unity 在 Android XR(比如 Meta Quest、Pico 头显)上,如果你要用 passthrough 功能,需要通过 URP (Universal Render Pipeline) 去控制渲染:

  • Passthrough 本质上是把摄像头流作为“背景层”绘制。

  • URP 默认的渲染设置可能会有额外开销(比如后处理、透明度处理),这会降低性能。

  • 所以官方提示你:要优化 URP 设置(比如关闭多余的 post-processing、调整透明排序),才能让 passthrough 画面更流畅。

比如你在 Meta Quest 里:

  • 关掉 passthrough → 玩家进入一个纯 VR 场景,只能看到虚拟房间。

  • 开启 passthrough → 玩家戴着头显能看到真实房间(摄像头视频),同时虚拟的 UI 界面或 3D 模型漂浮在真实房间里。

这就是 混合现实 (MR) 的关键功能。

Version: 2018.4 Version: 2018.4 

The graphics hardware that ultimately renders a Scene
 is controlled by specialised programs called Shaders
. The capabilities of the hardware have improved over time, and the general set of features that were introduced with each phase is known as a Shader Model. Successive Shader Models have added support for longer programs, more powerful branching instructions and other features, and these have enabled improvements in the graphics of games.

后续的着色器模型增加了对更长程序、更强大的分支指令和其他功能的支持,这些改进使得游戏中的图形得到了提升。

https://docs.unity3d.com/2018.4/Documentation/Manual/GraphicsEmulation.html

The Unity Editor supports emulation of several sets of Shader Models and graphics API restrictions, for getting a quick overview of how the game might look like when running on a particular GPU or graphics API. Note that the in-editor emulation is very approximate, and it is always advisable to actually run the game build on the hardware you are targeting.Unity编辑器支持模拟多套着色器模型和图形API限制,以便快速了解游戏在特定GPU或图形API上运行时可能的外观。请注意,编辑器内的模拟非常近似,始终建议在实际目标硬件上运行游戏构建版本。

To choose the graphics emulation level, go to the Edit > Graphics Emulation menu. Note that the available options change depending on the platform you are currently targeting in the Build Settings. You can restore the full capabilities of your hardware by choosing No Emulation. If your development computer doesn’t support a particular Shader Model then the menu entry will be disabled.要选择图形仿真级别,请转到“编辑”>“图形仿真”菜单。请注意,可用的选项会根据您在“构建设置”中当前针对的平台而变化。通过选择“无仿真”,您可以恢复硬件的全部功能。如果您的开发计算机不支持特定的着色器模型,则菜单项将被禁用。

Shader Model 4 (Standalone & Universal Windows Platform)着色器模型 4(独立版和通用Windows平台)
  • Emulates DirectX 10 feature set (PC GPUs made during 2007–2009).模拟DirectX 10功能集(2007-2009年间制造的PC GPU)。
  • Turns off support for compute Shaders and related features (compute buffers, random-write Textures), sparse Textures, and tessellation Shaders.关闭对计算着色器和相关功能(计算缓冲区、随机写入纹理)、稀疏纹理和细分着色器的支持。

Shader Model 3 (Standalone platform)着色器模型3(独立平台)
  • Emulates DirectX 9 SM3.0 feature set (PC GPUs made during 2004–2006).模拟DirectX 9 SM3.0功能集(2004-2006年间制造的PC GPU)。
  • In addition to features turned off by Shader Model 4 emulation, this also turns off support for draw call instancing, Texture Arrays, and geometry Shaders. It enforces a maximum of 4 simultaneous render targets, and a maximum of 16 Textures used in a single Shader.除了由Shader Model 4模拟关闭的功能外,这还将关闭对绘制调用实例化、纹理数组和几何着色器的支持。它强制最大同时渲染目标数为4个,单个着色器中使用的纹理最大数为16个

意思是说我现在是用的windows 的dx11,然后我在场景的某个物体的shader上,如果加上了这个#pragma target 4.0 这样的宏,这个shader文件就会固定的限制掉那些功能,无论是什么平台上都会被限制?

理解是对的——#pragma target 4.0会全局生效!

gamma空间我记得是非线性的2.2还是0.45来着?

两者都对,但语境不同。

  • 解码到线性(sRGB→Linear):幂 2.4(实际效果接近 2.2)。

  • 编码到 sRGB(Linear→sRGB):幂 1/2.4≈0.4167,常被近似为 1/2.2≈0.4545(≈0.45)。sRGB 实际是分段函数,不是纯幂。 Wikipediaentropymine.com

Project Settings → Player → Other Settings 里选择 Color Space:Gamma 时:

  • Unity 会 直接以 gamma(sRGB)空间进行渲染,即纹理、颜色计算、Shader 输入都在 gamma 空间中处理。Unity 不会在输出前进行了线性–gamma 的转换。

Shader Compilation Target Levels

为了使用更现代的GPI功能,您必须使用更高的着色器编译目标。

使用更高的着色器编译目标可能会阻止着色器在较旧的GPU或平台上运行。

Note: Using higher shader compilation targets may prevent the shader from working on older GPUs or platforms.

通过使用#pragma targetname指令或更具体的#pragma requirefeature ...指令来指定编译目标

#pragma target 3.5
#pragma require integers 2darray instancing

默认情况下,Unity将着色器编译成几乎最低支持的目标(“2.5”),介于DirectX着色器模型2.0和3.0之间。

By default, Unity compiles shaders into almost the lowest supported target (“2.5”); in between DirectX shader models 2.0 and 3.0. Some other compilation directives make the shader automatically be compiled into a higher target:

一些其他的编译指令会使着色器自动编译成更高的目标:

  • Using a geometry shader (#pragma geometry) sets the compilation target to 4.0.使用几何着色器(#pragma geometry)将编译目标设置为4.0。
  • Using tessellation shaders (#pragma hull or #pragma domain) sets the compilation target to 4.6.使用曲面细分着色器(#pragma hull 或 #pragma domain)将编译目标设置为4.6。

Any shader not explicitly setting a function entry point through #pragma for geometry, hull or domain shaders will downgrade internal shader capability requirements. This allows non-DX11 targets with broader run-time and feature differences to be more compatible with the existing shader content.

任何未通过#pragma显式设置函数入口点的着色器,对于几何、 hull或域着色器,将降低内部着色器能力要求。这允许非DX11目标具有更广泛的运行时和功能差异,以更好地与现有着色器内容兼容。

 Metal doesn’t support geometry shaders

Using #pragma target 5.0 is still valid, as long as you don’t use geometry shaders.

Unity支持在Metal图形上使用细分着色器,但Metal不支持几何着色器。只要不使用几何着色器,使用#pragma target 5.0仍然有效

Here is the list of shader models supported, with roughly increasing set of capabilities (and in some cases higher platform/GPU requirements):

#pragma target 2.0
  • Works on all platforms supported by Unity. DX9 shader model 2.0.在Unity支持的所有平台上都有效。DX9着色器模型2.0。

Limited amount of arithmetic & texture instructions; 8 interpolators; no vertex texture sampling; no derivatives in fragment shaders; no explicit LOD texture sampling.

有限的算术和纹理指令;8个插值器;没有顶点纹理采样;片段着色器中没有导数;“每像素”部分的着色器代码,在屏幕上每个物体占据的像素上执行。片段着色器部分通常用于计算并输出每个像素的颜色。

细节级别(LOD)技术是一种优化,当GameObject与相机的距离增加时,它减少了Unity需要渲染的三角形数量。每个LOD级别要么有一个带有Mesh Renderer组件的网格(网格LOD级别),要么有一个带有Billboard Renderer组件的Billboard资产(Billboard LOD级别)。通常,单个GameObject有三个或四个网格LOD级别和一个可选的Billboard LOD级别,以用逐渐减少的几何细节表示相同的GameObject。

#pragma target 2.5(默认)
  • Almost the same as 3.0 target (see below), except still only has 8 interpolators, and does not have explicit LOD texture sampling.几乎与3.0目标相同(见下文),但仍然只有8个插值器,并且没有显式的LOD纹理采样。
  • Compiles into DX11 feature level 9.3 on Windows Phone.在Windows Phone上编译为DX11功能级别9.3。
#pragma target 3.0
  • DX9 shader model 3.0: derivative instructions, texture LOD sampling, 10 interpolators, more math/texture instructions allowed.DX9着色器模型3.0:导数指令、纹理LOD采样、10个插值器,允许更多的数学/纹理指令。
  • Not supported on DX11 feature level 9.x GPUs (e.g. most Windows Phone devices).不支持DX11功能级别9.x GPU(例如,大多数Windows Phone设备)。
  • Might not be fully supported by some OpenGL ES 2.0 devices, depending on driver extensions present and features used.某些OpenGL ES 2.0设备可能不完全支持,具体取决于存在的驱动程序扩展和使用的功能。
#pragma target 3.5(或es3.0)
  • OpenGL ES 3.0 capabilities (DX10 SM4.0 on D3D platforms, just without geometry shaders).OpenGL ES 3.0功能(在D3D平台上为DX10 SM4.0,只是没有几何着色器)。
  • Not supported on DX11 9.x (WinPhone), OpenGL ES 2.0.不支持DX11 9.x(WinPhone)、OpenGL ES 2.0。
  • Supported on DX11+, OpenGL 3.2+, OpenGL ES 3+, Metal, Vulkan, PS4/XB1 consoles.支持DX11+、OpenGL 3.2+、OpenGL ES 3+、Metal、Vulkan、PS4/XB1游戏主机。
  • Native integer operations in shaders, texture arrays and so on.着色器中的原生整数运算、纹理数组等。

#pragma target 4.0
  • DX11 shader model 4.0.DX11着色器模型4.0。
  • Not supported on DX11 9.x (WinPhone), OpenGL ES 2.0/3.0/3.1, Metal.不支持在DX11 9.x(WinPhone)、OpenGL ES 2.0/3.0/3.1、Metal上运行。
  • Supported on DX11+, OpenGL 3.2+, OpenGL ES 3.1+AEP, Vulkan, PS4/XB1 consoles.支持在DX11+、OpenGL 3.2+、OpenGL ES 3.1+AEP、Vulkan、PS4/XB1游戏机上运行。
  • Has geometry shaders and everything that es3.0 target has.具有几何着色器和es3.0目标所拥有的所有功能。

#pragma target 4.5(或es3.1)
  • OpenGL ES 3.1 capabilities (DX11 SM5.0 on D3D platforms, just without tessellation shaders).OpenGL ES 3.1功能(在D3D平台上相当于DX11 SM5.0,只是没有曲面细分着色器)。
  • Not supported on DX11 before SM5.0, OpenGL before 4.3 (i.e. Mac), OpenGL ES 2.0/3.0.不支持DX11(在SM5.0之前),OpenGL(在4.3之前,即Mac),OpenGL ES 2.0/3.0。
  • Supported on DX11+ SM5.0, OpenGL 4.3+, OpenGL ES 3.1, Metal, Vulkan, PS4/XB1 consoles.支持DX11+ SM5.0,OpenGL 4.3+,OpenGL ES 3.1,Metal,Vulkan,PS4/XB1游戏机。
  • Has compute shaders, random access texture writes, atomics and so on. No geometry or tessellation shaders.具有计算着色器、随机访问纹理写入、原子操作等功能。没有几何或曲面细分着色器。
#pragma target 4.6(或gl4.1)
  • OpenGL 4.1 capabilities (DX11 SM5.0 on D3D platforms, just without compute shaders). This is basically the highest OpenGL level supported by Macs.OpenGL 4.1功能(在D3D平台上为DX11 SM5.0,只是没有计算着色器)。这基本上是Mac支持的最高OpenGL级别。
  • Not supported on DX11 before SM5.0, OpenGL before 4.1, OpenGL ES 2.0/3.0/3.1, Metal.不支持DX11(在SM5.0之前)、OpenGL(在4.1之前)、OpenGL ES 2.0/3.0/3.1、Metal。
  • Supported on DX11+ SM5.0, OpenGL 4.1+, OpenGL ES 3.1+AEP, Vulkan, Metal (without geometry), PS4/XB1 consoles.支持DX11+ SM5.0、OpenGL 4.1+、OpenGL ES 3.1+AEP、Vulkan、Metal(不含几何体)、PS4/XB1游戏机。
#pragma target 5.0
  • DX11 shader model 5.0.DX11着色器模型5.0。
  • Not supported on DX11 before SM5.0, OpenGL before 4.3 (i.e. Mac), OpenGL ES 2.0/3.0/3.1, Metal.不支持DX11 SM5.0之前、OpenGL 4.3之前(即Mac)、OpenGL ES 2.0/3.0/3.1、Metal。
  • Supported on DX11+ SM5.0, OpenGL 4.3+, OpenGL ES 3.1+AEP, Vulkan, Metal (without geometry), PS4/XB1 consoles.支持DX11+ SM5.0、OpenGL 4.3+、OpenGL ES 3.1+AEP、Vulkan、Metal(不含几何体)、PS4/XB1游戏机。

Note that all OpenGL-like platforms (including mobile) are treated as “capable of shader model 3.0”. WP8/WinRT platforms (DX11 feature level 9.x) are treated as only capable of shader model 2.5.请注意,所有类似OpenGL的平台(包括移动平台)都被视为“支持着色器模型3.0”。WP8/WinRT平台(DX11功能级别9.x)仅被视为支持着色器模型2.5。

List of supported feature names for the #pragma require directive:支持的功能名称列表,用于#pragma require指令:

  • interpolators10: At least 10 vertex-to-fragment interpolators (“varyings”) are available.interpolators10: 至少有10个顶点到片段插值器(“可变”)可用。
  • interpolators15: At least 15 vertex-to-fragment interpolators (“varyings”) are available.interpolators15: 至少有15个顶点到片段插值器(“可变”)可用。
  • interpolators32: At least 32 vertex-to-fragment interpolators (“varyings”) are available.interpolators32: 至少有32个顶点到片段插值器(“可变参数”)可用。
  • mrt4: Multiple Render Targets, at least 4.mrt4: 多重渲染目标,至少4个。
  • mrt8: Multiple Render Targets, at least 8.mrt8: 多重渲染目标,至少8个。
  • derivativesPixel
     shader derivative instructions (ddx/ddy).

derivatives(像素微分 / 派生),在 Shader 里主要指 HLSL 的 ddx / ddy / fwidth 这类指令。它们并不是数学意义上的解析导数,而是 GPU 在像素着色阶段,用相邻像素的值来近似计算的有限差分

  • ddx(expr):计算 exprx 屏幕方向 上的变化率。

  • ddy(expr):计算 expry 屏幕方向 上的变化率。

  • fwidth(expr):等价于 abs(ddx(expr)) + abs(ddy(expr)),常用来估计某个值在屏幕空间上的变化幅度。

它们是 屏幕空间的像素差分,不是模型空间/世界空间的数学导数。

为什么 GPU 能这么干

在 GPU 的像素着色器(fragment shader)阶段,像素是 以 2x2 的 quad 为基本单元并行执行的。
所以 GPU 天然可以知道同一 quad 内 4 个像素的某个表达式的结果,然后相减,就得到了近似的偏导数:

  • ddx ≈ (右像素值 – 左像素值)

  • ddy ≈ (下像素值 – 上像素值)

这种做法非常高效,因为 GPU 本来就是这样批处理像素的。

常见用途

  1. 自动 mipmap 选择

    • 采样贴图时,GPU 用 ddx/ddy 来估算 UV 的缩放变化,从而决定取哪一级 mipmap(防止远处采样过锐或者摩尔纹)。

    • Unity 的 tex2D / Sample 等函数默认就是用 derivatives 来决定 mipmap level。

  2. 抗锯齿 / 平滑过渡

    • 例如在实现 procedural shapes(圆形、方块、mask)时,可以用 fwidth 来根据像素变化率生成边缘的平滑过渡:

float edge = smoothstep(0.5 - fwidth(val), 0.5 + fwidth(val), val);

  • 屏幕空间效果

    • 计算法线差分(Sobel、Normal Edge Detection)

    • 屏幕 UV 的梯度,用于模糊、TAA、DOF 等。

  • 调试 / 可视化

    • 可以直接输出 ddx/ddy 的值来观察像素梯度分布。

限制与注意点

  • 只能在 fragment shader 里用,不能在 vertex / compute shader 里用,因为后者没有 quad 并行的概念。

  • 动态分支(if/loop) 里,如果一个 quad 内像素走了不同分支,derivative 结果可能 undefined(称为 non-uniform flow 问题)。

  • 对于常量或不依赖于像素位置的表达式,ddx/ddy 会返回零。

float val = i.uv.x * 10;    // 横向变化很快
float dx = ddx(val);        // 大值
float dy = ddy(val);        // 小值(几乎为 0)
float fw = fwidth(val);     // 横向梯度大小

广泛的#pragma target指令是上述要求的简写,它们对应于:

  • 2.5: derivatives2.5:导数
  • 3.0: 2.5 + interpolators10 + samplelod + fragcoord3.0: 2.5 + 插值器10 + 样本LOD + 片段坐标
  • 3.5: 3.0 + interpolators15 + mrt4 + integers + 2darray + instancing3.5: 3.0 + 插值器15 + MRT4 + 整数 + 2D数组 + 实例化
  • 4.0: 3.5 + geometry4.0: 3.5 + 几何
  • 5.0: 4.0 + compute + randomwrite + tesshw + tessellation5.0: 4.0 + compute + randomwrite + tesshw + tessellation
  • 4.5: 3.5 + compute + randomwrite4.5: 3.5 + compute + randomwrite
  • 4.6: 4.0 + cubearray + tesshw + tessellation4.6: 4.0 + cubearray + tesshw + tessellation

神图--prema

Note that in Direct3D terms shader model 4.0 also implies “mrt8”; and shader model 5.0 implies “interpolators32” and “cubearray”. However, these are not guaranteed to be available on many mobile platforms. So for backwards compatibility with existing shaders, writing #pragma target 4.0 does not automatically require 8 MRTs support; and writing #pragma target 5.0 does not require 32 interpolators nor cubemap arrays.请注意,在Direct3D术语中,着色器模型4.0也意味着“mrt8”;而着色器模型5.0意味着“interpolators32”和“cubearray”。然而,这些在许多移动平台上并不保证可用。因此,为了与现有着色器保持向后兼容性,编写#pragma target 4.0并不自动要求支持8个MRT;编写#pragma target 5.0也不需要32个插值器或立方体贴图数组。

https://docs.unity3d.com/2020.1/Documentation/Manual/SL-ShaderCompileTargets.html

the general set of features that were introduced with each phase is known as a Shader Model.

直接打包是最佳的手段了,但游戏一大解决方法必须要换,不然太低效了

https://docs.unity3d.com/2018.4/Documentation/Manual/GraphicsEmulation.html

Specifying GPU features or a shader model指定GPU功能或着色器模型

To specify required features, use the #pragma require directive, followed by a list of space-delimited values. For example:要指定所需功能,请使用#pragma require指令,后跟一个空格分隔的值列表。例如:

#pragma require integers mrt8

您还可以使用后跟空格分隔的着色器关键字的#pragma target指令。这意味着该要求仅适用于在启用任何给定关键字时使用的变体。

For information on shader model support for console platforms, see the platform-specific documentation.

  • In the DirectX definitions, shader model 4.0 includes mrt8, and shader model 5.0 includes interpolators32 and cubearray. Unity does not include these, for broader compatibility. To require these features, use an explicit #pragma require directive.在DirectX定义中,着色器模型4.0包括mrt8,着色器模型5.0包括interpolators32和cubearray。Unity不包括这些,以实现更广泛的兼容性。要要求这些功能,请使用显式的#pragma require指令。
  • If you use a target that requires geometry but your shader does not define a geometry stage, Unity removes geometry from the list of requirements at compile time.如果你使用的目标需要几何形状,但你的着色器没有定义几何阶段,Unity将在编译时从要求列表中删除几何形状。
  • If you use a target that requires tessellation but your shader does not define a hull or domain stage, Unity removes tessellation from the list of requirements at compile time.如果你使用了一个需要细分但你的着色器没有定义 hull 或 domain 阶段的目标,Unity 会在编译时从需求列表中移除细分。

Unity - Manual: Targeting shader models and GPU features in HLSL

Minimal Android API level最小Android API级别

Set your project to a minimal API level of 24, which is required by the OpenXR Loader. Otherwise, your builds may fail.将您的项目设置为最小API级别24,这是OpenXR加载器所要求的。否则,您的构建可能会失败。

Complete these steps to set the minimal Android API level.按照以下步骤设置最小Android API级别。

  1. Go to Edit > Project Settings > Player.转到“编辑”>“项目设置”>“播放器”。
  2. Select the Android tab and open Other Settings.选择Android标签并打开其他设置。
  3. In the Identification section, for Minimal API level, select 24 or higher.在识别部分,对于最小API级别,选择24或更高。

Application entry point

  1. Go to Edit > Project Settings > Player.转到编辑 > 项目设置 > 播放器。
  2. Select the Android tab and open Other Settings.选择Android选项卡并打开其他设置。
  3. In the Configuration section, make sure Application Entry Point has GameActivity checked and Activity unchecked.在配置部分,确保应用程序入口点已选中GameActivity并取消选中Activity。

Sparse Textures”(稀疏纹理)是 GPU 的一种高级特性,也常叫 Tiled Resources / Partially Resident Textures (PRT)。它和普通纹理的最大区别是:纹理数据不是一次性全放进显存,而是以小块(tile/page)为单位,按需加载到显存中

原理

  • 一个超大纹理(例如 64k × 64k)在内存中可以被逻辑上完整声明。

  • GPU 显存只需要保存其中“用得到的部分 tile”。

  • 通过 API(如 DirectX 11.2+ Tiled Resources、OpenGL ARB_sparse_texture、Vulkan Sparse Image),你可以 绑定/解绑具体的 tile 到物理显存

  • 如果 shader 采样到一个还没映射的 tile,返回值通常是 0 或者某种默认值。

这就像虚拟内存里的“页表”,只不过对象换成了纹理。

实际应用例子

  • 《微软飞行模拟》:用 Sparse Texture 技术处理全球卫星图。

  • 大型 open-world 游戏:远景地形只加载低分辨率 tile,靠近时再替换成高分辨率。

  • 医学 / 科研可视化:显微镜/地球数据,分块加载。

  • Google Play 要求:在某些 API Level 和新设备上,Google 推荐(甚至强制)使用 GameActivity

  • 性能优势GameActivity 用更少的 Java 层开销,更贴近底层 Native。

  • 兼容特性:支持 Play Games Services、Performance Tuner、输入事件处理优化等。

  • 错误避免:如果入口点没配置对,可能会出现安装后启动黑屏、崩溃或无法响应输入的问题。

https://developer.android.com/develop/xr/unity/setup

就这些setup,还是比较少了

Linear or gamma workflow

While gamma color space is the historically standard format, linear color space rendering gives more precise results.

When calculating lighting on older hardware restricted to 8 bits per channel for the framebuffer format, using a gamma curve provides more precision in the human-perceivable range. More bits are used in the range where the human eye is the most sensitive.当在仅限于每通道8位帧缓冲格式的老式硬件上计算光照时,使用gamma曲线可以在人类可感知的范围内提供更高的精度。在人类眼睛最敏感的范围内使用了更多的位。

The reason both gamma and linear color spaces exist is because lighting calculations should be done in linear space in order to be mathematically correct, but the result should be presented in gamma space to look correct to our eyes.gamma和线性色彩空间之所以并存,是因为光照计算应该在线性空间中进行,以确保数学上的正确性,但结果应该以gamma空间呈现,以便我们的眼睛看起来是正确的。

Even though monitors today are digital, they still take a gamma-encoded signal as input. Image files and video files are explicitly encoded to be in gamma space (meaning they carry gamma-encoded values, not linear intensities). This is the standard; everything is in gamma space.尽管现在的显示器是数字的,但它们仍然以伽玛编码的信号作为输入。图像文件和视频文件被明确编码为伽玛空间(这意味着它们携带伽玛编码的值,而不是线性强度)。这是标准;一切都是伽玛空间。

The accepted standard for gamma space is called sRGB (see Wikipedia). This standard defines a mapping to linear space that allows our eyes to make the most of the 8 bits per channel of precision.伽玛空间的公认标准被称为sRGB(见维基百科)。这个标准定义了一个映射到线性空间的方法,使我们能够最大限度地利用每个通道8位的精度。

Linear rendering refers to the process of rendering a Scene
 with all inputs linear - that is to say, not gamma corrected for viewing with human eyes or for output to a display.

The accepted standard for gamma space is called sRGB (see Wikipedia). This standard defines a mapping to linear space that allows our eyes to make the most of the 8 bits per channel of precision.伽玛空间的公认标准被称为sRGB(见维基百科)。这个标准定义了一个映射到线性空间的方法,使我们能够最大限度地利用每个通道8位的精度

8 位颜色通道的含义

  • 在常见的图像/渲染中,每个颜色通道(R、G、B)通常只有 8 bit 存储空间

  • 这意味着每个通道最多只能表达 256 个离散等级(0–255)。

  • 如果直接用 线性空间 分配这 256 个等级:

    • 低亮度(接近黑色)部分的差别会非常小,人眼难以区分。

    • 而高亮度(接近白色)部分会浪费很多等级,因为人眼本来对亮度大的区域分辨不敏感。

👉 结果:存储精度没有对准人眼的感知特性,浪费了有限的 8bit。

  • 人眼对亮度的感知是 近似对数关系,对暗部非常敏感,对亮部不敏感。

  • 举个例子:

    • 黑色到深灰(0.01 → 0.02 的亮度差),人眼能明显区分。

    • 亮灰到白色(0.90 → 0.91 的亮度差),几乎分不出来。

所以如果存储方式是 线性分布,就会导致暗部不够精细、亮部过于浪费。

  • sRGB 通过 非线性编码 (gamma correction),把有限的 8bit 等级分布得更符合人眼感知规律。

  • 在相同的 256 阶梯下,暗部精细、亮部节省 → 人眼主观上感觉精度更高。

数据没有发生变化,数据的来源还是一样的,把值映射到gamma的srgb空间下再显示,

重点是,,原本的数据是不变的,,一张照片,原本上面的各个像素的值都是固定的一个数值,然后想要丢到电脑里面显示这张照片,是为了让整体的照片更加有变化的精细感,所以利用人眼的观察习惯

把这张照片拉进自己的gamma空间里,对数值进行处理之后,再用新值,丢给最终的像素三色管显示,

相比那张不p图的像素,这张被gamma空间p过的图会看上去更加丰富

这是gamma空间的意义

Even though monitors today are digital, they still take a gamma-encoded signal as input. Image files and video files are explicitly encoded to be in gamma space (meaning they carry gamma-encoded values, not linear intensities). This is the standard; everything is in gamma space.

尽管现在的显示器是数字的,但它们仍然以伽玛编码的信号作为输入。图像文件和视频文件被明确编码为伽玛空间(这意味着它们携带伽玛编码的值,而不是线性强度)。这是标准;一切都是伽玛空间。

意思是一张图放到unity里,不作任何设置的情况下,unity会把里面的像素值当做已经被gamma空间处理过的值是吗

yesyes

Unity 默认处理方式 —— sRGB 纹理假设

Unity 文档中说明:

“Textures tend to be saved in gamma color space... To specify a gamma or linear workflow... change the Color Space to Linear or Gamma... note that you can choose to bypass sRGB sampling in Color Space: Gamma mode by unchecking the sRGB (Color Texture) checkbox in the Inspector window for the Texture.”
Unity Documentation

这意味着:

  • 当项目设置里选择 Gamma 色彩空间时(这是 Unity 的默认设置),Unity 会认为导入的纹理是 sRGB(伽玛空间)的,也就是说这些纹理带有校正过的亮度值。

  • Unity 会自动将它们转为线性空间进行渲染,再在输出时(如屏幕显示)进行反向 gamma 校正,从而保证视觉一致。

The fall-off from distance and normal-based lighting differs in two ways:

 3D Head Scan image below demonstrates different light intensities on a human head model under linear lighting and gamma lighting.

Linear and gamma blending

When you use linear space rendering, blending occurs in linear color space: This is mathematically correct and gives precise results.当您使用线性空间渲染时,混合发生在线性颜色空间:这在数学上是正确的,并且给出精确的结果。

The image below demonstrates the different types of blending:下方的图像展示了不同类型的混合:

Top: Blending in linear color space produces expected blending results顶部:在线性色彩空间中混合产生预期的混合效果
Bottom: Blending in gamma color space results in over-saturated and overly-bright blends底部:在伽玛色彩空间中混合导致过度饱和和过于明亮的混合

https://docs.unity3d.com/6000.2/Documentation/Manual/differences-linear-gamma-color-space.html

While a linear workflow ensures more precise rendering, sometimes you may want a gamma workflow (for example, on some platforms the hardware only supports the gamma format).虽然线性工作流程可以确保更精确的渲染,但有时您可能想要伽玛工作流程(例如,在某些平台上,硬件仅支持伽玛格式)。

To do this, set Color Space to Gamma in the Player settings (menu: Edit > Project Settings, then select the Player category). Unity uses the gamma color space for color calculations, and keeps imported textures in the gamma color space. Unity also makes sure shaders keep textures in gamma color space, calculate in gamma color space, and write to a framebuffer that doesn’t reapply gamma correction.

为此,请在播放器设置中将色彩空间设置为伽玛(菜单:编辑 > 项目设置,然后选择播放器类别)。Unity使用伽玛色彩空间进行颜色计算,并保持导入的纹理在伽玛色彩空间中。Unity还确保着色器将纹理保持在伽玛色彩空间,在伽玛色彩空间中进行计算,并将数据写入不重新应用伽玛校正的帧缓冲区。

Texture Import Settings might show textures as being in linear format, because this avoids shaders recognising the textures as being in gamma color space and automatically removing the gamma correction.纹理导入设置可能会显示纹理为线性格式,因为这可以避免着色器将纹理识别为伽玛色彩空间并自动移除伽玛校正。

Note that you can choose to bypass sRGB sampling in Color Space: Gamma mode by unchecking the sRGB (Color Texture) checkbox in the Inspector window for the Texture.

Selecting Color Space: Linear assumes your Textures are in gamma color space. Unity uses your GPU’s sRGB sampler by default to crossover from gamma to linear color space. If your Textures are authored in linear color space, you need to bypass the sRGB sampling. See documentation on Working with linear Textures for more information.选择色彩空间:线性假定您的纹理处于伽玛色彩空间。Unity默认使用您的GPU的sRGB采样器从伽玛色彩空间转换到线性色彩空间。如果您的纹理是在线性色彩空间中编写的,您需要绕过sRGB采样。

For colors, this conversion is applied implicitly, because the Unity Editor already converts the values to floating point before passing them to the GPU as constants. When sampling Textures, the GPU automatically removes the gamma correction, converting the result to linear space.对于颜色,此转换是隐式应用的,因为Unity编辑器在将值传递给GPU作为常量之前,已经将它们转换为浮点数。当采样纹理时,GPU会自动去除伽玛校正,将结果转换为线性空间。

These inputs are then passed to the Shader, with lighting calculations taking place in linear space as they normally do. When writing the resulting value to a framebuffer, it is either gamma-corrected straight away or left in linear space for later gamma correction - this depends on the current rendering configuration. For example, in high dynamic range (HDR), rendering results are left in linear space and gamma corrected later.这些输入随后传递给着色器,光照计算在线性空间中进行,就像通常那样。当将结果写入帧缓冲区时,要么立即进行伽玛校正,要么保留在线性空间以供后续伽玛校正——这取决于当前的渲染配置。例如,在高动态范围(HDR)中,渲染结果保留在线性空间,并在稍后进行伽玛校正。

Unity - Manual: Linear or gamma workflow

5.5的版本https://docs.unity3d.com/550/Documentation/Manual/LinearRendering-LinearOrGammaWorkflow.html

这个也是一直没有变过,说明地基还是很牢靠的了

http://www.xdnf.cn/news/1461223.html

相关文章:

  • Bug 排查日记:从问题浮现到解决的技术之旅
  • C++ opencv RTSP小工具 RTSP流播放、每一帧保存
  • 爆改YOLOv8 | 即插即用的AKConv让目标检测既轻量又提点
  • 光伏运维迎来云端革命!AcrelCloud-1200如何破解分布式光伏四大痛点?
  • Elasticsearch面试精讲 Day 9:复合查询与过滤器优化
  • PPT中如何将设置的文本框边距设为默认
  • 【Javascript】Capacitor 文件存储在 Windows 上的位置
  • Git 同步最新代码:用 stash -> pull -> pop 安全同步更新
  • Docker 容器核心指令与数据库容器化实践
  • 安全运维-云计算系统安全
  • 【1】策略模式 + 模板方法模式的联合应用
  • 具身智能的工程落地:视频-控制闭环的实践路径
  • 手写React状态hook
  • AI测试:自动化测试框架、智能缺陷检测、A/B测试优化
  • 分片上传-
  • Boost搜索引擎 网络库与前端(4)
  • 力扣hot100:搜索二维矩阵 II(常见误区与高效解法详解)(240)
  • OpenBMC之编译加速篇
  • 三、神经网络
  • VisionPro联合编程相机拍照 九点标定实战
  • pinia状态管理的作用和意义
  • SSD固态硬盘加速优化-明显提高固态硬盘的效率并保持峰值性能-供大家学习研究参考
  • Ubuntu 22.04 网络服务安装配置
  • 硬件开发1-51单片机3-串口
  • 三阶Bezier曲线曲率极值及对应的u的计算方法
  • LeetCode 994 腐烂的橘子
  • 【C语言】深入理解指针(4)
  • 【LeetCode热题100道笔记】旋转图像
  • pycharm解释器使用anaconda建立的虚拟环境里面的python,无需系统里面安装python。
  • MySQL复制技术的发展历程