Jump to content

Shader - Cut box at top and bottom


royibernthal
 Share

Recommended Posts

You've yet to tackle the shader I'm trying to create :D Notice it's different than what you did in the old "Real 3D Mask" topic.

 

On 9/16/2016 at 1:53 AM, royibernthal said:

This topic is a bit similar to an old topic of mine "Real 3D Mask", only this time I know a little more what I need and how I need it.

 

I created a box mesh.

 

I'd like to create a shader in which I can specify the y of 2 lines:

-top line - nothing above it is drawn

-bottom line - nothing below it is drawn

 

The areas above the top line or below the bottom line should be "cut" in a 3d way, a 2d mask or anything of the sort will have a bad looking result.

 

I added a sketch to demonstrate how the result should look like.

Second picture from the left - Red parts are either above top line or below bottom line and shouldn't be drawn, green part should be drawn.

example.png

 

From what I understood from the previous topic ("Real 3D Mask"), it should be pretty simple, just check in a vertex shader if the current pixel is above the top line or below the bottom line, and if so, discard it.

As simple as it is, I have no idea how to do it, as I'm clueless when it comes to shaders.

Here's a link to the old Playground (by @NasimiAsl), please don't use it, it's just to remind you of a similar solution so we don't start from scratch.

http://www.babylonjs-playground.com/#QHMT1#7

 

For this topic I created a new playground:

http://www.babylonjs-playground.com/#1IUU00#0

 

Can you please help me write this shader? I'd be twice more grateful if you'll help me understand what you write.

Link to comment
Share on other sites

ok  let look that step by step

this is a simple shader but have Mathematical Stuff ( that make it compilicated )

step 1 : setup ShaderBuilder ---------------------------------------------------------------------------------------------------

line 1,2,3,4  : Add ShaderBuilder js

line 38         : wait to load SB.js

**** in real sample you dont need wait

line 39 : BABYLONX.ShaderBuilder.InitializeEngine();  // initialize some script ShaderBuilder need to Create ShaderMaterial from BABYLON.JS

**** for use the ShaderBuilder you Most be Initialize That

line 82-87 : this put default information about scene to Shader like Camera position , ... per each render

 

step 2 : make CustomShader From ShaderBuilder ------------------------------------------------------------------------ 

line 40 : make a new mesh array

line 51 - 66 : make on 4 box and add in to the array and calculate 3 const uniforms

line 59,60  :     minh , maxh : minimum  and maximum height (we don't access  to this part in shader so we need send that for shader)

line 61 :            steph : all box have some space between we can calculate that in javascript easly so add this like const uniform too

line 116 : start making Shader    material =   new BABYLONX.ShaderBuilder()

line 117 : .Solid({ b: 1 }) : //no matter :D just set the blue color for first shade

line 118 : 

          // me : current instance

           .Func(function (me) {  // this method let us to change some parameter like setting inside the fluent pattern
                  me.Setting.FragmentWorld  = true; // enable wordviewMatrix for fragment shader this let shader now about world poistion
                  return me; // back to current state
            })  

line 112 :

inline let you make your wanted code inside the shaderBuilder generated you can do anything you do in main method in shader

              .InLine('   vec3 wpos =  vec3(world * vec4(pos,1.));') // calculate world position

line 123 : define a new uniform 'maskHeight' this is main position all box is cut

line 124,125 , 126 : define ather uniforms

line 127 : .VertexShader : this help you to make Vertex Shader (other line  make a fragment shader )

math part :

// this condition choose all top 4 vertex on all box becase top position have positive value in y 

if(nrm.y>0.)                keep x pos                                                                        keep z pos

   result =  vec4(vec3( pos.x ,             max(minh,min(maxh,maskHeight))-steph    , pos.z ) ,1.);

*****    max(minh,min(maxh,maskHeight))-steph :  choose between minh and maxh and step and dont let this go more than  maskHeight     

// this condition choose all bottom 4 vertex on all box

else result = vec4(pos,1.); // keep defult position

.Map({path:'http://jerome.bousquie.fr/BJS/images/spriteAtlas.png'  }) // texture box

.InLine('if((nrm.y<=0. && wpos.y > maskHeight ) ||  maskHeight < minh ) discard;') // hide other position

line 132 : make your final shader

this function called 4 times  

line 74 - 79 : this part set special Uniform "maskHeight" in shader per each render

line 69 :   var maskH =  Math.sin(time*0.01)*3.+3.;  make animation per time for maskHieght

 

see this is simple :DDD 

 

ps : http://www.babylonjs-playground.com/#QHMT1#7
          

 

 

 

Link to comment
Share on other sites

NasimiAsl - thank you very much for the detailed explanation. Did you go over the old playground in that explanation or is it a modified solution according to this topic?

 

adam - thank you, looks nice and simple. Is there a way to display the fill of the box along with the cut top and bottom faces as well? At the moment only the surface is displayed.

You can see what I mean in this picture:

example.png

Here's another example, if we were to cut only the top part of a 3d triangle it'd look something like that:

example2.png

Link to comment
Share on other sites

When using a Sphere instead of a box this is what I get:

http://babylonjs-playground.com/#QO7CR#7

I might not have mentioned this very important detail - this shader will later on be applied to much more complex loaded 3d models. It should be a dynamic 3d mask for any given 3d shape / model.

In addition, the above playground only cuts the top and doesn't cut bottom - but I think once we figure out the idea it won't be a problem to add this.

By the way, such a feature would also be a nice addition to the bjs core in my opinion. Hopefully I'm not asking for too much.

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...