• 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
0
Question by Neutron · Jun 16, 2012 at 12:36 PM · materialmatrixsurface shader

Surface Shader with Material Matrix

Has anyone successfully passed a matrix to a surface shader. I've found several examples online passing matrices to shaders, but I can't get my matrix to have any value other than identity. The only difference I can see is that the examples I've seen are fragment shaders, and mine is a surface shader.

My script has

    var m = Matrix4x4.TRS(new Vector3(0, 10, 0), Quaternion.identity, Vector3.one);
    targetMaterial.SetMatrix("_BoneMatrix0", m);

and my shader has

  uniform float4x4 _BoneMatrix0;

Seems straightforward enough, so I'm wondering if this feature works at all with surface shaders

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
0

Answer by ScroodgeM · Jul 20, 2012 at 10:03 PM

set matrix works fine. see code below that just tested. this code rotates texture using matrix.

in your code i saw you apply matrix with offset 10 only. you should not see the difference because offset texture for a natural number will not make any visual affect.

shader code:

Shader "Unity Answers/Diffuse with material matrix" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200

     CGPROGRAM
     #pragma surface surf Lambert
 
     sampler2D _MainTex;
     float4x4 _Matrix0;

     struct Input
     {
         float2 uv_MainTex;
     };

     void surf (Input IN, inout SurfaceOutput o)
     {
         fixed4 c = tex2D(_MainTex, mul(_Matrix0, float4(IN.uv_MainTex, 0, 0)).xy);
         o.Albedo = c.rgb;
         o.Alpha = c.a;
     }
     ENDCG
 }
 Fallback "VertexLit"

}

script code:

using UnityEngine;
using System.Collections;
public class Matrix2Material : MonoBehaviour
{
    public Material Material2SetMatrix;
    void Update()
    {
        Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, Time.time * 10f), Vector3.one);
        Material2SetMatrix.SetMatrix("_Matrix0", m);
    }
}
Comment
Add comment · Show 1 · 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
avatar image rectalogic · Mar 06, 2013 at 08:54 PM 0
Share

In your surf() function, you need the last element of the uv float4 to be 1.0 or else matrix translation will be ignored:

 fixed4 c = tex2D(_$$anonymous$$ainTex, mul(_$$anonymous$$atrix0, float4(IN.uv_$$anonymous$$ainTex, 0, 1.0)).xy);

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Changing codes for use in "surface shader" 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

How to Modify Standard Surface Shader's SpecCube? 1 Answer

uv array and surface shader? 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