using System; using System.Collections.Generic; using UnityEngine; public class MaterialManager : MonoBehaviour { public Texture2D groutTexture; // Текстура для затирки public Texture2D tileTexture; // Текстура для плитки public Texture2D normalTileMap; // Нормальная карта для плитки public string materialType; // Тип материала, например "polish" public float tileWidth = 1.0f; // Ширина плитки в метрах public float tileHeight = 1.0f; // Высота плитки в метрах public float groutWidth = 0.05f; // Ширина шва в метрах private float wallWidth; // Ширина стены в метрах private float wallHeight; // Высота стены в метрах void Start() { // Получаем размеры стены Renderer wallRenderer = GetComponent(); if (wallRenderer != null) { wallWidth = wallRenderer.bounds.size.x; wallHeight = wallRenderer.bounds.size.y; } else { Debug.LogError("Renderer not found on the wall object."); return; } Material material1 = new Material(Shader.Find("Universal Render Pipeline/Lit")); material1.mainTexture = groutTexture; material1.SetFloat("_Smoothness", 1.0f); material1.SetFloat("_Surface", 1); material1.SetFloat("_Blend", 0); material1.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.SrcAlpha); material1.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material1.SetFloat("_ZWrite", 0); material1.DisableKeyword("_ALPHATEST_ON"); material1.EnableKeyword("_ALPHABLEND_ON"); material1.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material1.renderQueue = 3000; Material material2 = new Material(Shader.Find("Universal Render Pipeline/Lit")); material2.mainTexture = tileTexture; material2.SetTexture("_BumpMap", normalTileMap); material2.SetFloat("_EmissionScaleUI", 1.0f); material2.SetColor("_EmissionColor", Color.white * 1.0f); material2.EnableKeyword("_EMISSION"); material2.SetFloat("_Smoothness", materialType == "polish" ? 0.3f : 1.0f); // Adjust smoothness based on material type material2.SetFloat("_Metallic", 0.2f); Mesh mesh = new Mesh(); int tilesX = Mathf.FloorToInt(wallWidth / (tileWidth + groutWidth)); int tilesY = Mathf.FloorToInt(wallHeight / (tileHeight + groutWidth)); Vector3[] vertices = new Vector3[(tilesX + 1) * (tilesY + 1) * 4]; Vector2[] uvs = new Vector2[vertices.Length]; int[] triangles = new int[tilesX * tilesY * 6]; int v = 0, t = 0; for (int y = 0; y < tilesY; y++) { for (int x = 0; x < tilesX; x++) { Vector3 bottomLeft = new Vector3(x * (tileWidth + groutWidth), y * (tileHeight + groutWidth), 0); Vector3 bottomRight = bottomLeft + new Vector3(tileWidth, 0, 0); Vector3 topLeft = bottomLeft + new Vector3(0, tileHeight, 0); Vector3 topRight = bottomLeft + new Vector3(tileWidth, tileHeight, 0); vertices[v] = bottomLeft; vertices[v + 1] = bottomRight; vertices[v + 2] = topLeft; vertices[v + 3] = topRight; uvs[v] = new Vector2(0, 0); uvs[v + 1] = new Vector2(1, 0); uvs[v + 2] = new Vector2(0, 1); uvs[v + 3] = new Vector2(1, 1); triangles[t] = v; triangles[t + 1] = v + 2; triangles[t + 2] = v + 1; triangles[t + 3] = v + 1; triangles[t + 4] = v + 2; triangles[t + 5] = v + 3; t += 6; v += 4; } } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); GetComponent().mesh = mesh; Material[] materials = new Material[2]; materials[0] = material1; materials[1] = material2; GetComponent().materials = materials; } }