sexta-feira, 31 de julho de 2015

Unity C# Sample

using UnityEngine;
using System.Collections;

public class SceneUtils {
                   
       public static Vector3[] minmaxBoundsEx(Transform root)
       {
             Vector3 p2;
             Vector3 p3;
            
             bool first = true;
             Vector3 min = new Vector3(0,0,0);
             Vector3 max = new Vector3(0,0,0);
            
             Vector3 min2 = new Vector3(0,0,0);
             Vector3 max2 = new Vector3(0,0,0);
            
             {
                    Renderer[] r2 = root.GetComponentsInChildren<Renderer>();
                    foreach (Renderer r in r2)
                    {
                           Mesh mesh = null;
                          
                           if (r is SkinnedMeshRenderer)
                           {
                                  SkinnedMeshRenderer smr = r as SkinnedMeshRenderer;                            
                                  mesh = smr.sharedMesh;

                                  min2 = smr.bounds.min;
                                  max2 = smr.bounds.max;
                                 
                                  min2 = root.InverseTransformPoint( min2 );
                                  max2 = root.InverseTransformPoint( max2 );
                          
                                  if (first)
                                  {
                                        first = false;
                                        min = min2;
                                        max = max2;
                                  }
                                 
                                  if (min2.x < min.x)
                                        min.x = min2.x;
                                  if (min2.y < min.y)
                                        min.y = min2.y;
                                  if (min2.z < min.z)
                                        min.z = min2.z;
                                 
                                  if (max2.x > max.x)
                                        max.x = max2.x;
                                  if (max2.y > max.y)
                                        max.y = max2.y;
                                  if (max2.z > max.z)
                                        max.z = max2.z;
                          
                                  //min = min2;
                                  //max = max2;
                           }
                           else
                           if (r is MeshRenderer)
                           {
                                  TextMesh tm = r.GetComponent<TextMesh>();
                                 
                                  if (tm != null)
                                  {                                      
                                        Quaternion saverotation = tm.transform.localRotation;                                
                                        tm.transform.localRotation = Quaternion.identity;                                     
                                        min2 = /*r.transform.TransformPoint(*/r.bounds.min;//World Coordinates - AA: Axis Aligned
                                        max2 = /*r.transform.TransformPoint(*/r.bounds.max;
                                       
                                        min2 = root.InverseTransformPoint( min2 );
                                        max2 = root.InverseTransformPoint( max2 );
                                       
                                        tm.transform.localRotation = saverotation;
                                       
                                        //p3 = root.transform.InverseTransformPoint(p2);
                                       
                                        if (first)
                                        {
                                               first = false;
                                               min = min2;
                                               max = max2;
                                        }
                                       
                                        if (min2.x < min.x)
                                               min.x = min2.x;
                                        if (min2.y < min.y)
                                               min.y = min2.y;
                                        if (min2.z < min.z)
                                               min.z = min2.z;
                                       
                                        if (max2.x > max.x)
                                               max.x = max2.x;
                                        if (max2.y > max.y)
                                               max.y = max2.y;
                                        if (max2.z > max.z)
                                               max.z = max2.z;
                                  }
                                  else
                                  {
                                              
                                        MeshFilter mf = r.GetComponent<MeshFilter>();
                                        if (mf != null)
                                               mesh = mf.sharedMesh;
                                 
                                        if (mesh != null)
                                        {
                                               foreach (Vector3 p in mesh.vertices)
                                               {
                                                      p2 = r.transform.TransformPoint( p );
                                                      p3 = root.transform.InverseTransformPoint(p2);
                                                      if (first)
                                                      {
                                                            first = false;
                                                            min = p3;
                                                            max = p3;
                                                      }
                                                      if (p3.x < min.x)
                                                            min.x = p3.x;
                                                      if (p3.y < min.y)
                                                            min.y = p3.y;
                                                      if (p3.z < min.z)
                                                            min.z = p3.z;
                                                     
                                                      if (p3.x > max.x)
                                                            max.x = p3.x;
                                                      if (p3.y > max.y)
                                                            max.y = p3.y;
                                                      if (p3.z > max.z)
                                                            max.z = p3.z;
                                               }
                                        }
                                  }
                           }
                          
                    }           
             }           
            
             return new Vector3[] {min, max};
       }
      
