public static Mesh CreateMesh(GradationMaterial material, Vector2 drawSize, Color lightColor, Color darkColor)
    {
        var result = CreateMesh(material);

        if (material.keys.Any(x => x.color.a < 1.0f))
        {
            var cellSize    = new Vector2(32.0f / drawSize.x, 32.0f / drawSize.y);
            var checkerMesh = CreateCheckerMesh(new Rect(-1.0f, -1.0f, 2.0f, 2.0f), cellSize, Color.white, Color.gray);

            var vertices = new Vector3[result.vertices.Length + checkerMesh.vertices.Length];
            Array.Copy(checkerMesh.vertices, 0, vertices, 0, checkerMesh.vertices.Length);
            Array.Copy(result.vertices, 0, vertices, checkerMesh.vertices.Length, result.vertices.Length);

            var colors = new Color[result.colors.Length + checkerMesh.colors.Length];
            Array.Copy(checkerMesh.colors, 0, colors, 0, checkerMesh.colors.Length);
            Array.Copy(result.colors, 0, colors, checkerMesh.colors.Length, result.colors.Length);

            var indices = checkerMesh.GetIndices(0)
                          .Concat(result.GetIndices(0)
                                  .Select(x => x + checkerMesh.vertices.Length)
                                  )
                          .ToArray();

            result.Clear();
            result.vertices = vertices;
            result.colors   = colors;
            result.SetIndices(indices, MeshTopology.Triangles, 0);
        }
        return(result);
    }
Esempio n. 2
0
    private const float k_MakerRadius  = 10.0f;         //マーカー半径

    public static GradationWindow Instantiate(GradationMaterial target)
    {
        var result = EditorWindow.GetWindow <GradationWindow>();

        result.target = target;
        result.Show();
        return(result);
    }
 public Grid(GradationMaterial material)
 {
     this.material = material;
     xThresholds   = material.keys.Select(x => x.position.x)
                     .Concat(new[] { 0.0f, 1.0f })
                     .OrderBy(x => x)
                     .Distinct()
                     .ToArray();
     yThresholds = material.keys.Select(x => x.position.y)
                   .Concat(new[] { 0.0f, 1.0f })
                   .OrderBy(x => x)
                   .Distinct()
                   .ToArray();
     colors = GetColors();
 }
    public static Mesh CreateMesh(GradationMaterial material)
    {
        var result = new Mesh();

        var grid        = material.GetGrid();
        var vertexCount = grid.xThresholds.Length * grid.yThresholds.Length;

        var vertices = new Vector3[vertexCount];

        for (int y = 0, yMax = grid.yThresholds.Length; y < yMax; ++y)
        {
            for (int x = 0, xMax = grid.xThresholds.Length; x < xMax; ++x)
            {
                var index = x + y * grid.xThresholds.Length;
                vertices[index] = new Vector3(grid.xThresholds[x] * 2.0f - 1.0f
                                              , 1.0f - grid.yThresholds[y] * 2.0f
                                              , 0.0f
                                              );
            }
        }
        result.vertices = vertices;

        result.colors = grid.colors;

        var indices = new int[6 * (grid.yThresholds.Length - 1) * (grid.xThresholds.Length - 1)];

        for (int y = 0, yMax = grid.yThresholds.Length - 1; y < yMax; ++y)
        {
            for (int x = 0, xMax = grid.xThresholds.Length - 1; x < xMax; ++x)
            {
                var index           = x + y * (grid.xThresholds.Length - 1);
                var upperLeftIndex  = x + y * grid.xThresholds.Length;
                var upperRightIndex = upperLeftIndex + 1;
                var lowerLeftIndex  = upperLeftIndex + grid.xThresholds.Length;
                var lowerRightIndex = lowerLeftIndex + 1;
                indices[index * 6 + 0] = upperLeftIndex;
                indices[index * 6 + 1] = upperRightIndex;
                indices[index * 6 + 2] = lowerRightIndex;
                indices[index * 6 + 3] = lowerRightIndex;
                indices[index * 6 + 4] = lowerLeftIndex;
                indices[index * 6 + 5] = upperLeftIndex;
            }
        }
        result.SetIndices(indices, MeshTopology.Triangles, 0);

        return(result);
    }
    public static Texture2D CreatePreviewTexture2D(GradationMaterial material, Vector2 size, Color lightColor, Color darkColor)
    {
        var previewRender = new PreviewRenderUtility();

        previewRender.m_Camera.orthographic     = true;
        previewRender.m_Camera.orthographicSize = 1.0f;
        previewRender.m_Camera.nearClipPlane    = 0.0f;
        previewRender.m_Camera.farClipPlane     = 2.0f;
        var previewMaterial = new Material(Shader.Find("Sprites/Default"));

        Rect r = new Rect(Vector2.zero, size);

        previewRender.BeginStaticPreview(r);

        var matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(previewRender.m_Camera.aspect, 1.0f, 1.0f));
        var mesh   = CreateMesh(material, size, lightColor, darkColor);

        previewRender.DrawMesh(mesh, matrix, previewMaterial, 0);

        previewRender.m_Camera.Render();
        var result = previewRender.EndStaticPreview();

        return(result);
    }