浏览代码
Merge pull request #1178 from Unity-Technologies/decals/texture_atlas
Merge pull request #1178 from Unity-Technologies/decals/texture_atlas
Decals/texture atlas/main
GitHub
7 年前
当前提交
bc2cc021
共有 18 个文件被更改,包括 495 次插入 和 103 次删除
-
33ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Common.hlsl
-
170ScriptableRenderPipeline/HDRenderPipeline/HDRP/Decal/DecalSystem.cs
-
3ScriptableRenderPipeline/HDRenderPipeline/HDRP/Decal/GlobalDecalSettings.cs
-
3ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/RenderLoopSettings/GlobalDecalSettingsUI.cs
-
6ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/RenderLoopSettings/SerializedGlobalDecalSettings.cs
-
7ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
-
2ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
-
9ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDUtils.cs
-
2ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Decal/DBufferManager.cs
-
3ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Decal/Decal.cs
-
3ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Decal/Decal.cs.hlsl
-
3ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Decal/Decal.hlsl
-
8ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Decal/DecalData.hlsl
-
103ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Decal/DecalUtilities.hlsl
-
33ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/Blit.shader
-
4ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderPass/ShaderPassDBuffer.hlsl
-
195ScriptableRenderPipeline/HDRenderPipeline/HDRP/Texture2DAtlas.cs
-
11ScriptableRenderPipeline/HDRenderPipeline/HDRP/Texture2DAtlas.cs.meta
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using UnityEngine.Rendering; |
|||
using UnityEngine.Experimental.Rendering.HDPipeline; |
|||
|
|||
|
|||
#if UNITY_EDITOR
|
|||
using UnityEditor; |
|||
#endif
|
|||
|
|||
namespace UnityEngine.Experimental.Rendering |
|||
{ |
|||
public class AtlasAllocator |
|||
{ |
|||
private class AtlasNode |
|||
{ |
|||
public AtlasNode m_RightChild = null; |
|||
public AtlasNode m_BottomChild = null; |
|||
public Vector4 m_Rect = new Vector4(0,0,0,0); // x,y is width and height (scale) z,w offset into atlas (bias)
|
|||
|
|||
public AtlasNode Allocate(int width, int height) |
|||
{ |
|||
// not a leaf node, try children
|
|||
if(m_RightChild != null) |
|||
{ |
|||
AtlasNode node = m_RightChild.Allocate(width, height); |
|||
if(node == null) |
|||
{ |
|||
node = m_BottomChild.Allocate(width, height); |
|||
} |
|||
return node; |
|||
} |
|||
|
|||
//leaf node, check for fit
|
|||
if ((width <= m_Rect.x) && (height <= m_Rect.y)) |
|||
{ |
|||
// perform the split
|
|||
m_RightChild = new AtlasNode(); |
|||
m_BottomChild = new AtlasNode(); |
|||
|
|||
if (width > height) // logic to decide which way to split
|
|||
{ // +--------+------+
|
|||
m_RightChild.m_Rect.z = m_Rect.z + width; // | | |
|
|||
m_RightChild.m_Rect.w = m_Rect.w; // +--------+------+
|
|||
m_RightChild.m_Rect.x = m_Rect.x - width; // | |
|
|||
m_RightChild.m_Rect.y = height; // | |
|
|||
// +---------------+
|
|||
m_BottomChild.m_Rect.z = m_Rect.z; |
|||
m_BottomChild.m_Rect.w = m_Rect.w + height; |
|||
m_BottomChild.m_Rect.x = m_Rect.x; |
|||
m_BottomChild.m_Rect.y = m_Rect.y - height; |
|||
} |
|||
else |
|||
{ // +---+-----------+
|
|||
m_RightChild.m_Rect.z = m_Rect.z + width; // | | |
|
|||
m_RightChild.m_Rect.w = m_Rect.w; // | | |
|
|||
m_RightChild.m_Rect.x = m_Rect.x - width; // +---+ +
|
|||
m_RightChild.m_Rect.y = m_Rect.y; // | | |
|
|||
// +---+-----------+
|
|||
m_BottomChild.m_Rect.z = m_Rect.z; |
|||
m_BottomChild.m_Rect.w = m_Rect.w + height; |
|||
m_BottomChild.m_Rect.x = width; |
|||
m_BottomChild.m_Rect.y = m_Rect.y - height; |
|||
} |
|||
m_Rect.x = width; |
|||
m_Rect.y = height; |
|||
return this; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public void Release() |
|||
{ |
|||
if (m_RightChild != null) |
|||
{ |
|||
m_RightChild.Release(); |
|||
m_BottomChild.Release(); |
|||
} |
|||
m_RightChild = null; |
|||
m_BottomChild = null; |
|||
} |
|||
} |
|||
|
|||
private AtlasNode m_Root; |
|||
private int m_Width; |
|||
private int m_Height; |
|||
|
|||
public AtlasAllocator(int width, int height) |
|||
{ |
|||
m_Root = new AtlasNode(); |
|||
m_Root.m_Rect.Set(width, height, 0, 0); |
|||
m_Width = width; |
|||
m_Height = height; |
|||
} |
|||
|
|||
public bool Allocate(ref Vector4 result, int width, int height) |
|||
{ |
|||
AtlasNode node = m_Root.Allocate(width, height); |
|||
if(node != null) |
|||
{ |
|||
result = node.m_Rect; |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
result = Vector4.zero; |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
public void Release() |
|||
{ |
|||
m_Root.Release(); |
|||
m_Root = new AtlasNode(); |
|||
m_Root.m_Rect.Set(m_Width, m_Height, 0, 0); |
|||
} |
|||
} |
|||
|
|||
public class Texture2DAtlas |
|||
{ |
|||
private RTHandle m_AtlasTexture = null; |
|||
private int m_Width; |
|||
private int m_Height; |
|||
private RenderTextureFormat m_Format; |
|||
private AtlasAllocator m_AtlasAllocator = null; |
|||
private Dictionary<IntPtr, Vector4> m_AllocationCache = new Dictionary<IntPtr, Vector4>(); |
|||
|
|||
public RTHandle AtlasTexture |
|||
{ |
|||
get |
|||
{ |
|||
return m_AtlasTexture; |
|||
} |
|||
} |
|||
|
|||
public Texture2DAtlas(int width, int height, RenderTextureFormat format) |
|||
{ |
|||
m_Width = width; |
|||
m_Height = height; |
|||
m_Format = format; |
|||
m_AtlasTexture = RTHandle.Alloc(m_Width, |
|||
m_Height, |
|||
1, |
|||
DepthBits.None, |
|||
m_Format, |
|||
FilterMode.Point, |
|||
TextureWrapMode.Clamp, |
|||
TextureDimension.Tex2D, |
|||
false, |
|||
false, |
|||
true, |
|||
false); |
|||
|
|||
m_AtlasAllocator = new AtlasAllocator(width, height); |
|||
} |
|||
|
|||
public void Release() |
|||
{ |
|||
ResetAllocator(); |
|||
RTHandle.Release(m_AtlasTexture); |
|||
} |
|||
|
|||
public void ResetAllocator() |
|||
{ |
|||
m_AtlasAllocator.Release(); |
|||
m_AllocationCache.Clear(); |
|||
} |
|||
|
|||
public bool AddTexture(CommandBuffer cmd, ref Vector4 scaleBias, Texture texture) |
|||
{ |
|||
IntPtr key = texture.GetNativeTexturePtr(); |
|||
if (!m_AllocationCache.TryGetValue(key, out scaleBias)) |
|||
{ |
|||
int width = texture.width; |
|||
int height = texture.height; |
|||
if (m_AtlasAllocator.Allocate(ref scaleBias, width, height)) |
|||
{ |
|||
scaleBias.Scale(new Vector4(1.0f / m_Width, 1.0f / m_Height, 1.0f / m_Width, 1.0f / m_Height)); |
|||
for (int mipLevel = 0; mipLevel < (texture as Texture2D).mipmapCount; mipLevel++) |
|||
{ |
|||
cmd.SetRenderTarget(m_AtlasTexture, mipLevel); |
|||
HDUtils.BlitQuad(cmd, texture, new Vector4(1, 1, 0, 0), scaleBias, mipLevel, false); |
|||
} |
|||
m_AllocationCache.Add(key, scaleBias); |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 081cfbdef2e1c014aa1d1c9aa16fb952 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue