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

 


Camera 3rd Person (Object)


You'll find that there are lots of differnet types of cameras and control configurations. We create a 3rd person camera here.


Single triangle provides a point of references for our camera - as we move around we see the triangle moving.
Single triangle provides a point of references for our camera - as we move around we see the triangle moving.


Functions Used: setVertexBuffer(), setIndexBuffer(), drawIndexed(), createBuffer(), getMappedRange(), getContext(), requestAdapter(), getPreferredCanvasFormat(), createCommandEncoder(), beginRenderPass(), setPipeline(), draw(), end(), submit(), getCurrentTexture(), createView(), createShaderModule()

The camera in the example has a single small triangle as the target - so as you move around with the cursor keys - you'll see that the triangle always stays in front the camera.


Create a static object to hold the camera - so the functionality can be contained in one place.

// A 'static' property in JavaScript is best represented by a property directly on the constructor function itself

function camera() {}

camera.zero     = {x:0y:0z:0};
camera.up       = {x:0y:1z:0};
  
camera.position camera.zero;
camera.view     = {x:0y:0z:-1};
  

  
// This function sets the camera's position and view and up vectors
camera.PositionCamera = function ( positionXpositionYpositionZ// target
                                     
viewX,     viewY,     viewZ,     // position of the camera
                                   
upX,       upY,       upZ)
  {
    
camera.position = {x:positionXy:positionYz:positionZ};
    
camera.view        = {x:viewX,     y:viewY,     z:viewZ};
    
camera.up       = {x:upX,       y:upY,       z:upZ};
  }

  
// This rotates the view around the position using an axis-angle rotation
camera.RotateView = function(angle,  x,  y,  z)
  {
    
// Get the view vector (The direction we are facing)
    
let tmpview = {x:camera.view.x-camera.position.x,
                   
y:camera.view.y-camera.position.y,
                   
z:camera.view.z-camera.position.z};        

    
// Calculate the sine and cosine of the angle once
    
let cosTheta Math.cos(angle);
    
let sinTheta Math.sin(angle);

    
// Find the new x position for the new rotated point
    
camera.view.x  = (cosTheta + (cosTheta) * x)        * tmpview.x;
    
camera.view.+= ((cosTheta) * sinTheta)    * tmpview.y;
    
camera.view.+= ((cosTheta) * sinTheta)    * tmpview.z;

    
// Find the new y position for the new rotated point
    
camera.view.y  = ((cosTheta) * sinTheta)    * tmpview.x;
    
camera.view.+= (cosTheta + (cosTheta) * y)        * tmpview.y;
    
camera.view.+= ((cosTheta) * sinTheta)    * tmpview.z;

    
// Find the new z position for the new rotated point
    
camera.view.z  = ((cosTheta) * sinTheta)    * tmpview.x;
    
camera.view.+= ((cosTheta) * sinTheta)    * tmpview.y;
    
camera.view.+= (cosTheta + (cosTheta) * z)        * tmpview.z;

    
// Add the newly rotated vector to our position to set our new rotated view of our camera.
    
camera.view = {x:camera.view.x+camera.position.x,
                   
y:camera.view.y+camera.position.y,
                   
z:camera.view.z+camera.position.z};
  }


  
// This rotates the position around a given point
camera.RotateAroundPoint = function(centeranglexyz)
  {
    
// To rotate our position around a point, what we need to do is find
    // a vector from our position to the center point we will be rotating around.
    // Once we get this vector, then we rotate it along the specified axis with
    // the specified degree.  Finally the new vector is added center point that we
    // rotated around (center) to become our new position.  

    // Get the vVector from our position to the center we are rotating around
    
let tmppos = {x:camera.position.x-center.x,
                  
y:camera.position.y-center.y,
                  
z:camera.position.z-center.z}; 

    
// Calculate the sine and cosine of the angle once
    
let cosTheta Math.cos(angle);
    
let sinTheta Math.sin(angle);

    
// Find the new x position for the new rotated point
    
camera.position.x  = (cosTheta + (cosTheta) * x)        * tmppos.x;
    
camera.position.+= ((cosTheta) * sinTheta)    * tmppos.y;
    
camera.position.+= ((cosTheta) * sinTheta)    * tmppos.z;

    
// Find the new y position for the new rotated point
    
camera.position.y  = ((cosTheta) * sinTheta)    * tmppos.x;
    
camera.position.+= (cosTheta + (cosTheta) * y)        * tmppos.y;
    
camera.position.+= ((cosTheta) * sinTheta)    * tmppos.z;

    
// Find the new z position for the new rotated point
    
camera.position.z  = ((cosTheta) * sinTheta)    * tmppos.x;
    
camera.position.+= ((cosTheta) * sinTheta)    * tmppos.y;
    
camera.position.+= (cosTheta + (cosTheta) * z)        * tmppos.z;

    
// Now we just add the newly rotated vector to our position to set
    // our new rotated position of our camera.
    
camera.position = {x:camera.position.x+center.x,
                       
y:camera.position.y+center.y,
                       
z:camera.position.z+center.z};
  }

  
// This will move the camera forward or backward depending on the speed
camera.MoveCamera = function(speed)
  {
    
// Get our view vector (The direciton we are facing)
    
let dir = {x:camera.view.x-camera.position.x,
               
y:camera.view.y-camera.position.y,
               
z:camera.view.z-camera.position.z};
    
    
camera.position.+= dir.speed;        // Add our acceleration to our position's X
    
camera.position.+= dir.speed;        // Add our acceleration to our position's Z
    
camera.view.+= dir.speed;            // Add our acceleration to our view's X
    
camera.view.+= dir.speed;            // Add our acceleration to our view's Z
  
}

camera.GetViewMatrix = function()
  {
    
// Give webgpu our camera position, then camera view, then camera up vector
      
let viewMatrix mat4.create();
  
    
mat4.lookAt(viewMatrix
               
Object.values(camera.position), 
               
Object.values(camera.view),  
               
Object.values(camera.up));
  
      return 
viewMatrix;
  }


// Store camera object so it can be accessed anywhere by any other objects
window.camera camera;


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.