Exemplo n.º 1
0
        protected Material GetColorMaterial(SVGFill fill)
        {
            Material output = null;
            Shader   shader = null;

            switch (fill.blend)
            {
            case FILL_BLEND.OPAQUE:
                shader = SVGShader.SolidColorOpaque;
                break;

            case FILL_BLEND.ALPHA_BLENDED:
                shader = SVGShader.SolidColorAlphaBlended;
                break;

            case FILL_BLEND.ADDITIVE:
                shader = SVGShader.SolidColorAdditive;
                break;

            case FILL_BLEND.MULTIPLY:
                shader = SVGShader.SolidColorMultiply;
                break;

            default:
                shader = SVGShader.SolidColorOpaque;
                break;
            }

            for (int i = 0; i < materials.Count; i++)
            {
                if (materials [i] == null)
                {
                    continue;
                }
                if (materials [i].shader != shader)
                {
                    continue;
                }

                output = materials [i];
            }

            if (output == null)
            {
                output = new Material(shader);
                materials.Add(output);
            }
            return(output);
        }
Exemplo n.º 2
0
        public Material GetMaterial(SVGFill fill)
        {
            Material output = null;

            switch (fill.fillType)
            {
            case FILL_TYPE.SOLID:
                output = GetColorMaterial(fill);
                break;

            case FILL_TYPE.GRADIENT:
                output = GetGradientMaterial(fill);
                break;

            case FILL_TYPE.TEXTURE:
                break;
            }
            return(output);
        }
Exemplo n.º 3
0
Arquivo: SVG.cs Projeto: stuart2w/SAW
        private static XAttribute GetStyleAttribute(SVGStroke stroke, SVGFill fill)
        {
            // Returns the "style" attribute which can be added to most elements
            string str;

            if (fill == null)
            {
                str = "fill:none; ";
            }
            else
            {
                str = fill.GetStyle() + " ";
            }
            if (stroke != null)
            {
                str += stroke.GetStyle();
            }
            return(new XAttribute("style", str));
        }
Exemplo n.º 4
0
        protected Material GetGradientMaterial(SVGFill fill)
        {
            Material output = null;
            Shader   shader = null;

            switch (fill.blend)
            {
            case FILL_BLEND.OPAQUE:
                shader = SVGShader.GradientColorOpaque;
                break;

            case FILL_BLEND.ALPHA_BLENDED:
                shader = SVGShader.GradientColorAlphaBlended;
                break;

            case FILL_BLEND.ADDITIVE:
                shader = SVGShader.GradientColorAdditive;
                break;

            case FILL_BLEND.MULTIPLY:
                shader = SVGShader.GradientColorMultiply;
                break;

            default:
                shader = SVGShader.GradientColorOpaque;
                break;
            }

            for (int i = 0; i < materials.Count; i++)
            {
                if (materials [i] == null)
                {
                    continue;
                }
                if (materials [i].shader != shader)
                {
                    continue;
                }
                if (fill.gradientColors.atlasIndex < 0 || fill.gradientColors.atlasIndex >= atlasTextures.Count)
                {
                    continue;
                }
                Texture texture = atlasTextures [fill.gradientColors.atlasIndex];
                if (texture == null)
                {
                    continue;
                }
                if (materials [i].GetTexture("_GradientColor") != texture)
                {
                    continue;
                }

                output = materials [i];
                output.SetTexture("_GradientShape", gradientShapeTexture);
                output.SetVector("_Params", new Vector4(atlasTextureWidth, atlasTextureHeight, gradientWidth, gradientHeight));
            }

            if (output == null)
            {
                output = new Material(shader);
                Texture2D texture = atlasTextures [fill.gradientColors.atlasIndex];
                output.SetTexture("_GradientColor", texture);
                output.SetTexture("_GradientShape", gradientShapeTexture);
                output.SetVector("_Params", new Vector4(atlasTextureWidth, atlasTextureHeight, gradientWidth, gradientHeight));
                materials.Add(output);
            }

            return(output);
        }
Exemplo n.º 5
0
 public SVGFill GetGradient(SVGFill gradient)
 {
     gradient.gradientColors = GetGradient(gradient.gradientColors);
     return(gradient);
 }
