Skip to content

Commit

Permalink
Merge branch 'main' into feature/disable-mr-boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Jul 7, 2024
2 parents 3ef54b7 + f46d55e commit 4170254
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ jobs:
- name: Set masking
run: echo "::add-mask::DoNotStealThis1"
- name: Free extra space
# This takes several minutes, so we only do it where required. As of 12/10/2023, this increases free space from 18GB to 41GB
if: matrix.targetPlatform == 'Android' || matrix.targetPlatform == 'StandaloneWindows64' || matrix.targetPlatform == 'StandaloneLinux64'
# As of 06/07/2024, this increases free space from 21GB to 38GB
run: |
echo "Initial free space"
df -h /
Expand Down
9 changes: 8 additions & 1 deletion Assets/Scripts/GUI/LoadBackgroundImageButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ override protected void OnButtonPressed()
{
return;
}
SceneSettings.m_Instance.LoadCustomSkybox(ReferenceImage.FileName);
if (ReferenceImage.NotLoaded)
{
// Load-on-demand.
ReferenceImage.SynchronousLoad();
}


SceneSettings.m_Instance.LoadCustomSkyboxFromCache(ReferenceImage.FilePath);
}

override public void ResetState()
Expand Down
53 changes: 51 additions & 2 deletions Assets/Scripts/ReferenceImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ public bool RequestLoad(bool allowMainThread = false)
// TODO Move into the async code path?
var importer = new RuntimeSVGImporter();
var tex = importer.ImportAsTexture(FilePath);

if (!ValidateDimensions(tex.width, tex.height, App.PlatformConfig.ReferenceImagesMaxDimension))
{
m_State = ImageState.ErrorImageTooLarge;
Object.Destroy(tex);
return true;
}

ImageCache.SaveImageCache(tex, FilePath);
m_ImageAspect = (float)tex.width / tex.height;
int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension;
Expand All @@ -370,6 +378,14 @@ public bool RequestLoad(bool allowMainThread = false)
RadianceHDRTexture hdr = new RadianceHDRTexture(fileData);
Texture2D tex = new Texture2D(2, 2, TextureFormat.RGB24, false);
tex = hdr.texture;

if (!ValidateDimensions(tex.width, tex.height, App.PlatformConfig.ReferenceImagesMaxDimension))
{
m_State = ImageState.ErrorImageTooLarge;
Object.Destroy(tex);
return true;
}

ImageCache.SaveImageCache(tex, FilePath);
m_ImageAspect = (float)tex.width / tex.height;
int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension;
Expand Down Expand Up @@ -495,6 +511,14 @@ IEnumerator<Timeslice> RequestLoadCoroutineMainThread()
// from WWW.LoadImageIntoTexture
Texture2D inTex = new Texture2D(2, 2, TextureFormat.RGBA32, true);
loader.LoadImageIntoTexture(inTex);

if (!ValidateDimensions(inTex.width, inTex.height, App.PlatformConfig.ReferenceImagesMaxDimension))
{
m_State = ImageState.ErrorImageTooLarge;
Object.Destroy(inTex);
yield break;
}

DownsizeTexture(inTex, ref m_Icon, ReferenceImageCatalog.MAX_ICON_TEX_DIMENSION);
m_Icon.wrapMode = TextureWrapMode.Clamp;
m_ImageAspect = (float)inTex.width / inTex.height;
Expand Down Expand Up @@ -548,6 +572,18 @@ IEnumerable<Timeslice> RequestLoadCoroutine()
if (imageLoad != null)
{
ControllerConsoleScript.m_Instance.AddNewLine(imageLoad.Message, true);
if (imageLoad.imageLoadErrorCode == ImageLoadError.ImageLoadErrorCode.ImageTooLargeError)
{
m_State = ImageState.ErrorImageTooLarge;
}
else
{
m_State = ImageState.Error;
}

reader = null;
yield break;

}
}

Expand Down Expand Up @@ -578,8 +614,6 @@ IEnumerable<Timeslice> RequestLoadCoroutine()
else
{
// Problem reading the file?
// images with state ImageState.Error display a generic error message 'Image failed to load' when hovering over them in the reference panel
// TODO: use more specific error messages (e.g "Too large dimensions", "Unknown format") that are set in ImageUtils.cs? (that is called by ThreadedImageReader.cs)
m_State = ImageState.Error;
reader = null;
yield break;
Expand All @@ -595,6 +629,21 @@ private bool ValidateFileSize()
return info.Length <= App.PlatformConfig.ReferenceImagesMaxFileSize;
}

private bool ValidateDimensions(int imageWidth, int imageHeight, int maxDimension)
{

// Cast to long as maxDimension is big enough on desktop to overflow
if (imageWidth * imageHeight > ((long)maxDimension * (long)maxDimension))
{
return false;
}
else
{
return true;
}

}

public string GetExportName()
{
return Path.GetFileNameWithoutExtension(FileName);
Expand Down
27 changes: 27 additions & 0 deletions Assets/Scripts/SceneSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,33 @@ public void LoadCustomSkybox(string filename)
}
}

// note: this takes in the full file path, instead of just the name
public void LoadCustomSkyboxFromCache(string filepath)
{
Texture2D tex = ImageCache.LoadImageCache(filepath);

if (tex == null)
{
LoadCustomSkybox(Path.GetFileName(filepath));
}
else
{
float aspectRatio = tex.width / tex.height;
if (aspectRatio > 1.5)
{
m_CustomSkyboxMaterial = Resources.Load<Material>("Environments/CustomSkybox");
}
else
{
m_CustomSkyboxMaterial = Resources.Load<Material>("Environments/CustomStereoSkybox");
}
m_CustomSkyboxMaterial.mainTexture = tex;
m_CustomSkyboxMaterial.SetColor("_Tint", Color.gray);
RenderSettings.skybox = m_CustomSkyboxMaterial;
RenderSettings.ambientMode = AmbientMode.Skybox;
}
}

public Quaternion GradientOrientation
{
get { return m_GradientSkew; }
Expand Down
21 changes: 20 additions & 1 deletion Assets/Scripts/Util/ImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@

namespace TiltBrush
{



public class ImageLoadError : Exception
{

public enum ImageLoadErrorCode
{
// This is the default one; errors like unknown format, corruption, etc.
ImageGenericError,
// too large dimensions
ImageTooLargeError
}

public ImageLoadErrorCode imageLoadErrorCode = ImageLoadErrorCode.ImageGenericError;

public ImageLoadError(string message) : base(message) { }
public ImageLoadError(string fmt, params System.Object[] args)
: base(string.Format(fmt, args))
Expand All @@ -31,6 +45,11 @@ public ImageLoadError(Exception inner, string fmt, params System.Object[] args)
: base(string.Format(fmt, args), inner)
{
}

public ImageLoadError(string message, ImageLoadErrorCode imageLoadErrorCode) : base(message)
{
this.imageLoadErrorCode = imageLoadErrorCode;
}
}

static public class ImageUtils
Expand Down Expand Up @@ -92,7 +111,7 @@ static public void ValidateDimensions(byte[] data, int maxDimension)
{
throw new ImageLoadError(
String.Format("Image dimensions {0}x{1} are greater than max dimensions of {2}x{2}!",
imageWidth, imageHeight, maxDimension));
imageWidth, imageHeight, maxDimension), ImageLoadError.ImageLoadErrorCode.ImageTooLargeError);
}
}

Expand Down

0 comments on commit 4170254

Please sign in to comment.