Jump to content

Need advice for make Shader


Blax
 Share

Recommended Posts

Hi, guru's!

 

I need advice or dig direction.

 

Problem:

Make shader material (glsl) and in vertex shader i change vertexes position. It work ok and results is pretty.

 

BUT (yes, again this damn "BUT") :)

 

In fragment shader i need normal for compute reflection, diffuse and etc.

 

Source mesh is flat, and all normal look up and equal.  Vertex shader change vertex position, but normals stay not changed.

 

How i can recalculate (or get somewhere :)) ) new normals?  In vertex shader i have only ONE current vertex, but for calculate normal we need face (i think :) i.e. 3 vertexes).

How get new normal in current vertex in new position? Help please!

 

p. s.

http://www.babylonjs.com/cyos/  Template "wave"  we have near situation, but normal (as i understand) not recalc and in distortion result we not get real light piscture.... or have?  Trap...

 

Link to comment
Share on other sites

p. s.

http://www.babylonjs.com/cyos/  Template "wave"  we have near situation, but normal (as i understand) not recalc and in distortion result we not get real light piscture.... or have?  Trap...

 

OK, so I don't know what your shaders look like, but let's use the example of the wave effect you link to. In that case you could easily rotate the normal without looking at adjacent vertices - you have a function used to offset a vertex, so the normal that is used as in input to the vertex shader needs to be rotated by the normal to the offset function (the amount of rotation needs to be scaled by the amount of that offset nb: the scaling of the offset may not be linear... I'm too lazy to do the maths).

Link to comment
Share on other sites

http://www.babylonjs.com/cyos/#1YDNAM#5

 

 

it is a simple way for find  a new normal after change pos

 

step 1 : you need find your function like 

 

          new_pos = function(old_pos);

 

step 2 : call function for 2 new pos

 

         new_pos1 = function(old_pos+vec3(0.,0.,x));

 

         new_pos2 = function(old_pos+vec3(x,0.,0.0));

 

x: 0.2 : need some test for diffrent object:)

 

step 3 :

 

      vec3 nrm = -1.0*normalize( cross(new_pos1 - new_pos ,new_pos2 - new_pos)); 

      vNormal = nrm*0.5+normal*0.5;

 

 

 

 

 

http://www.babylonjs.com/cyos/#1YDNAM#6

Link to comment
Share on other sites

http://www.babylonjs.com/cyos/#1YDNAM#5

 

 

it is a simple way for find  a new normal after change pos

 

step 1 : you need find your function like 

 

          new_pos = function(old_pos);

 

step 2 : call function for 2 new pos

 

         new_pos1 = function(old_pos+vec3(0.,0.,x));

 

         new_pos2 = function(old_pos+vec3(x,0.,0.0));

 

x: 0.2 : need some test for diffrent object:)

 

step 3 :

 

      vec3 nrm = -1.0*normalize( cross(new_pos1 - new_pos ,new_pos2 - new_pos)); 

      vNormal = nrm*0.5+normal*0.5;

 

 

 

 

 

http://www.babylonjs.com/cyos/#1YDNAM#6

 

 

Thank you!!!  I need think about it and make tests :)

 

Now i wonder can i make function x2=f(x1), because we dont know how is vertexes sequences in shader.   Look, we have quad surface (2 triangles). Sequence may be p1-p2-p3,p1-p3-p4  or p1-p2-p4,p2-p3-p4 or another variants.

And x2 (in midn shader) and x2 (in us mind) may be different. Or i mistaken?

Link to comment
Share on other sites

just move the current position to the sides like vec3(0.,0.,0.1) or vec3(0.1,0.,0.) maybe you change that pos per case but this way is not final answer just closer to the final normal

 

** you can find  flat normal with normalize(cross(dFdx(pos*-1.),dFdy(pos))) 

 

Thank you again!

While i can't get good result (small shiffts i was try). Will continue dig and tell here about progress.... or lack of progress :)

Link to comment
Share on other sites

NasimiAsl, hi!

 

I made this :)

 

You method is right.

float	delta		=	0.08;vec3    p		=	position;vec3 	p1		=	normalize(vec3(p.x-delta,	yFXYZ(vec3(p.x-delta,p.yz)),	p.z));vec3 	p2		=	normalize(vec3(p.x,		yFXYZ(vec3(p.xy,p.z-delta)),	p.z-delta));	vNormal			=	normalize(cross((p1-p),(p2-p)));

Thank's!

 

But i have other absorbing problem - near zero point of mesh we have "quirk edge" effect. I think because, when number's is very small we have math mistakes in GPU. 

I cant beat this (three day's test for catch problem). And invented only shift all mesh away from zero point :)  Yes, shame with me for this "solution", but while this work :)))

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...