Пример #1
0
 /// <summary>
 /// 加载图像数据到纹理对象
 /// </summary>
 ///
 /// <param name="texture">纹理对象,必须开启可读属性(isReadable=true)</param>
 ///
 /// <exception cref="ArgumentException">纹理对象的格式与图像数据格式不匹配</exception>
 /// <author>Nianchen Deng</author>
 public void LoadDataTo(Texture2D texture)
 {
     if (format.GetTextureFormat() != texture.format)
     {
         throw new ArgumentException(
                   "Texture's format does not match with CImage's format", nameof(texture));
     }
     texture.LoadRawTextureData(data,
                                format.GetSizePerPixel() * size.Area());
     texture.Apply();
 }
Пример #2
0
        public void GenerateMesh(Vector2Int gridSize, Vector3[] vertices = null,
                                 Vector2[] uvs    = null, Color[] colors = null, Vector2 quadSize = default,
                                 bool dynamicMesh = false)
        {
            if (gridSize.x <= 1 || gridSize.y <= 1)
            {
                throw new ArgumentException("Grid size must be larger than 1", nameof(gridSize));
            }
            var mesh = new UnityEngine.Mesh();

            if (dynamicMesh)
            {
                mesh.MarkDynamic();
            }

            // 使用指定的顶点数组或生成默认顶点数组(大小为quadSize的矩形网格)
            if (vertices != null)
            {
                mesh.vertices = vertices;
            }
            else
            {
                // 若未指定quadSize,则默认为单位正方形
                if (Math.Abs(quadSize.Area()) < 1e-5f)
                {
                    quadSize = Vector2.one;
                }
                vertices = new Vector3[gridSize.Area()];
                for (int r = 0, i = 0; r < gridSize.y; ++r)
                {
                    for (var c = 0; c < gridSize.x; ++c, ++i)
                    {
                        vertices[i] = new Vector3(quadSize.x * c / (gridSize.x - 1),
                                                  quadSize.y * r / (gridSize.y - 1), 0.0f);
                    }
                }
                mesh.vertices = vertices;
            }

            // 使用指定的UV数组或生成默认UV数组(均匀坐标)
            if (uvs != null)
            {
                mesh.uv = uvs;
            }
            else
            {
                uvs = new Vector2[gridSize.Area()];
                for (int r = 0, i = 0; r < gridSize.y; ++r)
                {
                    for (var c = 0; c < gridSize.x; ++c, ++i)
                    {
                        uvs[i] = new Vector2((float)c / (gridSize.x - 1),
                                             (float)r / (gridSize.y - 1));
                    }
                }
                mesh.uv = uvs;
            }

            // 使用指定的颜色数组或生成默认颜色数组(白色)
            if (colors != null)
            {
                mesh.colors = colors;
            }
            else
            {
                colors = new Color[gridSize.Area()];
                for (var i = 0; i < colors.Length; ++i)
                {
                    colors[i] = Color.white;
                }
                mesh.colors = colors;
            }
            mesh.triangles = _GenerateTriangleList(gridSize).ToArray();
            mesh.SetTriangles(_GenerateTriangleList(gridSize), 0);
            mesh.RecalculateNormals();
            mesh.RecalculateTangents();
            mesh.UploadMeshData(!dynamicMesh);
            MeshObject   = mesh;
            _dynamicMesh = dynamicMesh;
        }
Пример #3
0
 protected GridPattern(IntPtr ptr, Vector2Int patternSize) :
     base(ptr, patternSize.Area()) =>
     this.patternSize = patternSize;