Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
calderarchinuk committed Feb 11, 2019
2 parents 1ea7a82 + cf03e81 commit a152bb9
Show file tree
Hide file tree
Showing 19 changed files with 323 additions and 46 deletions.
Expand Up @@ -180,8 +180,8 @@ public override void OnInspectorGUI()
//GUILayout.BeginHorizontal();


GUIContent[] textureQualityNames = new GUIContent[] { new GUIContent("Full"), new GUIContent("Half"), new GUIContent("Quarter"), new GUIContent("Eighth"), new GUIContent("Sixteenth") };
int[] textureQualities = new int[] { 1, 2, 4, 8, 16 };
GUIContent[] textureQualityNames = new GUIContent[] { new GUIContent("Full"), new GUIContent("Half"), new GUIContent("Quarter"), new GUIContent("Eighth"), new GUIContent("Sixteenth"), new GUIContent("Thirty Second"), new GUIContent("Sixty Fourth") };
int[] textureQualities = new int[] { 1, 2, 4, 8, 16, 32, 64 };
CognitiveVR_Preferences.Instance.TextureResize = EditorGUILayout.IntPopup(new GUIContent("Texture Export Quality", "Reduce textures when uploading to scene explorer"), CognitiveVR_Preferences.Instance.TextureResize, textureQualityNames, textureQualities);


