• unity 绘制三角形


    哎 该学的还是要学

    参考:http://www.narkii.com/club/thread-369573-1.html

    unity 顶点绘制三角形

    脚本绘制;

    其实filter和render就是进行资源的加载以及渲染的设置,和device和context类似。

    其中注意的是Material中shader路径的加载。。。。

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 //meshFilter 数据资源的加载
     5 //meshRender 加载后渲染的设置
     6 public class DrawTriangleTest : MonoBehaviour
     7 {
     8     
     9     private Mesh m_mesh;
    10     public Material m_material;
    11 
    12     private void Awake()
    13     {
    14         if(m_material.shader==null)
    15         m_material = new Material(Shader.Find("Custom/TriangleTest"));
    16     }
    17 
    18     // Use this for initialization
    19     void Start ()
    20     {
    21         Vector3[] trianglePos = new Vector3[]
    22         {
    23             new Vector3(0,0,0),
    24             new Vector3(1,1,0),
    25             new Vector3(-1,1,0),
    26         };
    27 
    28         int[]  mIndexArr = new int[] {0, 2,1};
    29         Color[] mColorArr = new Color[] {Color.green,Color.red,Color.blue};
    30 
    31         m_mesh = new Mesh();
    32         m_mesh.vertices = trianglePos;
    33         m_mesh.triangles = mIndexArr;
    34         m_mesh.colors = mColorArr;
    35 
    36     }
    37     
    38     // Update is called once per frame
    39     void Update ()
    40     {
    41         Graphics.DrawMesh(m_mesh,Vector3.zero,Quaternion.identity,m_material,0);
    42     }
    43 }
    View Code

    shader进行cull back 操作:

     1 Shader "Custom/TriangleTest" {
     2     Properties {
     3         }
     4     SubShader {
     5         Pass{
     6             
     7         Fog{Mode off}
     8         Cull Back
     9         
    10         CGPROGRAM
    11         #pragma vertex vert
    12         #pragma fragment frag
    13         #include "UnityCG.cginc"
    14 
    15     struct vToFragment
    16     {
    17        float4 pos : SV_POSITION;
    18        float4 color : color0;
    19     };
    20 
    21     vToFragment vert(appdata_full inV)
    22     {
    23         vToFragment outToFrag;
    24         outToFrag.pos = mul(UNITY_MATRIX_MVP,inV.vertex);
    25         outToFrag.color= inV.color;
    26         return outToFrag;
    27      }
    28 
    29     float4 frag(vToFragment inData):SV_Target
    30     {
    31         float4 outColor;
    32         outColor = inData.color;
    33         return outColor;
    34     }
    35 
    36         ENDCG
    37     }
    38     }
    39     FallBack "Diffuse"
    40 }
    View Code
    改变自己
  • 相关阅读:
    什么是 CLR
    常用的数据结构以及算法
    Array和ArrayList的异同点
    什么是CMS?
    .NET Framework3.0答疑
    C#中的委托
    C#速成之三(Quick C#)
    C#速成之五(Quick C#)
    C#速成之四(Quick C#)
    类,对象,封装,接口,多态,继承
  • 原文地址:https://www.cnblogs.com/sun-shadow/p/5102772.html
Copyright © 2020-2023  润新知