Hi,
In my terrain material, I am using the below code in my fragment shader to fix the texture repeating effect.
// The MIT License
// Copyright © 2017 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// A technique to avoid texture tile repetition when using one small
// texture to cover a huge area. Basically, it creates 8 different
// offsets for the texture and picks two to interpolate between.
//
// Unlike previous methods that tile space (https://www.shadertoy.com/view/lt2GDd
// or https://www.shadertoy.com/view/4tsGzf), this one uses a random
// low frequency texture (cache friendly) to pick the actual
// texture's offset.
//
// Also, this one mipmaps to something (ugly, but that's better than
// not having mipmaps at all like in previous methods)
//
// More info here: https://iquilezles.org/articles/texturerepetition
vec4 textureNoTile3( sampler2D diffuseMap, in vec2 texCoord, float diffuseScale, float noiseScale, int noiseLayerCount, float noiseOffsetFactor, float noiseRotationFactor) {
vec2 uv = texCoord * diffuseScale;
vec4 color;
#ifdef NOISEMAP
if (noiseScale > 0.0) {
// sample variation pattern
vec4 noise = texture(m_NoiseMap, noiseScale * texCoord); // cheap (cache friendly) lookup
// compute index
float index = noise.x * noiseLayerCount;
// float ia = floor(l); // my method
// float ib = ia + 1.0;
float indexA = floor(index + 0.5);
float indexB = floor(index);
float fraction = fract(index);
fraction = min(fraction, 1.0 - fraction) * 2.0;
// compute offsets for the different virtual patterns
vec2 offsetA = sin(vec2(3.0, 7.0) * indexA) * noiseOffsetFactor; // can replace with any other hash
vec2 offsetB = sin(vec2(3.0, 7.0) * indexB) * noiseOffsetFactor; // can replace with any other hash
// compute rotations for the different virtual patterns
const float TWO_PI = 2.0 * 3.141592f;
float angleA = offsetA.x * offsetA.y * TWO_PI * noiseRotationFactor;
float angleB = offsetB.x * offsetB.y * TWO_PI * noiseRotationFactor;
// compute derivatives for mip-mapping
vec2 dx = dFdx(uv);
vec2 dy = dFdy(uv);
// sample the two closest virtual patterns
vec3 colorA = textureGrad(diffuseMap, rotateUV(uv, angleA) + offsetA , dx, dy).xyz;
vec3 colorB = textureGrad(diffuseMap, rotateUV(uv, angleB) + offsetB , dx, dy).xyz;
// interpolate between the two virtual patterns
color = vec4(mix(colorA, colorB, smoothstep(0.2, 0.8, fraction - 0.1 * sum(colorA - colorB))), 1.0);
} else {
color = texture2D(diffuseMap, uv);
}
#else
color = texture2D(diffuseMap, uv);
#endif
return color;
}
it reads diffuse texture two times each time with a slightly offseted UV and mixes the colors to remove texture repeating effect. Also, there is one texture read for a noise map.
I noticed it decreases by around 5 fps per texture layer on terrain. I am using 4 texture layers on terrain and am losing 20 fps just for running the above method.
Any suggestion on how I can improve its performance?
Is it possible to bake the result to vertex color or into an in-memory texture and only run it when re-baking is required?
P.S:
I also found about extensions like ARB_shader_image_load_store and SSBO but sounds like they require OpenGL 4. Also could not find an example of usage in JME