| 1 | /** |
| 2 | * Live body+equipment compositor — port of compositor.rs compose(). |
| 3 | * Per pixel: nearest valid depth wins; ties keep lower layer index (body first). |
| 4 | * Valid = depth alpha ≠ 0 AND diffuse alpha ≠ 0. |
| 5 | */ |
| 6 | var PpdaCompositor = |
| 7 | { |
| 8 | DEPTH_EPSILON_U16: 1, |
| 9 | |
| 10 | /** |
| 11 | * @param {Array<{width,height,diffuse:Uint8ClampedArray|Uint8Array,depthGray:Uint16Array,depthAlpha:Uint16Array,normal?:Uint8Array,specular?:Uint8Array}>} layers |
| 12 | */ |
| 13 | compose: function (layers) |
| 14 | { |
| 15 | if (!layers || !layers.length) |
| 16 | { throw new Error("geen lagen om te composeren"); } |
| 17 | var width = layers[0].width; |
| 18 | var height = layers[0].height; |
| 19 | var pixels = width * height; |
| 20 | if (!pixels) |
| 21 | { throw new Error("lege afmetingen"); } |
| 22 | for (var li = 0; li < layers.length; li++) |
| 23 | { |
| 24 | var L = layers[li]; |
| 25 | if (L.width !== width || L.height !== height) |
| 26 | { throw new Error("laag " + li + " heeft andere afmetingen"); } |
| 27 | if (L.diffuse.length !== pixels * 4 || L.depthGray.length !== pixels || L.depthAlpha.length !== pixels) |
| 28 | { throw new Error("laag " + li + " inconsistente buffers"); } |
| 29 | } |
| 30 | |
| 31 | var diffuse = new Uint8ClampedArray(pixels * 4); |
| 32 | var normal = new Uint8ClampedArray(pixels * 4); |
| 33 | var specular = new Uint8ClampedArray(pixels * 4); |
| 34 | var depthGray = new Uint16Array(pixels); |
| 35 | var depthAlpha = new Uint16Array(pixels); |
| 36 | var winner = new Uint8Array(pixels); |
| 37 | for (var i = 0; i < pixels; i++) |
| 38 | { winner[i] = 255; } |
| 39 | |
| 40 | var eps = this.DEPTH_EPSILON_U16; |
| 41 | for (var i = 0; i < pixels; i++) |
| 42 | { |
| 43 | var bestDepth = null; |
| 44 | var bestLayer = null; |
| 45 | for (var layerIdx = 0; layerIdx < layers.length; layerIdx++) |
| 46 | { |
| 47 | var layer = layers[layerIdx]; |
| 48 | if (layer.depthAlpha[i] === 0 || layer.diffuse[i * 4 + 3] === 0) |
| 49 | { continue; } |
| 50 | var gray = layer.depthGray[i]; |
| 51 | if (bestDepth == null) |
| 52 | { |
| 53 | bestDepth = gray; |
| 54 | bestLayer = layerIdx; |
| 55 | } |
| 56 | else if (gray + eps < bestDepth) |
| 57 | { |
| 58 | bestDepth = gray; |
| 59 | bestLayer = layerIdx; |
| 60 | } |
| 61 | else if (gray <= bestDepth + eps) |
| 62 | { |
| 63 | /* tie: keep lower index */ |
| 64 | } |
| 65 | } |
| 66 | if (bestLayer != null) |
| 67 | { |
| 68 | var src = layers[bestLayer]; |
| 69 | var o = i * 4; |
| 70 | diffuse[o] = src.diffuse[o]; |
| 71 | diffuse[o + 1] = src.diffuse[o + 1]; |
| 72 | diffuse[o + 2] = src.diffuse[o + 2]; |
| 73 | diffuse[o + 3] = src.diffuse[o + 3]; |
| 74 | if (src.normal) |
| 75 | { |
| 76 | normal[o] = src.normal[o]; |
| 77 | normal[o + 1] = src.normal[o + 1]; |
| 78 | normal[o + 2] = src.normal[o + 2]; |
| 79 | normal[o + 3] = src.normal[o + 3]; |
| 80 | } |
| 81 | if (src.specular) |
| 82 | { |
| 83 | specular[o] = src.specular[o]; |
| 84 | specular[o + 1] = src.specular[o + 1]; |
| 85 | specular[o + 2] = src.specular[o + 2]; |
| 86 | specular[o + 3] = src.specular[o + 3]; |
| 87 | } |
| 88 | depthGray[i] = src.depthGray[i]; |
| 89 | depthAlpha[i] = src.depthAlpha[i]; |
| 90 | winner[i] = bestLayer; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return { |
| 95 | width: width, |
| 96 | height: height, |
| 97 | diffuse: diffuse, |
| 98 | normal: normal, |
| 99 | specular: specular, |
| 100 | depthGray: depthGray, |
| 101 | depthAlpha: depthAlpha, |
| 102 | winner: winner |
| 103 | }; |
| 104 | }, |
| 105 | |
| 106 | /** Simple ambient+Lambert approx for preview (Preview2d-achtig, licht). */ |
| 107 | relightDiffuse: function (composed, lightAmt) |
| 108 | { |
| 109 | lightAmt = lightAmt == null ? 1 : lightAmt; |
| 110 | var out = new Uint8ClampedArray(composed.diffuse.length); |
| 111 | for (var i = 0; i < composed.diffuse.length; i += 4) |
| 112 | { |
| 113 | var a = composed.diffuse[i + 3]; |
| 114 | if (a === 0) |
| 115 | { continue; } |
| 116 | var ndot = 0.75; |
| 117 | if (composed.normal && composed.normal[i + 3]) |
| 118 | { |
| 119 | var nx = composed.normal[i] / 255 * 2 - 1; |
| 120 | var ny = composed.normal[i + 1] / 255 * 2 - 1; |
| 121 | var nz = composed.normal[i + 2] / 255 * 2 - 1; |
| 122 | ndot = Math.max(0, nx * 0.3 + ny * 0.2 + nz * 0.9); |
| 123 | } |
| 124 | var shade = Math.min(1, 0.35 + 0.65 * ndot) * lightAmt; |
| 125 | out[i] = Math.min(255, Math.round(composed.diffuse[i] * shade)); |
| 126 | out[i + 1] = Math.min(255, Math.round(composed.diffuse[i + 1] * shade)); |
| 127 | out[i + 2] = Math.min(255, Math.round(composed.diffuse[i + 2] * shade)); |
| 128 | out[i + 3] = a; |
| 129 | } |
| 130 | return out; |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | if (typeof module !== "undefined" && module.exports) |
| 135 | { module.exports = PpdaCompositor; } |