Beispiel #1
0
        public float GetBilinear(Common.Painter.GridManagerInt gridManager, Vector3 position)
        {
            var gridSize = gridManager.GetGridSize();
            var xDiff    = Mathf.Abs(position.x) % gridSize;
            var zDiff    = Mathf.Abs(position.z) % gridSize;
            var xSign    = Mathf.Sign(position.x);

            if (xDiff < gridSize / 2f)
            {
                xSign *= -1;
            }
            var zSign = Mathf.Sign(position.z);

            if (zDiff < gridSize / 2f)
            {
                zSign *= -1;
            }
            var xOffset = new Vector2(gridSize, 0) * xSign;
            var zOffset = new Vector2(0, gridSize) * zSign;

            var p0 = gridManager.GetCellCenter(position);
            var p1 = p0 + xOffset;
            var p2 = p0 + zOffset;
            var p3 = p0 + xOffset + zOffset;

            var v0 = GetValue(gridManager.GetCell(p0.x0z()));
            var v1 = GetValue(gridManager.GetCell(p1.x0z()));
            var v2 = GetValue(gridManager.GetCell(p2.x0z()));
            var v3 = GetValue(gridManager.GetCell(p3.x0z()));

            var total = gridSize * gridSize;

            var w0 = ((gridSize - Mathf.Abs(position.x - p0.x)) * (gridSize - Mathf.Abs(position.z - p0.y))) / total;
            var w1 = ((gridSize - Mathf.Abs(position.x - p1.x)) * (gridSize - Mathf.Abs(position.z - p1.y))) / total;
            var w2 = ((gridSize - Mathf.Abs(position.x - p2.x)) * (gridSize - Mathf.Abs(position.z - p2.y))) / total;
            var w3 = ((gridSize - Mathf.Abs(position.x - p3.x)) * (gridSize - Mathf.Abs(position.z - p3.y))) / total;

            return(w0 * v0 + w1 * v1 + w2 * v2 + w3 * v3);
        }
Beispiel #2
0
        public void Invalidate(Serializable2DFloatArray heights,
                               Func <Vector3> displaySizeGetter,
                               Func <Vector3> positionGetter,
                               Func <Vector3> scaleGetter,
                               Func <Quaternion> rotationGetter,
                               Func <Vector3> dataSizeGetter, bool flipHeights,
                               WorldStampMask mask, Common.Painter.GridManagerInt gridManager,
                               Func <bool> existenceHook, int res)
        {
            _dataSize      = dataSizeGetter;
            _displaySize   = displaySizeGetter;
            _existenceHook = existenceHook;
            _position      = positionGetter;
            _scale         = scaleGetter;
            _rotation      = rotationGetter;

            if (_mesh == null)
            {
                _mesh = new Mesh();
            }
            if (_material == null)
            {
                _material = Resources.Load <Material>("MadMaps/WorldStamp/WorldStampPreviewMaterial");
            }
            var verts    = new Vector3[(res + 1) * (res + 1)];
            var uv       = new Vector2[verts.Length];
            var colors   = new Color[verts.Length];
            int counter  = 0;
            var dataSize = dataSizeGetter();

            for (int u = 0; u <= res; u++)
            {
                var uF = u / (float)res;
                for (int v = 0; v <= res; v++)
                {
                    var vF          = v / (float)res;
                    var samplePoint = flipHeights ? new Vector2(uF, vF) : new Vector2(vF, uF);
                    var height      = heights.BilinearSample(samplePoint);

                    var pos = new Vector3(uF * dataSize.x, height * dataSize.y, vF * dataSize.z) - dataSize.xz().x0z() / 2;

                    float val = 1;
                    if (mask != null && gridManager != null)
                    {
                        val    = mask.GetBilinear(gridManager, new Vector3(pos.x, 0, pos.z) + dataSize.xz().x0z() / 2);
                        pos.y *= val;
                    }

                    verts[counter]  = pos;
                    uv[counter]     = new Vector2(uF, vF);
                    colors[counter] = Color.Lerp(Color.clear, Color.white, val);
                    counter++;
                }
            }

            _mesh.vertices = verts;
            _mesh.uv       = uv;
            _mesh.colors   = colors;

            var tris = new int[((res + 1) * (res)) * 6];

            for (var i = 0; i < tris.Length; i += 6)
            {
                var vIndex = i / 6;

                var t1 = vIndex + 0;
                var t2 = vIndex + 1;
                var t3 = vIndex + 2 + (res - 1);

                var t1r = t1 / (res + 1);
                var t2r = t2 / (res + 1);
                var t3r = (t3 / (res + 1)) - 1;

                if (t1r == t2r && t2r == t3r && t3r == t1r)
                {
                    tris[i + 0] = t1;
                    tris[i + 1] = t2;
                    tris[i + 2] = t3;
                }

                var t4 = vIndex + 2 + (res - 1);
                var t5 = vIndex + 1 + (res - 1);
                var t6 = vIndex + 0;

                var t4r = (t4 / (res + 1)) - 1;
                var t5r = (t5 / (res + 1)) - 1;
                var t6r = t6 / (res + 1);

                if (t4r == t5r && t5r == t6r && t6r == t4r)
                {
                    tris[i + 3] = t4;
                    tris[i + 4] = t5;
                    tris[i + 5] = t6;
                }
            }

            _mesh.triangles = tris;
            _mesh.RecalculateNormals(0);
            _mesh.RecalculateBounds();

#if UNITY_EDITOR
            UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUIDelegate;
            UnityEditor.SceneView.onSceneGUIDelegate += OnSceneGUIDelegate;
#endif
        }