Search Unity

Custom billboard created via script, not displaying correctly

Discussion in 'Scripting' started by AkamaruDesign, May 17, 2020.

  1. AkamaruDesign

    AkamaruDesign

    Joined:
    Dec 17, 2016
    Posts:
    13
    Hello, I have read the documentation of unity about billboard asset and how to create them, but I do not get the result that I expect, there must be something that I am leaving aside.
    Captura de Pantalla 2020-05-17 a la(s) 2.57.52 p. m..png
    for some reason, it is not displayed at the scale indicated by the asset, and although it has the coordinates of the textures in place, it is not displayed as expected but the entire texture is there

    Here is the code I used to generate the asset:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class MyBillboard : MonoBehaviour
    7. {
    8.     [Header("Initial preparations")]
    9.  
    10.     [Tooltip("This should be a billboard material")]
    11.     [SerializeField] Material mat;
    12.     [Tooltip("This dictates the center of the render for the object")]
    13.     [SerializeField] Transform cameraBillboardPivot;
    14.     [Tooltip("The camera should be a child of the Camera Billboard Pivot")]
    15.     [SerializeField] Camera cam;
    16.  
    17.     [Header("Object Setup")]
    18.  
    19.     [Tooltip("Units in height of the object, roughly, this can be fine-tuned later on the final asset")]
    20.     [SerializeField] float objectHeight = 5.0f;
    21.     [Tooltip("Units in width of the object, roughly, this can be fine-tuned later on the final asset")]
    22.     [SerializeField] float objectWidth = 4.0f;
    23.     [Tooltip("Usually negative and small, to make it sit in the ground slightly, can be modifed on final asset")]
    24.     [SerializeField] float bottomOffset = 0.0f;
    25.  
    26.     [Header("Mesh Re-Dimention")]
    27.  
    28.     [Range(0, 1)]
    29.     public float topWidth = 3;
    30.     [Range(0, 1)]
    31.     public float midWidth = 3;
    32.     [Range(0, 1)]
    33.     public float botWidth = 9;
    34.  
    35.  
    36.     [Header("Image Setup")]
    37.  
    38.     [SerializeField] int atlasPixelWidth = 1024;
    39.     [SerializeField] int atlasPixelHeight = 1024;
    40.     [Min(1)]
    41.     [SerializeField] int atlasRowImageCount = 1;
    42.     [Min(1)]
    43.     [SerializeField] int atlasColumnImageCount = 1;
    44.     [Min(1)]
    45.     [SerializeField] int totalImageCount = 1;
    46.     [Tooltip("Color to replace with complete alpha - set Camera to depth only and this to black")]
    47.     [SerializeField] Color clearColor = Color.black;
    48.  
    49.     [Header("Create")]
    50.     [SerializeField] bool create;
    51.  
    52.     // Start is called before the first frame update
    53.     void Start()
    54.     {
    55.     }
    56.  
    57.     // Update is called once per frame
    58.     void Update()
    59.     {
    60.         if (create)
    61.         {
    62.             StartCoroutine(CreateBillboard());
    63.             create = false;
    64.         }
    65.     }
    66.     IEnumerator CreateBillboard()
    67.     {
    68.         BillboardAsset newBillboard = new BillboardAsset();
    69.  
    70.         newBillboard.material = mat;
    71.         Rect[] photoRect = new Rect[totalImageCount];
    72.         Vector4[] textCoords = new Vector4[totalImageCount];
    73.         ushort[] meshtriangles = new ushort[12];
    74.         Vector2[] vertex = new Vector2[6];
    75.  
    76.         //create a texture
    77.         Texture2D texture = new Texture2D(atlasPixelWidth, atlasPixelHeight, TextureFormat.ARGB32, false);
    78.         //set all pixels to tranparent
    79.         Color[] allpixels = texture.GetPixels();
    80.         for(int i =0;i<allpixels.Length;i++)
    81.         {
    82.             allpixels[i].a = 0.0f;
    83.         }
    84.         texture.SetPixels(allpixels);
    85.         //inicialize rotation
    86.         cameraBillboardPivot.eulerAngles = Vector3.zero;
    87.         int imageat = 0;
    88.         //atlas them left-right, top-bottom, 0,0 is bottom left
    89.         for (int j = 0; j < atlasRowImageCount; j++)
    90.         {
    91.             for (int i = 0; i < atlasColumnImageCount; i++)
    92.             {
    93.                 if (imageat < totalImageCount)
    94.                 {
    95.                     float xRatio = (float)i / atlasColumnImageCount;
    96.                     float yRatio = (float)(atlasRowImageCount - j - 1) / atlasRowImageCount;
    97.  
    98.                     //photoRect[imageat].Set(xRatio, yRatio, (float)1 / atlasColumnImageCount, (float)1 / atlasRowImageCount);
    99.  
    100.                     textCoords[imageat] = new Vector4(xRatio, yRatio, 1f / atlasColumnImageCount, 1f / atlasRowImageCount);
    101.                     cam.targetTexture = RenderTexture.GetTemporary(Mathf.FloorToInt(atlasPixelWidth/ atlasColumnImageCount), Mathf.FloorToInt(atlasPixelHeight/ atlasRowImageCount), 16);
    102.  
    103.                     float xPos = xRatio * atlasPixelWidth;
    104.                     float yPos = yRatio * atlasPixelWidth;
    105.  
    106.                     yield return new WaitForEndOfFrame();
    107.                     Rect rect = new Rect(0, 0, Mathf.FloorToInt(atlasPixelWidth / atlasColumnImageCount), Mathf.FloorToInt(atlasPixelHeight / atlasRowImageCount));
    108.                     RenderTexture.active = cam.targetTexture;
    109.                     texture.ReadPixels(rect, Mathf.FloorToInt(xPos), Mathf.FloorToInt(yPos), false);
    110.                     texture.Apply();
    111.                    
    112.  
    113.                     var eulerStore = cameraBillboardPivot.eulerAngles;
    114.                     eulerStore.y -= (360f / totalImageCount);
    115.                     cameraBillboardPivot.eulerAngles = eulerStore;
    116.                     print(imageat);
    117.  
    118.                     imageat++;
    119.                 }
    120.             }
    121.         }
    122.         //Set triangles from vertex
    123.         meshtriangles[0] = 4;
    124.         meshtriangles[1] = 3;
    125.         meshtriangles[2] = 0;
    126.         meshtriangles[3] = 1;
    127.         meshtriangles[4] = 4;
    128.         meshtriangles[5] = 0;
    129.         meshtriangles[6] = 5;
    130.         meshtriangles[7] = 4;
    131.         meshtriangles[8] = 1;
    132.         meshtriangles[9] = 2;
    133.         meshtriangles[10] = 5;
    134.         meshtriangles[11] = 1;
    135.         //Set Vertex positions
    136.         vertex[0].Set(-botWidth / 2 + 0.5f, 0);
    137.         vertex[1].Set(-midWidth / 2 + 0.5f, 0.5f);
    138.         vertex[2].Set(-topWidth / 2 + 0.5f, 1);
    139.         vertex[3].Set(botWidth / 2 + 0.5f, 0);
    140.         vertex[4].Set(midWidth / 2 + 0.5f, 0.5f);
    141.         vertex[5].Set(topWidth / 2 + 0.5f, 1);
    142.  
    143.         //assing billbord Data
    144.         newBillboard.SetImageTexCoords(textCoords);
    145.         newBillboard.SetIndices(meshtriangles);
    146.         newBillboard.SetVertices(vertex);
    147.         newBillboard.width = objectWidth;
    148.         newBillboard.height = objectHeight;
    149.         newBillboard.bottom = bottomOffset;
    150.         print(newBillboard.imageCount);
    151.         string path;
    152.         int nameLength = AssetDatabase.GetAssetPath(mat).Length;
    153.         //take out ".mat" prefix
    154.         path = AssetDatabase.GetAssetPath(mat).Substring(0, nameLength - 4) + ".asset";
    155.         AssetDatabase.CreateAsset(newBillboard, path);
    156.         path = AssetDatabase.GetAssetPath(mat).Substring(0, nameLength - 4) + ".png";
    157.         byte[] byteArray = ImageConversion.EncodeToPNG(texture);
    158.         System.IO.File.WriteAllBytes(path, byteArray);
    159.  
    160.         Debug.Log("Billboard Asset Created & PNG File Bytes Saved: " + path);
    161.  
    162.     }
    163. }
    164.  
    Please Help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    It will be necessary for you to attach the debugger and step through the sections that are not doing what you want, or else use Debug.Log() statements to gain insight into what's going wrong.

    Did you write this? If you did not, then perhaps approach the original author for guidance, as it is quite complex and none of us will be able to replicate your issue trivially.