- Home /
Question by
Reverend-Speed · Nov 14, 2019 at 02:10 PM ·
shadertextureshader programminguv mapping
Why is my UV'd texture displaying wrapped with the wrong scale?
Hey folks. Still getting to grips with shaders here and I'm having an issue where my UV unwrapped texture for my creature is either appearing multiple times on the creature surface or (if I mark the texture clamped) is only showing part of the texture stretched across the creature.
Shader code follows:
Shader "Custom/CaveCreatureShader2"
{
Properties
{
_MainTex ("C", 2D) = "white" {}
// How much creature vs cave material.
_PercentCreature ("Percentage Creature", Range(0,1)) = 0
// Standard shader
_Color("Color", Color) = (1,1,1,1)
_CreatureTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
//Pass
//{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
// Use 'custom stupid' lighting...
#pragma surface surf CustomStupid vertex:vert
#include "UnityPBSLighting.cginc"
//fixed4 _Color;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Input
{
float2 uv_MainTex;
//--- TRIPLANAR STUFF ---//
float3 worldNormal;
float3 worldPos;
float3 localPos; // Local position of vertex
};
// Helper funcs!
float4 tex3D_2D(in float3 pos, in float3 normal, sampler2D tex) {
return tex2Dlod(tex, float4(pos.y, pos.z, 0, 0)) * abs(normal.x)
+ tex2Dlod(tex, float4(pos.x, pos.z, 0, 0)) * abs(normal.y)
+ tex2Dlod(tex, float4(pos.x, pos.y, 0, 0)) * abs(normal.z);
}
sampler2D _MainTex;
sampler2D _CreatureTex;
// float4 _MainTex_ST;
float _PercentCreature;
half _Glossiness;
half _Metallic;
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.localPos = v.vertex.xyz;
}
// Apply bump map.
float3 material_TextureBump(in sampler2D tex, inout float3 normal, float3 p) {
// TEMP!!
const float scale = 0.04;
const float normScale = 0.03;
normal = normalize(normal);
float3 col = tex3D_2D(p*scale, normal, tex).rgb;
return col;
}
float3 getLights(in float3 color, in float3 pos, in float3 normal) {
float3 lightValue = 0.1 * float3(1, 1, 1);
const float lightStr = 0.5;
const float3 lightDir = float3(1, 1, 0);
float diff = max(lightStr * dot(-lightDir, normal), 0.0);
float3 diffuse = diff * lightValue;
lightValue = color * diffuse;
return lightValue;
}
half4 LightingCustomStupid(SurfaceOutputStandard s, half3 viewDir, UnityGI gi) {
float p = 0; // Disable custom lighting for the time being... Doesn't play nicely.
return lerp(half4(s.Albedo, 1), LightingStandard(s, viewDir, gi), p);
}
inline void LightingCustomStupid_GI(
SurfaceOutputStandard s,
UnityGIInput data,
inout UnityGI gi)
{
LightingStandard_GI(s, data, gi);
}
void surf(Input IN, inout SurfaceOutputStandard o)
{
float3 p = IN.worldPos;
float3 lp = IN.localPos * 10; // TODO: Tweak scaling.
float3 n = IN.worldNormal;
// Then, we get the color of the world at that point, based on our material ids.
// Interpolated between cave and wall colours
float3 colorBase =
material_TextureBump(_MainTex, n, p) * (1-_PercentCreature) + // Cave/Wall texture
tex2D(_CreatureTex, IN.uv_MainTex).rgb * _PercentCreature; // Creature texture
float3 light = getLights(colorBase, p, n);
// The ambient color is applied.
const float3 _AmbientColor = float4(13.0,13.0,13.0,255).rgb / 255;
float3 color = colorBase * _AmbientColor;
// And lights are added.
color += light;
// Dist to camera
float dist = distance(p, _WorldSpaceCameraPos) / 35;
// Fog!
const float4 FogColor = float4(169, 190, 191, 255) / 255.0;
const float FogDensity = 0.01;
color = lerp(color, FogColor, 1.0-exp2(-FogDensity * dist * dist));
o.Albedo = color.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = 1;
}
ENDCG
//}
}
}
I'm really confused. Can anybody help me get the creature texture appearing as a normal skinned image? Running out of time here, so a fast response would be appreciated...!
Comment
Your answer
Follow this Question
Related Questions
Overlaying a Darkening Texture over Certain UVs with a Shader 0 Answers
Shader: Mask Without a Texture? 2 Answers
How to remove a color from texture using Shader? 0 Answers
How to blend two textures? 1 Answer
Shader Graph texture limit? 1 Answer