Пример #1
0
    public void ReplaceAlphaWith(D2D_Pixels newPixels)
    {
        DeserializeAlphaTex();

#if UNITY_EDITOR
        if (alphaTex != null)
        {
            D2D_Helper.Record(alphaTex, "Modify Alpha Tex");
        }
#endif
        if (newPixels != null)
        {
            // Remake texture?
            if (alphaTex == null || alphaTex.width != newPixels.Width || alphaTex.height != newPixels.Height)
            {
                DestroyAlphaTex();

                alphaTex    = newPixels.Apply(TextureFormat.Alpha8);
                alphaScaleX = D2D_Helper.Reciprocal(alphaTex.width - 1);
                alphaScaleY = D2D_Helper.Reciprocal(alphaTex.height - 1);

                alphaTex.wrapMode = TextureWrapMode.Clamp;
            }
            // Copy settings over?
            else
            {
                alphaTex.SetPixels32(newPixels.Pixels);
                alphaTex.Apply();
            }

            UpdateRect(0, alphaTex.width, 0, alphaTex.height);
        }
        else
        {
            DestroyAlphaTex();

            UpdateRect(0, 0, 0, 0);
        }

        cachedSolidPixelCount = 0;
        alphaTexData          = null;
        alphaTexWidth         = 0;
        alphaTexHeight        = 0;
        Dirty = true;

        NotifyChanges();

#if UNITY_EDITOR
        D2D_Helper.SetDirty(this);
#endif
    }
Пример #2
0
    public static Vector3 GetBarycentric(Vector2 a, Vector2 b, Vector2 c, Vector2 p)
    {
        var barycentric = Vector3.zero;
        var v0          = b - a;
        var v1          = c - a;
        var v2          = p - a;
        var d00         = Vector2.Dot(v0, v0);
        var d01         = Vector2.Dot(v0, v1);
        var d11         = Vector2.Dot(v1, v1);
        var d20         = Vector2.Dot(v2, v0);
        var d21         = Vector2.Dot(v2, v1);
        var denom       = D2D_Helper.Reciprocal(d00 * d11 - d01 * d01);

        barycentric.y = (d11 * d20 - d01 * d21) * denom;
        barycentric.z = (d00 * d21 - d01 * d20) * denom;
        barycentric.x = 1.0f - barycentric.y - barycentric.z;

        return(barycentric);
    }
Пример #3
0
    // Doesn't reallocate memory if the new alpha data is the same size
    private void ReplaceAlphaData(byte[] newAlphaData, int newAlphaWidth, int newAlphaHeight)
    {
        var newAlphaTotal = newAlphaWidth * newAlphaHeight;

        if (alphaData == null || alphaData.Length != newAlphaTotal)
        {
            alphaData = new byte[newAlphaTotal];
        }

        for (var i = 0; i < newAlphaTotal; i++)
        {
            alphaData[i] = newAlphaData[i];
        }

        alphaWidth  = newAlphaWidth;
        alphaHeight = newAlphaHeight;
        alphaScaleX = D2D_Helper.Reciprocal(originalWidth);
        alphaScaleY = D2D_Helper.Reciprocal(originalHeight);
    }
	private void UpdateColliders()
	{
		if (ColliderType != D2D_SpriteColliderType.None)
		{
			var cellTransform = default(Transform);
			
			switch (ColliderType)
			{
				case D2D_SpriteColliderType.Edge:
				{
					DestroyAutoPolygonCollider();
					DestroyPolygonColliders();
					
					if (edgeColliders == null)
					{
						edgeColliders = D2D_Helper.CreateGameObject("Edge Colliders", transform).AddComponent<D2D_EdgeColliders>();
						edgeColliders.RebuildAllColliders(AlphaTex);
					}
					
					cellTransform = edgeColliders.transform;
				}
				break;
				
				case D2D_SpriteColliderType.Polygon:
				{
					DestroyAutoPolygonCollider();
					DestroyEdgeColliders();
					
					if (polygonColliders == null)
					{
						polygonColliders = D2D_Helper.CreateGameObject("Polygon Colliders", transform).AddComponent<D2D_PolygonColliders>();
						polygonColliders.RebuildAllColliders(AlphaTex);
					}
					
					cellTransform = polygonColliders.transform;
				}
				break;
				
				case D2D_SpriteColliderType.AutoPolygon:
				{
					DestroyPolygonColliders();
					DestroyEdgeColliders();
					
					if (autoPolygonCollider == null)
					{
						autoPolygonCollider = D2D_Helper.CreateGameObject("Auto Polygon Collider", transform).AddComponent<D2D_AutoPolygonCollider>();
						autoPolygonCollider.RebuildCollider(AlphaTex);
					}
					
					cellTransform = autoPolygonCollider.transform;
				}
				break;
			}
			
			if (cellTransform != null)
			{
				var cellScale  = Vector3.one;
				var cellOffset = Vector3.zero;
				
				if (sprite != null && MainTex != null && AlphaTex != null)
				{
					cellScale  = D2D_Helper.Reciprocal(PixelsToUnits) * D2D_Helper.Divide(MainTex.width, MainTex.height, AlphaTex.width, AlphaTex.height);
					cellOffset = sprite.bounds.min; cellOffset.z = 0.0f;
				}
				
				if (cellTransform.localPosition != cellOffset)
				{
					cellTransform.localPosition = cellOffset;
				}
				
				if (cellTransform.localScale != cellScale)
				{
					cellTransform.localScale = cellScale;
				}
			}
		}
		else
		{
			DestroyAutoPolygonCollider();
			DestroyPolygonColliders();
			DestroyEdgeColliders();
		}
	}