Friday, May 8, 2015

c# - UV World mapping in shader with Unity.


Please refer to this Answer.


DMGregory script is working for me, although I have a problem I can't fix because I don't talk shader :)


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

Answer



Thanks for your patience. Here's a way to get decent worldspace texturing on flat/hard-edged surfaces for walls & ramps, aligning the vertical axis of the texture with the world up direction:


Example of the shader tiling the texture evenly


This won't work for purely horizontal surfaces like floors & ceilings, where world "up" points into the surface, not along it, so we'll include a fallback alignment for those cases (say, aligning the texture vertical with world "north" instead)


Here since we're in 3D I'm assuming we want to use a lit Surface Shader, so starting with a new default Surface Shader we need to do two things:





  1. Find the #pragma surface line and add "vertex:vert" to the end of it, to say we want to use a custom vertex-modifying function named "vert"




  2. Write a vertex shader called "vert" that takes an inout appdata_full argument and modifies its texture coordinates.


        CGPROGRAM
    // Physically based Standard lighting model, and enable shadows on all light types
    #pragma surface surf Standard fullforwardshadows vertex:vert


    void vert(inout appdata_full v) {

    // Get the worldspace normal after transformation, and ensure it's unit length.
    float3 n = normalize(mul(unity_ObjectToWorld, v.normal).xyz);

    // Pick a direction for our texture's vertical "v" axis.
    // Default for floors/ceilings:
    float3 vDirection = float3(0, 0, 1);

    // For non-horizontal planes, we'll choose

    // the closest vector in the polygon's plane to world up.
    if(abs(n.y) < 1.0f) {
    vDirection = normalize(float3(0, 1, 0) - n.y * n);
    }

    // Get the perpendicular in-plane vector to use as our "u" direction.
    float3 uDirection = normalize(cross(n, vDirection));

    // Get the position of the vertex in worldspace.
    float3 worldSpace = mul(unity_ObjectToWorld, v.vertex).xyz;


    // Project the worldspace position of the vertex into our texturing plane,
    // and use this result as the primary texture coordinate.
    v.texcoord.xy = float2(dot(worldSpace, uDirection), dot(worldSpace, vDirection));
    }


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...