Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MihaP · May 27, 2014 at 03:22 PM · shadermatrix

Transformation matrix with surface shader

Hello,

I'm writing a minimap with fog of war, focus, rotation and scaling features. Now I've hit a snag with the rotation part; somehow, it insists on rotating at the texture corner.

I'm using a matrix thusly:

  void UpdateMatrix()
         {
             if(target != null)
             {
                 // get UV coordinates plus offset
                 posV = target.transform.position - world.transform.position;
                 posV.x = (posV.x/worldSize.x);
                 posV.z = (posV.z/worldSize.y);
                 posV.y = posV.z; // transform to XZ axis
                 posV.z = 0; 
     
                 if(canZoom)
                     // update the zoom level
                     scaV = new Vector3(zoom,zoom,zoom);
                 else
                     // restore the zoom level
                     scaV = new Vector3(1f,1f,1f);
     
                 if(canRotate)
                 {
                     // rotation on Y axis is remapped to texture's Z axis
                     rotQ = Quaternion.Euler(0,0,target.transform.eulerAngles.y);
                 }
                 else
                 {
                     rotQ = Quaternion.identity; // restore rotation
                 }
             }
             else
             {
                 // no target, fixed view settings - restore position, no rotation, original scale
                 posV = new Vector3(0.5f,0.5f,0);
                 rotQ = Quaternion.identity;
                 scaV = new Vector3(1f,1f,1f);
     
             }
     
             matrix = Matrix4x4.TRS(posV,rotQ,scaV);

And the shader part:

 SubShader {
         LOD 100
         Tags
         {
             "Queue" = "Transparent"
         }
         
         Cull Off
         Lighting Off
         
         CGPROGRAM
         #pragma surface surf Lambert alpha
         
         struct Input
         {
             float2 uv_MainTex;
         };
         
         float _Zoom;
         float4x4 _Matrix;
         
         half4 _ActiveColor;
         half4 _ExploredColor;
         half4 _UnexploredColor;
         sampler2D _MainTex;
         sampler2D _FoWMask;
         sampler2D _CutoutMask;
         
         void surf (Input IN, inout SurfaceOutput o)
         {    
         
             half4 basec = tex2D(_MainTex, mul(_Matrix, float4(IN.uv_MainTex, 0, 1.0)).xy); // base
             half4 fowc = tex2D(_FoWMask, mul(_Matrix, float4(IN.uv_MainTex, 0, 1.0)).xy);  // FoW
             half4 finalc = lerp(lerp(_UnexploredColor,_ExploredColor,fowc.g),_ActiveColor,fowc.r); // FoW adjusted color
             o.Emission = basec.rgb * finalc.rgb;
             o.Alpha = basec.a * tex2D(_CutoutMask, IN.uv_MainTex).a;
         }

Note that the world uses XZ plane reference. No matter what I do, I can't seem to get the shader to rotate the textures at the given pivot. Why is that? What am I doing wrong here? I am at the end of my wits.

And yes, I am a beginner, so please do go easy on me!

Thanks for your time.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MihaP · May 28, 2014 at 07:29 AM

Welp, solved on my own after sleeping it through. The issue was that it rotated the texture around offset from the desired position.

The solution: use two matrices - one to translate,rotate around the desired pivot and scale up. And the other to translate to center of the texture AFTER the rotation and scaling have been done.

     Matrix4x4 m0 = Matrix4x4.TRS(posV,rotQ,scaV); // translate to target position, rotate and scale
     Matrix4x4 m1 = Matrix4x4.TRS(new Vector3(-0.5f,-0.5f,0),Quaternion.identity,Vector3.one); // translate to the center
     matrix = m0*m1;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Replicating the Minecraft Acid Mod 1 Answer

_WorldSpaceCameraPos and converting to object space 1 Answer

Why does not perspective projectors work well? 0 Answers

Efficient way to keep a fixed rotation for sprite? 0 Answers

Previous Model*View*Projection Matrix 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges