Hallo everyone, this is my first post in this forum so forgive me if this is the wrong section.
Sorry for my english too, isn’t my mother language sorry
I’ve done the full system to procedurally generate my world (in this case my World Map) and I want to take care about perfomance using shader (well the appearence too since using shader result in a more gorgeus appearence with some custom made calculations).
Previously I was using mat.mainTexture = myTexture2D in applyin my Texture2D heightMapTexture and everything was fine (except for the appearence ofcourse), now I switched to a Custom Shader and tryin to use the exact same custom texture using mat.SetTexture(), but here comes the problem.
Most of my systems takes care about heights, I draw colors using those heights and when I have to spawn something I refer to the heightArray to check if it is a valid spot or not, so the height are crucial in the game.
Since using shader often my spawns popup on uncorrect spots (like water ones), I’ve analyzed the problem so far and finally found what cause the problem: applyin the procedural generated texture to the material via mat.mainTexture = myTexture2D results in a correct texture where pixel greyScale (or color, doesn’t make any difference) are equivalent to the height inside heightMap array, while applying the exact same texture to material using mat.SetTexture(“_MainTex”, myTexture2D) results in a slightly different texture where pixels are darker like if the height is lower.
In the example below I’ve used the same material with the same shader, the same values to generate the heightMap array, the same heightMap array, two same procedural generated Meshes and the exact same way to draw pixels with the exact same value on my Texture2D (the height from heightMap array).
The shader is just a Standard Surface Shader and I’ve also used different shaders (built in ones and new custom ones too, as can be seen in the example below) with the exact same result.
Ofcourse the result won’t change if I switch to actual colors from a table of greyscale.
Finally no interpolation (lerp, inverselerp, planar mapping, etc) and blending are being applied to the values used for pixels (float height) neither to the color value drawn on the texture (which is in the form of colorMap[ID] = new Color(height, height, height); ), just the plain “float height” value applied as Color using SetPixels using an array of colors (not Color32 and SetPixels32, anyway I’ve tried using them too with the same result as below) on an object, and the “float height” as Color using mat.mainTexture = myTexture2D in the other obejct.
I’ve also tried to use repeat wrapMode over clamp and point filtering over bilinear and trilinear, changed the anisioLevel too, I’ve also used some sort of interpolation (lerping colors on CPU, on shader, blending colors on CPU and on shader) but while the result cover the uncorrect spots, raise the other ones too and doesn’t fix the problem at all, and I’ve tried to use different textureformats (ARGB32, RGBA32, RGB24, RGBA444, RGB565, etc) but the result is always the same as shown here below.
I really need to have the exact pixel rapresentation from the heightMap array inside shaders, I’ve thought to use mat.mainTexture = myTexture2D but from my big ignorance with shaders I think isn’t an elegant aproach and I guess is way more flexible to use SetTexture(); so please help me since this problem is driving me crazy.
Inside shader (Standard Surface, used to show the example in the images below) I’ve used so many aproaches that I can’t remember (inverseLerp between colors, blending, plain color, greyscale over colors, maths to get the height starting from the vertex position inside vert method declared in #pragma, getting greyscale from the tex2D(_MainTex, IN.uv_MaintTex and using it to color the pixel), I’ve just tryed the most basic operation possible to study the problem: “o.Albedo = tex2D(_MainTex, IN.uv_MainTex)”; and also used “fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a;” and as you can easly know at this point nothing won’t to fix this unwanted behaviour.
PS: I didn’t post the code since it’s pretty huge, so if you need something specifically just tell me and I’ll post it: the meshes (and their UVs) are built in a method which calls Mesh_Data class ctor, the method to build mesh is called twice to build 2 different meshes, the heightMap array is built in a method called from a class, the heithMapTexture is built inside another method called at the end of the method in charge of building the heightMap array, can’t copy paste the entire code since is something over 4000 lines of code fully commented, so if you need soemthing just tell me and I’ll post the exact piece, every method works regardless the others so can be called directly without any problem or correlation with other methods. The shader used inside this example shown in the following images is a Surface one having inside surf function the only line “o.Albedo = tex2D(_MainTex, IN.uv_MainTex);”.
Please help me, I need pixels to have colors exactly to the height values but using shader over CPU; I really don’t know what else to do since I’m a total noob with shaders (and I have 0 or less knowledge about ShaderGraph neither I know where I can find tutorials or explanation about them).
Thanks a lot
Following the Images explain the problem: