使用mesh自己创建一个cube

参考链接:https://www.cnblogs.com/JLZT1223/p/6089996.html

直接上代码:

void Update()
    {

        if (Input.GetKeyUp(KeyCode.J))
        {
            GameObject NewObj=CreateCube(1, 1, 1,Vector3.zero);
            NewObj.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Standard"));
        }

       
    }

 /// <summary>
    /// 绘制立方体
    /// </summary>
    /// <param name="length">长</param>
    /// <param name="width">宽</param>
    /// <param name="heigth">高</param>
    /// <returns>GameObject类型物体</returns>
    GameObject CreateCube(float length, float width, float heigth,Vector3 Point)
     {

        GameObject NewObj = new GameObject("新建");
        //NewObj.name = ;
        NewObj.AddComponent<MeshFilter>();
        NewObj.AddComponent<MeshRenderer>();
        MeshFilter meshFilter = NewObj.GetComponent<MeshFilter>();

        //vertices(顶点、必须):
        int vertices_count = 4 * 6;                                 //顶点数(每个面4个点,六个面)
         Vector3[] vertices = new Vector3[vertices_count];
         vertices[0] = new Vector3(Point .x- length/2, Point .y- heigth/2, Point .z- width/2);                     //前面的左下角的点
         vertices[1] = new Vector3(Point .x- length / 2, Point.y+heigth / 2, Point.z - width / 2);                //前面的左上角的点
         vertices[2] = new Vector3(Point.x+length / 2, Point.y - heigth / 2, Point.z - width / 2);                 //前面的右下角的点
        vertices[3] = new Vector3(Point.x+length / 2, Point.y+heigth / 2, Point.z - width / 2);           //前面的右上角的点

        vertices[4] = new Vector3(Point.x+length / 2, Point.y - heigth / 2, Point.z+width / 2);           //后面的右下角的点
         vertices[5] = new Vector3(Point.x+length / 2, Point.y+heigth / 2, Point.z+width / 2);      //后面的右上角的点
         vertices[6] = new Vector3(Point .x- length / 2, Point.y - heigth / 2, Point.z+ width /2);                //后面的左下角的点
         vertices[7] = new Vector3(Point .x- length / 2, Point.y+heigth / 2, Point.z+width / 2);           //后面的左上角的点
 
         vertices[8] = vertices[6];                              //左
         vertices[9] = vertices[7];
         vertices[10] = vertices[0];
         vertices[11] = vertices[1];
 
         vertices[12] = vertices[2];                              //右
         vertices[13] = vertices[3];
         vertices[14] = vertices[4];
         vertices[15] = vertices[5];
 
         vertices[16] = vertices[1];                              //上
         vertices[17] = vertices[7];
         vertices[18] = vertices[3];
         vertices[19] = vertices[5];
 
         vertices[20] = vertices[2];                              //下
         vertices[21] = vertices[4];
         vertices[22] = vertices[0];
         vertices[23] = vertices[6];
 
 
         //triangles(索引三角形、必须):
         int SplitTriangle = 6 * 2;//分割三角形数量
         int triangles_cout = SplitTriangle * 3;                  //索引三角形的索引点个数
         int[] triangles = new int[triangles_cout];            //索引三角形数组
         for(int i=0,vi=0;i<triangles_cout;i+=6,vi+=4)
         {
             triangles[i] = vi;
             triangles[i + 1] = vi+1;
             triangles[i + 2] = vi+2;
 
             triangles[i + 3] = vi+3;
             triangles[i + 4] = vi+2;
             triangles[i + 5] = vi+1;
 
         }
 
         //uv:
         //.........
 
         //负载属性与mesh
         Mesh mesh = new Mesh();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        meshFilter.mesh= mesh;
        return NewObj;
     }

主要流程就是获取顶点位置和三角索引点然后进行绘制立方体,具体详细的可以看这边文章https://blog.csdn.net/yffgamestart/article/details/90268680

解释的非常到位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要使用一个JavaScript库来创建和呈现3D模型。在本例中,我们将使用Three.js库。在HTML文件中引入Three.js库: ``` <script src="https://cdn.jsdelivr.net/npm/three@0.124.0/build/three.min.js"></script> ``` 接下来,我们需要一个canvas元素来呈现3D模型。在HTML文件中添加一个canvas元素: ``` <canvas id="canvas"></canvas> ``` 然后,在JavaScript文件中,创建一个场景、摄像机、灯光和一个Cube。代码如下: ``` var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); var renderer = new THREE.WebGLRenderer({canvas: document.getElementById("canvas")}); renderer.setSize( window.innerWidth, window.innerHeight ); var geometry = new THREE.BoxGeometry(); var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } ); var cube = new THREE.Mesh( geometry, material ); scene.add( cube ); camera.position.z = 5; var animate = function () { requestAnimationFrame( animate ); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render( scene, camera ); }; animate(); ``` 这段代码创建了一个场景、摄像机、灯光和一个绿色的Cube。摄像机被放置在z轴上,以便我们可以看到3D模型。然后,我们在animate函数中循环旋转Cube,并调用renderer.render函数以呈现场景。 现在,我们可以在浏览器中查看3D模型了。需要注意的是,由于Three.js库需要WebGL支持,因此在某些旧的浏览器上可能无法正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值