Modify the map Uvs for each polygon of the cube separately (Unity3d development 18)

Source: Internet
Author: User

Monkey original, welcome reprint. Reproduced please specify: Reproduced from COCOS2DER-CSDN, thank you!
Original address: http://blog.csdn.net/cocos2der/article/details/46611169

In today's project, you need to modify the map Uvs for each polygon in a cube, that is, there are multiple rectangular maps in the map, and you need the program to read a portion of the map to assign to each polygon of the cube.

It's already been achieved here.

CustomUVS.cs

usingUnityengine;usingSystem.Collections; [Executeineditmode] Public classCustomuvs:monobehaviour { PublicVector2 Toppoint; PublicVector2 Bottompoint; PublicVector2 Leftpoint; PublicVector2 RightPoint; PublicVector2 FrontPoint; PublicVector2 Backpoint;PrivateMesh M_mesh; Public enumCubefacetype {Top, Bottom, left, right, Front, back};// Use this for initialization    voidStart () {Meshfilter meshfilter = getcomponent<meshfilter> ();if(Meshfilter = =NULL) {Debug.logerror ("Script needs Meshfilter component");return; }#if unity_editorMesh meshcopy = mesh.instantiate (Meshfilter.sharedmesh) asMesh;//Make a deep copyMeshcopy.name ="Cube"; M_mesh = Meshfilter.mesh = meshcopy;//Assign the copy to the meshes#Else M_mesh = Meshfilter.mesh;#endif         if(M_mesh = =NULL|| M_mesh.uv.Length! = -) {Debug.logerror ("Script needs to being attached to built-in cube");return;    } Updatemeshuvs (); }//Update is called once per frame    voidUpdate () {#if unity_editorUpdatemeshuvs ();#endif }voidUpdatemeshuvs () {vector2[] Uvs = M_MESH.UV;//FrontSetfacetexture (Cubefacetype.front, Uvs);//TopSetfacetexture (Cubefacetype.top, Uvs);// backSetfacetexture (Cubefacetype.back, Uvs);//BottomSetfacetexture (Cubefacetype.bottom, Uvs);// LeftSetfacetexture (Cubefacetype.left, Uvs);// RightSetfacetexture (Cubefacetype.right, Uvs);    M_MESH.UV = Uvs; } vector2[] Getuvs (floatOriginx,floatOriginy) {vector2[] Uvs =Newvector2[4]; uvs[0] =NewVector2 (Originx/3.0F, Originy/3.0f); uvs[1] =NewVector2 ((Originx +1) /3.0F, Originy/3.0f); uvs[2] =NewVector2 (Originx/3.0F, (Originy +1) /3.0f); uvs[3] =NewVector2 ((Originx +1) /3.0F, (Originy +1) /3.0f);returnUvs }voidSetfacetexture (Cubefacetype facetype, vector2[] Uvs) {if(Facetype = = Cubefacetype.front)            {vector2[] Newuvs = Getuvs (Frontpoint.x, FRONTPOINT.Y); uvs[0] = newuvs[0]; uvs[1] = newuvs[1]; uvs[2] = newuvs[2]; uvs[3] = newuvs[3]; }Else if(Facetype = = Cubefacetype.back)            {vector2[] Newuvs = Getuvs (Backpoint.x, BACKPOINT.Y); uvs[Ten] = newuvs[0]; uvs[ One] = newuvs[1]; uvs[6] = newuvs[2]; uvs[7] = newuvs[3]; }Else if(Facetype = = cubefacetype.top)            {vector2[] Newuvs = Getuvs (Toppoint.x, TOPPOINT.Y); uvs[8] = newuvs[0]; uvs[9] = newuvs[1]; uvs[4] = newuvs[2]; uvs[5] = newuvs[3]; }Else if(Facetype = = Cubefacetype.bottom)            {vector2[] Newuvs = Getuvs (Bottompoint.x, BOTTOMPOINT.Y); uvs[ A] = newuvs[0]; uvs[ -] = newuvs[1]; uvs[ the] = newuvs[2]; uvs[ -] = newuvs[3]; }Else if(Facetype = = Cubefacetype.left)            {vector2[] Newuvs = Getuvs (Leftpoint.x, LEFTPOINT.Y); uvs[ -] = newuvs[0]; uvs[ -] = newuvs[1]; uvs[ +] = newuvs[2]; uvs[ -] = newuvs[3]; }Else if(Facetype = = cubefacetype.right)            {vector2[] Newuvs = Getuvs (Rightpoint.x, RIGHTPOINT.Y); uvs[ -] = newuvs[0]; uvs[ A] = newuvs[1]; uvs[ at] = newuvs[2]; uvs[ +] = newuvs[3]; }    }}
    1. Map used

    2. Add a change to the image material to a cube. and add the CustomUVS.cs script. Modify the area origin to intercept

Note: Since the picture is 3x3, the intercept area is truncated by 3.

Actual effect

Modify the map Uvs for each polygon of the cube separately (Unity3d development 18)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.