Skip to main content
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/
Source Link

Please refer to this Answerthis Answer.

Please refer to this Answer.

Please refer to this Answer.

Fixed the code display. Integrated links in the text.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

UV World mapping in shader with Unity.

Please refer to this Answer:

http://gamedev.stackexchange.com/a/112868/97174this Answer.

The tiling will be applied correctly on geometry aligned on XY plane BUT if you have an orthogonal plane (let's think a common room with 4 walls), the texture will stretch through the plane. Do you know how to fix that? I would like to apply world coord to texture for all 4 walls.

Cheers

Note: (Note that the "plane" iI am using, it is a simple quad made of 2 triangles.)

Here is a screenshot:

enter image description here

EDIT: afterAfter digging a while I found this guy here which did something similar.

https://www.youtube.com/watch?v=s79Zu0F8fuYthis guy here which did something similar.

My guess was to use the plane normal to set up the texture but, not knowing the syntax, the result was... well nothing :) Here

Here you can see the shader. Anyway, as pointed by DMGregory, it doesn't work for all wall angles. Notice the one in diagonal, shown in the picture.

Shader "Diffuse - Worldspace" 

{ Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _Scale ("Texture Scale", Float) = 1.0 }

{
  Properties 
  {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Scale ("Texture Scale", Float) = 1.0
  }

  SubShader 
  {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    fixed4 _Color;
    float _Scale;

    struct Input
    {
        float3 worldNormal;
        float3 worldPos;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {
        float2 UV;
        fixed4 c;

        if(abs(IN.worldNormal.x)>0.5)
        {
            UV = IN.worldPos.yz; // side
            c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
        }
        else if(abs(IN.worldNormal.z)>0.5)
        {
            UV = IN.worldPos.xy; // front
            c = tex2D(_MainTex, UV* _Scale); // use WALL texture
        }
        else
        {
            UV = IN.worldPos.xz; // top
            c = tex2D(_MainTex, UV* _Scale); // use FLR texture
        }

        o.Albedo = c.rgb * _Color;
    }
    ENDCG
  }

  Fallback "VertexLit"
}

UV World mapping

Please refer to this Answer:

http://gamedev.stackexchange.com/a/112868/97174

The tiling will be applied correctly on geometry aligned on XY plane BUT if you have an orthogonal plane (let's think a common room with 4 walls), the texture will stretch through the plane. Do you know how to fix that? I would like to apply world coord to texture for all 4 walls.

Cheers

Note: the "plane" i am using, it is a simple quad made of 2 triangles.

enter image description here

EDIT: after digging a while I found this guy here which did something similar.

https://www.youtube.com/watch?v=s79Zu0F8fuY

My guess was to use the plane normal to set up the texture but, not knowing the syntax, the result was... well nothing :) Here you can see the shader. Anyway, as pointed by DMGregory, it doesn't work for all wall angles. Notice the one in diagonal, shown in the picture.

Shader "Diffuse - Worldspace" 

{ Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _Scale ("Texture Scale", Float) = 1.0 }

SubShader 
{
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    fixed4 _Color;
    float _Scale;

    struct Input
    {
        float3 worldNormal;
        float3 worldPos;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {
        float2 UV;
        fixed4 c;

        if(abs(IN.worldNormal.x)>0.5)
        {
            UV = IN.worldPos.yz; // side
            c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
        }
        else if(abs(IN.worldNormal.z)>0.5)
        {
            UV = IN.worldPos.xy; // front
            c = tex2D(_MainTex, UV* _Scale); // use WALL texture
        }
        else
        {
            UV = IN.worldPos.xz; // top
            c = tex2D(_MainTex, UV* _Scale); // use FLR texture
        }

        o.Albedo = c.rgb * _Color;
    }
    ENDCG
}

Fallback "VertexLit"
}

UV World mapping in shader with Unity.

Please refer to this Answer.

The tiling will be applied correctly on geometry aligned on XY plane BUT if you have an orthogonal plane (let's think a common room with 4 walls), the texture will stretch through the plane. Do you know how to fix that? I would like to apply world coord to texture for all 4 walls. (Note that the "plane" I am using is a simple quad made of 2 triangles.)

Here is a screenshot:

enter image description here

EDIT: After digging a while I found this guy here which did something similar.

My guess was to use the plane normal to set up the texture but, not knowing the syntax, the result was... well nothing :)

Here you can see the shader. Anyway, as pointed by DMGregory, it doesn't work for all wall angles. Notice the one in diagonal, shown in the picture.

Shader "Diffuse - Worldspace" 
{
  Properties 
  {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Scale ("Texture Scale", Float) = 1.0
  }

  SubShader 
  {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    fixed4 _Color;
    float _Scale;

    struct Input
    {
      float3 worldNormal;
      float3 worldPos;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {
      float2 UV;
      fixed4 c;

      if(abs(IN.worldNormal.x)>0.5)
      {
        UV = IN.worldPos.yz; // side
        c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
      }
      else if(abs(IN.worldNormal.z)>0.5)
      {
        UV = IN.worldPos.xy; // front
        c = tex2D(_MainTex, UV* _Scale); // use WALL texture
      }
      else
      {
        UV = IN.worldPos.xz; // top
        c = tex2D(_MainTex, UV* _Scale); // use FLR texture
      }

      o.Albedo = c.rgb * _Color;
    }
    ENDCG
  }

  Fallback "VertexLit"
}
Image added. Added code.
Source Link
Chiodo
  • 13
  • 1
  • 1
  • 5

EDIT: after digging a while I found this guy here which did something similar.

https://www.youtube.com/watch?v=s79Zu0F8fuY

My guess was to use the plane normal to set up the texture but, not knowing the syntax, the result was... well nothing :) Here you can see the shader. Anyway, as pointed by DMGregory, it doesn't work for all wall angles. Notice the one in diagonal, shown in the picture.

Shader "Diffuse - Worldspace" 

{ Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _Scale ("Texture Scale", Float) = 1.0 }

SubShader 
{
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    fixed4 _Color;
    float _Scale;

    struct Input
    {
        float3 worldNormal;
        float3 worldPos;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {
        float2 UV;
        fixed4 c;

        if(abs(IN.worldNormal.x)>0.5)
        {
            UV = IN.worldPos.yz; // side
            c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
        }
        else if(abs(IN.worldNormal.z)>0.5)
        {
            UV = IN.worldPos.xy; // front
            c = tex2D(_MainTex, UV* _Scale); // use WALL texture
        }
        else
        {
            UV = IN.worldPos.xz; // top
            c = tex2D(_MainTex, UV* _Scale); // use FLR texture
        }

        o.Albedo = c.rgb * _Color;
    }
    ENDCG
}

Fallback "VertexLit"
}

EDIT: after digging a while I found this guy here which did something similar.

https://www.youtube.com/watch?v=s79Zu0F8fuY

My guess was to use the plane normal to set up the texture but, not knowing the syntax, the result was... well nothing :) Here you can see the shader. Anyway, as pointed by DMGregory, it doesn't work for all wall angles. Notice the one in diagonal, shown in the picture.

Shader "Diffuse - Worldspace" 

{ Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _Scale ("Texture Scale", Float) = 1.0 }

SubShader 
{
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert

    sampler2D _MainTex;
    fixed4 _Color;
    float _Scale;

    struct Input
    {
        float3 worldNormal;
        float3 worldPos;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {
        float2 UV;
        fixed4 c;

        if(abs(IN.worldNormal.x)>0.5)
        {
            UV = IN.worldPos.yz; // side
            c = tex2D(_MainTex, UV* _Scale); // use WALLSIDE texture
        }
        else if(abs(IN.worldNormal.z)>0.5)
        {
            UV = IN.worldPos.xy; // front
            c = tex2D(_MainTex, UV* _Scale); // use WALL texture
        }
        else
        {
            UV = IN.worldPos.xz; // top
            c = tex2D(_MainTex, UV* _Scale); // use FLR texture
        }

        o.Albedo = c.rgb * _Color;
    }
    ENDCG
}

Fallback "VertexLit"
}
Image added
Source Link
Chiodo
  • 13
  • 1
  • 1
  • 5
Loading
Source Link
Chiodo
  • 13
  • 1
  • 1
  • 5
Loading