void GenerateTexture()
    {
        int n = 512;

        Color[] pixels = new Color[n * n];

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (i >= j)
                {
                    Barycentric b = new Barycentric(new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(i * 1f / n, j * 1f / n));
                    var         c = b.Interpolate(Color.red, Color.green, Color.blue);
                    int         k = i * n + j;
                    pixels[k] = c;
                }
            }
        }

        tex = new Texture2D(n, n);
        tex.SetPixels(pixels);
        tex.Apply();
    }
示例#2
0
    /// <summary>
    /// Fill a triangle area on the texture, lerping vertex colors.
    /// </summary>
    ///
    private static void FillInTextureTriangle(Texture2D tex,
                                              Vector2 coord_a, Vector2 coord_b, Vector2 coord_c,
                                              Color color_a, Color color_b, Color color_c)
    {
        int xmin = Mathf.RoundToInt(Mathf.Min(coord_a.x, coord_b.x, coord_c.x));
        int xmax = Mathf.RoundToInt(Mathf.Max(coord_a.x, coord_b.x, coord_c.x));
        int ymin = Mathf.RoundToInt(Mathf.Min(coord_a.y, coord_b.y, coord_c.y));
        int ymax = Mathf.RoundToInt(Mathf.Max(coord_a.y, coord_b.y, coord_c.y));

        Color color = Color.clear;

        for (int x = xmin; x <= xmax; x++)
        {
            for (int y = ymin; y <= ymax; y++)
            {
                var baryCoord = new Barycentric(coord_a, coord_b, coord_c, new Vector2(x, y));
                if (baryCoord.IsInside)
                {
                    color = baryCoord.Interpolate(color_a, color_b, color_c);
                    tex.SetPixel(x, y, color);
                }
            }
        }
    }
示例#3
0
    private void FillInTextureTriangle(Texture2D tex,
                                       Vector2 ta, Vector2 tb, Vector2 tc,
                                       Color ca, Color cb, Color cc)
    {
        int xmin = Mathf.RoundToInt(Mathf.Min(ta.x, tb.x, tc.x));
        int xmax = Mathf.RoundToInt(Mathf.Max(ta.x, tb.x, tc.x));
        int ymin = Mathf.RoundToInt(Mathf.Min(ta.y, tb.y, tc.y));
        int ymax = Mathf.RoundToInt(Mathf.Max(ta.y, tb.y, tc.y));

        Color color = Color.clear;

        for (int x = xmin; x <= xmax; x++)
        {
            for (int y = ymin; y <= ymax; y++)
            {
                var baryCoord = new Barycentric(ta, tb, tc, new Vector2(x, y));
                if (baryCoord.IsInside)
                {
                    color = baryCoord.Interpolate(ca, cb, cc);
                    tex.SetPixel(x, y, color);
                }
            }
        }
    }
示例#4
0
        //                   rightPoint1
        //                   |\
        //                   | \
        // intersectionPoint2|__\ intersectionPoint1
        //                   |   \
        //        leftPoint2 |____\ leftPoint1
        //
        public void CreateTrianglesTwoPointsOnLeft(Vector3 leftPoint1, Vector3 leftPoint2, Vector3 rightPoint1, int i,
                                                   int n0, int n1, int n2)
        {
            var n = _verts.Count;
            var intersectionPoint1 = IntersectionPlaneLinePoint(leftPoint1, rightPoint1, _planePoint, _planeNormal);
            var intersectionPoint2 = IntersectionPlaneLinePoint(leftPoint2, rightPoint1, _planePoint, _planeNormal);

            var localLeftPoint1 = _srcObject.transform.InverseTransformPoint(leftPoint1);
            var localLeftPoint2 = _srcObject.transform.InverseTransformPoint(leftPoint2);

            var localIntersectionPoint1 = _srcObject.transform.InverseTransformPoint(intersectionPoint1);
            var localIntersectionPoint2 = _srcObject.transform.InverseTransformPoint(intersectionPoint2);

            _wholeSlicePlane.AddSlicePlanePoints(localIntersectionPoint1, localIntersectionPoint2);

            int t0 = _triangles2[i + n0];
            int t1 = _triangles2[i + n1];
            int t2 = _triangles2[i + n2];
            var localRightPoint1 = _srcObject.transform.InverseTransformPoint(rightPoint1);

            if (_tangents.Any())
            {
                AddTangents(_tangents[t0], _tangents[t1], _tangents[t2], _tangents[t2]);
            }

            if (_uvs.Any())
            {
                var b1 = new Barycentric(localLeftPoint1, localLeftPoint2, localRightPoint1, localIntersectionPoint1);
                var intersectionPoint1Uv = b1.Interpolate(_uvs[t0],
                                                          _uvs[t1],
                                                          _uvs[t2]);

                var b2 = new Barycentric(localLeftPoint1, localLeftPoint2, localRightPoint1, localIntersectionPoint2);
                var intersectionPoint2Uv = b2.Interpolate(_uvs[t0],
                                                          _uvs[t1],
                                                          _uvs[t2]);

                AddUVs(_uvs[t0],
                       _uvs[t1],
                       intersectionPoint1Uv,
                       intersectionPoint2Uv);
            }

            var b3 = new Barycentric(localLeftPoint1, localLeftPoint2, localRightPoint1, localIntersectionPoint1);
            var localIntersectionPoint1Normal = b3.Interpolate(_normals[t0],
                                                               _normals[t1],
                                                               _normals[t2]);

            var b4 = new Barycentric(localLeftPoint1, localLeftPoint2, localRightPoint1, localIntersectionPoint2);
            var localIntersectionPoint2Normal = b4.Interpolate(_normals[t0],
                                                               _normals[t1],
                                                               _normals[t2]);

            AddNormals(_normals[t0],
                       _normals[t1],
                       localIntersectionPoint1Normal,
                       localIntersectionPoint2Normal);

            AddVerts(localLeftPoint1,
                     localLeftPoint2,
                     localIntersectionPoint1,
                     localIntersectionPoint2);


            if (n0 == 0 && n1 == 2)
            {
                AddTriangle(n, 1, 0, 2);
                AddTriangle(n, 1, 2, 3);
            }
            else //if (n0 == 1)
            {
                AddTriangle(n, 0, 1, 3);
                AddTriangle(n, 0, 3, 2);
            }
        }
