ZapWorks Studio: Changing an Object's Texture Using a Button

Hey everyone,

I created a short tutorial in response to a user’s question on the forum but figured it’s worth adding here so it’s easier to find.

Enjoy!

Download the assets used in the tutorial from here.

Prefer to look at the final version? Download the completed project here

Before we begin, there’s a couple of things to note:

  • I’ve used a pod file, rather than an obj or fbx, but it should work with either.
  • The shading effect on the model was done manually by using a photo editor on the object’s UV map. It is not a result of dynamic lighting within Studio.

Now, for the actual steps.

  1. Import your 3D model to your scene, and double click on the resulting symbol in the 'Symbol Definitions’ panel.

image

  1. Import the texture files you’ll be using by dragging them into the 3D symbol’s 'Media Library’ panel.

image

  1. In the hierarchy, right click the 3D model’s node and select ‘New > Script > blank’.

image

  1. At the top of the script, create references for the texture files you want the object to switch between.
    Do this by creating a new ‘Z.ImageTexture’ object, and passing in the texture file’s name as the argument. E.g. ‘var newImageTexture = Z.ImageTexture (“myTextureFile”);

  2. Below the variables, create a new function and use the parent object’s ‘.skin’ function to change the object’s texture, passing in a previously created ‘Z.ImageTexture’ as the argument.
    E.g. ‘export function myTextureChangeFunction() {
    parent.skin (newImageTexture)}

    Create a new function for each texture file you’d like to apply to the model.

  • Remember to use the ‘export’ keyword before the function’s declaration so that we can call them from the parent symbol (more on this later).

image

  1. That’s all within the 3D model’s subsymbol, so head back to the parent symbol. Create an instance of the 3D object by dragging it into the ‘Hierarchy’ from the ‘Symbol Definitions’ panel.

image

  • For purposes of this demo, I’ve created a simple button by dragging in a ‘Plane’ from the ‘Media Library’ and adding a ‘pointerdown’ script to it, but feel free to add the following code to your own button.
  1. At the top of the script, create a new variable that references the 3D object’s instance by dragging the instance’s node in the hierarchy to the script, and selecting ‘Insert local variable’.

AddObjectRef

  1. Finally, we can use the variable we’ve just created to access and call the function that we coded within the 3D model’ssymbol. E.g. ‘myVariable.nodes.myScript.myTextureChangeFunction();

image

  • If we hadn’t used the ‘export’ keyword before when declaring our function, we would be able to access the script, but not call the function.
    Without that keyword, the function only has ‘local’ scope, but by using ‘export’ we ensure it has ‘global’ scope, so that it can be called from its parent symbol.
    Here’s a helpful docs page with more info on subsymbol communication:
    https://docs.zap.works/studio/subsymbols/subsymbol-communication/

Hope this proves helpful to some of you.
Seb

2 Likes

Nice example! Will there be more like this examples in te near future? :slight_smile: :slight_smile: :slight_smile:

Hi there,

We’re always looking to create more tutorials and examples for our users, so it’s definitely something we’ll be working on.

If there’s any specific examples you think could benefit our wider audience feel free to make suggestions :slight_smile:

Thanks,
Seb