# graphics3d/skinnedmodelrenderer (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/graphics3d/skinnedmodelrenderer/

Skinned model renderer. (owns the skinning PBR shader template + environment state, one per scene)

## SkinnedModelRenderer extends Object

Skinned model renderer. (owns the skinning PBR shader template + environment state, one per scene)
- SkinnedModel holds only data/pose, and this object takes a model instance and does the rendering.
- Environment state (shadow map / sun / fog) is set on the renderer, not the model. Without a shadow map, a 1x1 comparison depth texture is bound to the shadow unit.
- Injecting fragment shader source lets you build a different shading template on top of the same skinning/morph vertex path.
  (a derived renderer sets its own uniforms after use() and calls draw())

Example: Draw one GLB model under sun and fog

```js
import { SkinnedModel, SkinnedModelRenderer, Camera3D, Matrix4, Vector3 } from "./build/vanilla.js";

const width = 640;
const height = 360;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
document.body.appendChild(canvas);
const webGL2RenderingContext = canvas.getContext("webgl2");
const skinnedModelRenderer = new SkinnedModelRenderer(webGL2RenderingContext);
skinnedModelRenderer.setSunDirection(0.45, 0.76, 0.47);
skinnedModelRenderer.setFogColor(0.16, 0.2, 0.26);
skinnedModelRenderer.setFogRange(300, 800);
const skinnedModel = await SkinnedModel.loadFromUrl(webGL2RenderingContext, "examples/ancientmountain/assets/fox.glb", skinnedModelRenderer);
skinnedModel.update(0);

const cameraX = 0;
const cameraY = 80;
const cameraZ = 220;
const camera = new Camera3D();
camera.setPerspective(Math.PI / 3, 1, 2000);
camera.setAspectRatio(width / height);
const eyePosition = Vector3.create(cameraX, cameraY, cameraZ);
const targetPosition = Vector3.create(0, 45, 0);
camera.lookAt(eyePosition, targetPosition);
const viewProjectionMatrix = camera.getViewProjectionMatrix();
const viewProjectionElements = viewProjectionMatrix.getElements();
const modelMatrix = Matrix4.createIdentity();
const modelMatrixElements = modelMatrix.getElements();

webGL2RenderingContext.viewport(0, 0, width, height);
webGL2RenderingContext.enable(webGL2RenderingContext.DEPTH_TEST);
webGL2RenderingContext.clearColor(0.16, 0.2, 0.26, 1);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);
skinnedModelRenderer.draw(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ);
```

- `constructor(webGL2RenderingContext, fragmentShaderSource = null, vertexShaderSource = null)` (constructor, since 0.3.0): Constructor.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
  - `fragmentShaderSource` string | null: Replacement fragment shader source. (default PBR if null)
  - `vertexShaderSource` string | null: Replacement vertex shader source. (default skinning if null, must follow the same attribute / output conventions)

  Example: Build a different fragment shader on the same skinning path

  ```js
  import { SkinnedModel, SkinnedModelRenderer } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const unlitShaderSource = `#version 300 es
  precision highp float;
  in vec3 worldNormal;
  in vec2 fragmentTextureCoordinate;
  uniform sampler2D baseColorTexture;
  uniform vec3 baseColorFactor;
  out vec4 outputColor;
  void main() {
  	vec3 albedo = texture(baseColorTexture, fragmentTextureCoordinate).rgb * baseColorFactor;
  	float light = max(dot(normalize(worldNormal), vec3(0.0, 1.0, 0.0)), 0.0) * 0.6 + 0.4;
  	outputColor = vec4(albedo * light, 1.0);
  }
  `;
  const unlitRenderer = new SkinnedModelRenderer(webGL2RenderingContext, unlitShaderSource);
  const skinnedModel = await SkinnedModel.loadFromUrl(webGL2RenderingContext, "examples/ancientmountain/assets/fox.glb", unlitRenderer);
  const drawableList = skinnedModel.getDrawableList();
  console.log(drawableList.length);
  ```

- `uploadJointMatrices(jointMatrixArray)` (method, since 0.4.0): Uploads the joint matrices. (the JointBlock uniform block, ShaderProgram binds each program to the same binding point after linking)
  - `jointMatrixArray` Float32Array: Joint matrix values. (a multiple of 16)
- `createEmptyShadowTexture()` (method, since 0.4.0): Creates the empty shadow texture. (1x1 depth 1, comparison sampling)
  - If a texture without comparison mode is bound to a sampler2DShadow unit, WebGL refuses to draw, and if there is no depth texture, the Mac driver reports it.
  - returns WebGLTexture: 1x1 comparison depth texture.
