码迷,mamicode.com
首页 > 其他好文 > 详细

自定义网格面片(省的去三维软件创建)

时间:2014-10-20 22:54:33      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

//EDITOR CLASS TO CREATE QUAD MESH WITH SPECIFIED ANCHOR
//Created by Alan Thorn on 23.01.2013
//------------------------------------------------
using UnityEngine;
using UnityEditor;
using System.IO;
//------------------------------------------------
//Run from unity editor. This class should be placed in Editor folder
public class CreateQuad : ScriptableWizard
{
    //Anchor point for created quad
    public enum AnchorPoint
    {
        TopLeft,
        TopMiddle,
        TopRight,
        RightMiddle,
        BottomRight,
        BottomMiddle,
        BottomLeft,
        LeftMiddle,
        Center,
        Custom
    }
    
    //Name of Quad Asset
    public string MeshName = "Quad";
    
    //Game Object Name
    public string GameObjectName = "Plane_Object";
    
    //Name of asset folder to contain quad asset when created
    public string AssetFolder = "Assets";
    
    //Width of quad in world units (pixels)
    public float Width = 1.0f;
    
    //Height of quad in world units (pixels)
    public float Height = 1.0f;
    
    //Position of Anchor
    public AnchorPoint Anchor = AnchorPoint.Center;
    
    //Horz Position of Anchor on Plane
    public float AnchorX = 0.5f;
    
    //Vert Position of Anchor on Plane
    public float AnchorY = 0.5f;
    //------------------------------------------------
    [MenuItem("GameObject/Create Other/Custom Plane")]
    static void CreateWizard()
    {
        ScriptableWizard.DisplayWizard("Create Plane",typeof(CreateQuad));
    }
    
    //------------------------------------------------
    //Function called when window is created
    void OnEnable()
    {
        //Call selection change to load asset path from selected, if any
        OnSelectionChange();
    }
    //------------------------------------------------
    //Called 10 times per second
    void OnInspectorUpdate()
    {
        switch(Anchor)
        {
            //Anchor is set to top-left
            case AnchorPoint.TopLeft:
                AnchorX = 0.0f * Width;
                AnchorY = 1.0f * Height;
            break;
            
            //Anchor is set to top-middle
            case AnchorPoint.TopMiddle:
                AnchorX = 0.5f * Width;
                AnchorY = 1.0f * Height;
            break;
            
            //Anchor is set to top-right
            case AnchorPoint.TopRight:
                AnchorX = 1.0f * Width;
                AnchorY = 1.0f * Height;
            break;
            
            //Anchor is set to right-middle
            case AnchorPoint.RightMiddle:
                AnchorX = 1.0f * Width;
                AnchorY = 0.5f * Height;
            break;
            
            //Anchor is set to Bottom-Right
            case AnchorPoint.BottomRight:
                AnchorX = 1.0f * Width;
                AnchorY = 0.0f * Height;
            break;
            
            //Anchor is set to Bottom-Middle
            case AnchorPoint.BottomMiddle:
                AnchorX = 0.5f * Width;
                AnchorY = 0.0f * Height;
            break;
            
            //Anchor is set to Bottom-Left
            case AnchorPoint.BottomLeft:
                AnchorX = 0.0f * Width;
                AnchorY = 0.0f * Height;
            break;
            
            //Anchor is set to Left-Middle
            case AnchorPoint.LeftMiddle:
                AnchorX = 0.0f * Width;
                AnchorY = 0.5f * Height;
            break;
            
            //Anchor is set to center
            case AnchorPoint.Center:
                AnchorX = 0.5f * Width;
                AnchorY = 0.5f * Height;
            break;
            
            case AnchorPoint.Custom:
            default:
            break;
        }
    }
    //------------------------------------------------
    //Function called when window is updated
    void OnSelectionChange()
    {
        //Check user selection in editor - check for folder selection
        if (Selection.objects != null && Selection.objects.Length == 1)
        {
            //Get path from selected asset
            AssetFolder = Path.GetDirectoryName(AssetDatabase.GetAssetPath(Selection.objects[0]));
        }
    }
    //------------------------------------------------
    //Function to create quad mesh
    void OnWizardCreate()
    {        
        //Create Vertices
        Vector3[] Vertices = new Vector3[4];
        
        //Create UVs
        Vector2[] UVs = new Vector2[4];
        
        //Two triangles of quad
        int[] Triangles = new int[6];
        
        //Assign vertices based on pivot
        
        //Bottom-left
        Vertices[0].x = -AnchorX;
        Vertices[0].y = -AnchorY;
        
        //Bottom-right
        Vertices[1].x = Vertices[0].x+Width;
        Vertices[1].y = Vertices[0].y;
            
        //Top-left
        Vertices[2].x = Vertices[0].x;
        Vertices[2].y = Vertices[0].y+Height;
        
        //Top-right
        Vertices[3].x = Vertices[0].x+Width;
        Vertices[3].y = Vertices[0].y+Height;
        
        //Assign UVs
        //Bottom-left
        UVs[0].x=0.0f;
        UVs[0].y=0.0f;
        
        //Bottom-right
        UVs[1].x=1.0f;
        UVs[1].y=0.0f;
        
        //Top-left
        UVs[2].x=0.0f;
        UVs[2].y=1.0f;
        
        //Top-right
        UVs[3].x=1.0f;
        UVs[3].y=1.0f;
        
        //Assign triangles
        Triangles[0]=3;
        Triangles[1]=1;
        Triangles[2]=2;
        
        Triangles[3]=2;
        Triangles[4]=1;
        Triangles[5]=0;
        
        //Generate mesh
        Mesh mesh = new Mesh();
        mesh.name = MeshName;
        mesh.vertices = Vertices;
        mesh.uv = UVs;
        mesh.triangles = Triangles;
        mesh.RecalculateNormals();
        
        //Create asset in database
        AssetDatabase.CreateAsset(mesh, AssetDatabase.GenerateUniqueAssetPath(AssetFolder + "/" + MeshName) + ".asset");
        AssetDatabase.SaveAssets();
        
        //Create plane game object
        GameObject plane = new GameObject(GameObjectName);
        MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
        plane.AddComponent(typeof(MeshRenderer));
        
        //Assign mesh to mesh filter
        meshFilter.sharedMesh = mesh;
        mesh.RecalculateBounds();
        
        //Add a box collider component
        plane.AddComponent(typeof(BoxCollider));
    }
    
    //------------------------------------------------
}

 

自定义网格面片(省的去三维软件创建)

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/123ing/p/4039065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!