示例#5
0
        internal bool TryAddBlends(SurfaceData sd, Mesh mesh, int submeshID, Vector3 point, int triangleID, out float totalWeight)
        {
            totalWeight = 0;

            //Finds Barycentric
            var triangle = triangleID * 3;
            var t0       = triangles[triangle + 0];
            var t1       = triangles[triangle + 1];
            var t2       = triangles[triangle + 2];
            var a        = vertices[t0];
            var b        = vertices[t1];
            var c        = vertices[t2];

            point = transform.InverseTransformPoint(point);
            var bary = new Barycentric(a, b, c, point);

#if UNITY_EDITOR
            lastSampledColors.Clear();
#endif

            float totalTotalWeight = 0;
            for (int i = 0; i < blendMaps.Length; i++)
            {
                var bm = blendMaps[i];
                bm.sampled = false;

                for (int ii = 0; ii < bm.subMaterials.Length; ii++)
                {
                    if (bm.subMaterials[ii].materialID == submeshID)
                    {
                        var uv = bary.Interpolate(bm.uvs[t0], bm.uvs[t1], bm.uvs[t2]);
                        uv = uv * new Vector2(bm.uvScaleOffset.x, bm.uvScaleOffset.y) + new Vector2(bm.uvScaleOffset.z, bm.uvScaleOffset.w); //?

                        Color color = bm.map.GetPixelBilinear(uv.x, uv.y);                                                                   //this only works for clamp or repeat btw (not mirror etc.)
                        bm.sampledColor = color;

                        void SampleColor(BlendMap.SurfaceBlends2 sb2)
                        {
                            if (sb2.colorMap != null)
                            {
                                sb2.sampledColor = sb2.colorMap.GetPixelBilinear(uv.x, uv.y);
                            }
                            else
                            {
                                sb2.sampledColor = Color.white;
                            }
                        }

                        SampleColor(bm.r);
                        SampleColor(bm.g);
                        SampleColor(bm.b);
                        SampleColor(bm.a);

                        totalTotalWeight += bm.weight * (color.r + color.g + color.b + color.a);

#if UNITY_EDITOR
                        lastSampledColors.Add(color);
#endif

                        bm.sampled = true;
                        break;
                    }
                }
            }

            if (totalTotalWeight > 0)
            {
                float invTotalTotal = 1f / totalTotalWeight;

                for (int i = 0; i < blendMaps.Length; i++)
                {
                    var bm = blendMaps[i];

                    if (bm.sampled)
                    {
                        float invTotal = bm.weight * invTotalTotal;

                        var color = bm.sampledColor;
                        Add(sd, bm.r.result, bm.r.sampledColor, color.r * invTotal, ref totalWeight);
                        Add(sd, bm.g.result, bm.g.sampledColor, color.g * invTotal, ref totalWeight);
                        Add(sd, bm.b.result, bm.b.sampledColor, color.b * invTotal, ref totalWeight);
                        Add(sd, bm.a.result, bm.a.sampledColor, color.a * invTotal, ref totalWeight);
                    }
                }

                return(true);
            }

            return(false);
        }