reflection shader with vertex displacement

Dante's icon

I'm trying to adapt Wes' reflection shader with a vertex displacement shader and figure im at a good spot to ask for assistance. It doesnt seem to be coming together the way I hoped, but I lack the language to describe what the problem is. Text-based coding is frikin hard.

<pre><jittershader name="contrast-interp">
    <description>Sphere Map Reflection Shader</description>
    <param name="tex0" type="int" default="0" />
    <param name="tex1" type="int" default="1" />
    <param name="scale" type="vec2" default="1.0 1.0" />
    <param name="fresnel" type="vec3" default="0.05 2. 0.95" />
    <param name="extrude" type="float" default="1.0" />
    <language name="glsl" version="1.0">
        <bind param="tex0" program="fp" />
        <bind param="tex1" program="fp" />
        <bind param="scale" program="fp" />
        <bind param="fresnel" program="fp" />
        <bind param="extrude" program="vp" />
        <program name="vp" type="vertex">
<![CDATA[
//fresnel shader
varying vec2 texcoord0;
varying vec2 texdim0;
varying vec4 V;
varying vec3 N;
//vert dis
uniform sampler2DRect tex1;
uniform float extrude;

varying vec3 textureNoise;

void main (void)

{    
    texdim0 = vec2(abs(gl_TextureMatrix[0][0][0]),abs(gl_TextureMatrix[0][1][1]));//fres
    //extrusion
    texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); //texture for color

    vec2 texcoord1 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord1); // texture for extrude
    textureNoise = texture2DRect(tex1, texcoord1*0.5).xyz; //2 access 2nd tex data for VertexDis

    //fresenel
    V = gl_ModelViewMatrix * gl_Vertex;
    N = normalize(gl_NormalMatrix * gl_Normal);

    vec3 displacedPos = gl_Vertex.xyz + textureNoise*extrude; //2 add texNoise to the vertices

    //extrude
    vec4 eyePos = gl_ModelViewMatrix * gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * vec4(displacedPos, 1.0);

}
]]>        
        </program>
        <program name="fp" type="fragment">
<![CDATA[
//fresnel
varying vec2 texcoord0; //for extrude and fresnel
varying vec2 texdim0;
uniform sampler2DRect tex0;
uniform sampler2DRect tex1;

varying vec4 V;
varying vec3 N;

uniform vec2 scale;
uniform vec3 fresnel;
//extrude
varying vec3 textureNoise;

const vec4 backg = vec4(1.);

vec2 sphereMap(vec3 R)
{
    float m = 2.*sqrt(R.x*R.x + R.y*R.y + (R.z+1.)*(R.z+1.));    //p. 382 glspec
    vec2 reflectCoords;
    reflectCoords.s = (R.x/m + 0.5);
    reflectCoords.t = 1.-(R.y/m + 0.5);
    reflectCoords *= texdim0;

    return reflectCoords;
}

void main (void)
{
    vec3 scaleVertex = vec3(scale*V.xy, V.z);
    vec3 Nn = normalize(N);
    vec3 Vn = normalize(scaleVertex);

    //vec3 R = Vn-2.*Nn*dot(Nn, Vn);
    vec3 R = reflect(Vn, Nn);

    //Fresnel: (bias, scale, falloff)
    float reflectionCoeff = max(0., min(1., fresnel.x + fresnel.y *
                                            pow(1.-abs(dot(Vn, Nn)), fresnel.z)));

    vec2 reflectCoords = sphereMap(R);

    vec3 Rfract = refract(Vn, Nn, 1.33);
    vec2 refractCoords = sphereMap(Rfract);    

    vec4 R_color = texture2DRect(tex0, reflectCoords);
    vec4 Rf_color = texture2DRect(tex0, reflectCoords);//changed tex1
    vec4 Rf_color2 = texture2DRect(tex0, Rfract.xy*2.*texdim0);

    vec4 color = mix(R_color, Rf_color+Rf_color2*0.5, reflectionCoeff);
//    gl_FragColor = color;
    //extrude below
    gl_FragColor = gl_Color;
    vec4 textureVals = texture2DRect(tex0,texcoord0); //1
    gl_FragColor = vec4(textureVals.xyz * abs(textureNoise), 1.0);
}
]]>        
        </program>        
    </language>
</jittershader></pre>

Max Patch
Copy patch and select New From Clipboard in Max.

Dante's icon

Ok sry for the double, but I got it down to one problem. The reflection is working now, but the vertex displacement seems to be going out at a weird angle(ie not out-from-center of model) So here is the updated shader.

