Jump to content
  • Advertisement
Sylon87

OpenGL Shader and Rotation At Specified Angle

Recommended Posts

Posted (edited)

hello!

i really hope that someone can help me... i'm preatty new with the shader programming.. what i have to do is :

 

i have a big ring gameobject with a big hole in the center i will use this as 3d tutorial UI for an object rotation to show to the player how it will move..

what i need is that this big ring will be completly transparent (i did it with alpha channel) then make visible only a small section of it based on my angle imput, for example if i put 45 degrees i want to be able from 45 degrees to 50.. and so on..

i'll attach here a picture of what i'm looking to do.. i hope that someone can help me with it...

sorry for my bad english..

PS: i'm using unity and this is the code that i actually have... but for an angle upper than 179 or -179 degrees it will disappear...

 

Shader "Unlit/MaskedBasedOnAngle"
{
    Properties
    {
        _MainTex("Base (A=Opacity)", 2D) = "" {}
        _Transparency("Transparency", Range(0.0,0.5)) = 0.25
        _Portion("Angle",Range(-1,1)) = 0
        _Size("Size",float) = 5
    }
        SubShader
    {
        Tags{ "Queue" = "Transparent"  "RenderType" = "Transparent" "IgnoreProjector" = "true" }
        Zwrite Off
        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
    {
        CGPROGRAM
 
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
        struct appdata
    {
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
    };
    struct v2f
    {
        float2 uv : TEXCOORD0;
        float4 vertex : SV_POSITION;
        float3 worldSpacePos  : TEXCOORD1;
        float3 objectCenter : TEXCOORD2;
    };
    sampler2D _MainTex;
    float4 _MainTex_ST;
    float  _Transparency;
    float _Portion;
    float _Size;
 
    float4 setAxisAngle(float3 axis, float rad) {
        rad = rad * 0.5;
        float s = sin(rad);
        return float4(s * axis[0], s * axis[1], s * axis[2], cos(rad));
    }
    v2f vert(appdata v)
    {
        v2f o;
        o.vertex = UnityObjectToClipPos(v.vertex);
        o.uv = TRANSFORM_TEX(v.uv, _MainTex);
        o.worldSpacePos = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1)).xyz;
        o.objectCenter = mul(unity_ObjectToWorld, float4(0.0,0.0,0.0,1.0));
        return o;
    }
 
#define TWO_PI 6.28318530718
    fixed4 frag(v2f i) : SV_Target
    {
        float3 v = normalize(i.worldSpacePos - i.objectCenter);
        half4 c = tex2D(_MainTex,i.uv);
        float angle =( atan2(v.x, v.z)/ TWO_PI*4.) ;
 
        float t = (angle.xxx / _Size );
        c.a = 1.- smoothstep(0.079,0.08, abs(t - _Portion));
 
        return c;
    }
        ENDCG
    }
    }
}

Untitled.jpg

Edited by Sylon87

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

maybe im wrong

as I understand you did not get "how shaders work"?

base on you example, you can "send angle to shader" and shader will make "transparent zone" and non-transarent base on this angle

maybe practice using simply 2d shaders on shadertoy will help you https://www.shadertoy.com/new

put this code on shadertoy, this draw "line that rotate", that is what you want?

#define rotate2d(a) mat2(cos(a), -sin(a), sin(a), cos(a))

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = fragCoord/iResolution.y-(iResolution.xy / iResolution.y)/2.;
    uv*=rotate2d(0.5*iTime);//rotate line on angle
    float d = step(abs(uv.y),0.01);//draw line
    d*=step(0.,uv.x);//hide one side
    fragColor = vec4(vec3(d),1.0);
}

 

Edited by atri

Share this post


Link to post
Share on other sites

For each circle compose an uniform that stores circle center and radius now having this you test in fragment shader (calculate position of a fragment) if its within angle - i think even simple dot would suffice here ( you can try to compose -1..1 to bigger numbers to get better resolution

Share this post


Link to post
Share on other sites
15 hours ago, _WeirdCat_ said:

For each circle compose an uniform that stores circle center and radius now having this you test in fragment shader (calculate position of a fragment) if its within angle - i think even simple dot would suffice here ( you can try to compose -1..1 to bigger numbers to get better resolution

i exactly did it with Dot Product.. really thank's you for your help!

 

and Thank's also to atri i'll checkout shadertoy, because as you said, i really diden't get how shader work... thank's you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!