[Solved] Two textures in custom shader material

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
paul424
Gnome
Posts: 314
Joined: Thu May 24, 2012 7:16 pm
x 13

[Solved] Two textures in custom shader material

Post by paul424 »

Ogre Version: : 1 12 12?:
Operating System: :OpenSuse Tumbleweed?:
Render System: :OpenGL3+ Renderer?:


Hello, as in chinese proverb I did monkey see monkey do ; that is I copied and modified it for lava pattern for opendungeons to make it non repetitive

Vertex shader

Code: Select all

#version 330 core
#extension GL_ARB_explicit_uniform_location : enable
#extension GL_ARB_shading_language_include : enable

#include "PerlinNoise.glsl" 

uniform    mat4 projectionMatrix;
uniform    mat4 viewMatrix;
uniform    mat4 worldMatrix;
uniform float time1;
layout (location = 0) in vec4 aPos;
 
layout (location = 8) in vec2 uv_0;

out vec2 out_UV0;

out vec3 FragPos;
 
#define PI 3.1415926538

vec3 deform(vec3 pos) {
    pos.x += perlin(pos.x,pos.y);
    pos.y += perlin(pos.y,pos.x);
    pos.z = sin(time1/10.0 + (pos.x  ) * PI)/12.0 + cos(2*PI*cos(time1/10.0 + 2*pos.y ))/12.0;
    return pos;
}
 
 
void main() {
    // compute world space position, tangent, bitangent
    vec3 P = (worldMatrix * aPos).xyz;
    
    
    P = deform(P); 
    gl_Position = projectionMatrix * viewMatrix * vec4(P, 1.0);

    FragPos = P;
 
    out_UV0 = uv_0;
} 
fragment shader :

Code: Select all

#version 330 core
#extension GL_ARB_explicit_uniform_location : enable
 
// 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.


// One way to avoid texture tile repetition one 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: http://www.iquilezles.org/www/articles/texturerepetition/texturerepetition.htm

uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
in vec2 out_UV0;
in vec3 FragPos;
uniform float time2;
out vec4 color;



float sum( vec3 v ) { return v.x+v.y+v.z; }

vec3 textureNoTile( in vec2 x, float v )
{
    float k = texture( iChannel1, 0.005*x ).x; // cheap (cache friendly) lookup
    
    vec2 duvdx = dFdx( x );
    vec2 duvdy = dFdy( x );
    
    float l = k*8.0;
    float f = fract(l);
    
#if 1
    float ia = floor(l); // my method
    float ib = ia + 1.0;
#else
    float ia = floor(l+0.5); // suslik's method (see comments)
    float ib = floor(l);
    f = min(f, 1.0-f)*2.0;
#endif    
    
    vec2 offa = sin(vec2(3.0,7.0)*ia); // can replace with any other hash
    vec2 offb = sin(vec2(3.0,7.0)*ib); // can replace with any other hash

    vec3 cola = textureGrad( iChannel0, x + v*offa, duvdx, duvdy ).xyz;
    vec3 colb = textureGrad( iChannel0, x + v*offb, duvdx, duvdy ).xyz;
    
    return mix( cola, colb, smoothstep(0.2,0.8,f-0.1*sum(cola-colb)) );
}

void main(  )
{
	vec2 uv = FragPos.xy/4.0 ;
	
	float f = smoothstep( -1.0, 1.0, sin(time2/50.0)    );

        
	vec3 col = textureNoTile( (uv), f );
	
	color = vec4( col, 1.0 );
} 
material file :

Code: Select all

vertex_program myLavaVertexShader glsl
{
  source LavaDistortion.vert
    default_params
    {
        param_named_auto projectionMatrix projection_matrix
        param_named_auto viewMatrix view_matrix
        param_named_auto worldMatrix world_matrix
        param_named_auto time1 time 
    }
  
}

fragment_program myLavaFragmentShader glsl
{
  source LavaDistortion.frag
    default_params
    {
        param_named_auto time2 time 
    }
}



material Lava //: RTSS/NormalMapping_MultiPass
{
//     receive_shadows on

    technique
    {
        pass
        {      
            vertex_program_ref myLavaVertexShader
            {
                
            }
            fragment_program_ref myLavaFragmentShader
            { 
                param_named iChannel0 int 0
                param_named iChannel1 int 0
            }
 
            texture_unit 
            {
                texture Lava.png

            }

            
            
        }

    }
}   
problem is : The Lava.png is both Channel0 ( the basic texture) as well as Channel1 ( the NOISE texture ) . I have noise texture prepared in Channel1.png . How do I make it to be Channel1 texture ? I tried the obvious solution and it doesn't work .
Ogre.log (optional)
Last edited by paul424 on Wed Oct 27, 2021 5:17 pm, edited 1 time in total.
User avatar
paul424
Gnome
Posts: 314
Joined: Thu May 24, 2012 7:16 pm
x 13

Re: Two textures in custom shader material

Post by paul424 »

The solution which comes is :

Code: Select all

 
            fragment_program_ref myLavaFragmentShader
            { 
                param_named iChannel0 int 0
                param_named iChannel1 int 1
            }
 
            texture_unit 
            {
                texture Lava.png 2d

            }
            texture_unit 
            {
                texture Channel1.png 2d

            }            
I don't know whether is it fine or not ...
Image
Post Reply