protected override void FillTexture(Texture2D texture)
    {
        if (Data == null)
        {
            return;
        }
        Data.BeginInputInit();
        _color     = Data.CteateOutput(ValueType.Color, "Color");
        _position  = Data.CteateInput(ValueType.Vector2, "Position");
        _rToBorder = Data.CteateInput(ValueType.Float, "Radius To Border");
        _rToCorner = Data.CteateInput(ValueType.Float, "Radius To Corner");
        _angle     = Data.CteateInput(ValueType.Float, "Angle");
        Data.EndInputInit();

        if (Input == null)
        {
            Input = new NodeDataInput();
        }
        Input.InitializeFrom(Data);
        Input.SetTo(Data);

        int width  = texture.width;
        int height = texture.height;

        Color[] colors       = new Color[width * height];
        float   halfWidth    = width * 0.5f;
        float   halfHeight   = height * 0.5f;
        float   cornerlength = Mathf.Sqrt(2);

        for (int i = 0; i < colors.Length; i++)
        {
            float y = (int)(i / width);
            float x = i - y * width;
            y -= halfHeight - 0.5f;
            x -= halfWidth - 0.5f;
            Vector2 position = new Vector2(x / halfWidth, y / halfHeight);

            float r         = position.magnitude;
            float rToBorder = r;
            float rToCorner = r / cornerlength;
            float angle     = 0.5f + Mathf.Atan2(-x, -y) / (2 * Mathf.PI);

            Data.SetVector2(_position, new Vector2(position.x * 0.5f + 0.5f, position.y * 0.5f + 0.5f));
            Data.SetFloat(_rToBorder, rToBorder);
            Data.SetFloat(_rToCorner, rToCorner);
            Data.SetFloat(_angle, angle);
            Data.Process();
            colors[i] = Data.GetColor(_color);
        }

        texture.SetPixels(colors);
        texture.Apply();
        Texture = texture;
    }