       public static void CreateCollider(GameObject go)//, ItemAR itemAR)
       {
                                 
                                  string goname = go.name;
                                  if (goname.StartsWith("pivot_"))
                                        goname = goname.Substring("pivot_".Length);
                                                                  
                                  BoxCollider bc = go.GetComponentInChildren<BoxCollider>();
                                  if (bc == null)
                                        Vector3 center = minmax[0]+(minmax[1]-minmax[0])/2f;
                                        Vector3 extents = (minmax[1]-minmax[0])/2f;
                                              
                                 
                                        bc = go.AddComponent<BoxCollider>();
                                        bc.center = center;
                                        bc.extents = extents;
                                        bc.isTrigger = true;
                                       
                                        go.layer = LayerMask.NameToLayer("selectiontriggers");
                                       
                                        
                                       
                                       
                                       
                                  }                                             
                           }
                    }
             }
       }
      
       public static Mesh CreatePlaneMesh(float h)
    {
        Mesh mesh = new Mesh();

        Vector3[] vertices = new Vector3[]
        {
            new Vector3( 1, 0,  h),//1),
            new Vector3( 1, 0, 0),
            new Vector3(0, 0,  h),//1),
            new Vector3(0, 0, 0),
        };

        Vector2[] uv = new Vector2[]
        {
            new Vector2(1, 1),
            new Vector2(1, 0),
            new Vector2(0, 1),
            new Vector2(0, 0),
        };

        int[] triangles = new int[]
        {
            0, 1, 2,
            2, 1, 3,
        };

        mesh.vertices = vertices;
        mesh.uv = uv;
        mesh.triangles = triangles;
            
             mesh.RecalculateNormals();
            
            
        return mesh;
    }
      
       //ex = -0.5 .. 0.5
       public static Mesh CreatePlaneMeshEx2(float h)
    {
        Mesh mesh = new Mesh();

        Vector3[] vertices = new Vector3[]
        {
            new Vector3( 0.5f, 0,  0.5f),//1),
            new Vector3( 0.5f, 0, -0.5f),
            new Vector3(-0.5f, 0,  0.5f),//1),
            new Vector3(-0.5f, 0, -0.5f),
        };

        Vector2[] uv = new Vector2[]
        {
            new Vector2(1, 1),
            new Vector2(1, 0),
            new Vector2(0, 1),
            new Vector2(0, 0),
        };

        int[] triangles = new int[]
        {
            0, 1, 2,
            2, 1, 3,
        };

        mesh.vertices = vertices;
        mesh.uv = uv;
        mesh.triangles = triangles;
            
             mesh.RecalculateNormals();
            
            

        return mesh;
    }
      
       public static GameObject CreatePlaneObject(Material material)//Texture2D newtexture, string ShaderName)
       {     
             GameObject go = new GameObject();
             go.name = "newgameobject";
             //go.transform.parent = transform;
            
             MeshFilter mf = go.AddComponent<MeshFilter>();
             MeshRenderer r = go.AddComponent<MeshRenderer>();
            
            
             float h = 1;//((float)newtexture.height) / newtexture.width;
             mf.mesh = CreatePlaneMesh(h);
             //r.material = new Material();   
             r.material = material;
            
             return go;
       }
      
       //ex2 = plano -0.5..0.5
       public static GameObject CreatePlaneObjectEx2(Material material)//Texture2D newtexture, string ShaderName)
       {     
             GameObject go = new GameObject();
             go.name = "newgameobject";
             //go.transform.parent = transform;
            
             MeshFilter mf = go.AddComponent<MeshFilter>();
             MeshRenderer r = go.AddComponent<MeshRenderer>();
            
//           Shader s;
//           if (!string.IsNullOrEmpty(ShaderName))
//                  s = Shader.Find (ShaderName);
//           else
//                  s = Shader.Find ("Diffuse");
//           Material newmaterial = new Material(s);
//           newmaterial.shader = s;
//           newmaterial.mainTexture = newtexture;
            
             float h = 1;//((float)newtexture.height) / newtexture.width;
             mf.mesh = CreatePlaneMeshEx2(h);
             //r.material = new Material();   
             r.material = material;
            
             return go;
       }
      
       public static void AdjustWidthHeight(GameObject mainobj)
       {     
             //float z = Camera.main.farClipPlane-1f;//(dx / 2) / Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             float z = farClipPlane-1f;//(dx / 2) / Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             mainobj.transform.localPosition = new Vector3(0,0,z);
            
             MeshFilter mf = mainobj.transform.GetComponent<MeshFilter>();                   
             if (mf == null)
                    Debug.Log ("marcador nao tem meshFilter");
            
             Vector3 leftbottom = mainobj.transform.position;//Vector3.zero;//mf2.transform.TransformPoint(bc-b1);
             Vector3 righttop = mainobj.transform.position;//Vector3.zero;
             bool first = true;
             if (mf == null)
             {
                    //mf = mainobj.transform.GetChild(0).GetComponent();                
                    foreach (MeshFilter mf2 in mainobj.GetComponentsInChildren<MeshFilter>())
                    {
                           Bounds b = mf2.sharedMesh.bounds;
                           Vector3 bc = b.center;
                           Vector3 b1 = b.size / 2;
                          
                           Vector3 p1 = mf2.transform.TransformPoint(bc-b1);
                           Vector3 p2 = mf2.transform.TransformPoint(bc+b1);
                           Vector3 p3 = Vector3.Min(p1,p2);
                           Vector3 p4 = Vector3.Max(p1,p2);
                           if (first)
                           {
                                  first = false;
                                 
                                  leftbottom = p3;
                                  righttop = p4;
                           }
                           else
                           {
                                  leftbottom = Vector3.Min(leftbottom,p3);
                                  righttop = Vector3.Max(righttop,p4);
                           }
                    }
             }
             else
             {                         
                    Bounds b = mf.sharedMesh.bounds;
                    Vector3 bc = b.center;
                    Vector3 b1 = b.size / 2;
                   
                    leftbottom = mainobj.transform.TransformPoint(bc-b1);
                    righttop = mainobj.transform.TransformPoint(bc+b1);
             }
            
             //float x =
             //float y = (righttop-leftbottom).y;
             //float z = (righttop-leftbottom).z;
            
             //UpdateFOV();
             float dx = (righttop-leftbottom).x;
             float dz = (righttop-leftbottom).z;
             float newx = 2f * z * Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             float newz = 2f * z * Mathf.Tan( (vertFOV / 2f) * Mathf.Deg2Rad );
            
             // pequeno ajuste de escala para cobrir furo no android AsusTransformer
             newx *= 1.1f;
             //mainobj.transform.localScale = new Vector3(mainobj.transform.localScale.x * newx/dx,1,mainobj.transform.localScale.z * newz/dz);
       }
      
       public static void Show(GameObject go)
       {
             foreach (Renderer r in go.GetComponentsInChildren<Renderer>())
                    r.enabled = true;
             foreach (Collider c in go.GetComponentsInChildren<Collider>())
                    c.enabled = true;
       }
      
       public static void Hide(GameObject go)
       {
             foreach (Renderer r in go.GetComponentsInChildren<Renderer>())
                    r.enabled = false;
             foreach (Collider c in go.GetComponentsInChildren<Collider>())
                    c.enabled = false;
       }
      
       public static void AdjustObjectWidthTo(GameObject mainobj, float finalWidth)
       {     
             //float z = Camera.main.farClipPlane-1f;//(dx / 2) / Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             //float z = farClipPlane-1f;//(dx / 2) / Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             //mainobj.transform.localPosition = new Vector3(0,0,z);
            
             MeshFilter mf = mainobj.transform.GetComponent<MeshFilter>();                   
             if (mf == null)
                    Debug.Log ("marcador nao tem meshFilter");
            
             Vector3 leftbottom = mainobj.transform.position;//Vector3.zero;//mf2.transform.TransformPoint(bc-b1);
             Vector3 righttop = mainobj.transform.position;//Vector3.zero;
             bool first = true;
             if (mf == null)
             {
                    //mf = mainobj.transform.GetChild(0).GetComponent();                
                    foreach (MeshFilter mf2 in mainobj.GetComponentsInChildren<MeshFilter>())
                    {
                           Bounds b = mf2.sharedMesh.bounds;
                           Vector3 bc = b.center;
                           Vector3 b1 = b.size / 2;
                          
                           Vector3 p1 = mf2.transform.TransformPoint(bc-b1);
                           Vector3 p2 = mf2.transform.TransformPoint(bc+b1);
                           Vector3 p3 = Vector3.Min(p1,p2);
                           Vector3 p4 = Vector3.Max(p1,p2);
                           if (first)
                           {
                                  first = false;
                                 
                                  leftbottom = p3;
                                  righttop = p4;
                           }
                           else
                           {
                                  leftbottom = Vector3.Min(leftbottom,p3);
                                  righttop = Vector3.Max(righttop,p4);
                           }
                    }
             }
             else
             {                         
                    Bounds b = mf.sharedMesh.bounds;
                    Vector3 bc = b.center;
                    Vector3 b1 = b.size / 2;
                   
                    leftbottom = mainobj.transform.TransformPoint(bc-b1);
                    righttop = mainobj.transform.TransformPoint(bc+b1);
             }
            
             //float x =
             //float y = (righttop-leftbottom).y;
             //float z = (righttop-leftbottom).z;
            
             //UpdateFOV();
             float oldx = (righttop-leftbottom).x;
             //float dz = (righttop-leftbottom).z;
             float newx = finalWidth;//2f * z * Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             //float newz = 2f * z * Mathf.Tan( (vertFOV / 2f) * Mathf.Deg2Rad );
            
             float scale = newx/oldx;
            
             mainobj.transform.localScale = mainobj.transform.localScale * scale;//= new Vector3(mainobj.transform.localScale.x * newx/dx,1,mainobj.transform.localScale.z * newz/dz);
            
             //CreateBorderObjects(mainobj, SombraLivro, leftbottom, righttop);
       }
      
       public static float Z2X(float z)
       {
//           if (horzFOV == 0)
//                  UpdateFOV();       
             return 2f * z * Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             //float newz = (dx/2f)/Mathf.Tan (Mathf.Deg2Rad * SceneUtils.horzFOV);
       }
      
       public static float X2Z(float x)
       {
//           if (horzFOV == 0)
//                  UpdateFOV();       
             return (x/2f) / Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             //float newz = (dx/2f)/Mathf.Tan (Mathf.Deg2Rad * SceneUtils.horzFOV);
       }
       public static float Y2Z(float y)
       {
//           if (vertFOV == 0)
//                  UpdateFOV();       
             return (y/2f) / Mathf.Tan( (vertFOV / 2f) * Mathf.Deg2Rad );
             //float newz = (dx/2f)/Mathf.Tan (Mathf.Deg2Rad * SceneUtils.horzFOV);
       }
      
      
       public static float horzFOV;
       public static float vertFOV;
       public static float nearClipPlane;
       public static float farClipPlane;
       public static float aspect;
      
       private static Camera activecamera;
       public static void UpdateFOV(Camera cam)
       {
             if (vertFOV == 0)
             {
                    activecamera = cam;
                    nearClipPlane = cam.nearClipPlane;
                    farClipPlane = cam.farClipPlane;
                    aspect = cam.aspect;
                    //-----------------------------------------------------------------
                    // horizontal field of view
                    //-----------------------------------------------------------------
                    Vector3 ptop = cam.ScreenToWorldPoint( new Vector3( 0, 0, 1) );
                    Vector3 pbottom = cam.ScreenToWorldPoint( new Vector3( 0, Screen.height-1, 1 ));                                  
                                       
                    // Renato - 04/12/2012 - nao podemos utilizar o fieldOfView da camera, pois o Qualcomm modificou a matriz de projecao
                    float QualcommVertFOV = Mathf.Rad2Deg * 2 * Mathf.Atan( (ptop-pbottom).magnitude/2 );
                    vertFOV = QualcommVertFOV;//cam2.fieldOfView;
                   
                    Vector3 pleft = cam.ScreenToWorldPoint( new Vector3( 0, 0, 1) );
                    Vector3 pright = cam.ScreenToWorldPoint( new Vector3( Screen.width-1, 0, 1 ));                                    
                                       
                    // Renato - 04/12/2012 - nao podemos utilizar o fieldOfView da camera, pois o Qualcomm modificou a matriz de projecao
                    float QualcommHorzFOV = Mathf.Rad2Deg * 2 * Mathf.Atan( (pright-pleft).magnitude/2 );
                    horzFOV = QualcommHorzFOV;//cam2.fieldOfView;
                   
                    Debug.Log ("qualcomm camera aspect is:"+cam.aspect.ToString("N3"));
                    Debug.Log ("qualcomm camera Real aspect is:"+(horzFOV/vertFOV).ToString("N3"));
                   
                    //horzFOV = Mathf.Rad2Deg * 2 * Mathf.Atan(Mathf.Tan(vertFOV * Mathf.Deg2Rad / 2) * cam.aspect);
                   
                   
                    //Debug.Log ("vertFOV:"+vertFOV.ToString("N3"));
                    //Debug.Log ("horzFOV:"+horzFOV.ToString("N3"));           
             }
       }
      
       public static float GetCamSizeY(float distz)
       {
             //float distz = activecamera.nearClipPlane * 1.1f;
             float dy = 2f * distz * Mathf.Tan( (vertFOV / 2f) * Mathf.Deg2Rad );
             return dy;
       }
      
       public static float GetCamSizeX(float distz)
       {
             //float distz = activecamera.nearClipPlane * 1.1f;
             float dx = 2f * distz * Mathf.Tan( (horzFOV / 2f) * Mathf.Deg2Rad );
             return dx;
       }
      
       public static Vector3 ScreenToLocal(Vector3 position, float z)
       {
             z = activecamera.nearClipPlane * 1.1f;
            
             //activecamera.ScreenToWorldPoint
             float x = GetCamSizeX(z);
             float y = GetCamSizeY(z);
            
             float x2 = position.x/Screen.width;
             float y2 = position.y/Screen.height;
            
             x = (x/2f) * (2f * (x2 - 0.5f));
             y = (y/2f) * (2f * (y2 - 0.5f));
            
             return new Vector3(x,y,z);
       }
      
       // criar um plano de 1x1 com UV especifico
       public static GameObject CreatePlaneUV(float x1, float y1, float x2, float y2, Material material)
       {
             //public static void CreateBorderObjects(GameObject mainobj, Material material, Vector3 leftbottom, Vector3 righttop)    
             //float margem = (righttop - leftbottom).magnitude / 20f;
            
             GameObject borda;
             Mesh mesh;
             //Vector3[] vertices;
            
             //------------------------------------------         
             borda = CreatePlaneObject(material);//,"Transparent/Diffuse");
             //borda.name = "testeborda1";
             //borda.transform.position = righttop;
             //borda.transform.parent = mainobj.transform;
            
             mesh = borda.GetComponent<MeshFilter>().sharedMesh;        
//           vertices = mesh.vertices;
//           vertices[0] = borda.transform.InverseTransformPoint(new Vector3(righttop.x + margem, 0, righttop.z + margem));
//           vertices[1] = borda.transform.InverseTransformPoint(new Vector3(righttop.x, 0, righttop.z));
//           vertices[2] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x - margem, 0, righttop.z + margem));
//           vertices[3] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x, 0, righttop.z));
//           mesh.vertices = vertices;
             mesh.uv = new Vector2[]
        {
            new Vector2(x1,y1),
            new Vector2(x2,y1),
            new Vector2(x2,y2),
            new Vector2(x1,y2),
        };
             mesh.RecalculateBounds();
            
             return borda;
       }
       // criar um plano de 1x1 com UV especifico
       public static void AdjustPlaneUV(GameObject go, float x1, float y1, float x2, float y2)
       {
             //public static void CreateBorderObjects(GameObject mainobj, Material material, Vector3 leftbottom, Vector3 righttop)    
             //float margem = (righttop - leftbottom).magnitude / 20f;
             Mesh mesh;
             //Vector3[] vertices;
            
             //------------------------------------------         
            
             //borda.name = "testeborda1";
             //borda.transform.position = righttop;
             //borda.transform.parent = mainobj.transform;
            
             mesh = go.GetComponent<MeshFilter>().sharedMesh;           
//           vertices = mesh.vertices;
//           vertices[0] = borda.transform.InverseTransformPoint(new Vector3(righttop.x + margem, 0, righttop.z + margem));
//           vertices[1] = borda.transform.InverseTransformPoint(new Vector3(righttop.x, 0, righttop.z));
//           vertices[2] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x - margem, 0, righttop.z + margem));
//           vertices[3] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x, 0, righttop.z));
//           mesh.vertices = vertices;
             mesh.uv = new Vector2[]
        {
            new Vector2(x2,y2),
            new Vector2(x2,y1),
            new Vector2(x1,y2),
            new Vector2(x1,y1),
        };
             mesh.RecalculateBounds();
       }
      
       public static void CreateBorderObjects(GameObject mainobj, Material material)//Texture2D SombraLivro)
       {     
             MeshFilter mf = mainobj.transform.GetComponent<MeshFilter>();                   
//           if (mf == null)
//                  Debug.Log ("marcador nao tem meshFilter");
            
             Vector3 leftbottom = mainobj.transform.position;//Vector3.zero;//mf2.transform.TransformPoint(bc-b1);
             Vector3 righttop = mainobj.transform.position;//Vector3.zero;
             bool first = true;
             if (mf == null)
             {
                    //mf = mainobj.transform.GetChild(0).GetComponent();                
                    foreach (MeshFilter mf2 in mainobj.GetComponentsInChildren<MeshFilter>())
                    {
                           Bounds b = mf2.sharedMesh.bounds;
                           Vector3 bc = b.center;
                           Vector3 b1 = b.size / 2;
                          
                           Vector3 p1 = mf2.transform.TransformPoint(bc-b1);
                           Vector3 p2 = mf2.transform.TransformPoint(bc+b1);
                           Vector3 p3 = Vector3.Min(p1,p2);
                           Vector3 p4 = Vector3.Max(p1,p2);
                           if (first)
                           {
                                  first = false;
                                 
                                  leftbottom = p3;
                                  righttop = p4;
                           }
                           else
                           {
                                  leftbottom = Vector3.Min(leftbottom,p3);
                                  righttop = Vector3.Max(righttop,p4);
                           }
                    }
             }
             else
             {                         
                    Bounds b = mf.sharedMesh.bounds;
                    Vector3 bc = b.center;
                    Vector3 b1 = b.size / 2;
                   
                    leftbottom = mainobj.transform.TransformPoint(bc-b1);
                    righttop = mainobj.transform.TransformPoint(bc+b1);
             }
            
             CreateBorderObjects(mainobj, material, leftbottom, righttop);
             //return mainobj;
       }
      
       public static void CreateBorderObjects(GameObject mainobj, Material material, Vector3 leftbottom, Vector3 righttop)
       {     
             float margem = (righttop - leftbottom).magnitude / 20f;
            
             GameObject borda;
             Mesh mesh;
             Vector3[] vertices;
            
             //------------------------------------------         
             borda = CreatePlaneObject(material);//,"Transparent/Diffuse");
             //borda.name = "testeborda1";
             borda.transform.position = righttop;
             borda.transform.parent = mainobj.transform;
            
             mesh = borda.GetComponent<MeshFilter>().sharedMesh;        
             vertices = mesh.vertices;
             vertices[0] = borda.transform.InverseTransformPoint(new Vector3(righttop.x + margem, 0, righttop.z + margem));
             vertices[1] = borda.transform.InverseTransformPoint(new Vector3(righttop.x, 0, righttop.z));
             vertices[2] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x - margem, 0, righttop.z + margem));
             vertices[3] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x, 0, righttop.z));
             mesh.vertices = vertices;
             mesh.uv = new Vector2[]
        {
            new Vector2(0.98f, 0.98f),
            new Vector2(0.9217975937f, 0.02f),
            new Vector2(0.02f, 0.98f),
            new Vector2(0.0782024062f, 0.02f),
        };
             mesh.RecalculateBounds();

             //------------------------------------------         
             borda = CreatePlaneObject(material);//SombraLivro,"Transparent/Diffuse");
             //borda.name = "testeborda1";
             borda.transform.position = righttop;
             borda.transform.parent = mainobj.transform;
            
             mesh = borda.GetComponent<MeshFilter>().sharedMesh;
             vertices = mesh.vertices;
             vertices[0] = borda.transform.InverseTransformPoint(new Vector3(righttop.x + margem, 0, leftbottom.z - margem));
             vertices[1] = borda.transform.InverseTransformPoint(new Vector3(righttop.x, 0, leftbottom.z));
             vertices[2] = borda.transform.InverseTransformPoint(new Vector3(righttop.x + margem, 0, righttop.z + margem));           
             vertices[3] = borda.transform.InverseTransformPoint(new Vector3(righttop.x, 0, righttop.z));
            
             mesh.vertices = vertices;
             mesh.uv = new Vector2[]
        {
            new Vector2(0.98f, 0.98f),
            new Vector2(0.9217975937f, 0.02f),
            new Vector2(0.02f, 0.98f),
            new Vector2(0.0782024062f, 0.02f),
        };
             mesh.RecalculateBounds();

             //------------------------------------------         
             borda = CreatePlaneObject(material);//SombraLivro,"Transparent/Diffuse");
             //borda.name = "testeborda1";
             borda.transform.position = leftbottom;
             borda.transform.parent = mainobj.transform;
            
             mesh = borda.GetComponent<MeshFilter>().sharedMesh;        
             vertices = mesh.vertices;
             vertices[0] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x - margem, 0, leftbottom.z - margem));
             vertices[1] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x, 0, leftbottom.z));
             vertices[2] = borda.transform.InverseTransformPoint(new Vector3(righttop.x + margem, 0, leftbottom.z - margem));
             vertices[3] = borda.transform.InverseTransformPoint(new Vector3(righttop.x, 0, leftbottom.z));
            
            
             mesh.vertices = vertices;
             mesh.uv = new Vector2[]
        {
            new Vector2(0.98f, 0.98f),
            new Vector2(0.9217975937f, 0.02f),
            new Vector2(0.02f, 0.98f),
            new Vector2(0.0782024062f, 0.02f),
        };
             mesh.RecalculateBounds();
            
             //------------------------------------------         
             borda = CreatePlaneObject(material);
             borda.transform.position = leftbottom;
             borda.transform.parent = mainobj.transform;
            
             mesh = borda.GetComponent<MeshFilter>().sharedMesh;        
             vertices = mesh.vertices;
            
             vertices[0] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x - margem, 0, righttop.z + margem));
             vertices[1] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x, 0, righttop.z));      
             vertices[2] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x - margem, 0, leftbottom.z - margem));
             vertices[3] = borda.transform.InverseTransformPoint(new Vector3(leftbottom.x, 0, leftbottom.z));
            
             mesh.vertices = vertices;
            
             mesh.uv = new Vector2[]
        {
            new Vector2(0.98f, 0.98f),
            new Vector2(0.9217975937f, 0.02f),
            new Vector2(0.02f, 0.98f),
            new Vector2(0.0782024062f, 0.02f),
        };
            
             mesh.RecalculateBounds();
            
            
       }
      
}



Postar um comentário