WebGL オーバーレイ表示
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
サンプルを表示
WebGL オーバーレイ表示を利用すると、WebGL を直接使用したり、Three.js などの一般的なグラフィック ライブラリを使用したりして、地図にコンテンツを追加できます。WebGL オーバーレイ表示では、Google Maps Platform がベクター基本地図のレンダリングに使用しているものと同じ WebGL レンダリング コンテキストに直接アクセスできます。共有レンダリング コンテキストを使用すると、3D の建物のジオメトリによる深度オクルージョンや、2D / 3D コンテンツと基本地図のレンダリングの同期などのメリットが得られます。WebGL オーバーレイ表示でレンダリングされたオブジェクトは、緯度 / 経度の座標に関連付けることもできるため、地図のドラッグ、ズーム、パン、チルトに合わせて移動します。
要件
WebGL オーバーレイ表示を使用するには、ベクターマップを有効にした状態で、マップ ID を使用して地図を読み込む必要があります。マップ ID の作成時にチルトと回転を有効にすることを強くおすすめします。これにより、3D カメラを詳細に制御できるようになります。詳しくは概要をご覧ください。
WebGL オーバーレイ表示を追加する
地図にオーバーレイを追加するには、google.maps.WebGLOverlayView
を実装してから、setMap
を使用して地図インスタンスを渡します。
// Create a map instance.
const map = new google.maps.Map(mapDiv, mapOptions);
// Create a WebGL Overlay View instance.
const webglOverlayView = new google.maps.WebGLOverlayView();
// Add the overlay to the map.
webglOverlayView.setMap(map);
ライフサイクル フック
WebGL オーバーレイ表示では、ベクター基本地図の WebGL レンダリング コンテキストのライフサイクル中に、さまざまなタイミングでフックのセットが呼び出されます。これらのライフサイクル フックで、オーバーレイで表示するものを設定、描画、破棄します。
onAdd()
は、オーバーレイの作成時に呼び出されます。これを使用して、WebGL レンダリング コンテキストにすぐにアクセスする必要がないオーバーレイを描画する前に、中間データ構造をフェッチまたは作成します。
- レンダリング コンテキストが利用可能になると、
onContextRestored({gl})
が呼び出されます。これを使用して、WebGL の状態(シェーダー、GL バッファ オブジェクトなど)を初期化、バインドします。onContextRestored()
は WebGLStateOptions
インスタンスを受け取ります。このインスタンスは次のフィールドを持ちます。
gl
: 基本地図で使用される WebGLRenderingContext
のハンドルです。
onDraw({gl, transformer})
でシーンが基本地図にレンダリングされます。onDraw()
のパラメータは WebGLDrawOptions
オブジェクトです。このオブジェクトは次のフィールドを持ちます。
gl
: 基本地図で使用される WebGLRenderingContext
のハンドルです。
transformer
: 地図座標からモデルビュー射影行列への変換を行うヘルパー関数を提供します。この関数を使用することで、地図座標をワールド空間、カメラ空間、画面空間に変換できます。
onContextLost()
は、なんらかの理由でレンダリング コンテキストが失われたときに呼び出されます。既存の GL の状態は不要になるため、ここでクリーンアップします。
onStateUpdate({gl})
は、レンダリング ループの外で GL の状態を更新するフックで、requestStateUpdate
が呼び出されたときに起動されます。このフックは WebGLStateOptions
インスタンスを受け取ります。このインスタンスは次のフィールドを持ちます。
gl
: 基本地図で使用される WebGLRenderingContext
のハンドルです。
onRemove()
は、WebGLOverlayView.setMap(null)
でオーバーレイが地図から削除されたときに呼び出されます。中間オブジェクトはここですべて削除します。
すべてのライフサイクル フックの基本的な実装例は次のとおりです。
const webglOverlayView = new google.maps.WebGLOverlayView();
webglOverlayView.onAdd = () => {
// Do setup that does not require access to rendering context.
}
webglOverlayView.onContextRestored = ({gl}) => {
// Do setup that requires access to rendering context before onDraw call.
}
webglOverlayView.onStateUpdate = ({gl}) => {
// Do GL state setup or updates outside of the render loop.
}
webglOverlayView.onDraw = ({gl, transformer}) => {
// Render objects.
}
webglOverlayView.onContextLost = () => {
// Clean up pre-existing GL state.
}
webglOverlayView.onRemove = () => {
// Remove all intermediate objects.
}
webglOverlayView.setMap(map);
GL の状態をリセットする
WebGL オーバーレイ表示によって、基本地図の WebGL レンダリング コンテキストが公開されます。そのため、オブジェクトのレンダリングが完了したら、GL の状態を元の状態にリセットすることが非常に重要です。GL の状態のリセットに失敗すると、GL の状態の競合が生じる可能性が高まります。競合が生じると、指定した地図とオブジェクトのレンダリングを行うことができません。
GL の状態のリセットは通常、onDraw()
フックで処理します。たとえば、Three.js には、GL の状態への変更内容をすべて消去するヘルパー関数が用意されています。
webglOverlayView.onDraw = ({gl, transformer}) => {
// Specify an object to render.
renderer.render(scene, camera);
renderer.resetState();
}
地図やオブジェクトがレンダリングされない場合は、GL の状態がリセットされていない可能性があります。
ベクターマップ上のオブジェクトの位置は、緯度 / 経度の座標と高度を組み合わせて指定します。ただし、3D グラフィックは、ワールド空間、カメラ空間、画面空間のいずれかで指定します。こうしたよく使用される空間に、地図座標を簡単に変換するため、WebGL オーバーレイ表示では、onDraw()
フック内で coordinateTransformer.fromLatLngAltitude(latLngAltitude, rotationArr,
scalarArr)
ヘルパー関数を利用できます。この関数は以下を取得し、Float64Array
を返します。
latLngAltitude
: 緯度 / 経度 / 高度の座標(LatLngAltitude
または LatLngAltitudeLiteral
の形式)。
rotationArr
: 角度で指定されるオイラー回転角の Float32Array
。
scalarArr
: カーディナル軸に適用するスカラーの Float32Array
。
たとえば以下では、fromLatLngAltitude()
を使用して Three.js のカメラ射影行列を作成しています。
const camera = new THREE.PerspectiveCamera();
const matrix = coordinateTransformer.fromLatLngAltitude({
lat: mapOptions.center.lat,
lng: mapOptions.center.lng,
altitude: 120,
});
camera.projectionMatrix = new THREE.Matrix4().fromArray(matrix);
例
次の簡単な例では、一般的なオープンソースの WebGL ライブラリである Three.js を使用して、地図に 3D オブジェクトを配置しています。WebGL オーバーレイ表示を使用して、このページの最上部の例のような地図を作成する手順については、WebGL の高速化を取り入れた地図エクスペリエンスの構築に関する Codelab で詳細に解説しています。
const webglOverlayView = new google.maps.WebGLOverlayView();
let scene, renderer, camera, loader;
webglOverlayView.onAdd = () => {
// Set up the Three.js scene.
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera();
const ambientLight = new THREE.AmbientLight( 0xffffff, 0.75 ); // Soft white light.
scene.add(ambientLight);
// Load the 3D model with GLTF Loader from Three.js.
loader = new GLTFLoader();
loader.load("pin.gltf");
}
webglOverlayView.onContextRestored = ({gl}) => {
// Create the Three.js renderer, using the
// maps's WebGL rendering context.
renderer = new THREE.WebGLRenderer({
canvas: gl.canvas,
context: gl,
...gl.getContextAttributes(),
});
renderer.autoClear = false;
}
webglOverlayView.onDraw = ({gl, transformer}) => {
// Update camera matrix to ensure the model is georeferenced correctly on the map.
const matrix = transformer.fromLatLngAltitude({
lat: mapOptions.center.lat,
lng: mapOptions.center.lng,
altitude: 120,
});
camera.projectionMatrix = new THREE.Matrix4().fromArray(matrix);
// Request a redraw and render the scene.
webglOverlayView.requestRedraw();
renderer.render(scene, camera);
// Always reset the GL state.
renderer.resetState();
}
// Add the overlay to the map.
webglOverlayView.setMap(map);
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 UTC。"],[[["\u003cp\u003eWebGL Overlay View empowers you to seamlessly integrate 3D and 2D content into Google Maps using WebGL directly or via libraries like Three.js, leveraging the same rendering context for synchronized visuals and depth occlusion with buildings.\u003c/p\u003e\n"],["\u003cp\u003eIt's crucial to load the map with a vector map enabled map ID, ideally with tilt and rotation activated, to fully utilize 3D camera control and functionalities.\u003c/p\u003e\n"],["\u003cp\u003eWebGL Overlay View offers lifecycle hooks like \u003ccode\u003eonAdd\u003c/code\u003e, \u003ccode\u003eonContextRestored\u003c/code\u003e, \u003ccode\u003eonDraw\u003c/code\u003e, \u003ccode\u003eonContextLost\u003c/code\u003e, \u003ccode\u003eonStateUpdate\u003c/code\u003e, and \u003ccode\u003eonRemove\u003c/code\u003e, providing control over setup, rendering, and teardown of your overlay elements.\u003c/p\u003e\n"],["\u003cp\u003eRemember to reset the GL state after rendering using methods like \u003ccode\u003erenderer.resetState()\u003c/code\u003e (Three.js) to avoid rendering conflicts and ensure proper functionality.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003ecoordinateTransformer.fromLatLngAltitude()\u003c/code\u003e helper function within \u003ccode\u003eonDraw\u003c/code\u003e to transform latitude/longitude/altitude coordinates to world/camera/screen space for accurate object placement and georeferencing.\u003c/p\u003e\n"]]],[],null,["# WebGL Overlay View\n\n\u003cbr /\u003e\n\n\n[View Sample](/maps/documentation/javascript/examples/webgl/webgl-overlay-simple)\n\nWith WebGL Overlay View you can add content to your maps using WebGL directly,\nor popular Graphics libraries like Three.js. WebGL Overlay View provides direct\naccess to the same WebGL rendering context Google Maps Platform uses to render\nthe vector basemap. This use of a shared rendering context provides benefits\nsuch as depth occlusion with 3D building geometry, and the ability to sync 2D/3D\ncontent with basemap rendering. Objects rendered with the WebGL Overlay View can\nalso be tied to latitude/longitude coordinates, so they move when you drag,\nzoom, pan, or tilt the map.\n\nRequirements\n------------\n\nTo use WebGL Overlay View, you must load the map using a map ID with the vector\nmap enabled. We strongly recommend enabling tilt and rotation when you create\nthe map ID, to allow for full 3D camera control.\n[See the overview for details](/maps/documentation/javascript/webgl).\n\nAdd WebGL Overlay View\n----------------------\n\nTo add the overlay to your map, implement `google.maps.WebGLOverlayView`, then\npass it your map instance using `setMap`: \n\n // Create a map instance.\n const map = new google.maps.Map(mapDiv, mapOptions);\n\n // Create a WebGL Overlay View instance.\n const webglOverlayView = new google.maps.WebGLOverlayView();\n\n // Add the overlay to the map.\n webglOverlayView.setMap(map);\n\nLifecycle hooks\n---------------\n\nWebGL Overlay View provides a set of hooks that are called at various times in\nthe lifecycle of the WebGL rendering context of the vector basemap. These\nlifecycle hooks are where you setup, draw, and tear down anything you want\nrendered in the overlay.\n\n- **`onAdd()`** is called when the overlay is created. Use it to fetch or create intermediate data structures before the overlay is drawn that don't require immediate access to the WebGL rendering context.\n- **`onContextRestored({gl})`** is called once the rendering context is available. Use it to initialize or bind any WebGL state such as shaders, GL buffer objects, and so on. `onContextRestored()` takes a `WebGLStateOptions` instance, which has a single field:\n - `gl` is a handle to the `WebGLRenderingContext` used by the basemap.\n- **`onDraw({gl, transformer})`** renders the scene on the basemap. The parameters for `onDraw()` is a `WebGLDrawOptions` object, which has two fields:\n - `gl` is a handle to the `WebGLRenderingContext` used by the basemap.\n - `transformer` provides helper functions to transform from map coordinates to model-view-projection matrix, which can be used to translate map coordinates to world space, camera space, and screen space.\n- **`onContextLost()`** is called when the rendering context is lost for any reason, and is where you should clean up any pre-existing GL state, since it is no longer needed.\n- **`onStateUpdate({gl})`** updates the GL state outside of the render loop, and is invoked when `requestStateUpdate` is called. It takes a `WebGLStateOptions` instance, which has a single field:\n - `gl` is a handle to the `WebGLRenderingContext` used by the basemap.\n- **`onRemove()`** is called when the overlay is removed from the map with `WebGLOverlayView.setMap(null)`, and is where you should remove all intermediate objects.\n\nFor example, the following is a basic implementation of all lifecycle hooks: \n\n const webglOverlayView = new google.maps.WebGLOverlayView();\n\n webglOverlayView.onAdd = () =\u003e {\n // Do setup that does not require access to rendering context.\n }\n\n webglOverlayView.onContextRestored = ({gl}) =\u003e {\n // Do setup that requires access to rendering context before onDraw call.\n }\n\n webglOverlayView.onStateUpdate = ({gl}) =\u003e {\n // Do GL state setup or updates outside of the render loop.\n }\n\n webglOverlayView.onDraw = ({gl, transformer}) =\u003e {\n // Render objects.\n }\n\n webglOverlayView.onContextLost = () =\u003e {\n // Clean up pre-existing GL state.\n }\n\n webglOverlayView.onRemove = () =\u003e {\n // Remove all intermediate objects.\n }\n\n webglOverlayView.setMap(map);\n\nResetting GL State\n------------------\n\nWebGL Overlay View exposes the WebGL rendering context of the basemap. Because\nof this, it is extremely important that you reset the GL state to its original\nstate when you are done rendering objects. Failure to reset the GL state is\nlikely to result in GL state conflicts, which will cause rendering of both the\nmap and any objects you specify to fail.\n\nResetting the GL state is normally handled in the `onDraw()` hook. For example,\nThree.js provides a helper function that clears any changes to the GL state: \n\n webglOverlayView.onDraw = ({gl, transformer}) =\u003e {\n // Specify an object to render.\n renderer.render(scene, camera);\n renderer.resetState();\n }\n\nIf the map or your objects fail to render, it is very likely that the GL state\nhas not been reset.\n\nCoordinate Transformations\n--------------------------\n\nThe position of an object on the vector map is specified by providing a\ncombination of latitude and longitude coordinates, as well as altitude. 3D\ngraphics, however, are specified in world space, camera space, or screen space.\nTo make it easier to transform map coordinates to these more commonly used\nspaces, WebGL Overlay View provides the\n`coordinateTransformer.fromLatLngAltitude(latLngAltitude, rotationArr,\nscalarArr)` helper function in the `onDraw()` hook that takes the following and\nreturns a `Float64Array`:\n\n- `latLngAltitude`: Latitude/longitude/altitude coordinates either as a `LatLngAltitude` or `LatLngAltitudeLiteral`.\n- `rotationArr`: `Float32Array` of euler rotation angles specified in degrees.\n- `scalarArr`: `Float32Array` of scalars to apply to the cardinal axis.\n\nFor example, the following uses `fromLatLngAltitude()` to create a camera\nprojection matrix in Three.js: \n\n const camera = new THREE.PerspectiveCamera();\n const matrix = coordinateTransformer.fromLatLngAltitude({\n lat: mapOptions.center.lat,\n lng: mapOptions.center.lng,\n altitude: 120,\n });\n camera.projectionMatrix = new THREE.Matrix4().fromArray(matrix);\n\nExample\n-------\n\nThe following is a simple example of using [Three.js](https://threejs.org), a\npopular, open source WebGL library, to place a 3D object on the map. For a\ncomplete walkthrough of using WebGL Overlay View to build the example you see\nrunning at the top of this page, try the\n[Building WebGL-accelerated Map Experiences codelab](http://goo.gle/maps-platform-webgl-codelab). \n\n const webglOverlayView = new google.maps.WebGLOverlayView();\n let scene, renderer, camera, loader;\n\n webglOverlayView.onAdd = () =\u003e {\n // Set up the Three.js scene.\n scene = new THREE.Scene();\n camera = new THREE.PerspectiveCamera();\n const ambientLight = new THREE.AmbientLight( 0xffffff, 0.75 ); // Soft white light.\n scene.add(ambientLight);\n\n // Load the 3D model with GLTF Loader from Three.js.\n loader = new GLTFLoader();\n loader.load(\"pin.gltf\");\n }\n\n webglOverlayView.onContextRestored = ({gl}) =\u003e {\n // Create the Three.js renderer, using the\n // maps's WebGL rendering context.\n renderer = new THREE.WebGLRenderer({\n canvas: gl.canvas,\n context: gl,\n ...gl.getContextAttributes(),\n });\n renderer.autoClear = false;\n }\n\n webglOverlayView.onDraw = ({gl, transformer}) =\u003e {\n // Update camera matrix to ensure the model is georeferenced correctly on the map.\n const matrix = transformer.fromLatLngAltitude({\n lat: mapOptions.center.lat,\n lng: mapOptions.center.lng,\n altitude: 120,\n });\n camera.projectionMatrix = new THREE.Matrix4().fromArray(matrix);\n\n // Request a redraw and render the scene.\n webglOverlayView.requestRedraw();\n renderer.render(scene, camera);\n\n // Always reset the GL state.\n renderer.resetState();\n }\n\n // Add the overlay to the map.\n webglOverlayView.setMap(map);"]]