Search code examples
xamarinxamarin.formsxamarin.iosjpegurhosharp

Urhosharp Material.FromImage not woking with some jpg files


I'm using Xamarin.Forms with Urhosharp in my project. I'm tring to set a matrial from an image on a sphere, everything is OK in my Android project but in iOS project, when I set material from some jpg files it doesn't work and all I get is a black screen.

Here is the jpg that works correctly: enter image description here

And here is the other one that doesn't: enter image description here

This is my code:

            var scene = new Scene();
        scene.CreateComponent<Octree>();


        // Node (Rotation and Position)
        var node = scene.CreateChild("room");
        node.Position = new Vector3(0, 0, 0);
        //node.Rotation = new Quaternion(10, 60, 10);
        node.SetScale(1f);

        // Model
        var modelObject = node.CreateComponent<StaticModel>();
        modelObject.Model = ResourceCache.GetModel("CustomModels/SmoothSphere.mdl");

        var zoneNode = scene.CreateChild("Zone");
        var zone = zoneNode.CreateComponent<Zone>();
        zone.SetBoundingBox(new BoundingBox(-300.0f, 300.0f));
        zone.AmbientColor = new Color(1f, 1f, 1f);

        //get image from byte[]

        //var url = "http://www.wsj.com/public/resources/media/0524yosemite_1300R.jpg";
        //var wc = new WebClient() { Encoding = Encoding.UTF8 };

        //var mb = new MemoryBuffer(wc.DownloadData(new Uri(url)));
        var mb = new MemoryBuffer(PanoramaBuffer.PanoramaByteArray);
        var image = new Image(Context) { Name = "MyImage" };
        image.Load(mb);

        //or from resource

        //var image = ResourceCache.GetImage("Textures/grave.jpg");
        var isFliped = image.FlipHorizontal();
        if (!isFliped)
        {
            throw new Exception("Unsuccessful flip");
        }
        var m = Material.FromImage("1.jpg");
        m.SetTechnique(0, CoreAssets.Techniques.DiffNormal, 0, 0);
        m.CullMode = CullMode.Cw;
        //m.SetUVTransform(Vector2.Zero, 0, 0);
        modelObject.SetMaterial(m);

        // Camera
        var cameraNode = scene.CreateChild("camera");
        _camera = cameraNode.CreateComponent<Camera>();
        _camera.Fov = 75.8f;
        _initialZoom = _camera.Zoom;

        // Viewport
        Renderer.SetViewport(0, new Viewport(scene, _camera, null));

I already tried to change compression level, ICCC profile and ...


Solution

  • I asked the same question in forums.xamarin.com and someone answered the question and I'll share it here :

    In iOS every texture needs to have a power of two resolution, like 256 x 256 or 1024 x 512. Check if that is the issue. Additionally check that your using the latest UrhoSharp version.

    Also make sure that the image is set as BundleResource in the iOS project.