TransWikia.com

World space coords for 2d images

Game Development Asked by user3134909 on December 15, 2021

How can I make an 2d image using World space coords with a shape? Like a circle instead of a quad.In 3d you can modify the vertices but if I want a smooth circle there will be too much vertices. What I want to achieve is something like a cirle mask that will be move over the global texture at runtime.
So has anyone done something similar?
I’ve tried combining some shaders with two uv’s one for world space and one for alpha uv mesh but it gives some weird artifacts.
enter image description here

This is the how the shader looks on the right:

Shader "Unlit/WorldspaceTiling"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}


 }
 SubShader
 {
      Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
 LOD 100

 ZWrite Off
 Blend SrcAlpha OneMinusSrcAlpha 
 Cull Off
     Lighting Off
     ZWrite Off
     Fog{ Mode Off }
     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;
         };

         sampler2D _MainTex;
         float4 _MainTex_ST;

         v2f vert (appdata v)
         {
             v2f o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);

             // Gets the xy position of the vertex in worldspace.
             float2 worldXY = mul(_Object2World, v.vertex).xy;
             // Use the worldspace coords instead of the mesh's UVs.
             o.uv = TRANSFORM_TEX(worldXY, _MainTex);

             return o;
         }

         fixed4 frag (v2f i) : SV_Target
         {                   
             fixed4 col = tex2D(_MainTex, i.uv);
             return col;
         }
         ENDCG
     }
 }

}

One Answer

I'm unsure how to answer the question regarding the shaders you have pictured, as it seems that you already have that effect that you want.

For the circle, I have implemented a simple shader (unfortunately, it's HLSL and not the language you're in; or I just don't recognize your code type.) Note: I'm a bit of a shader newb.

What I did was add a Parameter to my shader called "LightPosition" in a second effect. Each pixel is calculated as it's XZ (my flat plane, Y is my depth) distance.

Disclaimer: I'm dredging this from some weeks ago and it may not be 100% working below Here's what my HLSL looks like, and hopefully it's helpful to you:

struct VertexToPixel
{
    float4 Position : POSITION; // reserved for Pixel Shader internals
    float4 rPos : TEXCOORDS0; // world position of the texture
    float2 texPos : TEXCOORDS1; // actual texture coords
};
float4 LightAroundAShot(VertexToPixel PSIn) : COLOR0
{
    float mult = 0.02 / (1 + lightpos.w);
    float x = (PSIn.rPos.x - lightpos.x) * mult;
    float z = (PSIn.rPos.z - lightpos.z) * mult;

    // get the color of the texture as passed in
    float4 texColor = tex2D(currSampler, PSIn.texPos);

    // the "magic numbers" here were just to get an appropriate amount of yellow color
    outputColor.rg = (0.55 - sqrt(x * x + z * z)) * lightpos.w * texColor.a;
    outputColor.b = (0.35 - sqrt(x * x + z * z)) * lightpos.w * texColor.a;

    return output;
}

Answered by blurry on December 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP