Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add two different textures/mats

Discussion in 'Scripting' started by Deleted User, Mar 22, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hey,
    I'm trying to modify the code of http://studentgamedev.blogspot.de/2013/08/VoxelTutP2.html.
    Only onte texture will be applied to the squares and i don't know how to add two seperate textures unlike he does in the tutorial (with one texture on which is every texute)

    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ExampleClass : MonoBehaviour {
    6.  
    7.     private List<Vector3> newVertices = new List<Vector3>();
    8.     private List<int> newTriangles = new List<int>();
    9.     private byte[,] blocks;
    10.     private int squarecount;
    11.     private Mesh mesh;
    12.  
    13.     public Material dirtMat;
    14.     public Material stoneMat;
    15.  
    16.     void Start () {
    17.         GenMap();
    18.         BuildMesh();
    19.         UpdateMesh();
    20.     }
    21.  
    22.     void GenMap()
    23.     {
    24.         blocks = new byte[5,10];
    25.  
    26.         for (int x = 0; x < blocks.GetLength(0); x++)
    27.         {
    28.             for(int y = 0; y < blocks.GetLength(1); y++)
    29.             {
    30.                 if (y == 3)
    31.                 {
    32.                         blocks[x, y] = 2;
    33.                 }
    34.                 else if (y > 3)
    35.                 {
    36.                     blocks[x, y] = 1;
    37.                 }
    38.             }
    39.         }
    40.     }
    41.  
    42.     void BuildMesh()
    43.     {
    44.         for (int x = 0; x < blocks.GetLength(0); x++)
    45.         {
    46.             for (int y = 0; y < blocks.GetLength(1); y++)
    47.             {
    48.                 if (blocks[x, y] != 0)
    49.                 {
    50.                     if (blocks[x, y] == 1)
    51.                     {
    52.                         GenSquare(x, y,stoneMat);
    53.                     }
    54.                     else if (blocks[x, y] == 2)
    55.                     {
    56.                         GenSquare(x, y, dirtMat);
    57.                     }
    58.                 }
    59.             }
    60.         }
    61.     }
    62.  
    63.     void GenSquare(int x, int y, Material mat)
    64.     {
    65.         mesh = GetComponent<MeshFilter>().mesh;
    66.         MeshRenderer m_Renderer = GetComponent<MeshRenderer>();
    67.         m_Renderer.material = mat;
    68.  
    69.         newVertices.Add(new Vector3(x, y));
    70.         newVertices.Add(new Vector3(x + 1, y));
    71.         newVertices.Add(new Vector3(x + 1, y - 1));
    72.         newVertices.Add(new Vector3(x, y - 1));
    73.  
    74.         newTriangles.Add(squarecount * 4);
    75.         newTriangles.Add((squarecount * 4) + 1);
    76.         newTriangles.Add((squarecount * 4) + 3);
    77.         newTriangles.Add((squarecount * 4) + 1);
    78.         newTriangles.Add((squarecount * 4) + 2);
    79.         newTriangles.Add((squarecount * 4) + 3);
    80.  
    81.         squarecount++;
    82.     }
    83.  
    84.     void UpdateMesh()
    85.     {
    86.         mesh.Clear();
    87.         mesh.vertices = newVertices.ToArray();
    88.         mesh.triangles = newTriangles.ToArray();
    89.         mesh.RecalculateNormals();
    90.     }
    91.    
    92.     void Update () {
    93.        
    94.     }
    95. }
    96.  
    Thanks to anyone wanting to help me
     
  2. Deleted User

    Deleted User

    Guest

    Here is a image of what I'm getting.
    And I also want to add textures like the other image, but in this case I only get a brown one :/
     

    Attached Files: