Shader поворота - не везде работает.

Шейдеры и все-все-все.

Shader поворота - не везде работает.

Сообщение mmf 23 сен 2015, 17:29

Всем доброго дня.

Тут http://forum.unity3d.com/threads/rotation-of-texture-uvs-directly-from-a-shader.150482 обсуждался shader вращающий текстуру. Он работает везде ( Windows, Android 4.1 - 4.4., Android 5.1 ).
Вот его код:
Код: Выделить всё
Shader "Custom/RotateUV" {
   Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
        _RotationSpeed ("Rotation Speed", Float) = 2.0
        }
        SubShader {
            Tags { "RenderType"="Opaque" }
            LOD 200
           
            CGPROGRAM
            #pragma surface surf Lambert vertex:vert
     
            sampler2D _MainTex;
     
            struct Input {
                float2 uv_MainTex;
            };
     
            float _RotationSpeed;
            void vert (inout appdata_full v) {
                v.texcoord.xy -=0.5;
                float s = sin ( _RotationSpeed * _Time );
                float c = cos ( _RotationSpeed * _Time );
                float2x2 rotationMatrix = float2x2( c, -s, s, c);
                rotationMatrix *=0.5;
                rotationMatrix +=0.5;
                rotationMatrix = rotationMatrix * 2-1;
                v.texcoord.xy = mul ( v.texcoord.xy, rotationMatrix );
                v.texcoord.xy += 0.5;
            }
     
            void surf (Input IN, inout SurfaceOutput o) { 
                half4 c = tex2D (_MainTex, IN.uv_MainTex);
                o.Albedo = c.rgb;
                o.Alpha = c.a;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }


Я сделал небольшие изменения. Мне нужен поворот на 90 градусов. Без вращения.
Вот измененный код:
Код: Выделить всё
Shader "Custom/Rotate90s" {
       Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _RotationSpeed ("Rotation Speed", Float) = 2.0
        _RotationDegrees ("Rotation Degrees", Float) = 0.0
        _isRotat ("isRotat", int) = 0   
       
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
       
        CGPROGRAM
        #pragma surface surf Lambert vertex:vert

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };
       
        float _RotationDegrees;
        int _isRotat;
        void vert (inout appdata_full v) {
            v.texcoord.xy -=0.5;
            if (_isRotat==1) { _RotationDegrees=4.712385; } // 90 degrees.
            float s = sin ( _RotationDegrees);
            float c = cos ( _RotationDegrees);
           
            float2x2 rotationMatrix = float2x2( c, -s, s, c);
            rotationMatrix *=0.5;
            rotationMatrix +=0.5;
            rotationMatrix = rotationMatrix * 2-1;
            v.texcoord.xy = mul ( v.texcoord.xy, rotationMatrix );
            v.texcoord.xy += 0.5;
        }

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}


Он работает в Windows на Android 4.1 - 4.4.

Но не работает на Android 5.1 !!!!

Бьюсь уже вторую неделю.
Кто нибудь может подсказать, из за чего не работает?
mmf
UNец
 
Сообщения: 20
Зарегистрирован: 04 июн 2013, 12:49

Re: Shader поворота - не везде работает.

Сообщение waruiyume 23 сен 2015, 18:11

Зачем матрицы если можно просто поменять местами оси.
v.texcoord.xy = v.texcoord.yx;
Аватара пользователя
waruiyume
Адепт
 
Сообщения: 6143
Зарегистрирован: 30 окт 2010, 05:03
Откуда: Ростов на Дону

Re: Shader поворота - не везде работает.

Сообщение jetyb 24 сен 2015, 11:34

Что не так:
Синтаксис:
Используется csharp
if (isRotat==1) { _RotationDegrees=4.712385; }
 

1. Вы меняете внешнюю общую переменную _RotationDegrees из кода шейдера. Лучше менять локальную переменную кода.
2. Сомнительно сравнение == в случае float чисел. Таже если isRotat типа инт , его может приводить в float сравнение с float литералом 1.
jetyb
Адепт
 
Сообщения: 1486
Зарегистрирован: 31 окт 2011, 17:21

Re: Shader поворота - не везде работает.

Сообщение mmf 24 сен 2015, 15:42

Спасибо, за отклик!
К сожалению я новичок в ShaderLab.
Просто сделав такой Shader:
Синтаксис:
Используется glsl
Shader "Custom/Rotate90m" {
        Properties {
                _Color ("Color", Color) = (1,1,1,1)
                _MainTex ("Albedo (RGB)", 2D) = "white" {}
                _Glossiness ("Smoothness", Range(0,1)) = 0.5
                _Metallic ("Metallic", Range(0,1)) = 0.0
           _isRotat ("isRotat", int) = 0  
        }
        SubShader {
                Tags { "RenderType"="Opaque" }
                LOD 200
               
                CGPROGRAM
                // Physically based Standard lighting model, and enable shadows on all light types
                #pragma surface surf Standard fullforwardshadows

                // Use shader model 3.0 target, to get nicer looking lighting
                #pragma target 3.0

                sampler2D _MainTex;

                struct Input {
                        float2 uv_MainTex;
                };

                half _Glossiness;
                half _Metallic;
                fixed4 _Color;
        int _isRotat;
         void vert (inout appdata_full v) {
           // if (_isRotat==1) {
            v.texcoord.xy = v.texcoord.yx;
            //} // 90 degrees.  
        }  
             
                void surf (Input IN, inout SurfaceOutputStandard o) {
                        // Albedo comes from a texture tinted by color
                        fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
                        o.Albedo = c.rgb;
                        // Metallic and smoothness come from slider variables
                        o.Metallic = _Metallic;
                        o.Smoothness = _Glossiness;
                        o.Alpha = c.a;
                }
                ENDCG
        }
        FallBack "Diffuse"
}
 


Он НЕ крутит текстуру!!!
Пожалуйста подскажите, что я не так написал?
mmf
UNец
 
Сообщения: 20
Зарегистрирован: 04 июн 2013, 12:49

Re: Shader поворота - не везде работает.

Сообщение mmf 24 сен 2015, 16:09






isRotat - имеет тип int.

я не точно описал что происходит.

Shader вообще не работает под Android 5.1. т.е. текстура не отображается. А там где текстура отображается она и крутится. ( это я про Rotate90s).
mmf
UNец
 
Сообщения: 20
Зарегистрирован: 04 июн 2013, 12:49

Re: Shader поворота - не везде работает.

Сообщение mmf 24 сен 2015, 16:33

if (isRotat==1) { _RotationDegrees=4.712385; }


1. Вы меняете внешнюю общую переменную _RotationDegrees из кода шейдера. Лучше менять локальную переменную кода.
2. Сомнительно сравнение == в случае float чисел. Таже если isRotat типа инт , его может приводить в float сравнение с float литералом 1.


isRotat - имеет тип int.

я не точно описал что происходит.

Shader вообще не работает под Android 5.1. т.е. текстура не отображается. А там где текстура отображается она и крутится. ( это я про Rotate90s).
mmf
UNец
 
Сообщения: 20
Зарегистрирован: 04 июн 2013, 12:49


Вернуться в Shader Lab

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 3