Author Topic: Using SetMatrix on UITexture  (Read 9549 times)

Kimos

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Using SetMatrix on UITexture
« on: June 27, 2014, 02:36:56 AM »
I have a simple custom shader I am trying to use with UITexture to rotate an image on a UITexture. I am trying to use UITexture.material.SetMatrix to set the rotation, however UITexture seems to be ignoring all SetMatrix calls. Aside from the lack of rotation, the texture is rendering just fine. I have confirmed that my shader and matrix init code works perfectly on a standard Unity renderer.

Here is how I have it setup:
I have a GameObject with a UITexture component.
That UITexture component is using a material that is using my custom shader.
In a script file, I set the UITexture.material.mainTexture to something.
I then try to set the UITexture.material.SetMatrix.
I get no error messages and there appears to be no effect on the SetMatrix call (although changing the material.mainTexture is working fine)

What do I need to do to make the SetMatrix stick?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #1 on: June 27, 2014, 01:06:43 PM »
You can modify UITexture.drawCall.dynamicMaterial, however this is not recommended. NGUI batches similar materials together, creating a copy of the material in the process. Modifying the draw call's dynamic material will work as long as you only have one widget like that and its material is not shared with any other.

Kimos

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #2 on: June 28, 2014, 01:46:58 AM »
The texture I am trying to apply to my UITexture is a snap shot taken from Unity's WebCamTexture. When sampling textures from the WebCamTexture, the image is rotated arbitrarily based on the physical camera device. I am trying to simultaneously display 4 different UIImages each with 4 different snap shots taken from 4 different physical camera devices (each with an arbitrarily different rotation).

I tried using UITexture.drawCall.dynamicMaterial to do the necessary setup in a Start method, but kept getting null references on the dynamicMaterial. I also tried instantiating a new material, setting the rotation on it, and then assigning that material to the UITexture. However, the matrix appears to be reset upon assignment (probably when the material gets copied into NGUI's pipeline).

I'm not concerned about increasing the number of draw calls by 1 per snap shot shown.  However, I do need some way to rotate each of the snap shots arbitrarily. I managed to get the rotation to work fine with a standard unity plane. If it is impossible to get the rotation working with UITexture, is it possible to put something else in the NGUI hierarchy and get the rotation working on it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #3 on: June 28, 2014, 04:37:08 PM »
If you want to rotate them... why not rotate the widget? Why do you mess with the material?

Kimos

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #4 on: June 30, 2014, 01:37:14 AM »
Originally I was hoping to do just that. However, the image is a rectangular aspect ratio and the texture may not have the same orientation as the target mesh (IE a landscape oriented texture being applied to a portrait oriented mesh). This leads to stretching of the image.

Theoretically the mesh could be rotated AND resized to accommodate the texture that is mapped to it. Would resizing the mesh cause distortions when the texture is applied (in particular I don't know if it would muck with the things you are doing to make the GUI "pixel perfect")? Are there any extra apply calls I would need to use after doing a resize on the mesh?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #5 on: July 01, 2014, 03:01:52 AM »
Rotating and resizing the image would be advisable here, and there won't be any distortions provided you get the math right. Going this route there is nothing else that needs to be done. Just set the width/height (or just adjust the transform's scale if you prefer), and set the transform's rotation.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #6 on: August 11, 2014, 09:37:56 AM »
So... I'm doing something similar (WebCamTexture with UITexture), but I also can't get the SetMatrix to take effect. Not sure if I understood what you meant (I didn't for sure), but I do have a custom shader and a material along with it setup on the UITexture, but material.SetMatrix does nothing. I can't rotate the widget because that will break the positioning and sizing of UITexture.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #7 on: August 12, 2014, 03:53:20 AM »
You can set UIWidget.onRender callback now in the last Pro version, and it receives a Material parameter inside. You can then do whatever you like to this material.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #8 on: August 12, 2014, 04:06:22 AM »
You can set UIWidget.onRender callback now in the last Pro version, and it receives a Material parameter inside. You can then do whatever you like to this material.

Well, I don't have pro, and I can't update to the latest version of NGUI for that matter (lots of custom implementations and we have a deadline for the current product september 1)... Any suggestions on what can be done ATM?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #9 on: August 12, 2014, 04:07:24 AM »
Use shader globals to pass the values that can then be used in the shader to rotate the UVs.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #10 on: August 12, 2014, 04:15:06 AM »
Use shader globals to pass the values that can then be used in the shader to rotate the UVs.

Hmm... Yeah that was what I've been trying actually... Must have missed something (write shaders once a year, tops...), I'll check again! TY!

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #11 on: August 13, 2014, 08:14:27 AM »
Use shader globals to pass the values that can then be used in the shader to rotate the UVs.

As suspected this is not going too well... Shader written, material.SetMatrix() used, but nothing, zip, bagel. I even found some dude with a tested shader, copy-pasted it, same calculations but still nothing... Any clue?

Look at the bottom of this page for tested solution: http://forum.unity3d.com/threads/webcamtexture-rotated-and-flipped-on-iphone-when-applied-to-renderer-material-texture.126702/

His code (but with test-values):
  1. Quaternion rotation = Quaternion.Euler(45f, 30f, 90f);
  2. Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, rotation, new Vector3(1, 1, 1));
  3. material.SetMatrix("_Rotation", rotationMatrix);
  4.  

Attached file is same as above but with color-blending.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using SetMatrix on UITexture
« Reply #12 on: August 14, 2014, 06:38:46 AM »
Sorry but I can't help you much with shaders, and it falls out of NGUI's support range anyway. Your best bet is to ask on the Unity Questions website.