纳金网

标题: 求问在unity中如何实现uv的旋转啊? [打印本页]

作者: herry7x    时间: 2013-9-27 13:39
标题: 求问在unity中如何实现uv的旋转啊?
本帖最后由 herry7x 于 2013-9-29 17:03 编辑

哪位大神知道如何在unity中实现uv的旋转呢?

谢谢各位帮助,找到方法了,unity帮助手册里就有相关介绍,把代码复制给大家看看。

        var rotateSpeed = 30;
        var texture : Texture;

        function Start() {
                // Create a new material with a shader
                // that rotates the texture. Texture rotation
                // is performed with a _Rotation matrix.
                var m : Material = new Material (
                        "Shader \"Rotating Texture\" {" +
                        "Properties { _MainTex (\"Base\", 2D) = \"white\" {} }" +
                        "SubShader {" +
                        "        Pass {" +
                        "                Material { Diffuse (1,1,1,0) Ambient (1,1,1,0) }" +
                        "                Lighting On" +
                        "                SetTexture [_MainTex] {" +
                        "                        matrix [_Rotation]" +
                        "                        combine texture * primary double, texture" +
                        "                }" +
                        "        }" +
                        "}" +
                        "}"
                );
                m.mainTexture = texture;
                renderer.material = m;
        }

        function Update() {
                // Construct a rotation matrix and set it for the shader
                var rot = Quaternion.Euler (0, 0, Time.time * rotateSpeed);
                var m = Matrix4x4.TRS (Vector3.zero, rot, Vector3(1,1,1) );
                renderer.material.SetMatrix ("_Rotation", m);
        }

作者: ZackD    时间: 2013-9-27 14:31
好像是不行吧?没法直接旋转UV
作者: herry7x    时间: 2013-9-27 15:49
ZackD 发表于 2013-9-27 14:31
好像是不行吧?没法直接旋转UV

我在网上找到一个修改材质属性的方法,修改uv的偏移属性都好用,但修改旋转属性怎么改都不好使。

function Start(){  
    var curve:AnimationCurve = AnimationCurve.Linear(0,0,4,360);  
    var clip:AnimationClip = new AnimationClip();  
        clip.SetCurve("",typeof(Material),"_MainTex.rotation",curve);  
    /**
        Material有几个属性可以控制:
        _MainTex.rotation,
        _MainTex.scale,(x左右,y上下)
        _MainTex.offset,(x左右,y上下)
        _Color(rgba),(控制颜色的变化)
    */  
    animation.AddClip(clip,clip.name);  
    animation.Play(clip.name);  
}  
作者: saviosun    时间: 2013-9-27 16:39
写一个shader用传入的matrix旋转uv
                        SetTexture [_MainTex]
                        {
                                constantColor [_Color]
                                matrix [_Rotation]
                                combine texture * constant
                        }
再写一个cs,传入matrix
renderer.material.SetMatrix ("_Rotation", t_inverse*m*t);       
作者: carefreeq    时间: 2013-9-27 17:03
楼上高人啊,,
作者: ZackD    时间: 2013-9-27 17:05
saviosun 发表于 2013-9-27 16:39
写一个shader用传入的matrix旋转uv
                        SetTexture [_MainTex]
                        {

厉害,学习了!
作者: nts    时间: 2013-9-28 12:14
不错的教程
作者: herry7x    时间: 2013-9-29 16:48
saviosun 发表于 2013-9-27 16:39
写一个shader用传入的matrix旋转uv
                        SetTexture [_MainTex]
                        {

大神,求一个好用的shader和脚本,我试了半天也没写出来。没有用过matrix在shader里。
作者: saviosun    时间: 2013-10-9 17:31
herry7x 发表于 2013-9-29 16:48
大神,求一个好用的shader和脚本,我试了半天也没写出来。没有用过matrix在shader里。 ...

Shader "RotateUV" {
        Properties
        {
                _Color ("Main Color", Color) = (1,1,1,1)
                _MainTex ("Base (RGB)", 2D) = "white" {}
        }
        SubShader
        {
                Tags { "RenderType"="Opaque" "Queue"="Transparent+1"}
                ZWrite Off
                Blend One One
                 
                Pass {
                        Lighting Off
       
                        SetTexture [_MainTex]
                        {
                                constantColor [_Color]
                                matrix [_Rotation]
                                combine texture * constant
                        }
                }
        }
        FallBack "Diffuse"
}


rotateuv.cs

using UnityEngine;
using System.Collections;

public class RotateUV : MonoBehaviour {

        public float mfSpeed  = 10.0f;
        public Vector2 mRotationCenter = new Vector2(0.5f, 0.5f);

       
        void OnUpdate()
        {
            Quaternion rot = Quaternion.Euler (0.0f, 0.0f, Time.time * mfSpeed);
            Matrix4x4 m = Matrix4x4.TRS ( Vector3.zero, rot, Vector3.one );
            Matrix4x4 t = Matrix4x4.TRS (-mRotationCenter, Quaternion.identity, Vector3.one);
            Matrix4x4 t_inverse = Matrix4x4.TRS (mRotationCenter, Quaternion.identity, Vector3.one);
            renderer.sharedMaterial.SetMatrix ("_Rotation", t_inverse*m*t);               
        }
}




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5