- Home /
Question by
AsurZar · Oct 13, 2019 at 08:49 AM ·
surface shaderuv coordinates
Problems with uv for surface shader
Shader "Ocean/OceanSurface"
{
Properties
{
// texture
_HeightMap("Height Map", 2D) = "white"{}
_NormalMap("Normal Map", 2D) = "white"{}
// water scale : x - height scale, y - height offset, z - choppy y scale, w - water tile size
_WaterScale("Scale Data", Vector) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
sampler2D_float _HeightMap;
sampler2D _NormalMap;
float4 _WaterScale;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv_ocean;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
// uv
o.uv_ocean = float2(v.vertex.x / _WaterScale.w, v.vertex.z / _WaterScale.w);
//o.uv_ocean = float2(v.vertex.x / 1.0f, v.vertex.z / 1.0f);
// map for fetch
float HeightChoppyMap = tex2Dlod(_HeightMap, float4(o.uv_ocean, 0.0f, 0.0f));
// height
v.vertex.y = (HeightChoppyMap * _WaterScale.x + _WaterScale.y);
}
void surf(Input IN, inout SurfaceOutput o)
{
// case 1
o.Albedo = float3(IN.uv_MainTex.x, IN.uv_MainTex.y, 0.5f);
// case 2
//o.Albedo = float3(IN.uv_ocean.x, IN.uv_ocean.y, 0.5f);
//o.Albedo = tex2D(_NormalMap, IN.uv_MainTex);
//o.Normal = 2.0f * tex2D(_NormalMap, IN.uv_MainTex) - 1.0f;
//o.Albedo = o.Normal;
}
ENDCG
}
Fallback "Diffuse"
}
Generation of mesh:
Mesh createMesh(int width, int height, float widthSize, float heightSize)
{
Vector3[] vertices = new Vector3[width * height];
Vector2[] texcoords = new Vector2[width * height];
int[] indices = new int[(width - 1) * (height - 1) * 6];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector2 uv = new Vector3((float)x / (float)(width - 1), (float)y / (float)(height - 1));
Vector3 pos = new Vector3((float)x / (float)width * widthSize, 0, (float)y / (float)height * heightSize);
texcoords[x + y * width] = uv;
vertices[x + y * width] = pos;
}
}
int num = 0;
for (int x = 0; x < width - 1; x++)
{
for (int y = 0; y < height - 1; y++)
{
indices[num++] = x + y * width;
indices[num++] = x + (y + 1) * width;
indices[num++] = (x + 1) + y * width;
indices[num++] = x + (y + 1) * width;
indices[num++] = (x + 1) + (y + 1) * width;
indices[num++] = (x + 1) + y * width;
}
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = texcoords;
mesh.triangles = indices;
mesh.UploadMeshData(false);
mesh.RecalculateNormals();
mesh.RecalculateTangents();
return mesh;
}
}
As you can see it's blue, in shader: // case 1 o.Albedo = float3(IN.uv_MainTex.x, IN.uv_MainTex.y, 0.5f); // case 2 //o.Albedo = float3(IN.uv_ocean.x, IN.uv_ocean.y, 0.5f);
This means uv is 0, and this is the same for both cases, what could be wrong?
issue-uv.jpg
(370.3 kB)
Comment
Best Answer
Answer by AsurZar · Oct 13, 2019 at 05:32 PM
Just needs to rename from uv_ocean to oceanUV to make works custom coordinates, also uv_MainTex not working without _MainTex