@tangram.gl/tangram-renderer 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -2
- package/dist/core.js +36923 -0
- package/dist/core.js.map +6 -0
- package/dist/index.js +32 -0
- package/dist/loaders-gl-worker.js +82 -0
- package/dist/loaders-gl-worker.js.map +6 -0
- package/dist/map-logic.js +2 -0
- package/dist/map-logic.js.map +6 -0
- package/dist/projected-basemaps-worker.js +3 -0
- package/dist/projected-basemaps-worker.js.map +6 -0
- package/dist/shader-modules.js +141 -0
- package/dist/shader-modules.js.map +6 -0
- package/dist/style-schema.js +126 -0
- package/dist/style-schema.js.map +6 -0
- package/dist/tangram-style.schema.json +882 -0
- package/dist/tangram.debug.mjs +37139 -0
- package/dist/tangram.debug.mjs.map +6 -0
- package/dist/tangram.min.mjs +4950 -0
- package/dist/types/builders/earcut.d.ts +15 -0
- package/dist/types/core.d.ts +29 -0
- package/dist/types/gl/constants.d.ts +2 -0
- package/dist/types/gl/globe_mesh.d.ts +48 -0
- package/dist/types/gl/glsl.d.ts +17 -0
- package/dist/types/gl/vertex-constants.d.ts +4 -0
- package/dist/types/gl/vertex-types.d.ts +34 -0
- package/dist/types/gl/vertex_data.d.ts +32 -0
- package/dist/types/gl/vertex_elements.d.ts +8 -0
- package/dist/types/gl/vertex_layout.d.ts +32 -0
- package/dist/types/glsl.d.ts +14 -0
- package/dist/types/gpu/luma_device_renderer.d.ts +66 -0
- package/dist/types/gpu/resource_lifecycle.d.ts +4 -0
- package/dist/types/gpu/tangram_gpu_backend.d.ts +151 -0
- package/dist/types/index.d.ts +155 -0
- package/dist/types/lights/light-definitions.d.ts +121 -0
- package/dist/types/map-logic/draped-terrain-bounds.d.ts +41 -0
- package/dist/types/map-logic/globe-terrain-preparation.d.ts +85 -0
- package/dist/types/map-logic/globe-terrain.d.ts +54 -0
- package/dist/types/map-logic/index.d.ts +33 -0
- package/dist/types/map-logic/label-identity.d.ts +22 -0
- package/dist/types/map-logic/label-placement.d.ts +32 -0
- package/dist/types/map-logic/screen-bounds.d.ts +15 -0
- package/dist/types/map-logic/terrain-data.d.ts +55 -0
- package/dist/types/map-logic/terrain-error.d.ts +51 -0
- package/dist/types/map-logic/terrain-ground-topology.d.ts +26 -0
- package/dist/types/map-logic/terrain-height-field.d.ts +59 -0
- package/dist/types/map-logic/terrain-navigation.d.ts +27 -0
- package/dist/types/map-logic/terrain-neighborhood.d.ts +5 -0
- package/dist/types/map-logic/terrain-query.d.ts +33 -0
- package/dist/types/map-logic/terrain-resource-budget.d.ts +51 -0
- package/dist/types/map-logic/terrain-transition-topology.d.ts +23 -0
- package/dist/types/map-logic/tile-build-queue.d.ts +51 -0
- package/dist/types/map-logic/tile-cache-policy.d.ts +46 -0
- package/dist/types/map-logic/tile-residency.d.ts +17 -0
- package/dist/types/map-logic/triangle-query-index.d.ts +87 -0
- package/dist/types/procedures/js-yaml.d.ts +15 -0
- package/dist/types/procedures/mesh-projector.d.ts +107 -0
- package/dist/types/procedures/mvt-dependencies.d.ts +20 -0
- package/dist/types/procedures/projected-coordinate-transform.d.ts +27 -0
- package/dist/types/procedures/projection-batch-executor.d.ts +53 -0
- package/dist/types/procedures/scene-yaml-legacy.d.ts +6 -0
- package/dist/types/procedures/terrain-draping.d.ts +165 -0
- package/dist/types/procedures/terrain-globe.d.ts +6 -0
- package/dist/types/procedures/terrain-styled-topology.d.ts +34 -0
- package/dist/types/procedures/web-mercator-legacy.d.ts +4 -0
- package/dist/types/procedures/web-mercator-math.d.ts +4 -0
- package/dist/types/scene/globals.d.ts +11 -0
- package/dist/types/scene/ground_footprint.d.ts +16 -0
- package/dist/types/scene/host_frame.d.ts +54 -0
- package/dist/types/scene/projection_math.d.ts +52 -0
- package/dist/types/scene/scene-resource-types.d.ts +177 -0
- package/dist/types/scene/scene_bundle.d.ts +69 -0
- package/dist/types/scene/visibility_adapter.d.ts +106 -0
- package/dist/types/selection/draped-terrain-surface.d.ts +78 -0
- package/dist/types/selection/terrain_surface.d.ts +149 -0
- package/dist/types/shader-modules/globe-horizon.d.ts +15 -0
- package/dist/types/shader-modules/height-decode.d.ts +13 -0
- package/dist/types/shader-modules/hillshade.d.ts +13 -0
- package/dist/types/shader-modules/index.d.ts +6 -0
- package/dist/types/shader-modules/planar.d.ts +12 -0
- package/dist/types/shader-modules/sphere-map.d.ts +13 -0
- package/dist/types/shader-modules/triplanar.d.ts +15 -0
- package/dist/types/sources/decoded_tile_store.d.ts +95 -0
- package/dist/types/sources/tile_source_metadata.d.ts +32 -0
- package/dist/types/styles/text/fontfaceobserver.d.ts +10 -0
- package/dist/types/tile/tile_id.d.ts +37 -0
- package/dist/types/tile/tile_traversal_adapter.d.ts +63 -0
- package/dist/types/types.d.ts +293 -0
- package/dist/types/utils/geo.d.ts +57 -0
- package/dist/types/utils/hash.d.ts +1 -0
- package/dist/types/utils/log.d.ts +27 -0
- package/dist/types/utils/props.d.ts +6 -0
- package/dist/types/utils/subscribe.d.ts +10 -0
- package/dist/types/utils/thread.d.ts +6 -0
- package/dist/types/utils/urls.d.ts +26 -0
- package/dist/types/utils/utils.d.ts +26 -0
- package/dist/types/utils/version.d.ts +2 -0
- package/dist/types/utils/worker-types.d.ts +68 -0
- package/dist/types/utils/worker_broker.d.ts +3 -0
- package/package.json +103 -3
- package/src/builders/common.ts +41 -0
- package/src/builders/earcut.d.ts +15 -0
- package/src/builders/geometry-types.ts +60 -0
- package/src/builders/points.ts +115 -0
- package/src/builders/polygons.ts +225 -0
- package/src/builders/polylines.ts +682 -0
- package/src/builders/wireframe.ts +31 -0
- package/src/core.d.ts +29 -0
- package/src/core.ts +33 -0
- package/src/experimental/loaders-gl-worker.ts +19 -0
- package/src/experimental/projected-basemaps-worker.ts +13 -0
- package/src/experimental/projected-mesh-cache.ts +137 -0
- package/src/experimental/projected-mesh-domain.ts +110 -0
- package/src/experimental/projected-mesh-refinement.ts +193 -0
- package/src/experimental/projected-mesh.ts +399 -0
- package/src/experimental/projected-normal.ts +26 -0
- package/src/experimental/projected-symbol-mesh.ts +74 -0
- package/src/gl/constants.ts +18 -0
- package/src/gl/context.ts +103 -0
- package/src/gl/extensions.ts +30 -0
- package/src/gl/globe_mesh.ts +285 -0
- package/src/gl/glsl.ts +257 -0
- package/src/gl/mesh-types.ts +56 -0
- package/src/gl/render_state.ts +111 -0
- package/src/gl/shader_program.ts +1310 -0
- package/src/gl/texture.ts +814 -0
- package/src/gl/uniform_buffer.ts +340 -0
- package/src/gl/vao.ts +154 -0
- package/src/gl/vbo_mesh.ts +349 -0
- package/src/gl/vertex-constants.ts +19 -0
- package/src/gl/vertex-types.ts +35 -0
- package/src/gl/vertex_data.ts +126 -0
- package/src/gl/vertex_elements.ts +54 -0
- package/src/gl/vertex_layout.ts +241 -0
- package/src/glsl.d.ts +14 -0
- package/src/gpu/luma_device_renderer.ts +579 -0
- package/src/gpu/pixel_readback.ts +51 -0
- package/src/gpu/resource_lifecycle.ts +32 -0
- package/src/gpu/tangram_gpu_backend.ts +195 -0
- package/src/index.d.ts +155 -0
- package/src/index.ts +105 -0
- package/src/labels/collision-types.ts +94 -0
- package/src/labels/collision.ts +273 -0
- package/src/labels/collision_grid.ts +61 -0
- package/src/labels/intersect.ts +30 -0
- package/src/labels/label-types.ts +91 -0
- package/src/labels/label.ts +207 -0
- package/src/labels/label_line.ts +801 -0
- package/src/labels/label_point.ts +131 -0
- package/src/labels/main-pass-types.ts +73 -0
- package/src/labels/main_pass.ts +221 -0
- package/src/labels/point_anchor.ts +100 -0
- package/src/labels/point_placement.ts +150 -0
- package/src/labels/projected-pass.ts +259 -0
- package/src/labels/repeat_group.ts +72 -0
- package/src/lights/ambient_light.glsl +18 -0
- package/src/lights/directional_light.glsl +39 -0
- package/src/lights/geographic-lights.ts +21 -0
- package/src/lights/light-definitions.ts +285 -0
- package/src/lights/light.ts +590 -0
- package/src/lights/lighting-uniforms.ts +80 -0
- package/src/lights/lighting-wgsl.ts +116 -0
- package/src/lights/material.glsl +237 -0
- package/src/lights/material.ts +145 -0
- package/src/lights/native-falloff.ts +37 -0
- package/src/lights/point_light.glsl +96 -0
- package/src/lights/spot_light.glsl +111 -0
- package/src/map-logic/draped-terrain-bounds.ts +71 -0
- package/src/map-logic/globe-terrain-preparation.ts +217 -0
- package/src/map-logic/globe-terrain.ts +296 -0
- package/src/map-logic/index.ts +37 -0
- package/src/map-logic/label-identity.ts +36 -0
- package/src/map-logic/label-placement.ts +117 -0
- package/src/map-logic/screen-bounds.ts +34 -0
- package/src/map-logic/terrain-data.ts +107 -0
- package/src/map-logic/terrain-error.ts +134 -0
- package/src/map-logic/terrain-ground-topology.ts +164 -0
- package/src/map-logic/terrain-height-field.ts +254 -0
- package/src/map-logic/terrain-navigation.ts +50 -0
- package/src/map-logic/terrain-neighborhood.ts +43 -0
- package/src/map-logic/terrain-query.ts +51 -0
- package/src/map-logic/terrain-resource-budget.ts +124 -0
- package/src/map-logic/terrain-transition-topology.ts +164 -0
- package/src/map-logic/tile-build-queue.ts +110 -0
- package/src/map-logic/tile-cache-policy.ts +89 -0
- package/src/map-logic/tile-residency.ts +50 -0
- package/src/map-logic/triangle-query-index.ts +265 -0
- package/src/procedures/js-yaml.d.ts +15 -0
- package/src/procedures/mesh-projector.ts +191 -0
- package/src/procedures/mlt-loaders.ts +55 -0
- package/src/procedures/mvt-dependencies.d.ts +20 -0
- package/src/procedures/mvt-legacy.ts +150 -0
- package/src/procedures/mvt-loaders.ts +53 -0
- package/src/procedures/mvt-parser.ts +48 -0
- package/src/procedures/mvt-properties.ts +36 -0
- package/src/procedures/mvt-tile-provider.ts +72 -0
- package/src/procedures/pmtiles-loader.ts +75 -0
- package/src/procedures/projected-coordinate-transform.ts +79 -0
- package/src/procedures/projection-batch-executor.ts +140 -0
- package/src/procedures/scene-yaml-legacy.ts +16 -0
- package/src/procedures/scene-yaml-loaders.ts +10 -0
- package/src/procedures/terrain-draping.ts +713 -0
- package/src/procedures/terrain-globe.ts +32 -0
- package/src/procedures/terrain-styled-topology.ts +184 -0
- package/src/procedures/web-mercator-legacy.ts +24 -0
- package/src/procedures/web-mercator-math.ts +50 -0
- package/src/scene/camera.ts +298 -0
- package/src/scene/camera_base.ts +116 -0
- package/src/scene/classic_scene.ts +20 -0
- package/src/scene/external_camera.ts +101 -0
- package/src/scene/globals.ts +120 -0
- package/src/scene/ground_footprint.ts +139 -0
- package/src/scene/host_frame.ts +326 -0
- package/src/scene/projected_tile_lod.ts +125 -0
- package/src/scene/projection_math.ts +142 -0
- package/src/scene/projection_shaders.ts +183 -0
- package/src/scene/renderer.ts +516 -0
- package/src/scene/scene-resource-types.ts +174 -0
- package/src/scene/scene.ts +2378 -0
- package/src/scene/scene_bundle.ts +304 -0
- package/src/scene/scene_debug.ts +129 -0
- package/src/scene/scene_loader.ts +402 -0
- package/src/scene/scene_worker.ts +528 -0
- package/src/scene/view.ts +683 -0
- package/src/scene/visibility_adapter.ts +319 -0
- package/src/scene/worker_url.ts +15 -0
- package/src/selection/draped-terrain-surface.ts +377 -0
- package/src/selection/selection-types.ts +79 -0
- package/src/selection/selection.ts +542 -0
- package/src/selection/selection_fragment.glsl +18 -0
- package/src/selection/selection_globals.glsl +9 -0
- package/src/selection/selection_pixels.ts +59 -0
- package/src/selection/selection_vertex.glsl +15 -0
- package/src/selection/terrain_surface.ts +298 -0
- package/src/shader-modules/globe-horizon.ts +52 -0
- package/src/shader-modules/height-decode.ts +41 -0
- package/src/shader-modules/hillshade.ts +70 -0
- package/src/shader-modules/index.ts +10 -0
- package/src/shader-modules/planar.ts +24 -0
- package/src/shader-modules/sphere-map.ts +38 -0
- package/src/shader-modules/triplanar.ts +45 -0
- package/src/sources/aligned_tile_source.ts +122 -0
- package/src/sources/data_source.ts +655 -0
- package/src/sources/decoded_tile_store.ts +245 -0
- package/src/sources/geojson.ts +300 -0
- package/src/sources/mvt.ts +247 -0
- package/src/sources/raster.ts +341 -0
- package/src/sources/shared_tile_source_adapter.ts +117 -0
- package/src/sources/sources.ts +10 -0
- package/src/sources/tile_source_adapter.ts +84 -0
- package/src/sources/tile_source_metadata.ts +65 -0
- package/src/sources/tile_source_state.ts +112 -0
- package/src/sources/topojson.ts +90 -0
- package/src/styles/feature_annotations.ts +16 -0
- package/src/styles/filter.ts +190 -0
- package/src/styles/geometry-style-types.ts +209 -0
- package/src/styles/globe_visibility_wgsl.ts +22 -0
- package/src/styles/layer-types.ts +45 -0
- package/src/styles/layer.ts +586 -0
- package/src/styles/lines/dasharray.ts +49 -0
- package/src/styles/lines/lines.ts +707 -0
- package/src/styles/lines/lines_wgsl.ts +224 -0
- package/src/styles/points/point-runtime-types.ts +125 -0
- package/src/styles/points/points.ts +1079 -0
- package/src/styles/points/points_fragment.glsl +126 -0
- package/src/styles/points/points_vertex.glsl +230 -0
- package/src/styles/points/points_wgsl.ts +189 -0
- package/src/styles/polygons/polygons.ts +268 -0
- package/src/styles/polygons/polygons_fragment.glsl +173 -0
- package/src/styles/polygons/polygons_vertex.glsl +213 -0
- package/src/styles/polygons/polygons_wgsl.ts +127 -0
- package/src/styles/property-types.ts +125 -0
- package/src/styles/raster/raster.ts +33 -0
- package/src/styles/raster/raster_globals.glsl +46 -0
- package/src/styles/style-mixing-types.ts +101 -0
- package/src/styles/style-runtime-types.ts +220 -0
- package/src/styles/style-schema.ts +133 -0
- package/src/styles/style.ts +1033 -0
- package/src/styles/style_globals.glsl +69 -0
- package/src/styles/style_manager.ts +464 -0
- package/src/styles/style_parser.ts +620 -0
- package/src/styles/text/font_manager.ts +180 -0
- package/src/styles/text/fontfaceobserver.d.ts +10 -0
- package/src/styles/text/text-types.ts +220 -0
- package/src/styles/text/text.ts +378 -0
- package/src/styles/text/text_canvas.ts +773 -0
- package/src/styles/text/text_labels.ts +436 -0
- package/src/styles/text/text_segments.ts +142 -0
- package/src/styles/text/text_settings.ts +228 -0
- package/src/styles/text/text_wgsl.ts +159 -0
- package/src/styles/text/text_wrap.ts +194 -0
- package/src/tile/globe_tile_preload.ts +40 -0
- package/src/tile/tangram_tileset_2d.ts +108 -0
- package/src/tile/tile-build-types.ts +155 -0
- package/src/tile/tile.ts +807 -0
- package/src/tile/tile_id.ts +172 -0
- package/src/tile/tile_manager.ts +889 -0
- package/src/tile/tile_pyramid.ts +119 -0
- package/src/tile/tile_resource_cache.ts +91 -0
- package/src/tile/tile_traversal_adapter.ts +132 -0
- package/src/types.ts +332 -0
- package/src/utils/debounce.ts +17 -0
- package/src/utils/debug_settings.ts +43 -0
- package/src/utils/errors.ts +12 -0
- package/src/utils/functions.ts +95 -0
- package/src/utils/geo.ts +364 -0
- package/src/utils/hash.ts +14 -0
- package/src/utils/log.ts +108 -0
- package/src/utils/media_capture.ts +193 -0
- package/src/utils/merge.ts +44 -0
- package/src/utils/obb.ts +134 -0
- package/src/utils/props.ts +43 -0
- package/src/utils/slice.ts +14 -0
- package/src/utils/subscribe.ts +58 -0
- package/src/utils/task.ts +154 -0
- package/src/utils/thread.ts +26 -0
- package/src/utils/urls.ts +223 -0
- package/src/utils/utils.ts +310 -0
- package/src/utils/vector.ts +197 -0
- package/src/utils/version.ts +8 -0
- package/src/utils/worker-types.ts +79 -0
- package/src/utils/worker_broker.ts +523 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Tangram
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) 2013-2016 Brett Camper and Mapzen
|
|
4
|
+
// Copyright (c) 2026 vis.gl contributors
|
|
5
|
+
|
|
6
|
+
import {MAX_PORTABLE_LIGHTS} from './lighting-uniforms';
|
|
7
|
+
import {buildNativeFalloff} from './native-falloff';
|
|
8
|
+
|
|
9
|
+
/** Specialize Tangram's constant-material equations to the bounded active scene light count. */
|
|
10
|
+
export function buildLightingWGSL(lightCount = MAX_PORTABLE_LIGHTS): string {
|
|
11
|
+
if (!Number.isInteger(lightCount) || lightCount < 0 || lightCount > MAX_PORTABLE_LIGHTS) {
|
|
12
|
+
throw new Error(`Portable lighting supports 0 to ${MAX_PORTABLE_LIGHTS} visible lights`);
|
|
13
|
+
}
|
|
14
|
+
return `
|
|
15
|
+
${buildNativeFalloff('wgsl')}
|
|
16
|
+
struct TangramSurfaceLight {
|
|
17
|
+
ambient: vec4<f32>, diffuse: vec4<f32>, specular: vec4<f32>,
|
|
18
|
+
position: vec4<f32>, direction: vec4<f32>, falloff: vec4<f32>,
|
|
19
|
+
cone: vec4<f32>, coefficients: vec4<f32>,
|
|
20
|
+
};
|
|
21
|
+
fn tangramSurfaceLight(index: i32) -> TangramSurfaceLight {
|
|
22
|
+
switch index {
|
|
23
|
+
${Array.from({length: lightCount}, (_, index) => `
|
|
24
|
+
case ${index}: { return TangramSurfaceLight(${['ambient', 'diffuse', 'specular', 'position', 'direction', 'falloff', 'cone', 'coefficients'].map(field => `TangramLighting.u_light_${index}_${field}`).join(', ')}); }`).join('')}
|
|
25
|
+
default: { return TangramSurfaceLight(vec4<f32>(0.0), vec4<f32>(0.0), vec4<f32>(0.0), vec4<f32>(0.0), vec4<f32>(0.0), vec4<f32>(0.0), vec4<f32>(0.0), vec4<f32>(0.0)); }
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
fn tangramDistanceFalloff(distance: f32, light: TangramSurfaceLight) -> f32 {
|
|
29
|
+
let exponent = light.falloff.x;
|
|
30
|
+
let inner = light.falloff.y;
|
|
31
|
+
let outer = light.falloff.z;
|
|
32
|
+
var attenuation = 1.0;
|
|
33
|
+
if (exponent != 0.0) {
|
|
34
|
+
let radius = select(1.0, inner, inner >= 0.0);
|
|
35
|
+
if (outer >= 0.0) {
|
|
36
|
+
let d = clamp(max(0.0, distance - radius) / max(outer - radius, 0.0001), 0.0, 1.0);
|
|
37
|
+
attenuation = 1.0 - pow(d, exponent);
|
|
38
|
+
} else {
|
|
39
|
+
let d = max(0.0, distance - radius) / max(radius, 0.0001) + 1.0;
|
|
40
|
+
attenuation = clamp(1.0 / pow(d, exponent), 0.0, 1.0);
|
|
41
|
+
}
|
|
42
|
+
} else if (inner >= 0.0) {
|
|
43
|
+
if (outer >= 0.0) {
|
|
44
|
+
let d = clamp(max(0.0, distance - inner) / max(outer - inner, 0.0001), 0.0, 1.0);
|
|
45
|
+
attenuation = 1.0 - d * d;
|
|
46
|
+
} else {
|
|
47
|
+
let d = max(0.0, distance - inner) / max(inner, 0.0001) + 1.0;
|
|
48
|
+
attenuation = clamp(1.0 / d, 0.0, 1.0);
|
|
49
|
+
}
|
|
50
|
+
} else if (outer >= 0.0) {
|
|
51
|
+
let d = clamp(distance / max(outer, 0.0001), 0.0, 1.0);
|
|
52
|
+
attenuation = 1.0 - d * d;
|
|
53
|
+
}
|
|
54
|
+
if (light.coefficients.w > 0.5) {
|
|
55
|
+
attenuation /= tangramNativeDistanceDenominator(light.coefficients.xyz, distance);
|
|
56
|
+
}
|
|
57
|
+
return attenuation;
|
|
58
|
+
}
|
|
59
|
+
fn tangramCalculateLighting(eye_to_point: vec3<f32>, normal: vec3<f32>, base_color: vec4<f32>) -> vec4<f32> {
|
|
60
|
+
var ambient = vec3<f32>(0.0);
|
|
61
|
+
var diffuse = vec3<f32>(0.0);
|
|
62
|
+
var specular = vec3<f32>(0.0);
|
|
63
|
+
let eye_direction = eye_to_point / max(length(eye_to_point), 0.0001);
|
|
64
|
+
for (var index = 0; index < ${lightCount}; index++) {
|
|
65
|
+
if (index >= TangramLighting.u_light_count) { break; }
|
|
66
|
+
let light = tangramSurfaceLight(index);
|
|
67
|
+
let kind = i32(light.position.w);
|
|
68
|
+
var direction = -light.direction.xyz;
|
|
69
|
+
var attenuation = 1.0;
|
|
70
|
+
if (kind >= 2) {
|
|
71
|
+
let offset = light.position.xyz - eye_to_point;
|
|
72
|
+
let distance = length(offset);
|
|
73
|
+
direction = offset / max(distance, 0.0001);
|
|
74
|
+
attenuation = tangramDistanceFalloff(distance, light);
|
|
75
|
+
if (kind == 3) {
|
|
76
|
+
let cosine = clamp(dot(-direction, light.direction.xyz), 0.0, 1.0);
|
|
77
|
+
var cone_factor = select(0.0, pow(cosine, light.falloff.w), cosine >= light.cone.z);
|
|
78
|
+
if (light.coefficients.w > 0.5) {
|
|
79
|
+
cone_factor = tangramNativeConeFactor(light.cone.xy, cosine) * pow(cosine, light.falloff.w);
|
|
80
|
+
}
|
|
81
|
+
attenuation *= cone_factor;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
ambient += light.ambient.rgb * attenuation;
|
|
85
|
+
if (kind > 0) {
|
|
86
|
+
let lambert = clamp(dot(normal, direction), 0.0, 1.0);
|
|
87
|
+
diffuse += light.diffuse.rgb * lambert * attenuation;
|
|
88
|
+
if (lambert > 0.0 && TangramMaterial.u_material_flags.w != 0) {
|
|
89
|
+
let reflected = reflect(-direction, normal);
|
|
90
|
+
// Preserve Tangram's directional versus positional eye-vector convention.
|
|
91
|
+
let eye = select(-eye_direction, eye_direction, kind == 1);
|
|
92
|
+
let power = pow(max(dot(eye, reflected), 0.0), TangramMaterial.u_material_shininess);
|
|
93
|
+
specular += light.specular.rgb * power * attenuation;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
var color = vec4<f32>(0.0, 0.0, 0.0, base_color.a);
|
|
98
|
+
let flags = TangramMaterial.u_material_flags;
|
|
99
|
+
if (flags.x != 0) {
|
|
100
|
+
color = vec4<f32>(TangramMaterial.u_material_emission.rgb, color.a * TangramMaterial.u_material_emission.a);
|
|
101
|
+
}
|
|
102
|
+
if (flags.y != 0) {
|
|
103
|
+
color = vec4<f32>(color.rgb + ambient * base_color.rgb * TangramMaterial.u_material_ambient.rgb, color.a * TangramMaterial.u_material_ambient.a);
|
|
104
|
+
} else if (flags.z != 0) {
|
|
105
|
+
color = vec4<f32>(color.rgb + ambient * base_color.rgb * TangramMaterial.u_material_diffuse.rgb, color.a);
|
|
106
|
+
}
|
|
107
|
+
if (flags.z != 0) {
|
|
108
|
+
color = vec4<f32>(color.rgb + diffuse * base_color.rgb * TangramMaterial.u_material_diffuse.rgb, color.a * TangramMaterial.u_material_diffuse.a);
|
|
109
|
+
}
|
|
110
|
+
if (flags.w != 0) {
|
|
111
|
+
color = vec4<f32>(color.rgb + specular * TangramMaterial.u_material_specular.rgb, color.a * TangramMaterial.u_material_specular.a);
|
|
112
|
+
}
|
|
113
|
+
return clamp(color, vec4<f32>(0.0), vec4<f32>(1.0));
|
|
114
|
+
}
|
|
115
|
+
`;
|
|
116
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Tangram
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) 2013-2016 Brett Camper and Mapzen
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
|
|
7
|
+
Defines globals:
|
|
8
|
+
material
|
|
9
|
+
light_accumulator_*
|
|
10
|
+
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// MATERIALS
|
|
15
|
+
//
|
|
16
|
+
struct Material {
|
|
17
|
+
#ifdef TANGRAM_MATERIAL_EMISSION
|
|
18
|
+
vec4 emission;
|
|
19
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
|
|
20
|
+
vec3 emissionScale;
|
|
21
|
+
#endif
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT
|
|
25
|
+
vec4 ambient;
|
|
26
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
|
|
27
|
+
vec3 ambientScale;
|
|
28
|
+
#endif
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE
|
|
32
|
+
vec4 diffuse;
|
|
33
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
|
|
34
|
+
vec3 diffuseScale;
|
|
35
|
+
#endif
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR
|
|
39
|
+
vec4 specular;
|
|
40
|
+
float shininess;
|
|
41
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
|
|
42
|
+
vec3 specularScale;
|
|
43
|
+
#endif
|
|
44
|
+
#endif
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
|
|
48
|
+
vec3 normalScale;
|
|
49
|
+
float normalAmount;
|
|
50
|
+
#endif
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Note: uniform is copied to a global instance to allow modification
|
|
54
|
+
uniform Material u_material;
|
|
55
|
+
Material material;
|
|
56
|
+
|
|
57
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
|
|
58
|
+
uniform sampler2D u_material_emission_texture;
|
|
59
|
+
#endif
|
|
60
|
+
|
|
61
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
|
|
62
|
+
uniform sampler2D u_material_ambient_texture;
|
|
63
|
+
#endif
|
|
64
|
+
|
|
65
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
|
|
66
|
+
uniform sampler2D u_material_diffuse_texture;
|
|
67
|
+
#endif
|
|
68
|
+
|
|
69
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
|
|
70
|
+
uniform sampler2D u_material_specular_texture;
|
|
71
|
+
#endif
|
|
72
|
+
|
|
73
|
+
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
|
|
74
|
+
uniform sampler2D u_material_normal_texture;
|
|
75
|
+
#endif
|
|
76
|
+
|
|
77
|
+
// Global light accumulators for each property
|
|
78
|
+
vec4 light_accumulator_ambient = vec4(vec3(0.0), 1.);
|
|
79
|
+
vec4 light_accumulator_diffuse = vec4(vec3(0.0), 1.);
|
|
80
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR
|
|
81
|
+
vec4 light_accumulator_specular = vec4(vec3(0.0), 1.);
|
|
82
|
+
#endif
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
#ifdef TANGRAM_MATERIAL_TEXTURE_SPHEREMAP
|
|
86
|
+
vec4 getSphereMap (in sampler2D _tex, in vec3 _eyeToPoint, in vec3 _normal, in vec2 _skew) {
|
|
87
|
+
// Normalized vector from camera to surface
|
|
88
|
+
vec3 eye = normalize(_eyeToPoint);
|
|
89
|
+
|
|
90
|
+
// Adjust for camera skew
|
|
91
|
+
eye.xy -= _skew;
|
|
92
|
+
eye = normalize(eye);
|
|
93
|
+
|
|
94
|
+
// Reflection of eye off of surface normal
|
|
95
|
+
vec3 r = reflect(eye, _normal);
|
|
96
|
+
|
|
97
|
+
// Map reflected vector onto the surface of a sphere
|
|
98
|
+
r.z += 1.0;
|
|
99
|
+
float m = 2. * length(r);
|
|
100
|
+
|
|
101
|
+
// Adjust xy to account for spherical shape, and center in middle of texture
|
|
102
|
+
vec2 uv = r.xy / m + .5;
|
|
103
|
+
|
|
104
|
+
// Sample the environment map
|
|
105
|
+
return texture2D(_tex, uv);
|
|
106
|
+
}
|
|
107
|
+
#endif
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
#ifdef TANGRAM_MATERIAL_TEXTURE_TRIPLANAR
|
|
111
|
+
vec3 getTriPlanarBlend (in vec3 _normal) {
|
|
112
|
+
vec3 blending = abs(_normal);
|
|
113
|
+
blending = normalize(max(blending, 0.00001));
|
|
114
|
+
float b = (blending.x + blending.y + blending.z);
|
|
115
|
+
return blending / b;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
vec4 getTriPlanar (in sampler2D _tex, in vec3 _pos, in vec3 _normal, in vec3 _scale) {
|
|
119
|
+
vec3 blending = getTriPlanarBlend(_normal);
|
|
120
|
+
vec4 xaxis = texture2D(_tex, fract(_pos.yz * _scale.x));
|
|
121
|
+
vec4 yaxis = texture2D(_tex, fract(_pos.xz * _scale.y));
|
|
122
|
+
vec4 zaxis = texture2D(_tex, fract(_pos.xy * _scale.z));
|
|
123
|
+
return xaxis * blending.x + yaxis * blending.y + zaxis * blending.z;
|
|
124
|
+
}
|
|
125
|
+
#endif
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
#ifdef TANGRAM_MATERIAL_TEXTURE_PLANAR
|
|
129
|
+
vec4 getPlanar (in sampler2D _tex, in vec3 _pos, in vec2 _scale) {
|
|
130
|
+
return texture2D( _tex, fract(_pos.xy * _scale.x) );
|
|
131
|
+
}
|
|
132
|
+
#endif
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
|
|
136
|
+
void calculateNormal (inout vec3 _normal) {
|
|
137
|
+
// Get NORMALMAP
|
|
138
|
+
//------------------------------------------------
|
|
139
|
+
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_UV
|
|
140
|
+
_normal += texture2D(u_material_normal_texture, fract(v_texcoord*material.normalScale.xy)).rgb*2.0-1.0;
|
|
141
|
+
#endif
|
|
142
|
+
|
|
143
|
+
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_PLANAR
|
|
144
|
+
vec3 normalTex = getPlanar(u_material_normal_texture, v_world_position.xyz, material.normalScale.xy).rgb*2.0-1.0;
|
|
145
|
+
_normal += normalTex;
|
|
146
|
+
#endif
|
|
147
|
+
|
|
148
|
+
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_TRIPLANAR
|
|
149
|
+
vec3 normalTex = getTriPlanar(u_material_normal_texture, v_world_position.xyz, _normal, material.normalScale).rgb*2.0-1.0;
|
|
150
|
+
_normal += normalTex;
|
|
151
|
+
#endif
|
|
152
|
+
|
|
153
|
+
_normal = normalize(_normal);
|
|
154
|
+
}
|
|
155
|
+
#endif
|
|
156
|
+
|
|
157
|
+
void calculateMaterial (in vec3 _eyeToPoint, inout vec3 _normal) {
|
|
158
|
+
// get EMISSION TEXTUREMAP
|
|
159
|
+
//------------------------------------------------
|
|
160
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
|
|
161
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_UV
|
|
162
|
+
material.emission *= texture2D(u_material_emission_texture,v_texcoord);
|
|
163
|
+
#endif
|
|
164
|
+
|
|
165
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_PLANAR
|
|
166
|
+
material.emission *= getPlanar(u_material_emission_texture, v_world_position.xyz, material.emissionScale.xy);
|
|
167
|
+
#endif
|
|
168
|
+
|
|
169
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_TRIPLANAR
|
|
170
|
+
material.emission *= getTriPlanar(u_material_emission_texture, v_world_position.xyz, _normal, material.emissionScale);
|
|
171
|
+
#endif
|
|
172
|
+
|
|
173
|
+
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_SPHEREMAP
|
|
174
|
+
material.emission *= getSphereMap(u_material_emission_texture, _eyeToPoint, _normal, u_vanishing_point);
|
|
175
|
+
#endif
|
|
176
|
+
#endif
|
|
177
|
+
|
|
178
|
+
// get AMBIENT TEXTUREMAP
|
|
179
|
+
//------------------------------------------------
|
|
180
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
|
|
181
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_UV
|
|
182
|
+
material.ambient *= texture2D(u_material_ambient_texture,v_texcoord);
|
|
183
|
+
#endif
|
|
184
|
+
|
|
185
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_PLANAR
|
|
186
|
+
material.ambient *= getPlanar(u_material_ambient_texture, v_world_position.xyz, material.ambientScale.xy);
|
|
187
|
+
#endif
|
|
188
|
+
|
|
189
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_TRIPLANAR
|
|
190
|
+
material.ambient *= getTriPlanar(u_material_ambient_texture, v_world_position.xyz, _normal, material.ambientScale);
|
|
191
|
+
#endif
|
|
192
|
+
|
|
193
|
+
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_SPHEREMAP
|
|
194
|
+
material.ambient *= getSphereMap(u_material_ambient_texture, _eyeToPoint, _normal, u_vanishing_point);
|
|
195
|
+
#endif
|
|
196
|
+
#endif
|
|
197
|
+
|
|
198
|
+
// get DIFFUSE TEXTUREMAP
|
|
199
|
+
//------------------------------------------------
|
|
200
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
|
|
201
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_UV
|
|
202
|
+
material.diffuse *= texture2D(u_material_diffuse_texture,v_texcoord);
|
|
203
|
+
#endif
|
|
204
|
+
|
|
205
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_PLANAR
|
|
206
|
+
material.diffuse *= getPlanar(u_material_diffuse_texture, v_world_position.xyz, material.diffuseScale.xy);
|
|
207
|
+
#endif
|
|
208
|
+
|
|
209
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_TRIPLANAR
|
|
210
|
+
material.diffuse *= getTriPlanar(u_material_diffuse_texture, v_world_position.xyz, _normal, material.diffuseScale);
|
|
211
|
+
#endif
|
|
212
|
+
|
|
213
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_SPHEREMAP
|
|
214
|
+
material.diffuse *= getSphereMap(u_material_diffuse_texture, _eyeToPoint, _normal, u_vanishing_point);
|
|
215
|
+
#endif
|
|
216
|
+
#endif
|
|
217
|
+
|
|
218
|
+
// get SPECULAR TEXTUREMAP
|
|
219
|
+
//------------------------------------------------
|
|
220
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
|
|
221
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_UV
|
|
222
|
+
material.specular *= texture2D(u_material_specular_texture,v_texcoord);
|
|
223
|
+
#endif
|
|
224
|
+
|
|
225
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_PLANAR
|
|
226
|
+
material.specular *= getPlanar(u_material_specular_texture, v_world_position.xyz, material.specularScale.xy);
|
|
227
|
+
#endif
|
|
228
|
+
|
|
229
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_TRIPLANAR
|
|
230
|
+
material.specular *= getTriPlanar(u_material_specular_texture, v_world_position.xyz, _normal, material.specularScale);
|
|
231
|
+
#endif
|
|
232
|
+
|
|
233
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_SPHEREMAP
|
|
234
|
+
material.specular *= getSphereMap(u_material_specular_texture, _eyeToPoint, _normal, u_vanishing_point);
|
|
235
|
+
#endif
|
|
236
|
+
#endif
|
|
237
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// Tangram
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) 2013-2016 Brett Camper and Mapzen
|
|
4
|
+
// Copyright (c) 2026 vis.gl contributors
|
|
5
|
+
|
|
6
|
+
import GLSL from '../gl/glsl';
|
|
7
|
+
import StyleParser from '../styles/style_parser';
|
|
8
|
+
|
|
9
|
+
import material_source from './material.glsl';
|
|
10
|
+
import type {PortableMaterial} from './lighting-uniforms';
|
|
11
|
+
|
|
12
|
+
const material_props = ['emission', 'ambient', 'diffuse', 'specular'];
|
|
13
|
+
|
|
14
|
+
export default class Material {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
/** Normalized constant/texture channels created from authored material definitions. */
|
|
17
|
+
declare emission: PortableMaterial['emission'];
|
|
18
|
+
declare ambient: PortableMaterial['ambient'];
|
|
19
|
+
declare diffuse: PortableMaterial['diffuse'];
|
|
20
|
+
declare specular: PortableMaterial['specular'];
|
|
21
|
+
declare normal: {texture?: unknown; mapping: string; scale: number[] | undefined; amount: number} | undefined;
|
|
22
|
+
static block: string;
|
|
23
|
+
|
|
24
|
+
constructor (config: any) {
|
|
25
|
+
|
|
26
|
+
config = config || {};
|
|
27
|
+
|
|
28
|
+
// These properties all have the same defaults, so they can be set in bulk
|
|
29
|
+
material_props.forEach(prop => {
|
|
30
|
+
const value = config[prop];
|
|
31
|
+
if (value != null) {
|
|
32
|
+
if (value.texture) {
|
|
33
|
+
this[prop] = {
|
|
34
|
+
texture: value.texture,
|
|
35
|
+
mapping: value.mapping || 'spheremap',
|
|
36
|
+
scale: GLSL.expandVec3(value.scale != null ? value.scale : 1),
|
|
37
|
+
amount: GLSL.expandVec4(value.amount != null ? value.amount : 1)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
else if (typeof value === 'number' || Array.isArray(value)) {
|
|
41
|
+
this[prop] = { amount: GLSL.expandVec4(value) };
|
|
42
|
+
}
|
|
43
|
+
else if (typeof value === 'string') {
|
|
44
|
+
this[prop] = { amount: (StyleParser as any).parseColor(value) };
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this[prop] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Extra specular props
|
|
53
|
+
if (this.specular) {
|
|
54
|
+
this.specular.shininess = config.shininess ? parseFloat(config.shininess) : 0.2;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Normal mapping
|
|
58
|
+
if (config.normal != null) {
|
|
59
|
+
this.normal = {
|
|
60
|
+
texture: config.normal.texture,
|
|
61
|
+
mapping: config.normal.mapping || 'triplanar',
|
|
62
|
+
scale: GLSL.expandVec3(config.normal.scale != null ? config.normal.scale : 1),
|
|
63
|
+
amount: config.normal.amount != null ? config.normal.amount : 1
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Determine if a material config block has sufficient properties to create a material
|
|
69
|
+
static isValid (config: any): boolean {
|
|
70
|
+
if (config == null) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (config.emission == null &&
|
|
75
|
+
config.ambient == null &&
|
|
76
|
+
config.diffuse == null &&
|
|
77
|
+
config.specular == null) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
inject (style: any): void {
|
|
85
|
+
// For each property, sets defines to configure texture mapping, with a pattern like:
|
|
86
|
+
// TANGRAM_MATERIAL_DIFFUSE, TANGRAM_MATERIAL_DIFFUSE_TEXTURE, TANGRAM_MATERIAL_DIFFUSE_TEXTURE_SPHEREMAP
|
|
87
|
+
// Also sets flags to keep track of each unique mapping type being used, e.g.:
|
|
88
|
+
// TANGRAM_MATERIAL_TEXTURE_SPHEREMAP
|
|
89
|
+
// Enables texture coordinates if needed and not already on
|
|
90
|
+
material_props.forEach(prop => {
|
|
91
|
+
let def = `TANGRAM_MATERIAL_${prop.toUpperCase()}`;
|
|
92
|
+
let texdef = def + '_TEXTURE';
|
|
93
|
+
style.defines[def] = (this[prop] != null);
|
|
94
|
+
if (this[prop] && this[prop].texture) {
|
|
95
|
+
style.defines[texdef] = true;
|
|
96
|
+
style.defines[texdef + '_' + this[prop].mapping.toUpperCase()] = true;
|
|
97
|
+
style.defines[`TANGRAM_MATERIAL_TEXTURE_${this[prop].mapping.toUpperCase()}`] = true;
|
|
98
|
+
style.texcoords = style.texcoords || (this[prop].mapping === 'uv');
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Normal mapping
|
|
103
|
+
// As anove, sets flags to keep track of each unique mapping type being used, e.g.:
|
|
104
|
+
// TANGRAM_MATERIAL_TEXTURE_SPHEREMAP
|
|
105
|
+
if (this.normal && this.normal.texture) {
|
|
106
|
+
style.defines['TANGRAM_MATERIAL_NORMAL_TEXTURE'] = true;
|
|
107
|
+
style.defines['TANGRAM_MATERIAL_NORMAL_TEXTURE_' + this.normal.mapping.toUpperCase()] = true;
|
|
108
|
+
style.defines[`TANGRAM_MATERIAL_TEXTURE_${this.normal.mapping.toUpperCase()}`] = true;
|
|
109
|
+
style.texcoords = style.texcoords || (this.normal.mapping === 'uv');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
style.replaceShaderBlock(Material.block, material_source, 'Material');
|
|
113
|
+
style.addShaderBlock('setup', '\nmaterial = u_material;\n', 'Material');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setupProgram (_program: any): void {
|
|
117
|
+
// For each property, sets uniforms in the pattern:
|
|
118
|
+
// u_material.diffuse, u_material.diffuseScale u_material_diffuse_texture
|
|
119
|
+
material_props.forEach(prop => {
|
|
120
|
+
if (this[prop]) {
|
|
121
|
+
if (this[prop].texture) {
|
|
122
|
+
_program.setTextureUniform(`u_material_${prop}_texture`, this[prop].texture);
|
|
123
|
+
_program.uniform('3fv', `u_material.${prop}Scale`, this[prop].scale);
|
|
124
|
+
_program.uniform('4fv', `u_material.${prop}`, this[prop].amount);
|
|
125
|
+
} else if (this[prop].amount) {
|
|
126
|
+
_program.uniform('4fv', `u_material.${prop}`, this[prop].amount);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Extra specular props
|
|
132
|
+
if (this.specular) {
|
|
133
|
+
_program.uniform('1f', 'u_material.shininess', this.specular.shininess);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Normal mapping
|
|
137
|
+
if (this.normal && this.normal.texture) {
|
|
138
|
+
_program.setTextureUniform('u_material_normal_texture', this.normal.texture);
|
|
139
|
+
_program.uniform('3fv', 'u_material.normalScale', this.normal.scale);
|
|
140
|
+
_program.uniform('1f', 'u_material.normalAmount', this.normal.amount);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
Material.block = 'material';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// tangram-layers
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Polynomial attenuation shared by GLSL/WGSL, matching luma.gl's native point-light denominator. */
|
|
6
|
+
const DISTANCE_DENOMINATOR = 'coefficients.x + coefficients.y * distance + coefficients.z * distance * distance';
|
|
7
|
+
|
|
8
|
+
/** Shared native cone transition; Tangram defines the otherwise undefined equal-cone limit explicitly. */
|
|
9
|
+
const CONE_TRANSITION = 'smoothstep(cone.y, cone.x, cosine)';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Native light calculations, independent of Tangram's color/material accumulators.
|
|
13
|
+
* The 0.0001 floor and outside-cone contribution retain the existing luma-compatible behavior.
|
|
14
|
+
* Legacy radius/exponent falloff remains a separate multiplier, not a substitute for native attenuation.
|
|
15
|
+
*/
|
|
16
|
+
export function buildNativeFalloff(language: 'glsl' | 'wgsl'): string {
|
|
17
|
+
if (language === 'glsl') return `
|
|
18
|
+
float tangramNativeDistanceDenominator(vec3 coefficients, float distance) {
|
|
19
|
+
return max(${DISTANCE_DENOMINATOR}, 0.0001);
|
|
20
|
+
}
|
|
21
|
+
float tangramNativeConeFactor(vec2 cone, float cosine) {
|
|
22
|
+
float factor = cone.x == cone.y ? step(cone.y, cosine) : ${CONE_TRANSITION};
|
|
23
|
+
return max(factor, 0.0001);
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
return `
|
|
27
|
+
fn tangramNativeDistanceDenominator(coefficients: vec3<f32>, distance: f32) -> f32 {
|
|
28
|
+
return max(${DISTANCE_DENOMINATOR}, 0.0001);
|
|
29
|
+
}
|
|
30
|
+
fn tangramNativeConeFactor(cone: vec2<f32>, cosine: f32) -> f32 {
|
|
31
|
+
var factor: f32;
|
|
32
|
+
if (cone.x == cone.y) { factor = step(cone.y, cosine); }
|
|
33
|
+
else { factor = ${CONE_TRANSITION}; }
|
|
34
|
+
return max(factor, 0.0001);
|
|
35
|
+
}
|
|
36
|
+
`;
|
|
37
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Tangram
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) 2013-2016 Brett Camper and Mapzen
|
|
4
|
+
// Copyright (c) 2026 vis.gl contributors
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
|
|
8
|
+
Expected globals:
|
|
9
|
+
material
|
|
10
|
+
light_accumulator_*
|
|
11
|
+
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
struct PointLight {
|
|
15
|
+
vec3 ambient;
|
|
16
|
+
vec3 diffuse;
|
|
17
|
+
vec3 specular;
|
|
18
|
+
vec4 position;
|
|
19
|
+
float useLumaAttenuation;
|
|
20
|
+
vec3 attenuationCoefficients;
|
|
21
|
+
|
|
22
|
+
float attenuationExponent;
|
|
23
|
+
float innerRadius;
|
|
24
|
+
float outerRadius;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
void calculateLight(in PointLight _light, in vec3 _eyeToPoint, in vec3 _normal) {
|
|
28
|
+
|
|
29
|
+
float dist = length(_light.position.xyz - _eyeToPoint);
|
|
30
|
+
|
|
31
|
+
// Compute vector from surface to light position
|
|
32
|
+
vec3 VP = (_light.position.xyz - _eyeToPoint) / dist;
|
|
33
|
+
|
|
34
|
+
// Normalize the vector from surface to light position
|
|
35
|
+
float nDotVP = clamp(dot(VP, _normal), 0.0, 1.0);
|
|
36
|
+
|
|
37
|
+
// Attenuation defaults
|
|
38
|
+
float attenuation = 1.0;
|
|
39
|
+
if (_light.attenuationExponent != 0.0) {
|
|
40
|
+
float Rin = _light.innerRadius >= 0.0 ? _light.innerRadius : 1.0;
|
|
41
|
+
float e = _light.attenuationExponent;
|
|
42
|
+
if (_light.outerRadius >= 0.0) {
|
|
43
|
+
float Rdiff = _light.outerRadius-Rin;
|
|
44
|
+
float d = clamp(max(0.0,dist-Rin)/Rdiff, 0.0, 1.0);
|
|
45
|
+
attenuation = 1.0-(pow(d,e));
|
|
46
|
+
} else {
|
|
47
|
+
// If no outer is provide behaves like:
|
|
48
|
+
// https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
|
|
49
|
+
float d = max(0.0,dist-Rin)/Rin+1.0;
|
|
50
|
+
attenuation = clamp(1.0/(pow(d,e)), 0.0, 1.0);
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
float Rin = max(_light.innerRadius, 0.0);
|
|
54
|
+
if (_light.innerRadius >= 0.0) {
|
|
55
|
+
if (_light.outerRadius >= 0.0) {
|
|
56
|
+
float Rdiff = _light.outerRadius-Rin;
|
|
57
|
+
float d = clamp(max(0.0,dist-Rin)/Rdiff, 0.0, 1.0);
|
|
58
|
+
attenuation = 1.0-d*d;
|
|
59
|
+
} else {
|
|
60
|
+
// If no outer is provide behaves like:
|
|
61
|
+
// https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
|
|
62
|
+
float d = max(0.0,dist-Rin)/Rin+1.0;
|
|
63
|
+
attenuation = clamp(1.0/d, 0.0, 1.0);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
if (_light.outerRadius >= 0.0) {
|
|
67
|
+
float d = clamp(dist/_light.outerRadius, 0.0, 1.0);
|
|
68
|
+
attenuation = 1.0-d*d;
|
|
69
|
+
} else {
|
|
70
|
+
attenuation = 1.0;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (_light.useLumaAttenuation > 0.5) {
|
|
76
|
+
attenuation /= tangramNativeDistanceDenominator(_light.attenuationCoefficients, dist);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Computer accumulators
|
|
80
|
+
light_accumulator_ambient.rgb += _light.ambient * attenuation;
|
|
81
|
+
|
|
82
|
+
#ifdef TANGRAM_MATERIAL_DIFFUSE
|
|
83
|
+
light_accumulator_diffuse.rgb += _light.diffuse * nDotVP * attenuation;
|
|
84
|
+
#endif
|
|
85
|
+
|
|
86
|
+
#ifdef TANGRAM_MATERIAL_SPECULAR
|
|
87
|
+
float pf = 0.0; // power factor for shiny speculars
|
|
88
|
+
if (nDotVP > 0.0) {
|
|
89
|
+
vec3 reflectVector = reflect(-VP, _normal);
|
|
90
|
+
float eyeDotR = max(0.0, dot(-normalize(_eyeToPoint), reflectVector));
|
|
91
|
+
pf = pow(eyeDotR, material.shininess);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
light_accumulator_specular.rgb += _light.specular * pf * attenuation;
|
|
95
|
+
#endif
|
|
96
|
+
}
|