Skip to content

Commit

Permalink
Merge remote-tracking branch 'ixxyxr/experiments/moonsharp' into feat…
Browse files Browse the repository at this point in the history
…ure/kitchen-sink
  • Loading branch information
andybak committed Nov 29, 2023
2 parents cbc9767 + 4e618ed commit 7852abf
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
28 changes: 22 additions & 6 deletions Assets/Scripts/API/Lua/LuaManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,15 @@ public void Init()
Directory.CreateDirectory(LuaModulesPath);
}

// Allow includes from Scripts/LuaModules
Script.DefaultOptions.ScriptLoader = new FileSystemScriptLoader();
((ScriptLoaderBase)Script.DefaultOptions.ScriptLoader).ModulePaths = new[] { Path.Join(LuaModulesPath, "?.lua") };

CopyLuaModules();

// Allow includes from Scripts/LuaModules
Script.DefaultOptions.ScriptLoader = new OpenBrushScriptLoader();
((ScriptLoaderBase)Script.DefaultOptions.ScriptLoader).ModulePaths = new[]
{
Path.Join(LuaModulesPath, "?.lua")
};

LoadExampleScripts();
LoadUserScripts();

Expand Down Expand Up @@ -446,9 +449,14 @@ private string LoadScriptFromString(string scriptFilename, string contents, bool
{
script.DoString(contents);
}
catch (ScriptRuntimeException e)
{
LogLuaInterpreterError(script, $"(Runtime Error loading: {scriptFilename})", e);
return null;
}
catch (SyntaxErrorException e)
{
LogLuaInterpreterError(script, $"(Loading: {scriptFilename})", e);
LogLuaInterpreterError(script, $"(Syntax Error loading: {scriptFilename})", e);
return null;
}
var catMatch = TryGetCategoryFromScriptName(scriptFilename);
Expand All @@ -465,7 +473,15 @@ private string LoadScriptFromString(string scriptFilename, string contents, bool
{
m_ActiveBackgroundScripts[scriptName] = script;
}
InitScriptOnce(script);
try
{
InitScriptOnce(script);
}
catch (ScriptRuntimeException e)
{
LogLuaInterpreterError(script, $"(ScriptRuntimeException InitScriptOnce: {scriptFilename})", e);
return null;
}
}
return scriptName;
}
Expand Down
37 changes: 37 additions & 0 deletions Assets/Scripts/API/Lua/OpenBrushScriptLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 The Open Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Loaders;
using System.IO;
using UnityEngine;

namespace TiltBrush
{
public class OpenBrushScriptLoader : ScriptLoaderBase
{
public override bool ScriptFileExists(string name)
{
LuaManager.LogLuaMessage($"ScriptFileExists: {name}? {File.Exists(name)}");
return File.Exists(name);
}

public override object LoadFile(string file, Table globalContext)
{
FileStream result = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
LuaManager.LogLuaMessage($"LoadFile: {file} CanRead? {result.CanRead}");
return result;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/API/Lua/OpenBrushScriptLoader.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Assets/Scripts/API/Lua/Wrappers/AppApiWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static string ReadFile(string path)
[LuaDocsParameter("height", "Image height")]
[LuaDocsParameter("superSampling", "The supersampling strength to apply (between 0.125 and 4.0)")]
[LuaDocsParameter("renderDepth", "If true then render the depth buffer instead of the image")]
public static void TakeSnapshot(TrTransform tr, string filename, int width, int height, float superSampling = 1f, bool renderDepth = false)
public static void TakeSnapshot(TrTransform tr, string filename, int width, int height, float superSampling = 1f, bool renderDepth = false, bool removeBackground = false)
{
bool saveAsPng;
if (filename.ToLower().EndsWith(".jpg") || filename.ToLower().EndsWith(".jpeg"))
Expand All @@ -205,16 +205,14 @@ public static void TakeSnapshot(TrTransform tr, string filename, int width, int
{
var rig = SketchControlsScript.m_Instance.MultiCamCaptureRig;
App.Scene.AsScene[rig.gameObject.transform] = tr;
var rMgr = rig.ManagerFromStyle(
MultiCamStyle.Snapshot
);
var rMgr = rig.ManagerFromStyle(MultiCamStyle.Snapshot);
var initialState = rig.gameObject.activeSelf;
rig.gameObject.SetActive(true);
RenderTexture tmp = rMgr.CreateTemporaryTargetForSave(width, height);
RenderWrapper wrapper = rMgr.gameObject.GetComponent<RenderWrapper>();
float ssaaRestore = wrapper.SuperSampling;
wrapper.SuperSampling = superSampling;
rMgr.RenderToTexture(tmp, asDepth: renderDepth);
rMgr.RenderToTexture(tmp, asDepth: renderDepth, removeBackground: removeBackground);
wrapper.SuperSampling = ssaaRestore;
using (var fs = new FileStream(path, FileMode.Create))
{
Expand Down
15 changes: 14 additions & 1 deletion Assets/Scripts/ScreenshotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public RenderTexture CreateTemporaryTargetForSave(int width, int height)

/// If m_AutoAlignRig is set, you should pass in a RenderTexture created
/// with CreateTemporaryTargetForSave().
public void RenderToTexture(RenderTexture rTexture, bool asDepth = false)
public void RenderToTexture(RenderTexture rTexture, bool asDepth = false, bool removeBackground = false)
{
RenderTextureFormat format = CameraFormat();
int depth = 24;
Expand All @@ -446,6 +446,19 @@ public void RenderToTexture(RenderTexture rTexture, bool asDepth = false)
camera.RenderWithShader(Shader.Find("Hidden/Internal-DepthNormalsTexture"), "");
camera.depthTextureMode = prevDepthTextureMode;
}
else if (removeBackground)
{
var prevClearFlags = camera.clearFlags;
var prevBackgroundColor = camera.backgroundColor;
var prevCullingMask = camera.cullingMask;
camera.clearFlags = CameraClearFlags.SolidColor;
camera.backgroundColor = new Color(0, 0, 0, 0);
camera.cullingMask = LayerMask.GetMask("MainCanvas");
camera.Render();
camera.clearFlags = prevClearFlags;
camera.backgroundColor = prevBackgroundColor;
camera.cullingMask = prevCullingMask;
}
else
{
camera.Render();
Expand Down
2 changes: 1 addition & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"com.unity.xr.management": "4.3.3",
"com.unity.xr.oculus": "3.3.0",
"com.unity.xr.openxr": "1.8.2",
"moonsharp": "https://github.com/nothke/moonsharp-unity-upm.git",
"moonsharp": "https://github.com/icosa-mirror/moonsharp-unity-upm.git",
"org.nuget.google.apis": "1.57.0",
"org.nuget.google.apis.auth": "1.57.0",
"org.nuget.google.apis.core": "https://github.com/icosa-mirror/org.nuget.google.apis.core.git#unity-newtonsoft",
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
"url": "https://packages.unity.com"
},
"moonsharp": {
"version": "https://github.com/nothke/moonsharp-unity-upm.git",
"version": "https://github.com/icosa-mirror/moonsharp-unity-upm.git",
"depth": 0,
"source": "git",
"dependencies": {},
Expand Down

0 comments on commit 7852abf

Please sign in to comment.