Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save unitycoder/993f7b3d46469a792715 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/993f7b3d46469a792715 to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist Apr 24, 2015. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions PlanarUVMap.cs
    Original file line number Diff line number Diff line change
    @@ -5,17 +5,19 @@

    public class PlanarUVMap : MonoBehaviour {

    public bool flipX=false;

    void Start()
    {
    UsePlanarUV();
    }

    void UsePlanarUV()
    {
    Mesh mesh = GetComponent<MeshFilter>().mesh;
    Vector3[] vertices = mesh.vertices;
    Vector2[] uvs = new Vector2[vertices.Length];

    Vector2 min = new Vector2(Mathf.Infinity, Mathf.Infinity);
    Vector2 max = new Vector2(Mathf.NegativeInfinity,Mathf.NegativeInfinity);

    @@ -24,24 +26,24 @@ void UsePlanarUV()
    min = new Vector2( Mathf.Min(min.x,vertices[i].x), Mathf.Min(min.y,vertices[i].z));
    max = new Vector2( Mathf.Max(max.x,vertices[i].x), Mathf.Max(max.y,vertices[i].z));
    }

    float fixScaleX = 16; // temporary fix for X uv map

    for (int i=0; i < uvs.Length; i++)
    {
    float uvx = Remap(vertices[i].x,min.x,max.x,0,1)*fixScaleX;
    float uvy = Remap(vertices[i].z,min.y,max.y,0,1);

    uvs[i] = new Vector2(uvx,uvy);
    uvs[i] = new Vector2(flipX?1-uvx:uvx,uvy);
    }
    mesh.uv = uvs;

    }

    // remap value from range to another range
    float Remap(float current, float sourceRangeFrom, float sourceRangeTo, float targetRangeFrom, float targetRangeTo)
    {
    return targetRangeFrom + (current-sourceRangeFrom)*(targetRangeTo-targetRangeFrom)/(sourceRangeTo-sourceRangeFrom);
    }

    }
  2. unitycoder renamed this gist Apr 24, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. unitycoder created this gist Apr 24, 2015.
    47 changes: 47 additions & 0 deletions PlanarUVMap
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    using UnityEngine;
    using System.Collections;

    // modified from : http://docs.unity3d.com/ScriptReference/Mesh-uv.html

    public class PlanarUVMap : MonoBehaviour {

    void Start()
    {
    UsePlanarUV();
    }

    void UsePlanarUV()
    {
    Mesh mesh = GetComponent<MeshFilter>().mesh;
    Vector3[] vertices = mesh.vertices;
    Vector2[] uvs = new Vector2[vertices.Length];

    Vector2 min = new Vector2(Mathf.Infinity, Mathf.Infinity);
    Vector2 max = new Vector2(Mathf.NegativeInfinity,Mathf.NegativeInfinity);

    for (int i=0; i < uvs.Length; i++)
    {
    min = new Vector2( Mathf.Min(min.x,vertices[i].x), Mathf.Min(min.y,vertices[i].z));
    max = new Vector2( Mathf.Max(max.x,vertices[i].x), Mathf.Max(max.y,vertices[i].z));
    }

    float fixScaleX = 16; // temporary fix for X uv map

    for (int i=0; i < uvs.Length; i++)
    {
    float uvx = Remap(vertices[i].x,min.x,max.x,0,1)*fixScaleX;
    float uvy = Remap(vertices[i].z,min.y,max.y,0,1);

    uvs[i] = new Vector2(uvx,uvy);
    }
    mesh.uv = uvs;

    }

    // remap value from range to another range
    float Remap(float current, float sourceRangeFrom, float sourceRangeTo, float targetRangeFrom, float targetRangeTo)
    {
    return targetRangeFrom + (current-sourceRangeFrom)*(targetRangeTo-targetRangeFrom)/(sourceRangeTo-sourceRangeFrom);
    }

    }