Ambiera Forum

Discussions, Help and Support.

Create a new account, Search, or Login:
Name: Password:
Ambiera Forum > CopperLicht
Wow I got plain Vertex Alpha working
Author Text

csp-games
Guest
Quote
2022-01-09 19:49:13

As you may know, CL does offer only the EMT_SOLID_VERTEX_ALPHA_TWO_TEXTURE_BLEND blendmode when it comes to vertex alpha, at least it seems so.

After some fiddling I have found a way to have Transparent Vertex alpha from one solid texture to full transparency of the mesh, depending the vertex alpha.
🔎︎


With this I can implement some really nice effects, like the sunbeams effect I made for directX:
[img]https://i.imgur.com/xvy8e0l.jpg[/img]

But I'd actually need to render to a small texture, if anybody knows how to render the scene to a texture, pls let me know, thx. I tried, but at some point was getting a serious slap by WebGL itself, complaining it will not allow a rendering feedback loop... that's when you have the rendertarget texture on a mesh that is also rendered.

Anyway, here's the experimental code for plain vertex alpha (in add blendmode only so far, but that's what I need for the rays effect anyway):

createVertex = function(x, y, z, s, t,s2,t2)
{
var vtx = new CL3D.Vertex3D(true);
vtx.Pos.X = x;
vtx.Pos.Y = y;
vtx.Pos.Z = z;
vtx.TCoords.X = s;
vtx.TCoords.Y = t;
vtx.TCoords2.X = s2;
vtx.TCoords2.Y = t2;
// so here I'm setting vertex alpha depending on the Y position of the vertex. RGB has to be somehow halfbright (0x7f7f7f=127,127,127)
var al=((y*20)&255)<<24;//(Math.random()*100)<<24;
vtx.Color=0x7f7f7f | al;// argb
return vtx;
}
// then in the mat assignement I do just this:
buf[i].Mat.Tex1 = engine.getTextureManager().getTexture(txx1[i], true);
buf[i].Mat.Type = CL3D.Material.EMT_TRANSPARENT_ALPHA_CHANNEL;
// Note this Texture has no alpha channel at all, it's a jpg



erik
Registered User
Quote
2022-01-10 08:00:41

Very nice!


csp-games
Guest
Quote
2022-01-11 14:00:31

Thanks!

It seems now: the vertex color, that contains alpha, is a 32 bit int, argb. But alpha is not 0 to 255, but 0 to 63 only (and opaque above, yet still unsorted). Furthermore, to make this work, the RGB component must be 0x7f7f7f, which is 50% grey.

Here's an example on how to alter the vertex alpha dynamically at runtime. Note the mesh of this node is then z-unsorted.
It emulates the Blitz3D command EntityAlpha() that sets the transparency of a mesh, regardless of textures etc. The example smoothly fades in and out the house mesh node "glob_mesh".


// note: Alpha goes from 0 to 1.0, but internally it uses 0 to 63 (not 0 to 255), and RGB must be set to 0x3f3f3f
// also, the materials must use a texture without alpha channel (24 bit), using yourmeshbuf.Mat.Type = CL3D.Material.EMT_TRANSPARENT_ALPHA_CHANNEL;
// note: these meshes have then no Z-sorting, so usage may be limited to certain things like distant LOD objects.
function EntityAlpha(name,al){
// somehow Nikos prototype function to create the mesh made me troubles in assigning a name to it, so I just stored the
// node handle as a global named glob_mesh. I guess you can fix this.
// var node= ccbGetSceneNodeFromName(name);
node=glob_mesh;
if(!node)alert("sum ting wong");
buf=node.GetMeshBuffers();
al*=63;
if(al<0)al=0;
if(al>255)al=255;
al=al<<24; // bit-shifting alpha to the top byte.
for(i=0; i<buf.length;i++){
for(j=0; j<buf[i].Vertices.length;j++){
buf[i].Vertices[j].Color=(al | 0x3f3f3f);
}
buf[i].update(false,false);
}
}

function onFrameDrawing()
{
EntityAlpha("House",Math.abs(Math.sin(al_glow)));
al_glow+=.01;
if(al_glow>6.282){al_glow=0;}
}
ccbRegisterOnFrameEvent(onFrameDrawing);




csp-games
Guest
Quote
2022-01-11 14:06:09

oops!!!


