|
| 1 | +// deck.gl |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +// Copyright (c) vis.gl contributors |
| 4 | + |
| 5 | +export default /* wgsl */ `\ |
| 6 | +struct VertexInputs { |
| 7 | + @location(0) positions: vec3<f32>, |
| 8 | +#ifdef HAS_NORMALS |
| 9 | + @location(1) normals: vec3<f32>, |
| 10 | +#endif |
| 11 | + @location(2) colors: vec4<f32>, |
| 12 | +#ifdef HAS_UV |
| 13 | + @location(3) texCoords: vec2<f32>, |
| 14 | +#endif |
| 15 | +#ifdef HAS_UV_REGIONS |
| 16 | + @location(4) uvRegions: vec4<f32>, |
| 17 | +#endif |
| 18 | +#ifdef HAS_FEATURE_IDS |
| 19 | + @location(5) rowIndexes: u32, |
| 20 | +#endif |
| 21 | + @location(6) instanceColors: vec4<f32>, |
| 22 | + @location(7) instanceModelMatrixCol0: vec3<f32>, |
| 23 | + @location(8) instanceModelMatrixCol1: vec3<f32>, |
| 24 | + @location(9) instanceModelMatrixCol2: vec3<f32>, |
| 25 | +}; |
| 26 | +
|
| 27 | +struct FragmentInputs { |
| 28 | + @builtin(position) position: vec4<f32>, |
| 29 | + @location(0) color: vec4<f32>, |
| 30 | + @location(1) texCoord: vec2<f32>, |
| 31 | + @location(2) pbrPosition: vec3<f32>, |
| 32 | + @location(3) pbrNormal: vec3<f32>, |
| 33 | + @location(4) pickingColor: vec3<f32>, |
| 34 | +}; |
| 35 | +
|
| 36 | +fn applyUVRegion(uv: vec2<f32>, uvRegion: vec4<f32>) -> vec2<f32> { |
| 37 | +#ifdef HAS_UV_REGIONS |
| 38 | + // https://github.com/Esri/i3s-spec/blob/master/docs/1.7/geometryUVRegion.cmn.md |
| 39 | + return fract(uv) * (uvRegion.zw - uvRegion.xy) + uvRegion.xy; |
| 40 | +#else |
| 41 | + return uv; |
| 42 | +#endif |
| 43 | +} |
| 44 | +
|
| 45 | +@vertex |
| 46 | +fn vertexMain( |
| 47 | + inputs: VertexInputs, |
| 48 | + @builtin(instance_index) instanceIndex: u32 |
| 49 | +) -> FragmentInputs { |
| 50 | + var outputs: FragmentInputs; |
| 51 | + var texCoord = vec2<f32>(0.0); |
| 52 | + var normal = vec3<f32>(0.0, 0.0, 1.0); |
| 53 | + var uvRegion = vec4<f32>(0.0); |
| 54 | +
|
| 55 | +#ifdef HAS_UV |
| 56 | + texCoord = inputs.texCoords; |
| 57 | +#endif |
| 58 | +#ifdef HAS_NORMALS |
| 59 | + normal = inputs.normals; |
| 60 | +#endif |
| 61 | +#ifdef HAS_UV_REGIONS |
| 62 | + uvRegion = inputs.uvRegions; |
| 63 | +#endif |
| 64 | +
|
| 65 | + texCoord = applyUVRegion(texCoord, uvRegion); |
| 66 | + geometry.uv = texCoord; |
| 67 | +#ifdef HAS_FEATURE_IDS |
| 68 | + geometry.pickingColor = picking_getPickingColorFromIndex(inputs.rowIndexes); |
| 69 | +#else |
| 70 | + geometry.pickingColor = picking_getPickingColorFromIndex(instanceIndex); |
| 71 | +#endif |
| 72 | +
|
| 73 | + let instanceModelMatrix = mat3x3<f32>( |
| 74 | + inputs.instanceModelMatrixCol0, |
| 75 | + inputs.instanceModelMatrixCol1, |
| 76 | + inputs.instanceModelMatrixCol2 |
| 77 | + ); |
| 78 | + let commonPosition = vec4<f32>(project_position_vec3_f32(inputs.positions), 1.0); |
| 79 | +
|
| 80 | + geometry.position = commonPosition; |
| 81 | + geometry.normal = project_normal(instanceModelMatrix * normal); |
| 82 | +
|
| 83 | + outputs.position = project_common_position_to_clipspace(commonPosition); |
| 84 | + outputs.color = vec4<f32>( |
| 85 | + inputs.colors.rgb * inputs.instanceColors.rgb, |
| 86 | + inputs.instanceColors.a |
| 87 | + ); |
| 88 | + outputs.texCoord = texCoord; |
| 89 | + outputs.pbrPosition = commonPosition.xyz; |
| 90 | + outputs.pbrNormal = geometry.normal; |
| 91 | + outputs.pickingColor = geometry.pickingColor; |
| 92 | + return outputs; |
| 93 | +} |
| 94 | +
|
| 95 | +@fragment |
| 96 | +fn fragmentMain(inputs: FragmentInputs) -> @location(0) vec4<f32> { |
| 97 | + fragmentGeometry.uv = inputs.texCoord; |
| 98 | +
|
| 99 | + if (picking.isActive > 0.5) { |
| 100 | + if (!picking_isColorValid(inputs.pickingColor)) { |
| 101 | + discard; |
| 102 | + } |
| 103 | + return vec4<f32>(inputs.pickingColor, 1.0); |
| 104 | + } |
| 105 | +
|
| 106 | + fragmentInputs.pbr_vPosition = inputs.pbrPosition; |
| 107 | + fragmentInputs.pbr_vUV0 = inputs.texCoord; |
| 108 | + fragmentInputs.pbr_vUV1 = vec2<f32>(0.0); |
| 109 | + fragmentInputs.pbr_vNormal = inputs.pbrNormal; |
| 110 | +
|
| 111 | + var color = inputs.color * pbr_filterColor(vec4<f32>(0.0)); |
| 112 | + color.a *= layer.opacity; |
| 113 | +
|
| 114 | + if (picking.isHighlightActive > 0.5) { |
| 115 | + let highlightedObjectColor = picking_normalizeColor(picking.highlightedObjectColor); |
| 116 | + if (picking_isColorZero(abs(inputs.pickingColor - highlightedObjectColor))) { |
| 117 | + let highlightAlpha = picking.highlightColor.a; |
| 118 | + let blendedAlpha = highlightAlpha + color.a * (1.0 - highlightAlpha); |
| 119 | + if (blendedAlpha > 0.0) { |
| 120 | + color = vec4<f32>( |
| 121 | + mix(color.rgb, picking.highlightColor.rgb, highlightAlpha / blendedAlpha), |
| 122 | + blendedAlpha |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + return deckgl_premultiplied_alpha(color); |
| 129 | +} |
| 130 | +`; |
0 commit comments