查看: 10974|回复: 8
打印 上一主题 下一主题

[提问] 求问在unity中如何实现uv的旋转啊?

[复制链接]

20

主题

5

听众

1096

积分

助理设计师

Rank: 4

纳金币
105
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-9-27 13:39:44 |只看该作者 |倒序浏览
pstatus"> 本帖最后由 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);
        }
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

ZackD    

715

主题

22

听众

4万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
26224
精华
17

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-9-27 14:31:37 |只看该作者
好像是不行吧?没法直接旋转UV
回复

使用道具 举报

20

主题

5

听众

1096

积分

助理设计师

Rank: 4

纳金币
105
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-9-27 15:49:12 |只看该作者
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);  
}  
回复

使用道具 举报

10

主题

2

听众

1545

积分

助理设计师

Rank: 4

纳金币
79
精华
0

活跃会员

地板
发表于 2013-9-27 16:39:44 |只看该作者
写一个shader用传入的matrix旋转uv
                        SetTexture [_MainTex]
                        {
                                constantColor [_Color]
                                matrix [_Rotation]
                                combine texture * constant
                        }
再写一个cs,传入matrix
renderer.material.SetMatrix ("_Rotation", t_inverse*m*t);       
回复

使用道具 举报

1

主题

3

听众

371

积分

设计实习生

Rank: 2

纳金币
40
精华
0

最佳新人

5#
发表于 2013-9-27 17:03:58 |只看该作者
楼上高人啊,,
回复

使用道具 举报

ZackD    

715

主题

22

听众

4万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
26224
精华
17

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2013-9-27 17:05:35 |只看该作者
saviosun 发表于 2013-9-27 16:39
写一个shader用传入的matrix旋转uv
                        SetTexture [_MainTex]
                        {

厉害,学习了!
回复

使用道具 举报

nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2013-9-28 12:14:15 |只看该作者
不错的教程
回复

使用道具 举报

20

主题

5

听众

1096

积分

助理设计师

Rank: 4

纳金币
105
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2013-9-29 16:48:35 |只看该作者
saviosun 发表于 2013-9-27 16:39
写一个shader用传入的matrix旋转uv
                        SetTexture [_MainTex]
                        {

大神,求一个好用的shader和脚本,我试了半天也没写出来。没有用过matrix在shader里。
回复

使用道具 举报

10

主题

2

听众

1545

积分

助理设计师

Rank: 4

纳金币
79
精华
0

活跃会员

9#
发表于 2013-10-9 17:31:41 |只看该作者
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);               
        }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-20 14:36 , Processed in 0.084164 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部