public StyleCompileContext(MaterialDatabase materialDatabase) { this.materialDatabase = materialDatabase; this.importedStyleConstants = new Dictionary <string, LightList <StyleConstant> >(); this.constantsWithReferences = new Dictionary <string, StyleConstant>(); this.constants = new LightList <StyleConstant>(); }
private void Awake() { inspectorManager = inspectorArea.GetComponent <InspectorManager>(); previewCamera.GetComponent <FlyCameraController>().enabled = true; inspectorCamera.GetComponent <FlyCameraController>().enabled = false; materialDatabase = GetComponent <MaterialDatabase>(); plantDatabase = GetComponent <PlantDatabase>(); }
public void RecreateSprite() { Vector3[] pos = new Vector3[4]; Vector3[] norm = new Vector3[4]; Vector2[] uv = new Vector2[4]; int[] offsetsX = { 0, 0, 1, 1 }; int[] offsetsY = { 1, 0, 0, 1 }; float normalizedX = rect.x / spriteSheet.width; float normalizedY = rect.y / spriteSheet.height; float normalizedW = rect.width / spriteSheet.width; float normalizedH = rect.height / spriteSheet.height; for (int i = 0; i < 4; ++i) { pos[i].Set(offsetsX[i] * rect.width, offsetsY[i] * rect.height, 0); norm[i].Set(0, 0, -1); //color[vertex] = new Color32(255, 255, 255, 255); uv[i].Set(normalizedX + offsetsX[i] * normalizedW, normalizedY + offsetsY[i] * normalizedH); } MeshFilter mf = GetComponent <MeshFilter>(); if (mf == null) { mf = gameObject.AddComponent <MeshFilter>(); } DestroyImmediate(mf.sharedMesh); mf.sharedMesh = new Mesh(); mf.sharedMesh.vertices = pos; mf.sharedMesh.normals = norm; mf.sharedMesh.uv = uv; mf.sharedMesh.triangles = new int[] { 0, 3, 1, 1, 3, 2 }; mf.sharedMesh.RecalculateBounds(); MeshRenderer mr = GetComponent <MeshRenderer>(); if (mr == null) { mr = gameObject.AddComponent <MeshRenderer>(); mr.sharedMaterial = MaterialDatabase.Get(spriteSheet); } if (mr.sharedMaterial.mainTexture != spriteSheet) { MaterialDatabase.Unload(mr.sharedMaterial.mainTexture as Texture2D); mr.sharedMaterial = MaterialDatabase.Get(spriteSheet); } }
private static MaterialInfo?SearchDatabase(MaterialDatabase database, string name) { foreach (MaterialInfo info in database.materials) { if (info.name == name) { return(info); } } return(null); }
public static CompiledTemplateData LoadPrecompiledTemplates(TemplateSettings templateSettings) { Assembly assembly = AppDomain.CurrentDomain.GetAssemblyByName(templateSettings.assemblyName); Type type = assembly.GetType("UIForia.Generated.UIForiaGeneratedTemplates_" + templateSettings.StrippedApplicationName); if (type == null) { throw new ArgumentException("Trying to use precompiled templates for " + templateSettings.StrippedApplicationName + " but couldn't find the type. Maybe you need to regenerate the code?"); } CompiledTemplateData compiledTemplateData = new CompiledTemplateData(templateSettings); compiledTemplateData.styleImporter.importResolutionPath = Path.Combine(UnityEngine.Application.streamingAssetsPath, "UIForia", compiledTemplateData.templateSettings.StrippedApplicationName); ITemplateLoader loader = (ITemplateLoader)Activator.CreateInstance(type); string[] files = loader.StyleFilePaths; compiledTemplateData.styleImporter.Reset(); // reset because in testing we will already have parsed files, nuke these LightList <UIStyleGroupContainer> styleList = new LightList <UIStyleGroupContainer>(128); Dictionary <string, StyleSheet> styleSheetMap = new Dictionary <string, StyleSheet>(128); MaterialDatabase materialDatabase = loader.GetMaterialDatabase(); for (int i = 0; i < files.Length; i++) { StyleSheet sheet = compiledTemplateData.styleImporter.ImportStyleSheetFromFile(files[i], materialDatabase); styleList.EnsureAdditionalCapacity(sheet.styleGroupContainers.Length); for (int j = 0; j < sheet.styleGroupContainers.Length; j++) { styleList.array[styleList.size++] = sheet.styleGroupContainers[j]; } styleSheetMap.Add(sheet.path, sheet); } compiledTemplateData.templates = loader.LoadTemplates(); compiledTemplateData.slots = loader.LoadSlots(); compiledTemplateData.bindings = loader.LoadBindings(); compiledTemplateData.templateMetaData = loader.LoadTemplateMetaData(styleSheetMap, styleList.array); for (int i = 0; i < compiledTemplateData.templateMetaData.Length; i++) { compiledTemplateData.templateMetaData[i].compiledTemplateData = compiledTemplateData; } compiledTemplateData.constructElement = loader.ConstructElement; compiledTemplateData.dynamicTemplates = loader.DynamicTemplates; return(compiledTemplateData); }
public StyleCompileContext CreateContext(LightList <StyleASTNode> rootNodes, MaterialDatabase materialDatabase) { StyleCompileContext context = new StyleCompileContext(materialDatabase); // first all imports must be collected as they can be referenced in exports and consts for (int i = 0; i < rootNodes.size; i++) { switch (rootNodes[i]) { case ImportNode importNode: StyleSheet importedStyle = styleSheetImporter.ImportStyleSheetFromFile(importNode.source, materialDatabase); LightList <StyleConstant> importedStyleConstants = new LightList <StyleConstant>(importedStyle.constants.Length); for (int constantIndex = 0; constantIndex < importedStyle.constants.Length; constantIndex++) { StyleConstant importedStyleConstant = importedStyle.constants[constantIndex]; if (importedStyleConstant.exported) { importedStyleConstants.Add(importedStyleConstant); } } context.importedStyleConstants.Add(importNode.alias, importedStyleConstants); break; } } // collect all constants that could be referenced for (int index = 0; index < rootNodes.size; index++) { switch (rootNodes[index]) { case ExportNode exportNode: TransformConstNode(context, exportNode.constNode, true); break; case ConstNode constNode: TransformConstNode(context, constNode, false); break; } } ResolveConstantReferences(context); context.constantsWithReferences.Clear(); return(context); }
public override void WriteMap(BrushMap map) { Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine(" Urho3D Prefab Writer"); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("Building material database"); MaterialDatabase matDb = new MaterialDatabase(Settings.ContentDir); Scene scene = new Scene("node"); Node entRoot = scene.CreateChild("Entities"); // Write 'void' entity nodes using (QMapConverter.Util.ConsoleProgress prog = new QMapConverter.Util.ConsoleProgress("Writing entities", map.Entities.Count)) for (int i = 0; i < map.Entities.Count; ++i) { prog.Increment(); prog.Write(); Entity entity = map.Entities[i]; if (entity.Brushes.Count == 0) { Node brushNode = entRoot.CreateChild(); brushNode.WriteVariables(entity.Properties); } } // Write world geometry elements Node geoNode = scene.CreateChild("Geometry"); string outputPath = System.IO.Path.Combine(Settings.ContentDir, "Data"); outputPath = System.IO.Path.Combine(outputPath, "Models"); outputPath = System.IO.Path.Combine(outputPath, System.IO.Path.GetFileNameWithoutExtension(outputFile)); outputPath = System.IO.Path.Combine(outputPath, "geo.mdl"); SceneBuilder sb = new SceneBuilder(map, QMapConverter.Settings.CellSize); List<string> materials = new List<string>(); sb.WriteModel(outputPath, materials); using (QMapConverter.Util.ConsoleProgress prog = new QMapConverter.Util.ConsoleProgress("Writing geometry", map.Entities.Count)) { Component staticModel = geoNode.CreateComponent("StaticModel"); string relPath = outputPath.Replace(Settings.ContentDir + "\\", "").Replace("\\","/"); staticModel.SetAttribute("Model", String.Format("Model;{0}", relPath)); StringBuilder matString = new StringBuilder(); foreach (string m in materials) { string matFile = matDb.GetMaterial(m); if (matFile != null && matFile.Length > 0) { if (matString.Length > 0) matString.AppendFormat(";Material;{0}", matFile); else matString.AppendFormat("Material;{0}", matFile); } } if (matString.Length > 0) staticModel.SetAttribute("Material", matString.ToString()); } using (QMapConverter.Util.ConsoleProgress prog = new QMapConverter.Util.ConsoleProgress("Writing file", map.Entities.Count)) scene.Save(outputFile); Console.WriteLine("File written: " + outputFile); }
private async void DeleteExecute() { await MaterialDatabase.DeleteMaterial(Material); await Application.Current.MainPage.Navigation.PopAsync(); }
public static void UpdateMaterials() { MaterialDatabase database = ScriptableObject.CreateInstance <MaterialDatabase>(); MaterialDatabase data_override = (MaterialDatabase)AssetDatabase.LoadAssetAtPath( "Assets/Resources/materials_override.asset", typeof(MaterialDatabase)); string[] guids = AssetDatabase.FindAssets("", new string[] { SEARCH_PATH }); foreach (string guid in guids) { string fullPath = AssetDatabase.GUIDToAssetPath(guid); Material material = AssetDatabase.LoadAssetAtPath <Material>(fullPath); MaterialInfo info; info.path = Path.ChangeExtension(fullPath.Substring(SEARCH_PATH.Length + 1), null); info.name = Path.GetFileName(info.path); if (info.path.Length <= info.name.Length) { info.parent = ""; } else { info.parent = info.path.Substring(0, info.path.Length - info.name.Length - 1); } info.isDirectory = material == null; info.sound = MaterialSound.GENERIC; if (material != null) { foreach (string label in AssetDatabase.GetLabels(material)) { if (label.StartsWith(SOUND_LABEL_PREFIX)) { string soundName = label.Replace(SOUND_LABEL_PREFIX, "").ToUpper(); info.sound = (MaterialSound)System.Enum.Parse(typeof(MaterialSound), soundName); } } } info.whitePoint = Color.white; // color styles don't work for cutout overlays info.supportsColorStyles = material == null ? false : material.mainTexture != null && material.renderQueue != (int)UnityEngine.Rendering.RenderQueue.AlphaTest; MaterialInfo?mat_override_maybe = SearchDatabase(data_override, info.name); if (mat_override_maybe != null) { MaterialInfo mat_override = mat_override_maybe.Value; if (mat_override.whitePoint != Color.clear) { info.whitePoint = mat_override.whitePoint * 0.8f; } } database.materials.Add(info); } AssetDatabase.CreateAsset(database, "Assets/Resources/materials.asset"); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Selection.activeObject = database; Resources.UnloadUnusedAssets(); Debug.Log("done!"); }
protected void OnDisable() { MaterialDatabase.Unload(spriteSheet); _allSprite.Remove(this); }
void OnDestroy() { MaterialDatabase.Unload(spriteSheet); }
public void MakeTilemap() { if (indexes == null || indexes.Length < _internalHeight * _internalWidth) { BuildMap(); } MeshFilter mf = GetComponent <MeshFilter>(); if (mf.sharedMesh == null) { mf.sharedMesh = new Mesh(); } Vector3[] positions = new Vector3[_internalWidth * _internalHeight * 4]; Vector2[] uv = new Vector2[_internalWidth * _internalHeight * 4]; Vector3[] normals = new Vector3[_internalWidth * _internalHeight * 4]; Color32[] color = new Color32[_internalWidth * _internalHeight * 4]; int[] triangles = new int[_internalWidth * _internalHeight * 6]; float normalizedW = tileSize / (float)spriteSheet.width; float normalizedH = tileSize / (float)spriteSheet.height; int nbTileLine = spriteSheet.width / tileSize; for (int i = 0; i < _internalWidth; ++i) { for (int j = 0; j < _internalHeight; ++j) { int idx = (i * _internalHeight + j); for (int k = 0; k < 4; ++k) { int[] offsetsX = { 0, 0, 1, 1 }; int[] offsetsY = { 0, 1, 1, 0 }; int vertex = idx * 4 + k; positions[vertex].Set(i * tileSize + offsetsX[k] * tileSize, j * tileSize + offsetsY[k] * tileSize, 0); int line = indexes[i * height + j] / nbTileLine; int col = indexes[i * height + j] - line * nbTileLine; normals[vertex].Set(0, 0, -1); if (indexes[i * _internalHeight + j] == -1) { color[vertex] = new Color32(0, 0, 0, 0); uv[vertex].Set(0, 0); } else { color[vertex] = new Color32(255, 255, 255, 255); uv[vertex].Set(col * normalizedW + offsetsX[k] * normalizedW, line * normalizedH + offsetsY[k] * normalizedH); } } triangles[idx * 6 + 0] = idx * 4; triangles[idx * 6 + 1] = idx * 4 + 1; triangles[idx * 6 + 2] = idx * 4 + 3; triangles[idx * 6 + 3] = idx * 4 + 1; triangles[idx * 6 + 4] = idx * 4 + 2; triangles[idx * 6 + 5] = idx * 4 + 3; } } mf.sharedMesh.Clear(); mf.sharedMesh.vertices = positions; mf.sharedMesh.uv = uv; mf.sharedMesh.normals = normals; mf.sharedMesh.colors32 = color; mf.sharedMesh.triangles = triangles; MeshRenderer mr = GetComponent <MeshRenderer>(); if (mr.sharedMaterial != null) { MaterialDatabase.Unload(mr.sharedMaterial.mainTexture as Texture2D); } mr.sharedMaterial = MaterialDatabase.Get(spriteSheet); }
// public StyleSheet Compile(string filePath, string contents) { // return Compile(filePath, StyleParser.Parse(contents)); // } // todo -- deprecate, use other method public StyleSheet Compile(string filePath, LightList <StyleASTNode> rootNodes, MaterialDatabase materialDatabase = default) { try { context = new StyleSheetConstantImporter(styleSheetImporter).CreateContext(rootNodes, materialDatabase); context.resourceManager = resourceManager; // context = new StyleCompileContext(); // todo resolve constants. should be done a per file level, should store all used constants without needing to later reference other files // StyleCompileContext.Create(styleSheetImporter) //new StyleSheetConstantImporter(styleSheetImporter).CreateContext(rootNodes); } catch (CompileException e) { e.SetFileName(filePath); throw; } context.fileName = filePath; // todo add imported style groups rootNodes.Sort((node1, node2) => { int left = (int)node1.type; int right = (int)node2.type; return(left - right); }); int containerCount = 0; int animationCount = 0; int soundCount = 0; for (int index = 0; index < rootNodes.Count; index++) { switch (rootNodes[index]) { case StyleRootNode _: containerCount++; break; case AnimationRootNode _: case SpriteSheetNode _: animationCount++; break; case SoundRootNode _: soundCount++; break; } } StyleSheet styleSheet = new StyleSheet( styleSheetImporter.ImportedStyleSheetCount, context.constants?.ToArray(), containerCount > 0 ? new UIStyleGroupContainer[containerCount] : ArrayPool <UIStyleGroupContainer> .Empty, animationCount > 0 ? new AnimationData[animationCount] : ArrayPool <AnimationData> .Empty, soundCount > 0 ? new UISoundData[soundCount] : ArrayPool <UISoundData> .Empty ); int containerIndex = 0; int animationIndex = 0; int soundIndex = 0; for (int index = 0; index < rootNodes.Count; index++) { switch (rootNodes[index]) { // we sorted the root nodes so all animations run first case SpriteSheetNode spriteSheetNode: styleSheet.animations[animationIndex] = CompileSpriteSheetAnimation(spriteSheetNode, styleSheet.animations, styleSheet.sounds); animationIndex++; break; case AnimationRootNode animNode: styleSheet.animations[animationIndex] = CompileAnimation(animNode, styleSheet.animations, styleSheet.sounds); animationIndex++; break; case SoundRootNode soundRootNode: styleSheet.sounds[soundIndex] = CompileSound(soundRootNode); soundIndex++; break; case StyleRootNode styleRoot: styleSheet.styleGroupContainers[containerIndex] = CompileStyleGroup(styleRoot, styleSheet.animations, styleSheet.sounds); styleSheet.styleGroupContainers[containerIndex].styleSheet = styleSheet; containerIndex++; break; } } context.Release(); return(styleSheet); }
private async void GetAllMaterials() { MaterialsList = await MaterialDatabase.GetAllMaterials(); }