RGB component must be 0x7f7f7f [b]that's wrong !/b]

correct is:

RGB component must be 0x3f3f3f


csp-games
Guest
Quote
2022-01-11 14:25:33

Here's a slight variation, when using an alpha value of 1.0 then it switches to SOLID mode, with z-sorting intact. This resembles the mentioned EntityAlpha command more precisely.


// note: Alpha goes from 0 to 1.0, but internally it uses 0 to 63 (not 0 to 255), and RGB must be set to 0x3f3f3f
// also, the materials must use a texture without alpha channel (24 bit), using yourmeshbuf.Mat.Type = CL3D.Material.EMT_TRANSPARENT_ALPHA_CHANNEL;
// note: these meshes have then no Z-sorting, so usage may be limited to certain things like distant LOD objects.
function EntityAlpha(name,al){
// somehow Nikos prototype function to create the mesh made me troubles in assigning a name to it, so I just stored the
// node handle as a global named glob_mesh. I guess you can fix this.
// var node= ccbGetSceneNodeFromName(name);
node=glob_mesh;
if(!node)alert("sum ting wong");
buf=node.GetMeshBuffers();
al*=63;
//al=63; //to test alpha=1.0, switches to EMT_SOLID
if(al<0)al=0;
if(al>255)al=255;
al=al<<24; // bit-shifting alpha to the top byte.
for(i=0; i<buf.length;i++){
if(al>0x3e000000){
buf[i].Mat.Type = CL3D.Material.EMT_SOLID;
}
else{
buf[i].Mat.Type = CL3D.Material.EMT_TRANSPARENT_ALPHA_CHANNEL;
}
for(j=0; j<buf[i].Vertices.length;j++){
buf[i].Vertices[j].Color=(al | 0x3f3f3f);
}
buf[i].update(false,false);
}
}




csp-games
Guest
Quote
2022-01-11 20:09:57

Additional note:

apparently, when a mesh is using no realtime dynamic light, the alpha range is as described above from 0 to 63, but when it's dynamically lit, then the range goes from 0 to 255.

So you have to tweak it so it does take that difference into consideration. I use an optional parameter is_dyn_lit


function EntityAlpha(node2,al,is_dyn_lit=0){
var node=node2.getMesh();
if(!node)alert("sum ting wong");
//var mes=node.GetMesh();
buf=node.GetMeshBuffers();
if(is_dyn_lit==1) {al*=4;}
al*=63;
if(al<0)al=0;
if(al>255)al=255;
al=al<<24; // bit-shifting alpha to the top byte.
var al_max=0x3e000000;
if(is_dyn_lit==1) {al_max=0xfe000000;}
for(i=0; i<buf.length;i++){
if(al>al_max){
if(is_dyn_lit==0) {buf[i].Mat.Type = CL3D.Material.EMT_SOLID;}
else {buf[i].Mat.Type = CL3D.Material.EMT_TRANSPARENT_ALPHA_CHANNEL_REF;} // this is used for dyn. lit plants and such
}
else{
buf[i].Mat.Type = CL3D.Material.EMT_TRANSPARENT_ALPHA_CHANNEL;
}
for(j=0; j<buf[i].Vertices.length;j++){
buf[i].Vertices[j].Color=(al | 0x3f3f3f);//0x3f3f3f);
}
buf[i].update(false,false);
}
}



csp-games
Guest
Quote
2022-01-11 21:39:57

Latest news:

I got this EntityAlpha thing working as much as neccessary for a LOD system that will fade seamlessly between low poly and high poly verions of trees. The high poly one here on the right has about 1000 tris, the low poly one on the right has - sit down - only 4 triangles, yet it appears very 3D, because I used a special method:

I render the tree from theoretically 8 angles, with the camera range limited so it will only render those branches that face the camera mostly, and clip any branches behind and in front. Theoretically, I say because back and fron is treated the same, so I need only 4 renders, in 45 degree steps. These 4 renders I put on 4 triangles that are crossing in the middle, bulding an octagon. It's not perfect, but with smooth alpha fading it will do.

Only grain of salt herein: the low poly one can't be used with dynamic light, ot just looks crappy. However, on a distance the difference is less obvios - again, when faded smoothly between the versions.

These two trees here have alpha 0.5. (internally the dark one has 31 and the other one has 127, as explained earlier)

🔎︎



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "?nternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© 2020 Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact