コード例 #1
0
    public override void OnImportAsset(AssetImportContext ctx)
    {
        // We're using a hardcoded window size of 100x100. This way, using a pixels per point value of 100
        // results in a sprite of size 1 when the SVG file has a viewbox specified.
        SVGParser.SceneInfo sceneInfo;
        using (StreamReader stream = new StreamReader(ctx.assetPath))
        {
            sceneInfo = SVGParser.ImportSVG(stream, 0, 1, 100, 100, PreserveViewport);
        }

        if (sceneInfo.Scene == null || sceneInfo.Scene.Root == null)
        {
            throw new Exception("Wowzers!");
        }

        float stepDist         = StepDistance;
        float samplingStepDist = SamplingStepDistance;
        float maxCord          = MaxCordDeviationEnabled ? MaxCordDeviation : float.MaxValue;
        float maxTangent       = MaxTangentAngleEnabled ? MaxTangentAngle : Mathf.PI * 0.5f;

        if (!AdvancedMode)
        {
            // Automatically compute sensible tessellation options from the
            // vector scene's bouding box and target resolution
            ComputeTessellationOptions(sceneInfo, TargetResolution, ResolutionMultiplier, out stepDist, out maxCord, out maxTangent);
        }

        var tessOptions = new ShapeUtils.TessellationOptions();

        tessOptions.MaxCordDeviation     = maxCord;
        tessOptions.MaxTanAngleDeviation = maxTangent;
        tessOptions.SamplingStepSize     = 1.0f / (float)samplingStepDist;
        tessOptions.StepDistance         = stepDist;

        var rect = Rect.zero;

        if (PreserveViewport)
        {
            rect = sceneInfo.SceneViewport;
        }

        var geometry = ShapeUtils.TessellateScene(sceneInfo.Scene, tessOptions, sceneInfo.NodeOpacity);

        string name = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath);

        var gameObject = new GameObject("Shape" + name, typeof(MeshFilter), typeof(MeshRenderer));

        var mesh = new Mesh();

        mesh.name = "Mesh" + name;
        CalculateSideExtrusion(mesh, geometry);

        mesh.RecalculateBounds();
        mesh.RecalculateNormals();

        var mat = new Material(Shader.Find("Standard"));

        mat.name = "Material" + name;

        gameObject.GetComponent <MeshFilter>().mesh       = mesh;
        gameObject.GetComponent <MeshRenderer>().material = mat;

        ctx.AddObjectToAsset("shape", gameObject);
        ctx.AddObjectToAsset("mesh", mesh);
        ctx.AddObjectToAsset("material", mat);
        ctx.SetMainObject(gameObject);
    }