Changing a Models Material In Urho Forums

AdamPriceAdamPrice USMember

Hey,

I have an Urhosurface in a Xamarin Forum that contains about 20 separate models that together build a bag. I want the user to be able to change the material of some of the models using controls on the form. I have this working but sometimes the app will crash when changing the material. Here is the relevant code:

Form the urho application:

`public class Bag : Application
{
Scene scene;
Node plotNode;
Camera camera;
Node cameraNode;

        StaticModel body;

        List<Material> materials;
        Octree octree;
        public Model SelectedModel { get; private set; }

        [Preserve]
    public Bag(ApplicationOptions options) : base(options) { }

    protected override void Start ()
    {
        base.Start ();
        CreateScene ();
        SetupViewport ();
    }

async void CreateScene ()
{
            scene = new Scene ();
    octree = scene.CreateComponent<Octree> ();
             plotNode = scene.CreateChild();

            materials = new List<Material>
            {
                Material.FromImage("Textures/blue.png"),
                Material.FromImage("Textures/dark-leather.png"),
                Material.FromImage("Textures/red.png")
            };

         //Mainbody
         body = plotNode.CreateComponent<StaticModel>();
         body.Model = ResourceCache.GetModel("Models/bag_blkOut_sd-001.mdl", false);
         body.SetMaterial(materials[0]);

         //Top close string
         StaticModel stringModel = plotNode.CreateComponent<StaticModel>();
         stringModel .Model = ResourceCache.GetModel("Models/Ellipse001.mdl", false);
         stringModel .SetMaterial(materials[0]);

    .....

         cameraNode = scene.CreateChild ();
        camera = cameraNode.CreateComponent<Camera>();
         cameraNode.Position = new Vector3(0, 2.8f, 14) / 18.0f;
         cameraNode.Rotation = new Quaternion(0f, 1f, 0f, 0f);

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

         plotNode.Rotation = new Quaternion(0, 1, 0, 0);
     }

     public void changeTextures(int texturesIndex)
     {         
             body.SetMaterial(materials[textureIndex]);         >         
      } 

void SetupViewport ()
{
    var renderer = Renderer;
    renderer.SetViewport (0, new Viewport (Context, scene, camera, null));
}
}`

From the form with the urhosurface:

`public class ItemPage : ContentPage
{
UrhoSurface urhoSurface;
Bag urhoApp;
List<Xamarin.Forms.Color> materialColours = new List<Xamarin.Forms.Color>();
Label textures;
int colour = 0;

    public ItemPage()
    {
        materialColours.Add(Xamarin.Forms.Color.Blue);
        materialColours.Add(Xamarin.Forms.Color.Gray);
        materialColours.Add(Xamarin.Forms.Color.Red);

        textures = new Label
        {
            HeightRequest = 70,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            BackgroundColor = materialColours[0],
        };

        var panGesture = new PanGestureRecognizer();
        panGesture.PanUpdated += (s, e) => {
            if (e.StatusType == GestureStatus.Running)
                xTotalTextures += e.TotalX;
            if (e.StatusType == GestureStatus.Completed)
                textureSwipe();
        };
        textures.GestureRecognizers.Add(panGesture);

        var button = new Button()
        {
            Text = "Apply",
            HorizontalOptions = LayoutOptions.FillAndExpand,
            HeightRequest = 40,
        };

        button.Clicked += (sender, e) => urhoApp.changeTextures(colour);

        urhoSurface = new UrhoSurface();
        urhoSurface.VerticalOptions = LayoutOptions.FillAndExpand;
        urhoSurface.HorizontalOptions = LayoutOptions.FillAndExpand;

        Content = new StackLayout
        {
            Padding = new Thickness(12, 12, 12, 40),
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = {
                urhoSurface,
                textures,
                button,
            }
        };
    }

    private void textureSwipe()
    {
        if (xTotalTextures > 0)
        {
            colour++;
        }
        if (xTotalTextures < 0)
        {
            colour--;
        }
        if (colour == texturesColours.Count)
        {
            colour = 0;
        }
        else if (colour == -1)
        {
            colour = texturesColours.Count - 1;
        }

        xTotalTextures = 0;
        textures.BackgroundColor = texturesColours[colour];
    }

    protected override async void OnAppearing()
    {
        StartUrhoApp();
    }

    async void StartUrhoApp()
    {
        urhoApp = await urhoSurface.Show<Bag>(new ApplicationOptions("Data"));          
    } 
}`

Any help would be greatly appricated, thanks.

Sign In or Register to comment.