Expand Down
Expand Up @@ -36,7 +36,7 @@ public SceneVersion GetLatestVersion()
int latest = 0;
SceneVersion latestscene = null;
if (versions == null) { Debug.LogError("SceneVersionCollection versions is null!"); return null; }
for (int i = 0; i<versions.Count;i++)
for (int i = 0; i < versions.Count; i++)
{
if (versions[i].versionNumber > latest)
{
Expand Down Expand Up @@ -122,7 +122,7 @@ public static void ExportSceneAR()
if (Directory.Exists(objPath))
{
var files = Directory.GetFiles(objPath);
for(int i=0;i<files.Length;i++)
for (int i = 0; i < files.Length; i++)
{
File.Delete(files[i]);
}
Expand All @@ -134,7 +134,6 @@ public static void ExportSceneAR()
}

//export and try to decimate the scene


#endregion

Expand Down Expand Up @@ -296,7 +295,6 @@ public static void UploadDecimatedScene(CognitiveVR_Preferences.SceneSettings se
else //upload as new scene
{
//posting wwwform with headers

Dictionary<string, string> headers = new Dictionary<string, string>();
if (EditorCore.IsDeveloperKeyValid)
{
Expand All @@ -319,7 +317,7 @@ static void PostSceneUploadResponse(int responseCode, string error, string text)
if (responseCode != 200 && responseCode != 201)
{
Debug.LogError("Scene Upload Error " + error);

UploadSceneSettings = null;
UploadComplete = null;
return;
Expand All @@ -329,7 +327,6 @@ static void PostSceneUploadResponse(int responseCode, string error, string text)
if (text.Contains("Internal Server Error") || text.Contains("Bad Request"))
{
Debug.LogError("Scene Upload Error:" + text);

UploadSceneSettings = null;
UploadComplete = null;
return;
Expand Down Expand Up @@ -414,7 +411,7 @@ public static void ExportGLTFScene()
static Queue<string> ResizeQueue = new Queue<string>();
static void UpdateResize()
{
while(ResizeQueue.Count > 0)
while (ResizeQueue.Count > 0)
{
ResizeTexturesInExportFolder(ResizeQueue.Dequeue());
}
Expand Down Expand Up @@ -442,7 +439,12 @@ static void ResizeTexturesInExportFolder(string folderpath)

texture.LoadImage(File.ReadAllBytes(file));

texture = RescaleForExport(texture, Mathf.NextPowerOfTwo(texture.width) / textureDivisor, Mathf.NextPowerOfTwo(texture.height) / textureDivisor);
var newWidth = Mathf.Max(1, Mathf.NextPowerOfTwo(texture.width) / textureDivisor);
var newHeight = Mathf.Max(1, Mathf.NextPowerOfTwo(texture.height) / textureDivisor);

TextureScale.Bilinear(texture, newWidth, newHeight);

//texture = RescaleForExport(texture, Mathf.NextPowerOfTwo(texture.width) / textureDivisor, Mathf.NextPowerOfTwo(texture.height) / textureDivisor);
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(path, bytes);
}
Expand Down Expand Up @@ -508,10 +510,10 @@ static void BakeNonstandardRenderers(DynamicObject rootDynamic, List<BakeableMes
continue;
}

var pos = skinnedMeshRenderer.transform.localPosition;
skinnedMeshRenderer.transform.localPosition = Vector3.zero;
//var rot = v.transform.localRotation;
//v.transform.localRotation = Quaternion.identity;
//var pos = skinnedMeshRenderer.transform.localPosition;
//skinnedMeshRenderer.transform.localPosition = Vector3.zero;
//var rot = skinnedMeshRenderer.transform.localRotation;
//skinnedMeshRenderer.transform.localRotation = Quaternion.identity;

BakeableMesh bm = new BakeableMesh();

Expand All @@ -533,8 +535,8 @@ static void BakeNonstandardRenderers(DynamicObject rootDynamic, List<BakeableMes
bm.meshFilter.sharedMesh = m;
meshes.Add(bm);

//v.transform.localPosition = pos;
//v.transform.localRotation = rot;
//skinnedMeshRenderer.transform.localPosition = pos;
//skinnedMeshRenderer.transform.localRotation = rot;
}

//TODO ignore parent rotation and scale
Expand All @@ -552,12 +554,20 @@ static void BakeNonstandardRenderers(DynamicObject rootDynamic, List<BakeableMes
continue;
}

//generate mesh from heightmap
BakeableMesh bm = new BakeableMesh();
bm.meshRenderer = v.gameObject.AddComponent<MeshRenderer>();

bm.tempGo = new GameObject(v.gameObject.name);
bm.tempGo.transform.parent = v.transform;
//bm.tempGo.transform.localScale = Vector3.one;
//bm.tempGo.transform.localRotation = Quaternion.identity;
bm.tempGo.transform.localPosition = Vector3.zero;


//generate mesh from heightmap
bm.meshRenderer = bm.tempGo.AddComponent<MeshRenderer>();
bm.meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
bm.meshRenderer.sharedMaterial.mainTexture = BakeTerrainTexture(path, v.terrainData);
bm.meshFilter = v.gameObject.AddComponent<MeshFilter>();
bm.meshFilter = bm.tempGo.AddComponent<MeshFilter>();
bm.meshFilter.sharedMesh = GenerateTerrainMesh(v);
meshes.Add(bm);
}
Expand All @@ -578,7 +588,12 @@ static void BakeNonstandardRenderers(DynamicObject rootDynamic, List<BakeableMes
}

BakeableMesh bm = new BakeableMesh();
bm.meshRenderer = v.gameObject.AddComponent<MeshRenderer>();
bm.tempGo = new GameObject(v.gameObject.name);
bm.tempGo.transform.parent = v.transform;
bm.tempGo.transform.localScale = Vector3.one;
bm.tempGo.transform.localRotation = Quaternion.identity;
bm.tempGo.transform.localPosition = Vector3.zero;
bm.meshRenderer = bm.tempGo.AddComponent<MeshRenderer>();
bm.meshRenderer.sharedMaterial = new Material(Shader.Find("Transparent/Diffuse"));

var width = v.GetComponent<RectTransform>().sizeDelta.x * v.transform.localScale.x;
Expand All @@ -589,7 +604,7 @@ static void BakeNonstandardRenderers(DynamicObject rootDynamic, List<BakeableMes
screenshot.name = v.gameObject.name.Replace(' ', '_');
bm.meshRenderer.sharedMaterial.mainTexture = screenshot;

bm.meshFilter = v.gameObject.AddComponent<MeshFilter>();
bm.meshFilter = bm.tempGo.AddComponent<MeshFilter>();
//write simple quad
var mesh = ExportQuad(v.gameObject.name + "_canvas", width, height, v.transform, UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().name, screenshot);
bm.meshFilter.sharedMesh = mesh;
Expand Down Expand Up @@ -747,7 +762,7 @@ public static Texture2D Snapshot(Transform target, int resolution = 128)
cam.orthographicSize = Mathf.Max(target.GetComponent<RectTransform>().sizeDelta.x * target.localScale.x, target.GetComponent<RectTransform>().sizeDelta.y * target.localScale.y) / 2;
cam.clearFlags = CameraClearFlags.Color; //WANT TO CLEAR EVERYTHING FROM THIS CAMERA
cam.backgroundColor = Color.clear;

//create render texture and assign to camera
RenderTexture rt = RenderTexture.GetTemporary(resolution, resolution, 16); //new RenderTexture(resolution, resolution, 16);
RenderTexture.active = rt;
Expand All @@ -759,15 +774,16 @@ public static Texture2D Snapshot(Transform target, int resolution = 128)
//RenderTexture.active = rt;

cam.Render();

//TODO write non-square textures, do full 0-1 uvs instead of saving blank space


//write rendertexture to png
Texture2D tex = new Texture2D(resolution, resolution);
RenderTexture.active = rt;

tex.ReadPixels(new Rect(0, 0, resolution, resolution), 0, 0);
tex.Apply();

//GL.Clear(true, true, Color.clear);
RenderTexture.active = null;

Expand All @@ -785,7 +801,7 @@ public static bool ExportSelectedObjectsPrefab()
{
List<Transform> entireSelection = new List<Transform>();
entireSelection.AddRange(Selection.GetTransforms(SelectionMode.Editable));

if (entireSelection.Count == 0)
{
EditorUtility.DisplayDialog("Dynamic Object Export", "No Dynamic Objects selected", "Ok");
Expand Down Expand Up @@ -883,7 +899,7 @@ public static bool ExportSelectedObjectsPrefab()

var exporter = new UnityGLTF.GLTFSceneExporter(new Transform[1] { dynamic.transform }, RetrieveTexturePath, dynamic);
exporter.SaveGLTFandBin(path + dynamic.MeshName + Path.DirectorySeparatorChar, dynamic.MeshName);

successfullyExportedCount++;


Expand All @@ -893,6 +909,8 @@ public static bool ExportSelectedObjectsPrefab()
temp[i].meshRenderer.transform.localScale = temp[i].originalScale;
DestroyImmediate(temp[i].meshFilter);
DestroyImmediate(temp[i].meshRenderer);
if (temp[i].tempGo != null)
DestroyImmediate(temp[i].tempGo);
}

EditorCore.SaveDynamicThumbnailAutomatic(dynamic.gameObject);
Expand All @@ -907,7 +925,7 @@ public static bool ExportSelectedObjectsPrefab()
//destroy baked skin, terrain, canvases
dynamic.transform.localPosition = originalOffset;
dynamic.transform.localRotation = originalRot;

ResizeQueue.Enqueue(path + dynamic.MeshName + Path.DirectorySeparatorChar);
EditorApplication.update -= UpdateResize;
EditorApplication.update += UpdateResize;
Expand Down Expand Up @@ -1056,7 +1074,7 @@ public static Texture2D BakeTerrainTexture(string destinationFolder, TerrainData

}
}

var sizemax = Mathf.Max(
Mathf.NextPowerOfTwo((int)(data.heightmapScale.z * data.heightmapResolution * 16)),
Mathf.NextPowerOfTwo((int)(data.heightmapScale.x * data.heightmapResolution * 16)));
Expand All @@ -1072,7 +1090,7 @@ public static Texture2D BakeTerrainTexture(string destinationFolder, TerrainData

float[] colorAtLayer = new float[layerCount];
SplatPrototype[] prototypes = data.splatPrototypes;

for (int y = 0; y < outTex.height; y++)
{
for (int x = 0; x < outTex.width; x++)
Expand Down Expand Up @@ -1216,7 +1234,7 @@ static bool UploadDynamicObjects(List<string> dynamicMeshNames, bool ShowPopupWi

if (string.IsNullOrEmpty(sceneid))
{
EditorUtility.DisplayDialog("Dynamic Object Upload Failed", "Could not find the SceneId for \"" + settings.SceneName + "\". Are you sure you've exported and uploaded this scene to SceneExplorer?","Ok");
EditorUtility.DisplayDialog("Dynamic Object Upload Failed", "Could not find the SceneId for \"" + settings.SceneName + "\". Are you sure you've exported and uploaded this scene to SceneExplorer?", "Ok");
return false;
}

Expand All @@ -1225,7 +1243,7 @@ static bool UploadDynamicObjects(List<string> dynamicMeshNames, bool ShowPopupWi

//
List<string> exportDirectories = new List<string>();
foreach(var v in subdirectories)
foreach (var v in subdirectories)
{
var split = v.Split(Path.DirectorySeparatorChar);

Expand All @@ -1237,7 +1255,7 @@ static bool UploadDynamicObjects(List<string> dynamicMeshNames, bool ShowPopupWi

if (ShowPopupWindow)
{
int option = EditorUtility.DisplayDialogComplex("Upload Dynamic Objects", "Do you want to upload " + exportDirectories.Count + " Objects to \"" + settings.SceneName + "\" (" + settings.SceneId + " Version:" + settings.VersionNumber+")?", "Ok", "Cancel", "Open Directory");
int option = EditorUtility.DisplayDialogComplex("Upload Dynamic Objects", "Do you want to upload " + exportDirectories.Count + " Objects to \"" + settings.SceneName + "\" (" + settings.SceneId + " Version:" + settings.VersionNumber + ")?", "Ok", "Cancel", "Open Directory");
if (option == 0) { } //ok
else if (option == 1) { return false; } //cancel
else
Expand All @@ -1252,7 +1270,7 @@ static bool UploadDynamicObjects(List<string> dynamicMeshNames, bool ShowPopupWi
#endif
}
}
string objectNames="";
string objectNames = "";
foreach (var subdir in exportDirectories)
{
var filePaths = Directory.GetFiles(subdir);
Expand Down

0 comments on commit a152bb9

Please sign in to comment.