0

我正在使用 Xamarin.Forms + Urhosharp,我在从球体上的图像设置纹理时遇到了问题。问题是 texture.Load 或 texture.SetData 总是返回 false。我确实尝试了不同的方法,如 SetData、Load、调整纹理和图像的大小(到 2 的幂)......但它们都没有奏效。这是我的代码:

    private async void CreateScene()
    {
        Input.SubscribeToTouchEnd(OnTouched);

        _scene = new Scene();
        _octree = _scene.CreateComponent<Octree>();

        _plotNode = _scene.CreateChild();
        var baseNode = _plotNode.CreateChild().CreateChild();
        var plane = baseNode.CreateComponent<StaticModel>();
        plane.Model = CoreAssets.Models.Sphere;

        var cameraNode = _scene.CreateChild();
        _camera = cameraNode.CreateComponent<Camera>();
        cameraNode.Position = new Vector3(10, 15, 10) / 1.75f;
        cameraNode.Rotation = new Quaternion(-0.121f, 0.878f, -0.305f, -0.35f);

        Node lightNode = cameraNode.CreateChild();
        var light = lightNode.CreateComponent<Light>();
        light.LightType = LightType.Point;
        light.Range = 100;
        light.Brightness = 1.3f;

        int size = 3;
        baseNode.Scale = new Vector3(size * 1.5f, 1, size * 1.5f);

        var imageStream = await new HttpClient().GetStreamAsync("some 512 * 512 jpg image");
        var ms = new MemoryStream();
        imageStream.CopyTo(ms);

        var image = new Image();
        var isLoaded = image.Load(new MemoryBuffer(ms));
        if (!isLoaded)
        {
            throw new Exception();
        }

        var texture = new Texture2D();
        //var isTextureLoaded = texture.Load(new MemoryBuffer(ms.ToArray()));
        var isTextureLoaded = texture.SetData(image);
        if (!isTextureLoaded)
        {
            throw new Exception();
        }

        var material = new Material();
        material.SetTexture(TextureUnit.Diffuse, texture);
        material.SetTechnique(0, CoreAssets.Techniques.Diff, 0, 0);
        plane.SetMaterial(material);

        try
        {
            await _plotNode.RunActionsAsync(new EaseBackOut(new RotateBy(2f, 0, 360, 0)));
        }
        catch (OperationCanceledException) { }
    }

请帮忙!

4

1 回答 1

0

要从 2D 纹理创建材质,可以使用 Material.FromImage 。

有关更多详细信息,请参阅以下文档。

model.SetMaterial(Material.FromImage("earth.jpg"));

https://developer.xamarin.com/api/type/Urho.Material/ https://developer.xamarin.com/api/member/Urho.Material.FromImage/p/System.String/

private async void CreateScene()
{


   _scene = new Scene();
   _octree = _scene.CreateComponent<Octree>();

   _plotNode = _scene.CreateChild();
   var baseNode = _plotNode.CreateChild().CreateChild();


   var plane = _plotNode.CreateComponent<StaticModel>();
   plane.Model = CoreAssets.Models.Sphere;
   plane.SetMaterial(Material.FromImage("earth.jpg"));

   var cameraNode = _scene.CreateChild();
   _camera = cameraNode.CreateComponent<Camera>();
   cameraNode.Position = new Vector3(10, 15, 10) / 1.75f;

   cameraNode.Rotation = new Quaternion(-0.121f, 0.878f, -0.305f, -0.35f);

   Node lightNode = cameraNode.CreateChild();
   var light = lightNode.CreateComponent<Light>();
   light.LightType = LightType.Point;
   light.Range = 100;
   light.Brightness = 1.3f;

   int size = 3;
   baseNode.Scale = new Vector3(size * 1.5f, 1, size * 1.5f);

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

   try
   {
       await _plotNode.RunActionsAsync(new EaseBackOut(new RotateBy(2f, 0, 360, 0)));
   }
   catch (OperationCanceledException) { }

}
于 2018-09-10T17:50:28.100 回答