# graphics3d/humanskinrenderer (vanilla.js 0.4.0)

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

Human skin renderer. (builds skin / eye / hair shading templates on top of the SkinnedModelRenderer skinning/morph vertex path)

## HumanSkinRenderer extends SkinnedModelRenderer

Human skin renderer. (builds skin / eye / hair shading templates on top of the SkinnedModelRenderer skinning/morph vertex path)
- Outputs irradiance / albedo / specular / normal split into MRT in linear HDR, and compositing is done by the caller after SubsurfaceScatteringEffect.
- Registers a shading mode per material (setMaterialShading). The opaque pass (draw) draws skin / eye / hair cutout,
  the blend pass (drawBlended) draws hair fringe / occlusion / fluid (the caller sets the blend state).
- Environment state: 3 lights (0 = key light, receives shadows) / 3-color hemisphere ambient / normal, detail, roughness, specular, wrinkle, micro, eye, hair parameters.

Example: Draw the opaque and blend passes into an MRT render target

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

const width = 640;
const height = 360;
const canvas = document.createElement("canvas");
const webGL2RenderingContext = canvas.getContext("webgl2");
const humanSkinRenderer = new HumanSkinRenderer(webGL2RenderingContext);
humanSkinRenderer.setLight(0, 0.4, 0.7, 0.6, 1.0, 0.95, 0.9, 3.0);
humanSkinRenderer.setAmbientColors([0.3, 0.32, 0.38], [0.15, 0.14, 0.15], [0.05, 0.04, 0.04]);
const skinnedModel = await SkinnedModel.loadFromUrl(webGL2RenderingContext, "examples/ancientmountain/assets/fox.glb", humanSkinRenderer);
const drawableList = skinnedModel.getDrawableList();
for (const drawable of drawableList) {
	humanSkinRenderer.setMaterialShading(drawable.material, SKIN_SHADING_SKIN);
}
skinnedModel.update(0);

const geometryRenderTarget = new RenderTarget(webGL2RenderingContext, width, height, true, { colorAttachmentCount: 4, useFloatColor: true, useDepthTexture: true });
geometryRenderTarget.bind();
webGL2RenderingContext.enable(webGL2RenderingContext.DEPTH_TEST);
webGL2RenderingContext.clearColor(0, 0, 0, 0);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);
const cameraX = 0;
const cameraY = 60;
const cameraZ = 200;
const camera = new Camera3D();
camera.setPerspective(0.8, 1, 1000);
camera.setAspectRatio(width / height);
const eyePosition = Vector3.create(cameraX, cameraY, cameraZ);
const targetPosition = Vector3.create(0, 40, 0);
camera.lookAt(eyePosition, targetPosition);
const viewProjectionMatrix = camera.getViewProjectionMatrix();
const viewProjectionElements = viewProjectionMatrix.getElements();
const modelMatrix = Matrix4.createIdentity();
const modelMatrixElements = modelMatrix.getElements();
humanSkinRenderer.draw(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ);

webGL2RenderingContext.enable(webGL2RenderingContext.BLEND);
webGL2RenderingContext.blendFunc(webGL2RenderingContext.SRC_ALPHA, webGL2RenderingContext.ONE_MINUS_SRC_ALPHA);
webGL2RenderingContext.depthMask(false);
humanSkinRenderer.drawBlended(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ);
webGL2RenderingContext.depthMask(true);
webGL2RenderingContext.disable(webGL2RenderingContext.BLEND);
const irradianceTexture = geometryRenderTarget.getColorTexture(0);
const albedoTexture = geometryRenderTarget.getColorTexture(1);
console.log(irradianceTexture !== null, albedoTexture !== null);
```

- `constructor(webGL2RenderingContext)` (constructor, since 0.3.0): Constructor.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
- `draw(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ)` (method, since 0.3.0): Draws the opaque pass. (skin / eye / hair cutout, writes depth)
  - `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.
- `drawBlended(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ)` (method, since 0.3.0): Draws the blend pass. (hair fringe / occlusion / fluid, called after the caller enables blending and disables depth writes)
  - `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.
- `drawPass(skinnedModel, viewProjectionElements, modelMatrixElements, cameraX, cameraY, cameraZ, passMode)` (method, since 0.3.0): Draws a pass. (common uniforms → iterate drawables, only the shading modes matching the pass)
  - `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.
  - `passMode` number: 0 = opaque, 1 = blend.