Ejemplo n.º 2
0
 void OnEnable()
 {
     if (Data == null)
     {
         return;
     }
     Data.Prepare();
     if (Input == null)
     {
         Input = new NodeDataInput();
     }
     Input.InitializeFrom(Data);
 }
    void GenerateMesh(Mesh mesh, int width, int height)
    {
        if (Data == null)
        {
            return;
        }
        Data.BeginInputInit();

        _x      = Data.CteateInput(ValueType.Float, "X");
        _y      = Data.CteateInput(ValueType.Float, "Y");
        _width  = Data.CteateInput(ValueType.Float, "Width");
        _height = Data.CteateInput(ValueType.Float, "Height");
        _start  = Data.CteateInput(ValueType.Bool, "Start");

        _position = Data.CteateOutput(ValueType.Vector3, "Position");
        _normal   = Data.CteateOutput(ValueType.Vector3, "Normal");
        _tangent  = Data.CteateOutput(ValueType.Vector4, "Tangent");
        _color    = Data.CteateOutput(ValueType.Color, "Color");
        _uv0      = Data.CteateOutput(ValueType.Vector2, "UV0");
        _uv1      = Data.CteateOutput(ValueType.Vector2, "UV1");
        _uv2      = Data.CteateOutput(ValueType.Vector2, "UV2");
        _uv3      = Data.CteateOutput(ValueType.Vector2, "UV3");

        Data.EndInputInit();

        if (Input == null)
        {
            Input = new NodeDataInput();
        }
        Input.InitializeFrom(Data);
        Input.SetTo(Data);


        int vertWidth  = width + 1;
        int vertHeight = height + 1;


        int verticesCount = vertWidth * vertHeight;

        Vector3[] positions = IsConnected(_position) ? new Vector3[verticesCount] : null;
        Vector3[] normals   = IsConnected(_normal) ? new Vector3[verticesCount] : null;
        Vector4[] tangents  = IsConnected(_tangent) ? new Vector4[verticesCount] : null;
        Color[]   colors    = IsConnected(_color) ? new Color[verticesCount] : null;
        Vector2[] uv0       = IsConnected(_uv0) ? new Vector2[verticesCount] : null;
        Vector2[] uv1       = IsConnected(_uv1) ? new Vector2[verticesCount] : null;
        Vector2[] uv2       = IsConnected(_uv2) ? new Vector2[verticesCount] : null;
        Vector2[] uv3       = IsConnected(_uv3) ? new Vector2[verticesCount] : null;

        if (positions == null)
        {
            mesh.Clear();
            return;
        }

        Data.SetFloat(_width, width);
        Data.SetFloat(_height, height);

        for (int y = 0, i = 0; y < vertHeight; y++)
        {
            for (int x = 0; x < vertWidth; x++, i++)
            {
                float fx = x / (float)width;
                float fy = y / (float)height;

                Data.SetBool(_start, i == 0);
                Data.SetFloat(_x, fx);
                Data.SetFloat(_y, fy);
                Data.Process();
                positions[i] = Data.GetVector3(_position);
                if (normals != null)
                {
                    normals[i] = Data.GetVector3(_normal);
                }
                if (tangents != null)
                {
                    tangents[i] = Data.GetVector4(_tangent);
                }
                if (colors != null)
                {
                    colors[i] = Data.GetColor(_color);
                }
                if (uv0 != null)
                {
                    uv0[i] = Data.GetVector2(_uv0);
                }
                if (uv1 != null)
                {
                    uv1[i] = Data.GetVector2(_uv1);
                }
                if (uv2 != null)
                {
                    uv2[i] = Data.GetVector2(_uv2);
                }
                if (uv3 != null)
                {
                    uv3[i] = Data.GetVector2(_uv3);
                }
            }
        }

        // \ | \ |
        // - 0 - 1 -
        // \ | \ | \
        // - 2 - 3 -
        //   | \ | \
        int[] triangles = new int[width * height * 6];
        for (int y = 0, t = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                int v0 = y * vertWidth + x;
                int v1 = v0 + 1;
                int v2 = v0 + vertWidth;
                int v3 = v2 + 1;

                triangles[t++] = v0;
                triangles[t++] = v2;
                triangles[t++] = v3;
                triangles[t++] = v3;
                triangles[t++] = v1;
                triangles[t++] = v0;
            }
        }

        mesh.Clear();
        mesh.vertices  = positions;
        mesh.triangles = triangles;
        if (normals != null)
        {
            mesh.normals = normals;
        }
        if (tangents != null)
        {
            mesh.tangents = tangents;
        }
        if (colors != null)
        {
            mesh.colors = colors;
        }
        if (uv0 != null)
        {
            mesh.uv = uv0;
        }
        if (uv1 != null)
        {
            mesh.uv2 = uv1;
        }
        if (uv2 != null)
        {
            mesh.uv3 = uv2;
        }
        if (uv3 != null)
        {
            mesh.uv4 = uv3;
        }
        mesh.RecalculateBounds();
        if (RecalculateNormals)
        {
            mesh.RecalculateNormals();
        }
        if (RecalculateTangents)
        {
            mesh.RecalculateTangents();
        }
    }
    protected override void FillTexture(Texture2D texture)
    {
        if (Data == null || Source == null) return;
        bool isReadable = false;
#if UNITY_EDITOR
        string path = UnityEditor.AssetDatabase.GetAssetPath(Source);
        UnityEditor.TextureImporter ti = (UnityEditor.TextureImporter)UnityEditor.TextureImporter.GetAtPath(path);
        if (ti != null)
            isReadable = ti.isReadable;
#endif
        if (!isReadable) return;

        Data.BeginInputInit();
        _in0 = Data.CteateInput(ValueType.Color, "In 0");
        _in1 = Data.CteateInput(ValueType.Color, "In 1");
        _in2 = Data.CteateInput(ValueType.Color, "In 2");
        _in3 = Data.CteateInput(ValueType.Color, "In 3");
        _original = Data.CteateInput(ValueType.Color, "Original");

        _pos = Data.CteateInput(ValueType.Vector2, "Position");
        _mipPos = Data.CteateInput(ValueType.Float, "Mip Position");
        _mipCount = Data.CteateInput(ValueType.Float, "Mip Count");

        _out = Data.CteateOutput(ValueType.Color, "Color");
        Data.EndInputInit();

        if (Input == null) Input = new NodeDataInput();
        Input.InitializeFrom(Data);
        Input.SetTo(Data);


        int mipmapCount = 1 + (int)Mathf.Log(Mathf.Max(Source.width, Source.height), 2);
        Color[] lastColors = null;

        Data.SetFloat(_mipCount, mipmapCount);
        for (int mI = 0; mI < mipmapCount; mI++)
        {
            int mipWidth = Mathf.Max(texture.width >> mI, 1);
            int mipHeight = Mathf.Max(texture.height >> mI, 1);
            int lastMipWidth = Mathf.Max(texture.width >> (mI - 1), 1);
            int lastMipHeight = Mathf.Max(texture.height >> (mI - 1), 1);
            int mipDelta = lastMipWidth > mipWidth ? 2 : 1;
            int mipDeltaY = mipDelta * (lastMipHeight > mipHeight ? 2 : 1);

            Data.SetFloat(_mipPos, mI / (float)mipmapCount);

            Color[] colors = lastColors == null || Source.mipmapCount == mipmapCount ? Source.GetPixels(mI) : new Color[mipWidth * mipHeight];

            if (lastColors != null)
            {
                for (int i = 0; i < colors.Length; i++)
                {
                    int y = i / mipWidth;
                    int x = i - y * mipWidth;
                    int i2 = x * mipDelta + y * mipWidth * mipDeltaY;
                    if (lastColors.Length < 4) lastMipWidth = 0;


                    Data.SetVector2(_pos, new Vector2(x / (float)mipWidth, y / (float)mipHeight));
                    Data.SetColor(_in0, lastColors[i2]);
                    Data.SetColor(_in1, lastColors[i2 + 1]);
                    Data.SetColor(_in2, lastColors[i2 + lastMipWidth]);
                    Data.SetColor(_in3, lastColors[i2 + lastMipWidth + 1]);
                    Data.SetColor(_original, colors[i]);

                    Data.Process();
                    colors[i] = Data.GetColor(_out);
                }


            }

            texture.SetPixels(colors, mI);
            lastColors = colors;
        }
        texture.Apply(false);
    }