www.xbdev.net
xbdev - software development
Saturday June 7, 2025
Home | Contact | Support | WebGPU Graphics and Compute ...
     
 

WebGPU/WGSL Tutorials and Articles

Graphics and Compute ...

 



dual-quaternions racers connected numbers illustation no like race track of runnings usually connected
Transparency (aka opacity or alpha) is such an important aspect of computer graphics - yet, it's ofen overlooked or an after thought!




WebGPU > WebGPU Transparency, Opacity, Alpha, ...


You start on the road to using the WebGPU API, putting triangles on screen, and then onto textures and all sorts of geometry... then you start tinkering with the fragment shader `alpha` only to discover, that it has no affect! Nothing happens, the triangles and shapes do not become ghost like! Why?

Well, to get transparency working, you'll need to do a few extra things!

The first is in the setup configuration:

context.configure({ device device
                   
alphaMode"premultiplied"// old name compositingAlphaMode: "opaque", 
                   
format presentationFormat });


The default setting for
alphaMode
is
opaque
which causes the renderer to ignore alpha values.


Pipeline


The pipeline is the other place, you can configure the `blend` settings - how the colors and alpha values will be mixed together:

const pipeline device.createRenderPipeline({
  
layoutdevice.createPipelineLayout({bindGroupLayouts: [sceneUniformBindGroupLayout]}),
  
vertex: {
      
moduledevice.createShaderModule({ codebasicVertWGSL }),
      
entryPoint"main",
  },
  
fragment: { moduledevice.createShaderModule({ codebasicPixelWGSL }),
              
entryPoint"main"
              
targets: [{ formatpresentationFormat,
                          
blend: { color: {srcFactor:'src-alpha'dstFactor:"one-minus-src-alpha" ,operation:"add"},
                                   
alpha: {srcFactor:'one',     dstFactor:"one" ,               operation:"add"}  }
                         
// blend: { color: { srcFactor: 'one',  dstFactor: 'one-minus-src-alpha', operation:"add" },
                         //          alpha: { srcFactor: 'one',  dstFactor: 'one-minus-src-alpha', operation:"add" }  }
                         
}]
  },
  
primitive:    { topology"triangle-strip",  cullMode'none' },
  
depthStencil: { format:   "depth24plus",    depthWriteEnabledfalse,  depthCompare"less" }
});


The important nugget is the extra setting for
blend
- to control the color mixing.


Sneaky
discard
WGSL shader command


As with GLSL the
discard
command is supported - so you can prevent the pixel from begin written to the output screen. So for example, if the
alpha
value is less than some threshold (e.g., 0.01), then you might want to have it not passed to the output.

 @group(0) @binding(1) var mySamplersampler;
 @
group(0) @binding(2) var myTexturetexture_2d<f32>;

@
fragment
fn main(@location(0fragUVvec2<f32>,
        @
location(1alpha:  f32        ) -> @location(0vec4<f32> {

  
// basic textured 
  
var texCol textureSample(myTexturemySamplerfragUV);
  
  if ( 
texCol.0.01 ) { discard; }
  
  
texCol.texCol.alpha;
  return 
texCol;
}



Links and References


• Transparency Demo (Minimal Working Example) - (LINK)
• Billboard Smoke Demo (Alpha) - (LINK)
setBlendConstant(..)
- (LINK)
blend
- (LINK)

















101 WebGPU Programming Projects. WebGPU Development Pixels - coding fragment shaders from post processing to ray tracing! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu wgsl compute graphics all in one kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright Ray-Tracing with WebGPU kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.