www.xbdev.net
xbdev - software development
Wednesday January 15, 2025
Home | Contact | Support | WebGPU Graphics and Compute ...
     
 

WebGPU/WGSL Tutorials and Articles

Graphics and Compute ...

 


Morph Targets


Morph targets give you greater control over the vertex positions - for example, if you wanted to animate a face or a ball deforming - you'd find it very difficult to do using skinning methods.

In fact mesh-animation techniques like morphing are some of the earliest approaches - becase they're usually much easier to understand and implement - for instance the file formats used in quake 2 for the animated charaters (.md2 format) - stores the mesh as multiple copies in the file. The animation was created by interpolating between the copies of the mesh!

The md2 file format was fine for simple fixed animaatons - but if you wanted to mix a walk with a jump animaion - you couldn't - as it stored the whole mesh (an identical copy).

Another way of doing animations using morph targets - is to store the difference between the original mesh and the desired mesh. One such file format that does this is the glTF format (popular open source 3d file formats).

The glTF file stores what are known as morph targets. These morph targets have the same number of vertices as the original mesh - however, they only store the
differences
from the original mesh to the desired offset.

The benefit of this is you can have multiple morph targets which can be added to the original mesh vertices to create the final deformed mesh.

The reason the glTF file format stores the differences is so multiple morph targets can be combined together to create the final solution. For example imagine you have morph targets for the eyes, nose and mouth - they can all have there own morph targets and be combined with the original mesh to create the final animation.


Example of a horse which is animated using morphing.
Example of a horse which is animated using morphing.


When you implement morph targets on the GPU - you want don't want to go looping over all the vertices on the CPU - it can be really slow if you've got hundreds of thousands of vertices and you're doing it 60 times a second.

Instead you want to copy them over to the GPU in a buffer - then you can index into them as you need. As you might have a few different morph targets - you don't want to create dozens of buffers - instead you can create a single one and keep track of the offsets.

The morph targets has the same number of vertices as the original mesh (so if the original mesh has 1000 vertices, then each morph target also has 1000 vertices). If there are 10 morph targets, you have 10,000 morph vertices.

Remember - the morph targets need to be mixed with the original mesh and cannot be used on their own.

The following is an example of a wgsl vertex shader - which combines the morph target with the original mesh to create the final output.

let numMorphVertices:u32    morphuniforms.numVertices;
var 
morphPosition:vec3<f32> = input.position.xyz;

// Apply morph targets
for (var i:u32 0numMorphTargets1
{
    
// builtin 'vertexIndex' index lets us know the index of the vertex in the original mesh
    // we can use this to offset into the morph target data
    
let offset i*numMorphVertices vertexIndex
    
    
// the weights let us know how much of each morph target should be mixed with the original
    // remember multiple morph targets can be combined with the oringla mesh for complex morph animations
    
morphPosition += morphuniforms.weights[i].morphtargets[offset].xyz;
}

output.position mvp vec4(morphPosition1.0);
output.vposition = ( transforms.model vec4(input.position.xyz1.0) ).xyz;
output.vnormal    = (transforms.model vec4<f32>(input.normal0.0)).xyz;




Resources & Links

Morph Animation Horse (WebGPULab)

Examples and information about the glTF file format



























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 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 webgpugems shading language cookbook 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-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.