void handleSearch(string response) { JSONNode json = Utils.JSONParse(response); JSONArray array = json["results"].AsArray; if (array == null) { return; } _prevCursorUrl = json["previous"]; _nextCursorUrl = json["next"]; // First model fetch from uid foreach (JSONNode node in array) { if (!isInModels(node["uid"])) { // Add model to results SketchfabModel model = new SketchfabModel(node, _defaultThumbnail); model.previewUrl = getThumbnailUrl(node, 768); _sketchfabModels.Add(node["uid"].Value, model); // Request model thumbnail SketchfabRequest request = new SketchfabRequest(getThumbnailUrl(node)); request.setCallback(handleThumbnail); _api.registerRequest(request); } } _isFetching = false; Refresh(); }
private void OnFinishImport() { SketchfabModel model = _browserManager.getModel(_currentUid); EditorUtility.ClearProgressBar(); EditorUtility.DisplayDialog("Import successful", "Model \n" + model.name + " by " + model.author + " has been successfully imported", "OK"); }
public void fetchModelInfo(string uid) { SketchfabModel model = _sketchfabModels[uid] as SketchfabModel; if (model.licenseJson == null) { SketchfabRequest request = new SketchfabRequest(SketchfabPlugin.Urls.modelEndPoint + "/" + uid); request.setCallback(handleModelData); _api.registerRequest(request); } }
void handleModelData(string request) { JSONNode node = Utils.JSONParse(request); string nodeuid = node["uid"]; if (_sketchfabModels == null || !isInModels(node["uid"])) { return; } string uid = node["uid"]; SketchfabModel model = _sketchfabModels[uid] as SketchfabModel; model.parseModelData(node); _sketchfabModels[uid] = model; }
public void displayModelPage(SketchfabModel model, SketchfabBrowser browser) { _window = browser; if (_currentModel == null || model.uid != _currentModel.uid) { _currentModel = model; _prefabName = GLTFUtils.cleanName(_currentModel.name).Replace(" ", "_"); _importDirectory = Application.dataPath + "/Import/" + _prefabName.Replace(" ", "_"); } else { _currentModel = model; } _ui = SketchfabPlugin.getUI(); show = true; }
void displayResult(SketchfabModel model) { GUILayout.BeginVertical(); if (GUILayout.Button(new GUIContent(model._thumbnail as Texture2D), GUILayout.MaxHeight(_thumbnailSize), GUILayout.MaxWidth(_thumbnailSize))) { _currentUid = model.uid; _browserManager.fetchModelInfo(_currentUid); if (_skfbWin != null) { _skfbWin.Focus(); } } GUILayout.BeginVertical(GUILayout.Width(_thumbnailSize), GUILayout.Height(50)); GUILayout.Label(model.name, _ui.sketchfabMiniModelname); GUILayout.Label("by " + model.author, _ui.sketchfabMiniAuthorname); GUILayout.EndVertical(); GUILayout.EndVertical(); }
void handleSearch(string response) { JSONNode json = Utils.JSONParse(response); JSONArray array = json["results"].AsArray; if (array == null) { return; } if (json["cursors"] != null) { if (json["cursors"]["next"].AsInt == 24) { _prevCursor = ""; } else if (_nextCursor != "null" && _nextCursor != "") { _prevCursor = int.Parse(_nextCursor) - 24 + ""; } _nextCursor = json["cursors"]["next"]; } // First model fetch from uid foreach (JSONNode node in array) { _resultUids.Add(node["uid"]); if (!_sketchfabModels.ContainsKey(node["uid"])) { // Add model to results SketchfabModel model = new SketchfabModel(node, _defaultThumbnail); model.previewUrl = getThumbnailUrl(node, 768); _sketchfabModels.Add(node["uid"], model); // Request model thumbnail SketchfabRequest request = new SketchfabRequest(getThumbnailUrl(node)); request.setCallback(handleThumbnail); _api.registerRequest(request); } } _isFetching = false; Refresh(); }
private void OnGUI() { if (_currentModel != null && show) { _scrollView = GUILayout.BeginScrollView(_scrollView); SketchfabModel model = _currentModel; GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); _ui.displayModelName(model.name); _ui.displayContent("by " + model.author); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); if (GUILayout.Button("View on Sketchfab")) { Application.OpenURL(SketchfabPlugin.Urls.modelUrl + "/" + _currentModel.uid); } GUILayout.EndHorizontal(); GUIStyle blackGround = new GUIStyle(GUI.skin.box); blackGround.normal.background = SketchfabUI.MakeTex(2, 2, new Color(0f, 0f, 0f, 1f)); GUILayout.BeginHorizontal(blackGround); GUILayout.FlexibleSpace(); if (PlayerSettings.colorSpace == ColorSpace.Linear) { bool backup = GL.sRGBWrite; GL.sRGBWrite = true; GUILayout.Label(model._preview); GL.sRGBWrite = backup; } else { GUILayout.Label(model._preview); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); displayImportSettings(); GUILayout.Label(""); GUILayout.BeginHorizontal(); GUILayout.BeginVertical(GUILayout.Width(250)); _ui.displayTitle("MODEL INFORMATION"); _ui.displayModelStats("Vertex count", " " + Utils.humanifySize(model.vertexCount)); _ui.displayModelStats("Face count", " " + Utils.humanifySize(model.faceCount)); if (model.hasAnimation != "") { _ui.displayModelStats("Animation", model.hasAnimation); } GUILayout.EndVertical(); GUILayout.BeginVertical(GUILayout.Width(300)); _ui.displayTitle("LICENSE"); if (model.licenseJson != null) { _ui.displayContent(model.licenseJson["fullName"]); _ui.displaySubContent(model.licenseJson["requirements"]); } else { _ui.displaySubContent("Fetching license data"); } GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.EndScrollView(); } }
private void OnGUI() { if (_currentModel != null && show) { _scrollView = GUILayout.BeginScrollView(_scrollView); SketchfabModel model = _currentModel; // Model name, author, view On Sketchfab bloc GUILayout.BeginHorizontal(); { GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); { // Name GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label(model.name, _ui.getSketchfabModelName()); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); // Author GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label(model.author, _ui.getSketchfabContentLabel()); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); // View on Sketchfab GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUIContent viewSkfb = new GUIContent("View on Sketchfab", _ui.SKETCHFAB_ICON); if (GUILayout.Button(viewSkfb, GUILayout.Height(24), GUILayout.Width(140))) { Application.OpenURL(SketchfabPlugin.Urls.modelUrl + "/" + _currentModel.uid); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } GUILayout.EndVertical(); GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); // Model preview GUILayout.BeginHorizontal(); { GUILayout.Space(4); GUILayout.FlexibleSpace(); GUILayout.Label(model._preview); GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); // Import settings GUILayout.BeginHorizontal(); { displayImportSettings(); } GUILayout.EndHorizontal(); // Model info title GUILayout.BeginHorizontal(); { GUILayout.FlexibleSpace(); _ui.displayTitle("ABOUT THE MODEL"); GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); // Model info data GUILayout.BeginHorizontal(); { GUILayout.BeginVertical(); { if (model.licenseJson != null && model.licenseJson["label"] != null) { GUILayout.BeginHorizontal(); { GUILayout.BeginVertical(); // License label GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label(model.licenseJson["label"], EditorStyles.boldLabel); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); // License detail GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Label(model.formattedLicenseRequirements, EditorStyles.miniLabel); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.FlexibleSpace(); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); } else if (model.vertexCount != 0) { _ui.displayContent("Personal"); _ui.displaySubContent("You own this model"); } else { _ui.displaySubContent("Fetching license data"); } } GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); { _ui.displayModelStats(" Vertex count", " " + Utils.humanifySize(model.vertexCount)); _ui.displayModelStats(" Face count", " " + Utils.humanifySize(model.faceCount)); if (model.hasAnimation != "") { _ui.displayModelStats(" Animation", model.hasAnimation); } GUILayout.FlexibleSpace(); } GUILayout.EndVertical(); GUILayout.Space(20); } GUILayout.EndHorizontal(); GUILayout.EndScrollView(); } }
void handleThumbnail(UnityWebRequest request) { bool sRGBBackup = GL.sRGBWrite; GL.sRGBWrite = true; string uid = extractUidFromUrl(request.url); if (!isInModels(uid)) { return; } // Load thumbnail image byte[] data = request.downloadHandler.data; Texture2D thumb = new Texture2D(2, 2); thumb.LoadImage(data); if (thumb.width >= _previewWidth) { var renderTexture = RenderTexture.GetTemporary(_previewWidth, (int)(_previewWidth * _previewRatio), 24); var exportTexture = new Texture2D(thumb.height, thumb.height, TextureFormat.ARGB32, false); #if UNITY_5_6 || UNITY_2017 if (PlayerSettings.colorSpace == ColorSpace.Linear) { Material linear2SRGB = new Material(Shader.Find("GLTF/Linear2sRGB")); linear2SRGB.SetTexture("_InputTex", thumb); Graphics.Blit(thumb, renderTexture, linear2SRGB); } else { Graphics.Blit(thumb, renderTexture); } #else Graphics.Blit(thumb, renderTexture); #endif exportTexture.ReadPixels(new Rect((thumb.width - thumb.height) / 2, 0, renderTexture.height, renderTexture.height), 0, 0); exportTexture.Apply(); TextureScale.Bilinear(thumb, _previewWidth, (int)(_previewWidth * _previewRatio)); SketchfabModel model = _sketchfabModels[uid] as SketchfabModel; model._preview = thumb; _sketchfabModels[uid] = model; } else { // Crop it to square var renderTexture = RenderTexture.GetTemporary(thumb.width, thumb.height, 24); var exportTexture = new Texture2D(thumb.height, thumb.height, TextureFormat.ARGB32, false); #if UNITY_5_6 || UNITY_2017 if (PlayerSettings.colorSpace == ColorSpace.Linear) { Material linear2SRGB = new Material(Shader.Find("GLTF/Linear2sRGB")); linear2SRGB.SetTexture("_InputTex", thumb); Graphics.Blit(thumb, renderTexture, linear2SRGB); } else { Graphics.Blit(thumb, renderTexture); } #else Graphics.Blit(thumb, renderTexture); #endif exportTexture.ReadPixels(new Rect((thumb.width - thumb.height) / 2, 0, renderTexture.height, renderTexture.height), 0, 0); exportTexture.Apply(); TextureScale.Bilinear(exportTexture, _thumbnailSize, _thumbnailSize); SketchfabModel model = _sketchfabModels[uid] as SketchfabModel; model._thumbnail = exportTexture; _sketchfabModels[uid] = model; } GL.sRGBWrite = sRGBBackup; Refresh(); }
private void OnGUI() { if (_currentModel != null && show) { _scrollView = GUILayout.BeginScrollView(_scrollView); SketchfabModel model = _currentModel; GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); _ui.displayModelName(model.name); _ui.displayContent("by " + model.author); GUILayout.BeginHorizontal(); GUIContent viewSkfb = new GUIContent("View on Sketchfab", _ui.SKETCHFAB_ICON); if (GUILayout.Button(viewSkfb, GUILayout.Height(24), GUILayout.Width(140))) { Application.OpenURL(SketchfabPlugin.Urls.modelUrl + "/" + _currentModel.uid); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUIStyle blackGround = new GUIStyle(GUI.skin.box); blackGround.normal.background = SketchfabUI.MakeTex(2, 2, new Color(0f, 0f, 0f, 1f)); GUILayout.BeginHorizontal(blackGround); GUILayout.FlexibleSpace(); GUILayout.Label(model._preview); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); displayImportSettings(); GUILayout.Label(""); GUILayout.BeginHorizontal(); GUILayout.BeginVertical(GUILayout.Width(250)); _ui.displayTitle("MODEL INFORMATION"); _ui.displayModelStats("Vertex count", " " + Utils.humanifySize(model.vertexCount)); _ui.displayModelStats("Face count", " " + Utils.humanifySize(model.faceCount)); if (model.hasAnimation != "") { _ui.displayModelStats("Animation", model.hasAnimation); } GUILayout.EndVertical(); GUILayout.BeginVertical(GUILayout.Width(300)); _ui.displayTitle("LICENSE"); if (model.licenseJson != null && model.licenseJson["fullName"] != null) { _ui.displayContent(model.licenseJson["fullName"]); _ui.displaySubContent(model.licenseJson["requirements"]); } else if (model.vertexCount != 0) { _ui.displayContent("Personal"); _ui.displaySubContent("You own this model"); } else { _ui.displaySubContent("Fetching license data"); } GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.EndScrollView(); } }