www.xbdev.net
xbdev - software development
Thursday February 19, 2026
Home | Contact | Support | WebGPU Graphics and Compute ... | WebGPU.. Games, Tutorials, Demos, Projects, and Code.....
     
 

WebGPU..

Games, Tutorials, Demos, Projects, and Code.....

 


Texture Transforms


Textures are just a set of `2d coordinates` which we can manipulate on the shader to create an assortment of effects. The 2d coordinates can be modified using linear algebra techniques (e.g., scaling, rotating and so on). As we're using basic linear algebra - that means matrices!! Of course, don't worry, matrices are a walk in the park using the WGSL shadaer langauge and are built in to the language.



Transforming textures as we do vertices - with matrices - to accomplish a whole variety of effects - rotation, scaling, inversi...
Transforming textures as we do vertices - with matrices - to accomplish a whole variety of effects - rotation, scaling, inversion, skewing, ...



Functions Used: getContext(), requestAdapter(), getPreferredCanvasFormat(), createCommandEncoder(), beginRenderPass(), setPipeline(), draw(), end(), submit(), getCurrentTexture(), createView(), createShaderModule()


Examples of texture operations
1. Scale
2. Rotate
3. Stretch


The following vertex shader shows a simple example of an animation effect - which makes the texture rotate. The UV texture coordiantes are transformed by a mat2x2 rotation matrix.

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

@
fragment 
fn psmain(@location(0uvvec2<f32>) -> @location(0vec4<f32
{
    
let angle myTimer 0.1;
    
let c cos(angle);
    
let s sin(angle);
    
    
let texTransform mat2x2<f32>( c,  -s,
                                    
s);
                                    
    
let newuv texTransform uv;
    
    return 
textureSample(myTexturemySamplernewuv );
    
    
//return vec4<f32>(1.0, 0.0, 0.5, 1.0);
}


The important thing to note, is that the texture coordinates are from the top left corner - so it rotates around the top left corner. To make it rotate around the center - we need to add in other transforms - first translate, the rotate then translate back.

We can combine multiple transform into a single matrix - apply it to the uv coordinates.

This lets us create some cool effects - which can even be combined with timing information (e.g., scroll or spin). Textures don't usually sit on a flat quad - they are used in all sorts of ways - so being able to trasform a texture brings lots of advantages.

Some examples:
• Height maps (scale/invert/tile textures to create larger scenes from simple texture samples)
• Animated surfaces (e.g., scrolling water surface)
• Animated sky (clouds and stars scroll/move around in a more organic fashion by using multiple transforms)


Transforms don't have to be linear and don't have to be the same across the entire texture - they can be mixed and manipulated in lots of ways - they can also be combined with logical conditions (e.g., uv.x is less than half then use this transform, otherwise use this other one).


Resources and Links


• WebGPU Lab Example [LINK]














WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Compute 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 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 wgsl webgpugems shading language cookbook 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.