- `drawDepth(skinnedModel, lightViewProjectionElements, modelMatrixElements)` (method, since 0.3.0): Draws the depth pass. (shadow map casting, cutout / hair materials remove transparent areas with the coverage texture, occlusion / fluid excluded)
  - `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)
- `applyEnvironmentUniforms(shaderProgram)` (method, since 0.3.0): Applies the environment uniforms. (light / ambient / skin / wrinkle / micro / eye / hair parameters)
  - `shaderProgram` ShaderProgram: Shader program to set uniforms on. (after use)
- `applyShadowUniforms(shaderProgram)` (method, since 0.3.0): Applies the shadow uniforms. (always bound regardless of strength so draws are not invalidated by a shadow sampler type mismatch)
  - `shaderProgram` ShaderProgram: Shader program to set uniforms on. (after use)
- `setShadowMap(shadowMapTexture, lightViewProjectionElements, texelSize)` (method, since 0.3.0): Sets the shadow map. (receives shadows in the main pass, also passed to the base renderer)
  - `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)
- `setShadowSoftness(shadowSoftness)` (method, since 0.3.0): Sets the shadow softness. (Poisson radius, in shadow texels)
  - `shadowSoftness` number: Poisson sample radius. (in shadow texels, default 2.5)
- `setShadowSampleCount(shadowSampleCount)` (method, since 0.3.0): Sets the shadow sample count. (1 ~ 24, more is smoother and slower)
  - `shadowSampleCount` number: Sample count. (fractions are dropped, clamped to 1 ~ 24, default 12)
- `setHairSway(amplitude, time, frequency)` (method, since 0.3.0): Sets the hair sway. (amplitude m / time seconds / frequency, none if amplitude is 0)
  - `amplitude` number: Sway amplitude. (meters, larger toward strand tips, none when 0, default 0)
  - `time` number: Elapsed time. (seconds, set every frame)
  - `frequency` number: Sway frequency. (multiplier on time, default 1.5)
- `setHairFringe(useHairFringe)` (method, since 0.3.0): Sets whether hair fringe blending is used. (false means cutout only, performance first)
  - `useHairFringe` boolean: When true, hair is drawn once more in the blend pass. (default true)
- `setMaterialShading(material, shadingMode)` (method, since 0.3.0): Registers a material shading mode. (unregistered materials are skin)
  - `material` Material: Material to set the shading mode for.
  - `shadingMode` number: Shading mode. (SKIN_SHADING_SKIN, EYE, HAIR, OCCLUSION, FLUID, FACIALHAIR)
- `getMaterialShadingMode(material)` (method, since 0.3.0): Returns the material shading mode.
  - `material` Material: Material.
  - returns number: Registered shading mode. (SKIN_SHADING_SKIN when not registered)
- `setLight(lightIndex, directionX, directionY, directionZ, red, green, blue, intensity)` (method, since 0.3.0): Sets a light. (0 = key light: receives the shadow map / 1 = fill / 2 = rim, direction is the vector from the surface toward the light)
  - `lightIndex` number: Light index. (0 key, 1 fill, 2 rim)
  - `directionX` number: X of the direction from the surface toward the light. (stored normalized)
  - `directionY` number: Y of the direction from the surface toward the light.
  - `directionZ` number: Z of the direction from the surface toward the light.
  - `red` number: Light red. (linear)
  - `green` number: Light green. (linear)
  - `blue` number: Light blue. (linear)
  - `intensity` number: Intensity. (multiplied into the color)
- `setAmbientColors(skyColor, horizonColor, groundColor)` (method, since 0.3.0): Sets the 3-color ambient. (sky / horizon / ground, linear color)
  - `skyColor` number[]: Upper sky color. ([r, g, b], linear, default [0.34, 0.36, 0.42])
  - `horizonColor` number[]: Horizon color. ([r, g, b], linear, default [0.16, 0.15, 0.16])
  - `groundColor` number[]: Lower ground color. ([r, g, b], linear, default [0.05, 0.04, 0.045])
