0
\$\begingroup\$

Based on this question (LINK)

Shader "Unlit/WorldspaceTiling"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

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

So this has these two issues:

  • a) If the object moves through worldspace (eg. a moving platform), the texture will appear to crawl along it.
  • b) Rotating the object won't rotate the texture, so it will tile across it diagonally.

How is this code supposed to be to fix these two problems?

EDIT:

This is what I'm trying to achieve over all:

enter image description here

I have no idea if my current method is good or bad, but its basically:

  1. Use a flat mesh created in blender with UVs unwrapped. They're all basically a bunch of quads with UVs having literally the shape same as the mesh itself.
  2. Attach a material to each mesh in Unity with a particular texture.
  3. Resize, rotate and align mesh into correct size and position.
  4. Use as physics object in a 2D sideview game. May have additional meshes attached to it as non-interactive decorations, but otherwise using the same method to build them.
\$\endgroup\$
6
  • \$\begingroup\$ Note that just below the portion of my answer you've copied from, I wrote "These problems can all be worked around, the fixes just require a bit more information about your setup." So, please edit your question to provide us with that information. What kind of content in your game are you using this shader for? Are these sprites, meshes, etc? Are you using scale to make them different sizes? How do they move? Do you use the same material on multiple objects in a single view, some of which might move? The more you can tell us about your game application, the better we can target answers. \$\endgroup\$
    – DMGregory
    Commented Dec 4, 2020 at 18:10
  • \$\begingroup\$ Understood. I'll create an image that explains what the setup is like. \$\endgroup\$ Commented Dec 5, 2020 at 13:11
  • \$\begingroup\$ @DMGregory, ok done. Hopefully everything makes more sense now. \$\endgroup\$ Commented Dec 5, 2020 at 14:37
  • \$\begingroup\$ I think that is a clearer question than the one in your title, and converting this shader to local space might not be the best way to achieve your goal. Try editing your question along the lines of "How can I easily create textured physics objects of different shapes and sizes?" and put your description text in plain text, rather than encoded in an image where it's invisible to search, translation, or screen reading tools. You may also be interested in past Q&A about creating a reshape-able polygon sprite \$\endgroup\$
    – DMGregory
    Commented Dec 5, 2020 at 15:14
  • \$\begingroup\$ @DMGregory, ok I'll do this. I'm more of a artist/designer type, so its tricky to know what I should ask for. Your suggestion of making a new question with that title helps a lot. Thank you. It'll take some time to extract the text out of the image. About the reshapable polygon, I don't know how to use that. I'd only want to scale primitives, not edit their structure, so its probably not what I'm looking for. \$\endgroup\$ Commented Dec 5, 2020 at 15:50

0

You must log in to answer this question.

Browse other questions tagged .