hch
2 天以前 af8dafd75f9a6fae017734d58fc7b34d6bdcd0f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
 
[DisallowMultipleComponent]
public class MirrorImage:BaseMeshEffect {
 
    [SerializeField]
    Align m_Align = Align.Right;
    public Align align { get { return m_Align; } }
 
    public override void ModifyMesh(VertexHelper vh) {
        if(!IsActive() || vh.currentVertCount == 0) {
            return;
        }
 
        var vertexs = new List<UIVertex>();
        vh.GetUIVertexStream(vertexs);
        var min = CalculateMin(vertexs);
        var max = CalculateMax(vertexs);
 
        if(align == Align.Orthogon) {
            vertexs.AddRange(Copy(Align.Right,min,max,vertexs));
            vertexs.AddRange(Copy(Align.Bottom,min,max,vertexs));
        }
        else {
            vertexs.AddRange(Copy(align,min,max,vertexs));
        }
 
        vh.Clear();
        vh.AddUIVertexTriangleStream(vertexs);
    }
 
    private List<UIVertex> Copy(Align _align,Vector2 _min,Vector2 _max,List<UIVertex> _originalVertexs) {
        var copyVertexs = new List<UIVertex>();
 
        var reversal = _align == Align.Top || _align == Align.Right;
        var axis = _align == Align.Left || _align == Align.Right ? 0 : 1;
        var count = _originalVertexs.Count;
        for(int i = 0;i < count;i++) {
            var vertex = _originalVertexs[i];
            vertex = _originalVertexs[i];
            var position = vertex.position;
            if(reversal) {
                position[axis] = _max[axis] - position[axis] + _max[axis];
            }
            else {
                position[axis] = _min[axis] - position[axis] + _min[axis];
            }
 
            vertex.position = position;
            copyVertexs.Add(vertex);
        }
 
        return copyVertexs;
    }
 
 
    Vector2 CalculateMin(List<UIVertex> _vertexs) {
        var count = _vertexs.Count;
        var min = _vertexs[0].position;
 
        for(int i = 1;i < count;i++) {
            var vertex = _vertexs[i];
            if(vertex.position[0] < min[0]) {
                min[0] = vertex.position[0];
            }
            else if(vertex.position[1] < min[1]) {
                min[1] = vertex.position[1];
            }
        }
 
        return min;
    }
 
    Vector2 CalculateMax(List<UIVertex> _vertexs) {
        var count = _vertexs.Count;
        var max = _vertexs[0].position;
 
        for(int i = 1;i < count;i++) {
            var vertex = _vertexs[i];
            if(vertex.position[0] > max[0]) {
                max[0] = vertex.position[0];
            }
            else if(vertex.position[1] > max[1]) {
                max[1] = vertex.position[1];
            }
        }
 
        return max;
    }
 
    public enum Align {
        Left,
        Right,
        Bottom,
        Top,
        Orthogon,
    }
 
}