Skip to content

RIDER-26339 texture debugger preview #2420

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: net233
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions debugger/texture-debugger/TextureUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Drawing;
using System.Linq;
using UnityEngine;
using Graphics = UnityEngine.Graphics;
using Object = UnityEngine.Object;

namespace JetBrains.Debugger.Worker.Plugins.Unity.Presentation.Texture
{
public class TexturePixelsInfo
{
public int Width;
public int Height;
public int[] Pixels;
public int OriginalWidth;
public int OriginalHeight;
public string GraphicsTextureFormat;
public string TextureFormat;


public TexturePixelsInfo(Size size, Color32[] pixels, Texture2D texture2D)
{
Pixels = pixels.Select(c => c.ToHex()).ToArray();
Width = size.Width;
Height = size.Height;
TextureFormat = texture2D.format.ToString();
GraphicsTextureFormat = texture2D.graphicsFormat.ToString();
OriginalWidth = texture2D.width;
OriginalHeight = texture2D.height;
}
}

public static class UnityTextureAdapter
{
public static int ToHex(this Color32 c)
{
return (c.r << 16) | (c.g << 8) | (c.b);
}

public static string GetPixelsInString(Texture2D texture2D)
{
return GetPixelsInString(texture2D, new Size(texture2D.width, texture2D.height));
}

public static string GetPixelsInString(Texture2D texture2D, Size size)
{
size = GetTextureConvertedSize(texture2D, size);
var color32 = GetPixels(texture2D, size);
return JsonUtility.ToJson(color32, true);
}

private static TexturePixelsInfo GetPixels(Texture2D texture2d, Size size)
{
var targetTexture = CreateTargetTexture(size);
CopyTexture(texture2d, targetTexture);
var pixels = targetTexture.GetPixels32();
var texturePixelsInfo = new TexturePixelsInfo(new Size(targetTexture.width, targetTexture.height)
, pixels
, texture2d);
Object.DestroyImmediate(targetTexture);
return texturePixelsInfo;
}

private static byte[] GetRawBytes(Texture2D texture2d, Size size)
{
var targetTexture = CreateTargetTexture(size);
CopyTexture(texture2d, targetTexture);
var rawTextureData = targetTexture.GetRawTextureData();
Object.DestroyImmediate(targetTexture);
return rawTextureData;
}

private static void CopyTexture(UnityEngine.Texture texture2d, Texture2D targetTexture)
{
var renderTexture = RenderTexture.GetTemporary(
targetTexture.width,
targetTexture.height,
0,
RenderTextureFormat.ARGB32
);

// Blit the pixels on texture to the RenderTexture
Graphics.Blit(texture2d, renderTexture);

// Backup the currently set RenderTexture
var previous = RenderTexture.active;

// Set the current RenderTexture to the temporary one we created
RenderTexture.active = renderTexture;

// Create a new readable Texture2D to copy the pixels to it

// Copy the pixels from the RenderTexture to the new Texture
targetTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
targetTexture.Apply();

// Reset the active RenderTexture
RenderTexture.active = previous;

// Release the temporary RenderTexture
RenderTexture.ReleaseTemporary(renderTexture);
}

private static Texture2D CreateTargetTexture(Size size)
{
var texture2D = new Texture2D(size.Width, size.Height, TextureFormat.RGBA32, false);
return texture2D;
}

private static Size GetTextureConvertedSize(UnityEngine.Texture texture2d, Size size)
{
var texture2dWidth = texture2d.width;
var texture2dHeight = texture2d.height;

var divider = 1;
while (texture2dWidth / divider > size.Width && texture2dHeight / divider > size.Height)
divider *= 2;

var targetTextureWidth = texture2dWidth / divider;
var targetTextureHeight = texture2dHeight / divider;
return new Size(targetTextureWidth, targetTextureHeight);
}
}
}
32 changes: 32 additions & 0 deletions debugger/texture-debugger/texture-debugger.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<InternalBuild Condition="Exists('ManagedProject.Generated.Targets')">True</InternalBuild>
<InternalBuild Condition="$(InternalBuild) == ''">False</InternalBuild>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="JetBrains.NET.Sdk" Version="0.0.4" Condition="$(InternalBuild)" />
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" Condition="!$(InternalBuild)" />
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" Condition="$(ImportGuard-DirectoryBuildProps) == ''" />
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>JetBrains.ReSharper.Plugins.Unity.Rider.Debugger.Presentation.Texture</AssemblyName>
<RootNamespace>JetBrains.Debugger.Worker.Plugins.Unity.Presentation.Texture</RootNamespace>
<LangVersion>9</LangVersion>
<AssemblyOriginatorKeyFile>..\..\sign.snk</AssemblyOriginatorKeyFile>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Unity.Libs.Ref.2019.2.0f1" Version="2021.11.29" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net472">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="mscorlib" />
</ItemGroup>
<Import Project="ManagedProject.Generated.Targets" Condition="$(InternalBuild)" />
<Import Project="Sdk.targets" Sdk="JetBrains.NET.Sdk" Version="0.0.4" Condition="$(InternalBuild)" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" Condition="!$(InternalBuild)" />
</Project>
7 changes: 7 additions & 0 deletions resharper/resharper-unity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EditorPlugin.SinceUnity.201
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EditorPlugin.SinceUnity.2019.2", "..\unity\EditorPlugin\EditorPlugin.SinceUnity.2019.2.csproj", "{117CAF5D-7FC0-4626-98E0-E81572964E56}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "texture-debugger", "..\debugger\texture-debugger\texture-debugger.csproj", "{DF886D30-1FE9-4296-9202-BCE567BFF2E5}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\debugger\usbmuxd\usbmuxd.projitems*{4e49cd68-6ba2-4765-80ac-d82537a0996b}*SharedItemsImports = 5
Expand Down Expand Up @@ -144,6 +146,10 @@ Global
{117CAF5D-7FC0-4626-98E0-E81572964E56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{117CAF5D-7FC0-4626-98E0-E81572964E56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{117CAF5D-7FC0-4626-98E0-E81572964E56}.Release|Any CPU.Build.0 = Release|Any CPU
{DF886D30-1FE9-4296-9202-BCE567BFF2E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF886D30-1FE9-4296-9202-BCE567BFF2E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF886D30-1FE9-4296-9202-BCE567BFF2E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF886D30-1FE9-4296-9202-BCE567BFF2E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -170,6 +176,7 @@ Global
{CA4516C7-2795-4F5D-B13C-EC5E46F19C68} = {27FAB5DF-2272-44E7-9BCE-41508F24C07B}
{27D56D2A-B11F-4BAA-B26B-1A73A055B398} = {27FAB5DF-2272-44E7-9BCE-41508F24C07B}
{117CAF5D-7FC0-4626-98E0-E81572964E56} = {27FAB5DF-2272-44E7-9BCE-41508F24C07B}
{DF886D30-1FE9-4296-9202-BCE567BFF2E5} = {D61F53F1-F209-4313-9FF2-B1DBD286BE11}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F132919A-F861-4C4F-923A-C956B82D9EE6}
Expand Down
7 changes: 7 additions & 0 deletions rider/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ val debuggerDllFiles = files(
"../resharper/build/debugger/bin/$buildConfiguration/net472/JetBrains.ReSharper.Plugins.Unity.Rider.Debugger.pdb"
)

val textureDebuggerDllFiles = files(
"../resharper/build/texture-debugger/bin/$buildConfiguration/net472/JetBrains.ReSharper.Plugins.Unity.Rider.Debugger.Presentation.Texture.dll",
"../resharper/build/texture-debugger/bin/$buildConfiguration/net472/JetBrains.ReSharper.Plugins.Unity.Rider.Debugger.Presentation.Texture.pdb"
)

val listIosUsbDevicesFiles = files(
"../resharper/build/ios-list-usb-devices/bin/$buildConfiguration/net7.0/JetBrains.Rider.Unity.ListIosUsbDevices.dll",
"../resharper/build/ios-list-usb-devices/bin/$buildConfiguration/net7.0/JetBrains.Rider.Unity.ListIosUsbDevices.pdb",
Expand Down Expand Up @@ -900,6 +905,7 @@ See CHANGELOG.md in the JetBrains/resharper-unity GitHub repo for more details a
doLast {
dotnetDllFiles.forEach { if (!it.exists()) error("File $it does not exist") }
debuggerDllFiles.forEach { if (!it.exists()) error("File $it does not exist") }
textureDebuggerDllFiles.forEach { if (!it.exists()) error("File $it does not exist") }
listIosUsbDevicesFiles.forEach { if (!it.exists()) error("File $it does not exist") }
unityEditorDllFiles.forEach { if (!it.exists()) error("File $it does not exist") }
}
Expand All @@ -908,6 +914,7 @@ See CHANGELOG.md in the JetBrains/resharper-unity GitHub repo for more details a

dotnetDllFiles.forEach { from(it) { into("${pluginName}/dotnet") } }
debuggerDllFiles.forEach { from(it) { into("${pluginName}/dotnetDebuggerWorker") } }
textureDebuggerDllFiles.forEach { from(it) { into("${pluginName}/DotFiles") } }
listIosUsbDevicesFiles.forEach { from(it) { into("${pluginName}/DotFiles") } }
unityEditorDllFiles.forEach { from(it) { into("${pluginName}/EditorPlugin") } }

Expand Down
Loading