Exemplo n.º 6
0
        public SVGMesh(Mesh mesh, SVGFill svgFill, float opacity = 1f)
        {
            if (mesh == null)
            {
                return;
            }

            _name = mesh.name;
            _fill = svgFill.Clone();
            if (_fill.blend == FILL_BLEND.OPAQUE)
            {
                if (opacity < 1f)
                {
                    _fill.blend = FILL_BLEND.ALPHA_BLENDED;
                }
            }

            int length          = mesh.vertices.Length;
            int trianglesLength = mesh.triangles.Length;

            _vertices = new Vector3[length];

            float minX = float.MaxValue;
            float maxX = float.MinValue;
            float minY = float.MaxValue;
            float maxY = float.MinValue;
            float minZ = float.MaxValue;
            float maxZ = float.MinValue;

            for (int i = 0; i < length; i++)
            {
                _vertices [i].x = mesh.vertices [i].x * SVGAssetImport.meshScale;
                _vertices [i].y = mesh.vertices [i].y * SVGAssetImport.meshScale;
                _vertices [i].z = _depth * -SVGAssetImport.minDepthOffset;

                if (_vertices [i].x < minX)
                {
                    minX = _vertices [i].x;
                }
                else if (_vertices [i].x > maxX)
                {
                    maxX = _vertices [i].x;
                }

                if (_vertices [i].y < minY)
                {
                    minY = _vertices [i].y;
                }
                else if (_vertices [i].y > maxY)
                {
                    maxY = _vertices [i].y;
                }

                if (_vertices [i].z < minZ)
                {
                    minZ = _vertices [i].z;
                }
                else if (_vertices [i].z > maxZ)
                {
                    maxZ = _vertices [i].z;
                }
            }

            _bounds = new Bounds(new Vector3(Mathf.Lerp(minX, maxX, 0.5f),
                                             Mathf.Lerp(minY, maxY, 0.5f),
                                             Mathf.Lerp(minZ, maxZ, 0.5f))
                                 , new Vector3(maxX - minX,
                                               maxY - minY,
                                               maxZ - minZ
                                               ));

            _triangles = new int[trianglesLength];

            // Correct Winding
            int     triangle0, triangle1, triangle2;
            Vector2 vector0, vector1, vector2;
            float   winding;

            for (int i = 0; i < trianglesLength; i += 3)
            {
                triangle0 = mesh.triangles [i];
                triangle1 = mesh.triangles [i + 1];
                triangle2 = mesh.triangles [i + 2];

                vector0 = _vertices[triangle0];
                vector1 = _vertices[triangle1];
                vector2 = _vertices[triangle2];

                winding  = (vector1.x - vector0.x) * (vector1.y + vector0.y);
                winding += (vector2.x - vector1.x) * (vector2.y + vector1.y);
                winding += (vector0.x - vector2.x) * (vector0.y + vector2.y);

                if (winding < 0)
                {
                    _triangles [i]     = mesh.triangles [i];
                    _triangles [i + 1] = mesh.triangles [i + 1];
                    _triangles [i + 2] = mesh.triangles [i + 2];
                }
                else
                {
                    _triangles [i]     = mesh.triangles [i];
                    _triangles [i + 2] = mesh.triangles [i + 1];
                    _triangles [i + 1] = mesh.triangles [i + 2];
                }
            }

            if (mesh.colors32 != null && mesh.colors32.Length > 0)
            {
                _colors = new Color32[length];
                for (int i = 0; i < length; i++)
                {
                    _colors [i] = mesh.colors32 [i];
                    if (opacity != 1f)
                    {
                        _colors[i].a = (byte)Mathf.RoundToInt(((_colors[i].a / 255f) * opacity) * 255);
                    }
                }
            }
            else
            {
                _colors = new Color32[length];
                Color32 color = new Color32((byte)255, (byte)255, (byte)255, (byte)Mathf.RoundToInt(opacity * 255));
                for (int i = 0; i < length; i++)
                {
                    _colors [i] = color;
                }
            }

            if (mesh.uv != null && mesh.uv.Length > 0)
            {
                _uvs = new Vector2[length];
                for (int i = 0; i < length; i++)
                {
                    _uvs [i] = mesh.uv [i];
                }
            }
            else
            {
                _uvs = new Vector2[length];
            }

            if (mesh.uv2 != null && mesh.uv2.Length > 0)
            {
                _uvs2 = new Vector2[length];
                for (int i = 0; i < length; i++)
                {
                    _uvs2 [i] = mesh.uv2 [i];
                }
            }
            else
            {
                _uvs2 = new Vector2[length];
            }
        }
