-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Port of PoC atmosphere layer. * Fix resize for draw_atmosphere * Add some options. * Allow to change sun date and time * Fix import warning * Render atmosphere only when a Globe projection is selected * Add some comments * Add some comments * Change key * Update changelog * Fix merge with globe branch * Fix documentation and default background color. * Use black clear color only when atmosphere is on * Use atmosphere uniform for globe position, raidus in camera frame and inv projection matrix. * Remove unused project method * Update maplibre-gl-style-spec to 20.3.0 and use sky atmosphere parameter * Fix globe tests and use light position as Sun position. * Avoid type name collisions. * Add atmosphere test for globe projection. * Update expectedBytes for build test. * Fix PR comments. * Update Style test. * Remove unused method on projection * Add Sky Test. * Fix style test and add sky unit test. * Move getSunPos method * Fix mercator updateProjection * Remove isGlobe method and fix merge. * Fix globe atmosphere tests with new projection style. * Clean-up some projection and light. Fix setSky and add tests. * Remove sky test during update. * Clean-up
- Loading branch information
Showing
81 changed files
with
1,345 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {createLayout} from '../util/struct_array'; | ||
|
||
export const atmosphereAttributes = createLayout([ | ||
{name: 'a_pos', type: 'Float32', components: 4} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {StencilMode} from '../gl/stencil_mode'; | ||
import {DepthMode} from '../gl/depth_mode'; | ||
import {CullFaceMode} from '../gl/cull_face_mode'; | ||
import {atmosphereUniformValues} from './program/atmosphere_program'; | ||
|
||
import type {Painter} from './painter'; | ||
import {ColorMode} from '../gl/color_mode'; | ||
import Sky from '../style/sky'; | ||
import {Light} from '../style/light'; | ||
import {AtmosphereBoundsArray, TriangleIndexArray} from '../data/array_types.g'; | ||
import {atmosphereAttributes} from '../data/atmosphere_attributes'; | ||
import {Mesh} from './mesh'; | ||
import {SegmentVector} from '../data/segment'; | ||
import {Transform} from '../geo/transform'; | ||
import {mat4, vec3} from 'gl-matrix'; | ||
|
||
function getSunPos(light: Light, transform: Transform): vec3 { | ||
const _lp = light.properties.get('position'); | ||
const lightPos = [-_lp.x, -_lp.y, -_lp.z] as vec3; | ||
|
||
const lightMat = mat4.identity(new Float64Array(16) as any); | ||
|
||
if (light.properties.get('anchor') === 'map') { | ||
mat4.rotateX(lightMat, lightMat, -transform.pitch * Math.PI / 180); | ||
mat4.rotateZ(lightMat, lightMat, -transform.angle); | ||
mat4.rotateX(lightMat, lightMat, transform.center.lat * Math.PI / 180.0); | ||
mat4.rotateY(lightMat, lightMat, -transform.center.lng * Math.PI / 180.0); | ||
} | ||
|
||
vec3.transformMat4(lightPos, lightPos, lightMat); | ||
|
||
return lightPos; | ||
} | ||
|
||
export function drawAtmosphere(painter: Painter, sky: Sky, light: Light) { | ||
const context = painter.context; | ||
const gl = context.gl; | ||
const program = painter.useProgram('atmosphere'); | ||
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadOnly, [0, 1]); | ||
|
||
const projection = painter.style.projection; | ||
const projectionData = projection.getProjectionData(null, null); | ||
|
||
const sunPos = getSunPos(light, painter.transform); | ||
|
||
const atmosphereBlend = sky.getAtmosphereBlend(); | ||
if (atmosphereBlend === 0) { | ||
// Don't draw anythink if atmosphere is fully transparent | ||
return; | ||
} | ||
|
||
const globePosition = projection.worldCenterPosition; | ||
const globeRadius = projection.worldSize; | ||
const invProjMatrix = projection.invProjMatrix; | ||
|
||
const uniformValues = atmosphereUniformValues(sunPos, atmosphereBlend, globePosition, globeRadius, invProjMatrix); | ||
|
||
// Create the atmosphere mesh the first time we need it | ||
if (!sky.atmosphereMesh) { | ||
const vertexArray = new AtmosphereBoundsArray(); | ||
vertexArray.emplaceBack(-1, -1, 0.0, 1.0); | ||
vertexArray.emplaceBack(+1, -1, 0.0, 1.0); | ||
vertexArray.emplaceBack(+1, +1, 0.0, 1.0); | ||
vertexArray.emplaceBack(-1, +1, 0.0, 1.0); | ||
|
||
const indexArray = new TriangleIndexArray(); | ||
indexArray.emplaceBack(0, 1, 2); | ||
indexArray.emplaceBack(0, 2, 3); | ||
|
||
sky.atmosphereMesh = new Mesh( | ||
context.createVertexBuffer(vertexArray, atmosphereAttributes.members), | ||
context.createIndexBuffer(indexArray), | ||
SegmentVector.simpleSegment(0, 0, vertexArray.length, indexArray.length) | ||
); | ||
} | ||
|
||
const mesh = sky.atmosphereMesh; | ||
|
||
program.draw(context, gl.TRIANGLES, depthMode, StencilMode.disabled, ColorMode.alphaBlended, CullFaceMode.disabled, uniformValues, null, projectionData, 'atmosphere', mesh.vertexBuffer, mesh.indexBuffer, mesh.segments); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type {Context} from '../../gl/context'; | ||
import {UniformValues, UniformLocations, Uniform1f, Uniform3f, UniformMatrix4f} from '../uniform_binding'; | ||
import {mat4, vec3} from 'gl-matrix'; | ||
|
||
export type atmosphereUniformsType = { | ||
'u_sun_pos': Uniform3f; | ||
'u_atmosphere_blend': Uniform1f; | ||
'u_globe_position': Uniform3f; | ||
'u_globe_radius': Uniform1f; | ||
'u_inv_proj_matrix': UniformMatrix4f; | ||
}; | ||
|
||
const atmosphereUniforms = (context: Context, locations: UniformLocations): atmosphereUniformsType => ({ | ||
'u_sun_pos': new Uniform3f(context, locations.u_sun_pos), | ||
'u_atmosphere_blend': new Uniform1f(context, locations.u_atmosphere_blend), | ||
'u_globe_position': new Uniform3f(context, locations.u_globe_position), | ||
'u_globe_radius': new Uniform1f(context, locations.u_globe_radius), | ||
'u_inv_proj_matrix': new UniformMatrix4f(context, locations.u_inv_proj_matrix), | ||
}); | ||
|
||
const atmosphereUniformValues = ( | ||
sunPos: vec3, | ||
atmosphereBlend: number, | ||
globePosition: vec3, | ||
globeRadius: number, | ||
invProjMatrix: mat4, | ||
): UniformValues<atmosphereUniformsType> => ({ | ||
'u_sun_pos': sunPos, | ||
'u_atmosphere_blend': atmosphereBlend, | ||
'u_globe_position': globePosition, | ||
'u_globe_radius': globeRadius, | ||
'u_inv_proj_matrix': invProjMatrix, | ||
}); | ||
|
||
export {atmosphereUniforms, atmosphereUniformValues}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.