- `setNormalStrength(normalStrength)` (method, since 0.3.0): Sets the normal map strength. (1 = original, larger makes wrinkles deeper)
  - `normalStrength` number: Normal map xy multiplier. (default 1)
- `setDetailStrength(detailStrength)` (method, since 0.3.0): Sets the height detail strength. (0 = no detail normal)
  - `detailStrength` number: Height derivative detail multiplier. (default 6)
- `setDetailTexelSize(detailTexelSize)` (method, since 0.3.0): Sets the height detail texel size. (1 / height map resolution)
  - `detailTexelSize` number: UV size of one height map texel. (default 1 / 4096)
- `setCavityStrength(cavityStrength)` (method, since 0.3.0): Sets the cavity strength. (0 = no occlusion)
  - `cavityStrength` number: Cavity multiplier. (multiplied by the difference between height and low-pass height, default 6)
- `setRoughnessRange(minimumRoughness, maximumRoughness)` (method, since 0.3.0): Sets the roughness range. (specular map 1 → minimum, 0 → maximum)
  - `minimumRoughness` number: Roughness where the specular map is 1. (default 0.32)
  - `maximumRoughness` number: Roughness where the specular map is 0. (default 0.62)
- `setSpecularStrength(specularStrength)` (method, since 0.3.0): Sets the specular strength. (skin F0 multiplier)
  - `specularStrength` number: Skin F0 multiplier. (default 1)
- `setSheenStrength(sheenStrength)` (method, since 0.3.0): Sets the vellus hair scattering strength.
  - `sheenStrength` number: Peach fuzz scattering strength. (default 0.35)