- `draw(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ)` (method, since 0.3.0): Draw. (iterates the model instance's drawables, applies materials + uploads joint matrices)
  - `skinnedModel` SkinnedModel: Model to draw. (after its pose is computed with update)
  - `viewProjectionElements` Float32Array: Elements of the combined view and projection matrix. (column-major 4x4)
  - `modelMatrixElements` Float32Array: Elements of the model world matrix. (column-major 4x4)
  - `cameraX` number: Camera world X.
  - `cameraY` number: Camera world Y.
  - `cameraZ` number: Camera world Z.
- `drawDepth(skinnedModel, lightViewProjectionElements, modelMatrixElements)` (method, since 0.3.0): Draws the depth pass. (shadow map casting, call after ShadowMap.beginRender())
  - `skinnedModel` SkinnedModel: Model that casts the shadow.
  - `lightViewProjectionElements` Float32Array: Elements of the light view-projection matrix. (column-major 4x4)
  - `modelMatrixElements` Float32Array: Elements of the model world matrix. (column-major 4x4)
- `drawWireframe(skinnedModel, viewProjectionElements, modelMatrixElements, red, green, blue, alpha)` (method, since 0.3.0): Draws the wireframe. (edge lines, reflects morph / skinning, the caller sets blend / depth state)
  - Temporarily swaps the drawable vertex array's element buffer to line indices, draws, and restores the original indices.
  - `skinnedModel` SkinnedModel: Model to draw as lines. (line indices are uploaded on the first draw)
  - `viewProjectionElements` Float32Array: Elements of the combined view and projection matrix. (column-major 4x4)
  - `modelMatrixElements` Float32Array: Elements of the model world matrix. (column-major 4x4)
  - `red` number: Line red. (0 ~ 1)
  - `green` number: Line green. (0 ~ 1)
  - `blue` number: Line blue. (0 ~ 1)
  - `alpha` number: Line alpha. (0 ~ 1, the caller enables blending)

  Example: Overlay translucent lines on the main pass

  ```js
  import { SkinnedModel, SkinnedModelRenderer, Camera3D, Matrix4, Vector3 } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const skinnedModelRenderer = new SkinnedModelRenderer(webGL2RenderingContext);
  const skinnedModel = await SkinnedModel.loadFromUrl(webGL2RenderingContext, "examples/ancientmountain/assets/fox.glb", skinnedModelRenderer);
  skinnedModel.update(0);
  const camera = new Camera3D();
  const eyePosition = Vector3.create(0, 80, 220);
  const targetPosition = Vector3.create(0, 45, 0);
  camera.setPerspective(1.0, 1, 2000);
  camera.lookAt(eyePosition, targetPosition);
  const viewProjectionMatrix = camera.getViewProjectionMatrix();
  const viewProjectionElements = viewProjectionMatrix.getElements();
  const modelMatrix = Matrix4.createIdentity();
  const modelMatrixElements = modelMatrix.getElements();
  webGL2RenderingContext.enable(webGL2RenderingContext.DEPTH_TEST);
  skinnedModelRenderer.draw(skinnedModel, viewProjectionElements, modelMatrixElements, 0, 80, 220);
  webGL2RenderingContext.enable(webGL2RenderingContext.BLEND);
  webGL2RenderingContext.blendFunc(webGL2RenderingContext.SRC_ALPHA, webGL2RenderingContext.ONE_MINUS_SRC_ALPHA);
  webGL2RenderingContext.depthFunc(webGL2RenderingContext.LEQUAL);
  skinnedModelRenderer.drawWireframe(skinnedModel, viewProjectionElements, modelMatrixElements, 0.2, 1.0, 0.6, 0.5);
  webGL2RenderingContext.depthFunc(webGL2RenderingContext.LESS);
  webGL2RenderingContext.disable(webGL2RenderingContext.BLEND);
  ```

- `applyMorphUniforms(shaderProgram, drawable)` (method, since 0.3.0): Applies morph uniforms. (drawable's morph texture/weights, disabled with count 0 if there are no targets)
  - `shaderProgram` ShaderProgram: Shader program to set uniforms on. (after use)
  - `drawable` object: A drawable of the model.
- `setShadowMap(shadowMapTexture, lightViewProjectionElements, texelSize)` (method, since 0.3.0): Sets the shadow map. (receives shadows in the main pass)
  - `shadowMapTexture` WebGLTexture | null: Shadow map depth texture. (comparison sampling, no shadows are received when null)
  - `lightViewProjectionElements` Float32Array | null: Elements of the light view-projection matrix. (no shadows are received when null)
  - `texelSize` number: Size of one shadow map texel in texture coordinates. (1 / resolution, the sample spacing)
- `setShadowStrength(shadowStrength)` (method, since 0.3.0): Sets the shadow strength. (0 = no receiving)
  - `shadowStrength` number: Shadow strength. (0 ~ 1, default 0)
- `setSunDirection(x, y, z)` (method, since 0.3.0): Sets the sun direction.
  - `x` number: X of the direction from the surface toward the sun. (normalized by the shader, default (0, 1, 0))
  - `y` number: Y of the direction from the surface toward the sun.
  - `z` number: Z of the direction from the surface toward the sun.
- `setFogColor(red, green, blue)` (method, since 0.3.0): Sets the fog color.
  - `red` number: Fog red. (0 ~ 1, default 0.8)
  - `green` number: Fog green. (0 ~ 1, default 0.85)
  - `blue` number: Fog blue. (0 ~ 1, default 0.92)
- `setFogRange(startDistance, endDistance)` (method, since 0.3.0): Sets the fog distance range.
  - `startDistance` number: Camera distance where fog begins. (world units, default 55)
  - `endDistance` number: Camera distance where fog fully covers. (world units, default 250)
- `getShaderProgram()` (method, since 0.3.0): Returns the shader program. (skinning PBR template)
  - returns ShaderProgram: Main pass shading program.
- `getDepthShaderProgram()` (method, since 0.3.0): Returns the depth shader program.
  - returns ShaderProgram: Depth-only skinning program.
- `getWireframeShaderProgram()` (method, since 0.3.0): Returns the wireframe shader program.
  - returns ShaderProgram: Wireframe skinning program.
- `getWebGL2RenderingContext()` (method, since 0.3.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getEmptyShadowTexture()` (method, since 0.4.0): Returns the empty shadow texture. (1x1 comparison depth texture bound to the shadow unit when not receiving shadows, shared by derived renderers)
  - returns WebGLTexture: 1x1 comparison depth texture.

## Functions and constants

- `MORPH_GLSL` (field, since 0.3.0): Morph target GLSL. (inserted into the vertex shader, weighted sum of position/normal deltas from a float texture)
  - texel index = gl_VertexID, target block = [position rows][normal rows] order.