Been a whole year of working towards this, this is the closest i've been. hope these tags work this time 🤷‍♂️

<pre>
<jittershader name="contrast-interp">
    <description>Sphere Map Reflection Shader</description>
    <param name="tex0" type="int" default="0" />
    <param name="tex1" type="int" default="1" />
    <param name="scale" type="vec2" default="1.0 1.0" />
    <param name="fresnel" type="vec3" default="0.05 2. 0.95" />
    <param name="extrude" type="float" default="1.0" />
    <language name="glsl" version="1.0">
        <bind param="tex0" program="fp" />
        <bind param="tex1" program="fp" />
        <bind param="scale" program="fp" />
        <bind param="fresnel" program="fp" />
        <bind param="extrude" program="vp" />
        <program name="vp" type="vertex">
<![CDATA[
//fresnel shader
varying vec2 texcoord0;
varying vec2 texdim0;
varying vec4 V;
varying vec3 N;
//vert dis
uniform sampler2DRect tex1;
uniform float extrude;
varying vec3 textureNoise;

void main (void)

{    
    texdim0 = vec2(abs(gl_TextureMatrix[0][0][0]),abs(gl_TextureMatrix[0][1][1]));//from fresnel
    //extrusion
    texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0); //texture for color
vec2 texcoord1 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord1); // texture for extrude

    textureNoise = texture2DRect(tex1, texcoord1*0.5).xyz; //2 access 2nd tex data for VertexDis

    //fresenel
    V = gl_ModelViewMatrix * gl_Vertex;
    N = normalize(gl_NormalMatrix * gl_Normal);

    vec3 displacedPos = gl_Vertex.xyz + textureNoise*extrude; //2 add texNoise to the vertices

    //extrude
    vec4 eyePos = gl_ModelViewMatrix * gl_Vertex;
    gl_Position = gl_ModelViewProjectionMatrix * vec4(displacedPos, 1.0);

}
]]>        
        </program>
        <program name="fp" type="fragment">
<![CDATA[
//fresnel
varying vec2 texcoord0; //for extrude and fresnel
varying vec2 texdim0;
uniform sampler2DRect tex0;
uniform sampler2DRect tex1;

varying vec4 V;
varying vec3 N;

uniform vec2 scale;
uniform vec3 fresnel;
//extrude
varying vec3 textureNoise;

const vec4 backg = vec4(1.);

vec2 sphereMap(vec3 R)
{
    float m = 2.*sqrt(R.x*R.x + R.y*R.y + (R.z+1.)*(R.z+1.));    //p. 382 glspec
    vec2 reflectCoords;
    reflectCoords.s = (R.x/m + 0.5);
    reflectCoords.t = 1.-(R.y/m + 0.5);
    reflectCoords *= texdim0;

    return reflectCoords;
}

void main (void)
{
    vec3 scaleVertex = vec3(scale*V.xy, V.z);
    vec3 Nn = normalize(N);
    vec3 Vn = normalize(scaleVertex);

    //vec3 R = Vn-2.*Nn*dot(Nn, Vn);
    vec3 R = reflect(Vn, Nn);

    //Fresnel: (bias, scale, falloff)
    float reflectionCoeff = max(0., min(1., fresnel.x + fresnel.y *
                                            pow(1.-abs(dot(Vn, Nn)), fresnel.z)));

    vec2 reflectCoords = sphereMap(R);

    vec3 Rfract = refract(Vn, Nn, 1.33);
    vec2 refractCoords = sphereMap(Rfract);    

    vec4 R_color = texture2DRect(tex0, reflectCoords);
    vec4 Rf_color = texture2DRect(tex0, reflectCoords);//changed tex1
    vec4 Rf_color2 = texture2DRect(tex0, Rfract.xy*2.*texdim0);

    vec4 color = mix(R_color, Rf_color+Rf_color2*0.5, reflectionCoeff);
    
    //extrude below
    
    vec4 textureVals = texture2DRect(tex0,texcoord0); //1
    vec4 idk = color * textureVals;
    //gl_FragColor = vec4(vec4(abs(textureNoise), 1.0) * idk);
    gl_FragColor = idk;
}
]]>        
        </program>        
    </language>
</jittershader>

</pre>

Rob Ramirez's icon

please attach a zip of your shader and your patch and i'll take a look

Dante's icon

Thanks so much Rob, I know this effect can be accomplished with jit.gl.material, but am trying to understand shaders. I have also included wes' reflection/ refraction shader for reference.

Reflect Vert Dis.zip
application/zip 14.77 KB