Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dre0Dru · Jun 07, 2017 at 02:09 PM · unity 5shadertexturespritesphere

[Shader] Projecting and moving sprite(texture) on sphere mesh

I have a sphere with texture on it (cubemap texture, but that's not important). I want to mix up this texture with another texture with ability to move and scale this texture on sphere.

Here is the code to move, rotate and scale texture on sphere, in vertex function:

 Shader "MyShaders/CubeMapShader"
 {
     Properties
     {
         _Tint("Tint Color", Color) = (.5, .5, .5, .5)
         _Exposure("Exposure", float) = 0
         [NoScaleOffset] _Tex("Cubemap", Cube) = "grey" {}
         _Rotation("Rotation", float) = 0
         _EnableColor("Enable color", Range(0, 1)) = 1
         _Smoothness("Specular Smoothness", Range(0.05, 1)) = 0.5
         _Smoothness2("Specular Radius", Range(0.1, 0.5)) = 0.5
         _LightIntensity("LightIntensity", Range(0.5, 50)) = 1
         //Sprite control
         _Sprite("Sprite", 2D) = "white" {}
         _SpriteRotation("Sprite Rotation", float) = 0
         _SpritePivotScale("Sprite Scale", Vector) = (0.5,0.5,1,1)
         _SpriteTranslation("Sprite Translation", Vector) = (0,0,0,0)
 
     }
     SubShader
     {
         Tags{ "Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox" "LightMode" = "ForwardBase" }
 
         //Cull Front
 
         //Blend One OneMinusSrcAlpha
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityStandardBRDF.cginc"
 
             float3 RotateAroundYInDegrees (float3 vertex, float degrees)
             {
                 float alpha = degrees * UNITY_PI / 180.0;
                 float sina, cosa;
                 sincos(alpha, sina, cosa);
                 float2x2 m = float2x2(cosa, -sina, sina, cosa);
                 return float3(mul(m, vertex.xz), vertex.y).xzy;
             }
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float3 uv : TEXCOORD0;
                 float3 normal: NORMAL;
             };
 
             struct v2f
             {
                 float3 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
                 float3 normal : TEXCOORD1;
                 float3 worldPos : TEXCOORD2;
                 float2 sprite_uv : TEXCOORD3;
             };
 
             float _Rotation;
             samplerCUBE _Tex;
             half4 _Tex_HDR; 
             float _Exposure;
             float4 _Tint;
             float _EnableColor;
             float _Smoothness;
             float _Smoothness2;
             float _LightIntensity;
             sampler2D _Sprite;
 
             float _SpriteRotation;
             Vector _SpritePivotScale;
             Vector _SpriteTranslation;
 
             v2f vert (appdata v)
             {
                 v2f o;
                 float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.vertex.xyz;
                 o.normal = UnityObjectToWorldNormal(v.normal);
                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
 
                 //sprite uv transform
                 float2x2 rotationMatrix;
                 float sinTheta;
                 float cosTheta;
 
                 o.sprite_uv = v.uv - _SpritePivotScale.xy;
                 sinTheta = sin(_SpriteRotation);
                 cosTheta = cos(_SpriteRotation);
                 rotationMatrix = float2x2(cosTheta, -sinTheta, sinTheta, cosTheta);
                 o.sprite_uv = (mul((o.sprite_uv - _SpriteTranslation.xy) * (1 / _SpritePivotScale.zw), rotationMatrix) + _SpritePivotScale.xy);
 
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 float3 diffuse = (1.0, 1.0, 1.0);
                 float3 lightDir = _WorldSpaceLightPos0.xyz;
                 i.normal = normalize(i.normal);
                 float3 lightColor = _LightColor0.rgb * _LightIntensity;
                 float3 temp = (DotClamped(lightDir*_Smoothness2, i.normal));
                 diffuse =  max(0.1, pow(lightColor * temp, _Smoothness *100));
                 half4 tex = texCUBE(_Tex, i.uv);
                 tex = tex * _Tint.rgba * float4(unity_ColorSpaceDouble.rgb, 1);
                 tex.rgb *= _Exposure;
                 tex.rgb *= diffuse;
                 //adding sprite to existing texture
                 tex += tex2D(_Sprite, i.sprite_uv);
                 return tex ;
             }
             ENDCG
         }
     }
 }
 

In fragment fuction I just tex2D this textute based on uv i calculated in vertex func and add it to my existing texture (just cubemap tex). Here is what I get in the middle of the sphere: alt text And this is what i get near the top of the sphere: alt text

I understand that it's beacuse how uv coords mapped to the sphere surface, so the question is: is it possible to add second texture/sprite to existing sphere with it's own texture and have ability to move and scale it. I need this second texture into shader so variants like putting a sprite in front of sphere is unsuitable.

1.png (217.3 kB)
4.png (128.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

216 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 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 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 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 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 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 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 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 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 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 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

Changing color on specific tile in texture at runtime 0 Answers

Blending tiles with larger textures 0 Answers

How can I apply a 9-slice scaling to a mash object using a shader? 0 Answers

Applying camouflage without spriting multiple sprites 0 Answers

How to partially change texture? 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