- `setSubsurfaceAmount(subsurfaceAmount)` (method, since 0.3.0): Sets the SSS mask strength. (irradiance output alpha, 0 excludes it from screen-space scattering)
  - `subsurfaceAmount` number: Scattering mask strength. (multiplied by the material's subsurface factor to become the irradiance output alpha, default 1)
- `setMicroDetail(microTiling, microStrength)` (method, since 0.3.0): Sets the micro normal. (tiling count / strength, 0 means unused)
  - `microTiling` number: Micro texture tiling count. (multiplied into the UV, default 0)
  - `microStrength` number: Micro normal and cavity strength. (default 0, unused when 0)
- `setWrinkleMaps(useWrinkleMaps, normalStrength, colorStrength)` (method, since 0.3.0): Sets whether the wrinkle map is used. (the mask atlas / wrinkle normal / color atlas must be attached to the material)
  - `useWrinkleMaps` boolean: Uses wrinkle maps when true. (default false)
  - `normalStrength` number: Wrinkle normal multiplier. (default 1)
  - `colorStrength` number: Wrinkle color multiplier. (default 1)
- `setWrinkleChannelWeight(channelIndex, weight1, weight2, weight3)` (method, since 0.3.0): Sets a wrinkle channel weight. (channel = mask atlas tile x 4 + RGBA order, value = wrinkle map 1 / 2 / 3 contribution)
  - `channelIndex` number: Channel index. (0 ~ 63, tile x 4 + RGBA order)
  - `weight1` number: Contribution to wrinkle map 1.
  - `weight2` number: Contribution to wrinkle map 2.
  - `weight3` number: Contribution to wrinkle map 3.

  Example: Set expression channel weights and finish

  ```js
  import { HumanSkinRenderer } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const humanSkinRenderer = new HumanSkinRenderer(webGL2RenderingContext);
  humanSkinRenderer.setWrinkleMaps(true, 1.0, 0.8);
  const browRaiseWeight = 0.7;
  const squintWeight = 0.4;
  humanSkinRenderer.setWrinkleChannelWeight(0, browRaiseWeight, 0, 0);
  humanSkinRenderer.setWrinkleChannelWeight(5, 0, squintWeight, 0);
  humanSkinRenderer.finishWrinkleChannelWeights();
  ```

- `finishWrinkleChannelWeights()` (method, since 0.3.0): Finishes applying the wrinkle channel weights. (picks only the tiles with weights into a bitmask, the shader skips empty tiles)
- `setEyeParameters(irisRadius, pupilRadius, irisDepth)` (method, since 0.3.0): Sets the eye parameters. (in UV units, iris radius / pupil radius / iris plane depth)
  - `irisRadius` number: Iris radius. (UV units, default 0.133)
  - `pupilRadius` number: Pupil radius. (UV units, default 0.04)
  - `irisDepth` number: Iris plane depth. (refraction parallax strength, default 0.05)
- `setEyeClosure(eyeClosure)` (method, since 0.3.0): Sets the eyelid closure. (0 = open eye, 1 = closed eye, darkens the eyeball by the eyelid shadow)
  - `eyeClosure` number: Closure amount. (clamped to 0 ~ 1, 0 open, 1 closed)
- `setHairParameters(specularShift, specularPower, specularIntensity, rootShade)` (method, since 0.3.0): Sets the hair parameters. (primary / secondary lobe shift, exponent, strength, transmission strength, root occlusion)
  - `specularShift` number[]: [primary, secondary]
  - `specularPower` number[]: [primary, secondary]
  - `specularIntensity` number[]: [primary, secondary, transmission]
  - `rootShade` number: Root occlusion strength. (none when 0, default 0.45)
- `getCutoutDepthShaderProgram()` (method, since 0.3.0): Returns the cutout depth shader program.
  - returns ShaderProgram: Cutout depth shader program.

## Functions and constants

- `SKIN_SHADING_SKIN = 0` (field, since 0.3.0): Shading mode. (specified per material with setMaterialShading, branches inside the same shader)
  - returns number
- `SKIN_SHADING_EYE = 1` (field, since 0.3.0): Eye. (corneal refraction, iris, sclera)
  - returns number
- `SKIN_SHADING_HAIR = 2` (field, since 0.3.0): Hair. (anisotropic highlights, cutout and fringe blend, sway)
  - returns number
- `SKIN_SHADING_OCCLUSION = 3` (field, since 0.3.0): Eyelid occlusion mesh. (blend pass, darkens only the irradiance)
  - returns number
- `SKIN_SHADING_FLUID = 4` (field, since 0.3.0): Fluids such as tear lines and saliva. (blend pass, adds only specular)
  - returns number
- `SKIN_SHADING_FACIALHAIR = 5` (field, since 0.3.0): Facial hair such as eyebrows and beards. (hair shading, no sway)
  - returns number
- `SKIN_TEXTURE_UNIT_DETAILHEIGHT = 9` (field, since 0.3.0): Extra texture units. (9 to 14, avoiding the material base slots 0 / 2 to 8, shadow 1, and morph 15)
  - Skin: 9 detail height, 10 micro (normal rgb + cavity a), 11 wrinkle mask atlas (4x4), 12 wrinkle normal atlas (2x2), 13 wrinkle color atlas (2x2)
  - Eye: 12 iris normal, 13 iris color / Hair: 12 card tangent (rgb) + strand coordinate (a)
  - returns number
- `SKIN_TEXTURE_UNIT_MICRO = 10` (field, since 0.3.0): Unit for the microTexture uniform. (micro normal rgb + cavity a, tiled)
  - returns number
- `SKIN_TEXTURE_UNIT_MASKATLAS = 11` (field, since 0.3.0): Unit for the maskAtlasTexture uniform. (wrinkle mask atlas 4x4)
  - returns number
- `SKIN_TEXTURE_UNIT_LAYERNORMAL = 12` (field, since 0.3.0): Unit for the layerNormalTexture uniform. (wrinkle normal atlas 2x2 for skin, iris normal for eyes, card tangent rgb + strand coordinate a for hair)
  - returns number
- `SKIN_TEXTURE_UNIT_LAYERCOLOR = 13` (field, since 0.3.0): Unit for the layerColorTexture uniform. (wrinkle color atlas 2x2 for skin, iris color for eyes)
  - returns number
- `SKIN_TEXTURE_UNIT_AUXILIARY = 14` (field, since 0.3.0): Unit for the auxiliaryTexture uniform. (declared in the shader but never read)
  - returns number
- `WRINKLE_CHANNEL_COUNT = 64` (field, since 0.3.0): Number of wrinkle mask channels. (4x4 atlas x RGBA, each channel a vec3 of weights for wrinkle maps 1 / 2 / 3)
  - returns number
