// Copyright | Raumgleiter AG | 2018 | All Rights Reserved //*********************************************************************************************************************** //Headers using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEditor; using Raumgleiter.BaseRGDK; using Raumgleiter.BaseRGDK.JSON; namespace Raumgleiter.RGDKDevLab { public class MtexDownload : MonoBehaviour { /// web related //public static string mtexturUrl = "https://www.mtextur.com/api/v1/"; //private string mtexturUrlStaging = "https://staging.mtextur.ch/api/v1/"; /// assign in Unity public Dropdown manDd, matDd, sizeDd, typeDd, colorDd; public RawImage Rimage; public GameObject cube; public Slider mainSlider; public Button saveMaterialButton, startButton; public Text selectedManufacturer, selectedMaterial, materialInfo, selectedType, selectedColor, numberOfMaterials; private int selectedManufacturerIx, selectedMaterialIx; /// Manufacturer and Mateirial lists public List MatClassList; public List ManClassList; public List TypeClassList; public List ColorClassList; /// Matrial and Texture private Material newMaterial; private Vector2 materialTiling = new Vector2(1f, 1f); private Texture newTexture, newBump, newNormal; private Dictionary urlVariabels = new Dictionary() { { "manufacturer_id ", 999999}, { "type_id", 999999 }, { "color_id", 999999} }; private int materialCount; // create an instance of the class and invoke the method on it // should be a singleton MtexDownloadFunctions mtexDownloadFunctions = new MtexDownloadFunctions(); void Start() { // start coroutines to get all manufacturers, types and colors startButton.onClick.AddListener(delegate {StartMtexDownloader(); }); // start "bake" button and scale slider mainSlider.onValueChanged.AddListener(delegate { SubmitSliderSetting(); }); manDd.onValueChanged.AddListener(delegate { ManDdValueChanged(manDd); }); matDd.onValueChanged.AddListener(delegate { MatDdValueChanged(matDd); }); typeDd.onValueChanged.AddListener(delegate { TypeDdValueChanged(typeDd); }); colorDd.onValueChanged.AddListener(delegate { ColorDdValueChanged(colorDd); }); sizeDd.onValueChanged.AddListener(delegate { SizeDdValueChanged(sizeDd); }); saveMaterialButton.onClick.AddListener(delegate { SaveMaterial(); }); } void StartMtexDownloader() { StartCoroutine(GetManufacturers()); StartCoroutine(GetTypes()); StartCoroutine(GetColors()); } void Update() { // no updates on per screen basis } private void ManDdValueChanged(Dropdown change) { // add manufacturer_id to url string if (change.value != 0) { urlVariabels["manufacturer_id"] = ManClassList[change.value - 1].id; selectedManufacturer.text = ManClassList[change.value - 1].name; } else { urlVariabels["manufacturer_id"] = 999999; selectedManufacturer.text = "-- Selecte Manufacturer --"; } StartCoroutine(GetMaterials(urlVariabels)); } private void TypeDdValueChanged(Dropdown change) { // add type_id to url string if (change.value != 0) { urlVariabels["type_id"] = TypeClassList[change.value - 1].id; selectedType.text = TypeClassList[change.value - 1].name; } else { urlVariabels["type_id"] = 999999; selectedType.text = "-- Selecte Type --"; } StartCoroutine(GetMaterials(urlVariabels)); } private void ColorDdValueChanged(Dropdown change) { // add type_id to url string if (colorDd.value != 0) { urlVariabels["color_id"] = ColorClassList[change.value - 1].id; selectedColor.text = ColorClassList[change.value - 1].name; } else { urlVariabels["color_id"] = 999999; selectedColor.text = "-- Selecte Color --"; } StartCoroutine(GetMaterials(urlVariabels)); } private void MatDdValueChanged(Dropdown change) { if (change.value != 0) { // get selected material and start coroutine to get material selectedMaterialIx = change.value - 1; StartCoroutine(GetMaterial(MatClassList[selectedMaterialIx].id)); } } private void SizeDdValueChanged(Dropdown change) { if (sizeDd.value != 0) { var selectedSize = sizeDd.options[sizeDd.value].text; Debug.Log("MtexDownload::SizeDdValueChanged: selected size: " + selectedSize); if (sizeDd.value != 0) { StartCoroutine(GetMaterial(MatClassList[selectedMaterialIx].id, selectedSize)); } } } private void SubmitSliderSetting() { // check if material already selected if (newTexture != null) { // get slider value var scaleFactor = mainSlider.value; // change material tiling according to slider materialTiling = mtexDownloadFunctions.MaterialTiling(newTexture, scaleFactor); newMaterial.mainTextureScale = materialTiling; // UpdateMaterialPreview(); } } private IEnumerator GetManufacturers() { // get types from web yield return StartCoroutine(mtexDownloadFunctions.GetManufacturers(value => ManClassList = value)); //populate dropdown var manList = MtexManufacturer.ManufacturersList(ManClassList); // populate selected dropdown with selected list PopulateDropdown(manDd, manList); } private IEnumerator GetTypes() { // get types from web yield return StartCoroutine(mtexDownloadFunctions.GetTypes(value => TypeClassList = value)); // make list from class var typeList = MtexType.TypesList(TypeClassList); // populate selected dropdown with selected list PopulateDropdown(typeDd, typeList); } private IEnumerator GetColors() { // get types from web yield return StartCoroutine(mtexDownloadFunctions.GetColors(value => ColorClassList = value)); // make list from class var colorsList = MtexColor.ColorsList(ColorClassList); // populate selected dropdown with selected list PopulateDropdown(colorDd, colorsList); } private IEnumerator GetMaterials(Dictionary urlVariabels) { var materialsUrl = MtexDownloadFunctions.BuildUrl(urlVariabels); yield return mtexDownloadFunctions.GetMaterials(materialsUrl, value => MatClassList = value); var matList = MtexMaterialBasic.MaterialList(MatClassList); PopulateMaterialDropdown(); matDd.value = 0; numberOfMaterials.text = "total materials: " + matList.Count.ToString(); } private void PopulateMaterialDropdown() { //populate dropdown matDd.ClearOptions(); matDd.options.Add(new Dropdown.OptionData("-- select material --")); foreach (var material in MatClassList) matDd.options.Add(new Dropdown.OptionData(material.id + "-" + material.name)); } private IEnumerator GetMaterial(int matId, string textureSize = "small") { // create empty json JSONNode json = JSONNode.Parse(""); // get selected material info and download textures yield return mtexDownloadFunctions.GetMaterial(matId, value => json = value); yield return mtexDownloadFunctions.GetMainTexture(json, value => newTexture = value, textureSize); yield return mtexDownloadFunctions.GetBumpTexture(json, value => newBump = value, textureSize); // check for bump - if there is a bump, convert to normal, else clear normal if (newBump != null) { newNormal = TextureHelper.BumpToNormalMap((Texture2D)newBump); } else { newNormal = null; } // create material and update preview newMaterial = MtexDownloadFunctions.CreateMaterial((Texture2D)newTexture,(Texture2D)newNormal, selectedMaterial.text); UpdateMaterialPreview(); // create material description materialInfo.text = mtexDownloadFunctions.MaterialDescription(json, newTexture); //populate size dropdown var textureSizes = MtexDownloadFunctions.GetTextureSizes(json); PopulateDropdown(sizeDd, textureSizes); } private void PopulateDropdown(Dropdown dropdown, List someList) { dropdown.ClearOptions(); dropdown.options.Add(new Dropdown.OptionData("alle")); dropdown.AddOptions(someList); } private void UpdateMaterialPreview() { cube.GetComponent().material = newMaterial; Rimage.texture = newTexture; } private void SaveMaterial() { // check if a material is selected if (matDd.value != 0) { // create material name from dropdown selection var materialName = MatClassList[selectedMaterialIx].id + "-" + selectedMaterial.text; // recheck for textrue resizing SubmitSliderSetting(); //save material MtexDownloadFunctions.SaveMaterial(newMaterial, materialName, (Texture2D)newTexture, (Texture2D)newNormal); } else { Debug.Log("Select material first."); } } } }