Exemplo n.º 7
0
        public SVGMesh(Mesh mesh, SVGFill svgFill, float opacity = 1f)
        {
            if (mesh == null)
                return;

            _name = mesh.name;
            _fill = svgFill.Clone();
            if(_fill.blend == FILL_BLEND.OPAQUE)
            {
                if(opacity < 1f) _fill.blend = FILL_BLEND.ALPHA_BLENDED;
            }

            int length = mesh.vertices.Length;
            int trianglesLength = mesh.triangles.Length;

            _vertices = new Vector3[length];

            float minX = float.MaxValue;
            float maxX = float.MinValue;
            float minY = float.MaxValue;
            float maxY = float.MinValue;
            float minZ = float.MaxValue;
            float maxZ = float.MinValue;

            for (int i = 0; i < length; i++)
            {
                _vertices [i].x = mesh.vertices [i].x * SVGAssetImport.meshScale;
                _vertices [i].y = mesh.vertices [i].y * SVGAssetImport.meshScale;
                _vertices [i].z = _depth * -SVGAssetImport.minDepthOffset;

                if(_vertices [i].x < minX) minX = _vertices [i].x;
                else if(_vertices [i].x > maxX) maxX = _vertices [i].x;

                if(_vertices [i].y < minY) minY = _vertices [i].y;
                else if(_vertices [i].y > maxY) maxY = _vertices [i].y;

                if(_vertices [i].z < minZ) minZ = _vertices [i].z;
                else if(_vertices [i].z > maxZ) maxZ = _vertices [i].z;
            }

            _bounds = new Bounds(new Vector3(Mathf.Lerp(minX, maxX, 0.5f),
                                             Mathf.Lerp(minY, maxY, 0.5f),
                                             Mathf.Lerp(minZ, maxZ, 0.5f))
                                 , new Vector3(maxX - minX,
                                                maxY - minY,
                                                maxZ - minZ
                          ));

            _triangles = new int[trianglesLength];

            // Correct Winding
            int triangle0, triangle1, triangle2;
            Vector2 vector0, vector1, vector2;
            float winding;
            for (int i = 0; i < trianglesLength; i+=3)
            {
                triangle0 = mesh.triangles [i];
                triangle1 = mesh.triangles [i + 1];
                triangle2 = mesh.triangles [i + 2];

                vector0 = _vertices[triangle0];
                vector1 = _vertices[triangle1];
                vector2 = _vertices[triangle2];

                winding = (vector1.x - vector0.x) * (vector1.y + vector0.y);
                winding += (vector2.x - vector1.x) * (vector2.y + vector1.y);
                winding += (vector0.x - vector2.x) * (vector0.y + vector2.y);

                if(winding < 0)
                {
                    _triangles [i] = mesh.triangles [i];
                    _triangles [i + 1] = mesh.triangles [i + 1];
                    _triangles [i + 2] = mesh.triangles [i + 2];
                } else {
                    _triangles [i] = mesh.triangles [i];
                    _triangles [i + 2] = mesh.triangles [i + 1];
                    _triangles [i + 1] = mesh.triangles [i + 2];
                }
            }

            if (mesh.colors32 != null && mesh.colors32.Length > 0)
            {
                _colors = new Color32[length];
                for (int i = 0; i < length; i++)
                {
                    _colors [i] = mesh.colors32 [i];
                    if(opacity != 1f) _colors[i].a = (byte)Mathf.RoundToInt(((_colors[i].a / 255f) * opacity) * 255);
                }
            } else {
                _colors = new Color32[length];
                Color32 color = new Color32((byte)255, (byte)255, (byte)255, (byte)Mathf.RoundToInt(opacity * 255));
                for (int i = 0; i < length; i++)
                {
                    _colors [i] = color;
                }
            }

            if (mesh.uv != null && mesh.uv.Length > 0)
            {
                _uvs = new Vector2[length];
                for (int i = 0; i < length; i++)
                {
                    _uvs [i] = mesh.uv [i];
                }
            } else {
                _uvs = new Vector2[length];
            }

            if (mesh.uv2 != null && mesh.uv2.Length > 0)
            {
                _uvs2 = new Vector2[length];
                for (int i = 0; i < length; i++)
                {
                    _uvs2 [i] = mesh.uv2 [i];
                }
            } else {
                _uvs2 = new Vector2[length];
            }
        }