Skip to content

🎥 Make videos programmatically with React

`@remotion/effects`: Add sphere projection and globe demo

Jonny Burger1e4b5bcc83e9
diff --git a/packages/docs/components/demos/index.tsx b/packages/docs/components/demos/index.tsx
index 97f4bdd8f5..3edef3d80c 100644
--- a/packages/docs/components/demos/index.tsx
+++ b/packages/docs/components/demos/index.tsx
@@ -21,6 +21,7 @@ import {
crossZoomPresentationDemo,
cubePresentationDemo,
cylinderTextDemo,
+ sphereGlobeDemo,
customPresentationDemo,
customTimingDemo,
dissolvePresentationDemo,
@@ -155,6 +156,7 @@ const demos: DemoType[] = [
irisPresentationDemo,
cubePresentationDemo,
cylinderTextDemo,
+ sphereGlobeDemo,
animationMathDemo,
roundedTextBoxDemo,
springDemo,
diff --git a/packages/docs/components/demos/types.ts b/packages/docs/components/demos/types.ts
index f143a80a54..ba0ebd1e87 100644
--- a/packages/docs/components/demos/types.ts
+++ b/packages/docs/components/demos/types.ts
@@ -1,4 +1,5 @@
import type {LogLevel} from 'remotion';
+import {SpinningGlobeDemo} from '../effects/effects-sphere-preview';
import {
ClockWipeDemo,
CubeDemo,
@@ -1757,3 +1758,16 @@ export const cylinderTextDemo: DemoType = {
logLevel: 'info',
options: [],
};
+
+export const sphereGlobeDemo: DemoType = {
+ comp: SpinningGlobeDemo,
+ compWidth: 1280,
+ compHeight: 720,
+ durationInFrames: 240,
+ fps: 30,
+ id: 'sphere-globe',
+ autoPlay: true,
+ controls: true,
+ logLevel: 'info',
+ options: [],
+};
diff --git a/packages/docs/components/effects-demos/registry.ts b/packages/docs/components/effects-demos/registry.ts
index d6b18c8a97..d84623621f 100644
--- a/packages/docs/components/effects-demos/registry.ts
+++ b/packages/docs/components/effects-demos/registry.ts
@@ -56,6 +56,7 @@ import {shine} from '@remotion/effects/shine';
import {shrinkwrap} from '@remotion/effects/shrinkwrap';
import {skew} from '@remotion/effects/skew';
import {speckle} from '@remotion/effects/speckle';
+import {sphere} from '@remotion/effects/sphere';
import {starburst} from '@remotion/effects/starburst';
import {tear} from '@remotion/effects/tear';
import {thermalVision} from '@remotion/effects/thermal-vision';
@@ -152,6 +153,7 @@ import {
} from '../effects/effects-shrinkwrap-preview';
import {EffectsSkewPreview} from '../effects/effects-skew-preview';
import {EffectsSpecklePreview} from '../effects/effects-speckle-preview';
+import {EffectsSpherePreview} from '../effects/effects-sphere-preview';
import {
EffectsStarburstPreview,
STARBURST_PREVIEW_PARAMS,
@@ -198,6 +200,14 @@ const shrinkwrapDemoSchema = {
const cylinderDemoSchema = cylinder().definition.schema;
export const effectsDemos: EffectsDemoType[] = [
+ {
+ ...defaults,
+ id: 'effects-sphere',
+ effectName: 'sphere',
+ effectImportPath: '@remotion/effects/sphere',
+ comp: EffectsSpherePreview,
+ schema: sphere().definition.schema,
+ },
{
...defaults,
id: 'effects-cylinder',
diff --git a/packages/docs/components/effects/effects-sphere-preview.tsx b/packages/docs/components/effects/effects-sphere-preview.tsx
new file mode 100644
index 0000000000..158e2b6e1a
--- /dev/null
+++ b/packages/docs/components/effects/effects-sphere-preview.tsx
@@ -0,0 +1,53 @@
+import {sphere, type SphereRender} from '@remotion/effects/sphere';
+import React from 'react';
+import {
+ AbsoluteFill,
+ CanvasImage,
+ useCurrentFrame,
+ useVideoConfig,
+} from 'remotion';
+
+// Natural Earth II, 1:50m, shaded relief and water; resized to 4096 × 2048.
+// Source: https://www.naturalearthdata.com/downloads/50m-raster-data/50m-natural-earth-2/
+// Public domain; attribution is explicitly unnecessary:
+// https://www.naturalearthdata.com/about/terms-of-use/
+export const SPHERE_GLOBE_TEXTURE_SRC =
+ 'https://remotion.media/natural-earth-2-4096-a45c3f8c73ed.jpg';
+
+export const EffectsSpherePreview: React.FC<{
+ readonly radius: number;
+ readonly rotationX: number;
+ readonly rotationY: number;
+ readonly rotationZ: number;
+ readonly position: readonly [number, number];
+ readonly render: SphereRender;
+ readonly shading: number;
+}> = (params) => {
+ return (
+ <AbsoluteFill style={{backgroundColor: 'black'}}>
+ <CanvasImage
+ src={SPHERE_GLOBE_TEXTURE_SRC}
+ width={1280}
+ height={720}
+ fit="fill"
+ effects={[sphere(params)]}
+ />
+ </AbsoluteFill>
+ );
+};
+
+export const SpinningGlobeDemo: React.FC = () => {
+ const frame = useCurrentFrame();
+ const {durationInFrames} = useVideoConfig();
+ return (
+ <EffectsSpherePreview
+ radius={300}
+ rotationX={0}
+ rotationY={(frame / durationInFrames) * 360}
+ rotationZ={-23.4}
+ position={[0.5, 0.5]}
+ render="both"
+ shading={0.35}
+ />
+ );
+};
diff --git a/packages/docs/docs/effects/sphere.mdx b/packages/docs/docs/effects/sphere.mdx
new file mode 100644
index 0000000000..3964b7187d
--- /dev/null
+++ b/packages/docs/docs/effects/sphere.mdx
@@ -0,0 +1,101 @@
+---
+image: /generated/articles-docs-effects-sphere.png
+slug: /effects/sphere
+title: sphere()
+sidebar_label: sphere()
+crumb: '@remotion/effects'
+---
+
+# sphere()<AvailableFrom v="4.0.537" />
+
+_Part of the [`@remotion/effects`](/docs/effects/api) package._
+
+Wraps canvas content around a sphere using an orthographic projection. Rotate it in three dimensions to create globes or spherical text and image effects.
+
+## Spinning globe
+
+<Demo type="sphere-globe" />
+
+## Preview
+
+<EffectsDemo type="effects-sphere" />
+
+## Example
+
+```tsx twoslash title="MyComp.tsx"
+import React from 'react';
+import {CanvasImage, useCurrentFrame} from 'remotion';
+import {sphere} from '@remotion/effects/sphere';
+
+export const MyComp: React.FC = () => {
+ const frame = useCurrentFrame();
+
+ return (
+ <CanvasImage
+ src="https://remotion.media/natural-earth-2-4096-a45c3f8c73ed.jpg"
+ width={1280}
+ height={720}
+ fit="fill"
+ effects={[
+ sphere({
+ radius: 300,
+ rotationY: frame * 1.5,
+ rotationZ: -23.4,
+ }),
+ ]}
+ />
+ );
+};
+```
+
+## Texture mapping
+
+The entire source width maps to 360° of longitude, and its height maps from the north pole to the south pole. Use an equirectangular image for a globe.
+
+Set [`<CanvasImage>`](/docs/canvasimage)'s `fit` to `'fill'` to preserve the full texture when the canvas and image have different aspect ratios. `'cover'` crops the texture before it reaches the effect.
+
+## API
+
+### `radius?`
+
+Sphere radius in pixels. Must be greater than `0`. Defaults to `250`.
+Pixels outside the sphere are transparent.
+
+### `rotationX?`
+
+Rotation around the X axis, in degrees. Defaults to `0`.
+
+### `rotationY?`
+
+Rotation around the Y axis, in degrees. Defaults to `0`. Animate from `0` to `360` to complete one revolution.
+
+### `rotationZ?`
+
+Rotation around the Z axis, in degrees. Defaults to `0`. Axis rotations are applied in X, Y, then Z order.
+
+### `position?`
+
+Center as `[x, y]` fractions of the canvas dimensions. Defaults to `[0.5, 0.5]`. Values outside the `0`–`1` range move the center beyond the canvas.
+
+### `render?`
+
+Which faces to render: `'both'`, `'front'`, or `'back'`. Defaults to `'both'`. The front face is composited over the back, which shows through transparent parts of the source.
+
+### `shading?`
+
+Darkening toward the sphere's silhouette, from `0` to `1`. Defaults to `0.35`. Set to `0` to preserve source colors.
+
+### `disabled?`
+
+When `true`, skips the effect. Defaults to `false`.
+
+## Requirements
+
+`sphere()` requires WebGPU. See [Using WebGL and WebGPU during renders](/docs/webgl) for rendering configuration.
+
+## See also
+
+- [`cylinder()`](/docs/effects/cylinder)
+- [`fisheye()`](/docs/effects/fisheye)
+- [`@remotion/effects`](/docs/effects/api)
+- [Source code](https://github.com/remotion-dev/remotion/blob/main/packages/effects/src/sphere/index.ts)
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 9d22ac40f6..34ea585c6e 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -340,6 +340,7 @@ const sidebars: SidebarsConfig = {
'effects/shrinkwrap',
'effects/skew',
'effects/speckle',
+ 'effects/sphere',
'effects/starburst',
'effects/tear',
'effects/thermal-vision',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 6c25dd09c2..f1eff21770 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -3915,6 +3915,15 @@ export const articles = [
noAi: false,
slug: 'effects/speckle',
},
+ {
+ id: 'effects/sphere',
+ title: 'sphere()',
+ relativePath: 'docs/effects/sphere.mdx',
+ compId: 'articles-docs-effects-sphere',
+ crumb: '@remotion/effects',
+ noAi: false,
+ slug: 'effects/sphere',
+ },
{
id: 'effects/starburst',
title: 'starburst()',
diff --git a/packages/docs/src/remotion/Root.tsx b/packages/docs/src/remotion/Root.tsx
index b117558ea0..748e75468b 100644
--- a/packages/docs/src/remotion/Root.tsx
+++ b/packages/docs/src/remotion/Root.tsx
@@ -95,6 +95,10 @@ import {
} from '../../components/effects/effects-shrinkwrap-preview';
import {EffectsSkewPreview} from '../../components/effects/effects-skew-preview';
import {EffectsSpecklePreview} from '../../components/effects/effects-speckle-preview';
+import {
+ EffectsSpherePreview,
+ SpinningGlobeDemo,
+} from '../../components/effects/effects-sphere-preview';
import {
EffectsStarburstPreview,
STARBURST_PREVIEW_PARAMS,
@@ -228,6 +232,29 @@ export const RemotionRoot: React.FC = () => {
})}
</Folder>
<Folder name="effect-previews">
+ <Still
+ id="effects-sphere-preview"
+ component={EffectsSpherePreview}
+ width={1280}
+ height={720}
+ defaultProps={{
+ radius: 300,
+ rotationX: 0,
+ rotationY: -20,
+ rotationZ: -23.4,
+ position: [0.5, 0.5],
+ render: 'both',
+ shading: 0.35,
+ }}
+ />
+ <Composition
+ id="sphere-globe"
+ component={SpinningGlobeDemo}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={240}
+ />
<Composition
id="cylinder-text"
component={CylinderTextDemo}
diff --git a/packages/docs/static/generated/articles-docs-effects-sphere.png b/packages/docs/static/generated/articles-docs-effects-sphere.png
new file mode 100644
index 0000000000..88bd75161b
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-effects-sphere.png differ
diff --git a/packages/docs/static/img/effects-sphere-preview.png b/packages/docs/static/img/effects-sphere-preview.png
new file mode 100644
index 0000000000..8052b28beb
Binary files /dev/null and b/packages/docs/static/img/effects-sphere-preview.png differ
diff --git a/packages/effects/bundle.ts b/packages/effects/bundle.ts
index 788d2821a8..66bce9b5b4 100644
--- a/packages/effects/bundle.ts
+++ b/packages/effects/bundle.ts
@@ -65,6 +65,7 @@ const effectEntrypoints = [
'src/shrinkwrap.ts',
'src/skew.ts',
'src/speckle.ts',
+ 'src/sphere.ts',
'src/starburst.ts',
'src/tear.ts',
'src/thermal-vision.ts',
diff --git a/packages/effects/package.json b/packages/effects/package.json
index 33330a105c..94993998e9 100644
--- a/packages/effects/package.json
+++ b/packages/effects/package.json
@@ -324,6 +324,11 @@
"module": "./dist/esm/speckle.mjs",
"import": "./dist/esm/speckle.mjs"
},
+ "./sphere": {
+ "types": "./dist/sphere.d.ts",
+ "module": "./dist/esm/sphere.mjs",
+ "import": "./dist/esm/sphere.mjs"
+ },
"./starburst": {
"types": "./dist/starburst.d.ts",
"module": "./dist/esm/starburst.mjs",
@@ -582,6 +587,9 @@
"speckle": [
"dist/speckle.d.ts"
],
+ "sphere": [
+ "dist/sphere.d.ts"
+ ],
"starburst": [
"dist/starburst.d.ts"
],
diff --git a/packages/effects/src/cylinder/index.ts b/packages/effects/src/cylinder/index.ts
index fb2a4f961c..b18e7f0568 100644
--- a/packages/effects/src/cylinder/index.ts
+++ b/packages/effects/src/cylinder/index.ts
@@ -5,7 +5,13 @@ import {
validateUnitInterval,
} from '../color-utils.js';
import {assertEffectParamsObject} from '../validate-effect-param.js';
-import {applyCylinder, cleanupCylinder, type CylinderState} from './runtime.js';
+import {
+ applyWebGpuProjection,
+ cleanupWebGpuProjection,
+ setupWebGpuProjection,
+ type WebGpuProjectionState,
+} from '../webgpu-projection-runtime.js';
+import {CYLINDER_SHADER} from './shader.js';
const DEFAULT_RADIUS = 250;
const DEFAULT_ROTATION = 0;
@@ -95,7 +101,10 @@ const resolve = (params: CylinderParams) => ({
shading: params.shading ?? DEFAULT_SHADING,
});
-export const cylinder = Internals.createEffect<CylinderParams, CylinderState>({
+export const cylinder = Internals.createEffect<
+ CylinderParams,
+ WebGpuProjectionState
+>({
type: 'remotion/cylinder',
label: 'cylinder()',
documentationLink: 'https://www.remotion.dev/docs/effects/cylinder',
@@ -143,27 +152,36 @@ export const cylinder = Internals.createEffect<CylinderParams, CylinderState>({
const r = resolve(params);
return `cylinder-${r.radius}-${r.rotation}-${r.rotationX}-${r.rotationY}-${r.rotationZ}-${r.position.join(',')}-${r.render}-${r.shading}`;
},
- setup: (target) => {
- const context = target.getContext('webgpu');
- if (!context) {
- throw new Error('WebGPU is not available for the cylinder effect');
- }
-
- return {context: context as GPUCanvasContext, resources: null};
- },
+ setup: (target) => setupWebGpuProjection(target, 'cylinder'),
apply: ({source, width, height, params, state, gpuDevice}) => {
if (!gpuDevice) {
throw new Error('A WebGPU device is required for the cylinder effect');
}
- applyCylinder({
+ const r = resolve(params);
+ const radians = Math.PI / 180;
+ applyWebGpuProjection({
source,
width,
height,
state,
device: gpuDevice as GPUDevice,
- ...resolve(params),
+ shader: CYLINDER_SHADER,
+ uniformData: new Float32Array([
+ width,
+ height,
+ r.position[0] * width,
+ r.position[1] * height,
+ r.radius,
+ (r.rotation % 360) * radians,
+ r.shading,
+ r.render === 'front' ? 1 : r.render === 'back' ? 2 : 0,
+ (r.rotationX % 360) * radians,
+ (r.rotationY % 360) * radians,
+ (r.rotationZ % 360) * radians,
+ 0,
+ ]),
});
},
- cleanup: cleanupCylinder,
+ cleanup: cleanupWebGpuProjection,
});
diff --git a/packages/effects/src/sphere.ts b/packages/effects/src/sphere.ts
new file mode 100644
index 0000000000..41263c6a20
--- /dev/null
+++ b/packages/effects/src/sphere.ts
@@ -0,0 +1 @@
+export {sphere, type SphereParams, type SphereRender} from './sphere/index.js';
diff --git a/packages/effects/src/sphere/index.ts b/packages/effects/src/sphere/index.ts
new file mode 100644
index 0000000000..ba305717b4
--- /dev/null
+++ b/packages/effects/src/sphere/index.ts
@@ -0,0 +1,178 @@
+import type {InteractivitySchema} from 'remotion';
+import {Internals} from 'remotion';
+import {
+ assertOptionalFiniteNumber,
+ validateUnitInterval,
+} from '../color-utils.js';
+import {assertEffectParamsObject} from '../validate-effect-param.js';
+import {
+ applyWebGpuProjection,
+ cleanupWebGpuProjection,
+ setupWebGpuProjection,
+ type WebGpuProjectionState,
+} from '../webgpu-projection-runtime.js';
+import {SPHERE_SHADER} from './shader.js';
+
+const DEFAULT_RADIUS = 250;
+const DEFAULT_ROTATION = 0;
+const DEFAULT_POSITION = [0.5, 0.5] as const;
+const DEFAULT_RENDER = 'both';
+const DEFAULT_SHADING = 0.35;
+
+const sphereSchema = {
+ radius: {
+ type: 'number',
+ min: 1,
+ max: 1000,
+ step: 1,
+ default: DEFAULT_RADIUS,
+ description: 'Radius',
+ hiddenFromList: false,
+ },
+ rotationX: {
+ type: 'rotation-degrees',
+ default: DEFAULT_ROTATION,
+ description: 'X rotation',
+ },
+ rotationY: {
+ type: 'rotation-degrees',
+ default: DEFAULT_ROTATION,
+ description: 'Y rotation',
+ },
+ rotationZ: {
+ type: 'rotation-degrees',
+ default: DEFAULT_ROTATION,
+ description: 'Z rotation',
+ },
+ position: {
+ type: 'uv-coordinate',
+ default: DEFAULT_POSITION,
+ description: 'Position',
+ },
+ render: {
+ type: 'enum',
+ variants: {both: {}, front: {}, back: {}},
+ default: DEFAULT_RENDER,
+ description: 'Faces',
+ },
+ shading: {
+ type: 'number',
+ min: 0,
+ max: 1,
+ step: 0.01,
+ default: DEFAULT_SHADING,
+ description: 'Shading',
+ hiddenFromList: false,
+ },
+} as const satisfies InteractivitySchema;
+
+export type SphereRender = 'both' | 'front' | 'back';
+
+export type SphereParams = {
+ /** Sphere radius in pixels. Defaults to 250. */
+ readonly radius?: number;
+ /** Sphere orientation in degrees, applied in X, Y, Z order. */
+ readonly rotationX?: number;
+ readonly rotationY?: number;
+ readonly rotationZ?: number;
+ /** Center as fractions of the canvas width and height. Defaults to [0.5, 0.5]. */
+ readonly position?: readonly [number, number];
+ /** Which faces to show. Defaults to 'both'. */
+ readonly render?: SphereRender;
+ /** Darkening at the silhouette, from 0 to 1. Defaults to 0.35. */
+ readonly shading?: number;
+};
+
+const resolve = (params: SphereParams) => ({
+ radius: params.radius ?? DEFAULT_RADIUS,
+ rotationX: params.rotationX ?? DEFAULT_ROTATION,
+ rotationY: params.rotationY ?? DEFAULT_ROTATION,
+ rotationZ: params.rotationZ ?? DEFAULT_ROTATION,
+ position: params.position ?? DEFAULT_POSITION,
+ render: params.render ?? DEFAULT_RENDER,
+ shading: params.shading ?? DEFAULT_SHADING,
+});
+
+export const sphere = Internals.createEffect<
+ SphereParams,
+ WebGpuProjectionState
+>({
+ type: 'remotion/sphere',
+ label: 'sphere()',
+ documentationLink: 'https://www.remotion.dev/docs/effects/sphere',
+ backend: 'webgpu',
+ schema: sphereSchema,
+ validateParams: (params) => {
+ assertEffectParamsObject(params, 'Sphere');
+ for (const key of [
+ 'radius',
+ 'rotationX',
+ 'rotationY',
+ 'rotationZ',
+ 'shading',
+ ] as const) {
+ assertOptionalFiniteNumber(params[key], key);
+ }
+
+ const r = resolve(params);
+ if (r.radius <= 0) {
+ throw new TypeError('"radius" must be greater than 0');
+ }
+
+ validateUnitInterval(r.shading, 'shading');
+ if (
+ params.position !== undefined &&
+ (!Array.isArray(params.position) ||
+ params.position.length !== 2 ||
+ !Number.isFinite(params.position[0]) ||
+ !Number.isFinite(params.position[1]))
+ ) {
+ throw new TypeError('"position" must contain two finite numbers');
+ }
+
+ if (
+ params.render !== undefined &&
+ params.render !== 'both' &&
+ params.render !== 'front' &&
+ params.render !== 'back'
+ ) {
+ throw new TypeError('"render" must be "both", "front", or "back"');
+ }
+ },
+ calculateKey: (params) => {
+ const r = resolve(params);
+ return `sphere-${r.radius}-${r.rotationX}-${r.rotationY}-${r.rotationZ}-${r.position.join(',')}-${r.render}-${r.shading}`;
+ },
+ setup: (target) => setupWebGpuProjection(target, 'sphere'),
+ apply: ({source, width, height, params, state, gpuDevice}) => {
+ if (!gpuDevice) {
+ throw new Error('A WebGPU device is required for the sphere effect');
+ }
+
+ const r = resolve(params);
+ const radians = Math.PI / 180;
+ applyWebGpuProjection({
+ source,
+ width,
+ height,
+ state,
+ device: gpuDevice as GPUDevice,
+ shader: SPHERE_SHADER,
+ uniformData: new Float32Array([
+ width,
+ height,
+ r.position[0] * width,
+ r.position[1] * height,
+ r.radius,
+ 0,
+ r.shading,
+ r.render === 'front' ? 1 : r.render === 'back' ? 2 : 0,
+ (r.rotationX % 360) * radians,
+ (r.rotationY % 360) * radians,
+ (r.rotationZ % 360) * radians,
+ 0,
+ ]),
+ });
+ },
+ cleanup: cleanupWebGpuProjection,
+});
diff --git a/packages/effects/src/sphere/shader.ts b/packages/effects/src/sphere/shader.ts
new file mode 100644
index 0000000000..9d0c1d8e25
--- /dev/null
+++ b/packages/effects/src/sphere/shader.ts
@@ -0,0 +1,47 @@
+export const SPHERE_SHADER = /* wgsl */ `
+struct Params {
+ resolution: vec2f,
+ center: vec2f,
+ radius: f32,
+ padding: f32,
+ shading: f32,
+ faces: f32,
+ rotation: vec4f,
+}
+@group(0) @binding(0) var source: texture_2d<f32>;
+@group(0) @binding(1) var sourceSampler: sampler;
+@group(0) @binding(2) var<uniform> params: Params;
+
+@vertex fn vertex(@builtin(vertex_index) index: u32) -> @builtin(position) vec4f {
+ let positions = array<vec2f, 3>(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3));
+ return vec4f(positions[index], 0, 1);
+}
+
+fn sampleFace(normal: vec3f, inverse: mat3x3f) -> vec4f {
+ let local = inverse * normal;
+ // Longitude wraps at the seam; latitude spans the complete image height.
+ let uv = vec2f(fract(atan2(local.x, local.z) / 6.28318530718 + 0.5), asin(clamp(local.y, -1.0, 1.0)) / 3.14159265359 + 0.5);
+ let color = textureSampleLevel(source, sourceSampler, uv, 0);
+ let shade = mix(1.0, abs(normal.z), params.shading);
+ return vec4f(color.rgb * shade, color.a);
+}
+
+@fragment fn fragment(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ let p = (position.xy - params.center) / params.radius;
+ let distanceSquared = dot(p, p);
+ let coverage = 1.0 - smoothstep(1.0 - fwidth(distanceSquared), 1.0, distanceSquared);
+ if (distanceSquared >= 1.0) { return vec4f(0); }
+ let z = sqrt(1.0 - distanceSquared);
+ let s = sin(params.rotation.xyz);
+ let c = cos(params.rotation.xyz);
+ let rx = mat3x3f(vec3f(1, 0, 0), vec3f(0, c.x, s.x), vec3f(0, -s.x, c.x));
+ let ry = mat3x3f(vec3f(c.y, 0, -s.y), vec3f(0, 1, 0), vec3f(s.y, 0, c.y));
+ let rz = mat3x3f(vec3f(c.z, s.z, 0), vec3f(-s.z, c.z, 0), vec3f(0, 0, 1));
+ let inverse = transpose(rz * ry * rx);
+ let front = sampleFace(vec3f(p, z), inverse);
+ let back = sampleFace(vec3f(p, -z), inverse);
+ if (params.faces == 1) { return front * coverage; }
+ if (params.faces == 2) { return back * coverage; }
+ return (front + back * (1.0 - front.a)) * coverage;
+}
+`;
diff --git a/packages/effects/src/cylinder/runtime.ts b/packages/effects/src/webgpu-projection-runtime.ts
similarity index 71%
rename from packages/effects/src/cylinder/runtime.ts
rename to packages/effects/src/webgpu-projection-runtime.ts
index e27de7caee..ffaaf50d6d 100644
--- a/packages/effects/src/cylinder/runtime.ts
+++ b/packages/effects/src/webgpu-projection-runtime.ts
@@ -1,7 +1,4 @@
-import type {CylinderRender} from './index.js';
-import {CYLINDER_SHADER} from './shader.js';
-
-export type CylinderState = {
+export type WebGpuProjectionState = {
context: GPUCanvasContext;
resources: {
device: GPUDevice;
@@ -15,41 +12,41 @@ export type CylinderState = {
} | null;
};
-export const cleanupCylinder = (state: CylinderState): void => {
+export const cleanupWebGpuProjection = (state: WebGpuProjectionState): void => {
state.resources?.texture.destroy();
state.resources?.uniforms.destroy();
state.resources = null;
state.context.unconfigure();
};
-export const applyCylinder = ({
+export const setupWebGpuProjection = (
+ target: HTMLCanvasElement,
+ label: string,
+): WebGpuProjectionState => {
+ const context = target.getContext('webgpu');
+ if (!context) {
+ throw new Error(`WebGPU is not available for the ${label} effect`);
+ }
+
+ return {context: context as GPUCanvasContext, resources: null};
+};
+
+export const applyWebGpuProjection = ({
state,
source,
width,
height,
device,
- radius,
- rotation,
- rotationX,
- rotationY,
- rotationZ,
- position,
- render,
- shading,
+ shader,
+ uniformData,
}: {
- state: CylinderState;
+ state: WebGpuProjectionState;
source: CanvasImageSource;
width: number;
height: number;
device: GPUDevice;
- radius: number;
- rotation: number;
- rotationX: number;
- rotationY: number;
- rotationZ: number;
- position: readonly [number, number];
- render: CylinderRender;
- shading: number;
+ shader: string;
+ uniformData: Float32Array<ArrayBuffer>;
}): void => {
if (
state.resources &&
@@ -57,13 +54,13 @@ export const applyCylinder = ({
state.resources.width !== width ||
state.resources.height !== height)
) {
- cleanupCylinder(state);
+ cleanupWebGpuProjection(state);
}
if (!state.resources) {
const format = navigator.gpu.getPreferredCanvasFormat();
state.context.configure({device, format, alphaMode: 'premultiplied'});
- const module = device.createShaderModule({code: CYLINDER_SHADER});
+ const module = device.createShaderModule({code: shader});
const pipeline = device.createRenderPipeline({
layout: 'auto',
vertex: {module, entryPoint: 'vertex'},
@@ -71,7 +68,7 @@ export const applyCylinder = ({
primitive: {topology: 'triangle-list'},
});
const uniforms = device.createBuffer({
- size: 48,
+ size: uniformData.byteLength,
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const sampler = device.createSampler({
@@ -113,25 +110,7 @@ export const applyCylinder = ({
{texture: resources.texture, premultipliedAlpha: true},
[width, height],
);
- const radians = Math.PI / 180;
- device.queue.writeBuffer(
- resources.uniforms,
- 0,
- new Float32Array([
- width,
- height,
- position[0] * width,
- position[1] * height,
- radius,
- (rotation % 360) * radians,
- shading,
- render === 'front' ? 1 : render === 'back' ? 2 : 0,
- (rotationX % 360) * radians,
- (rotationY % 360) * radians,
- (rotationZ % 360) * radians,
- 0,
- ]),
- );
+ device.queue.writeBuffer(resources.uniforms, 0, uniformData);
const encoder = device.createCommandEncoder();
const pass = encoder.beginRenderPass({
colorAttachments: [
diff --git a/packages/skills/skills/remotion-markup/effects.md b/packages/skills/skills/remotion-markup/effects.md
index e478be84c4..ee3b8a86b5 100644
--- a/packages/skills/skills/remotion-markup/effects.md
+++ b/packages/skills/skills/remotion-markup/effects.md
@@ -52,7 +52,7 @@ import {blur} from '@remotion/effects/blur';
Use the effect docs for exact props and imports. Most `@remotion/effects` imports use `@remotion/effects/<effect-slug>`; `uvTranslate()` and `xyTranslate()` use `@remotion/effects/translate`.
-Most effects use WebGL2; `cylinder()` uses WebGPU. During renders, enable WebGL and WebGPU with:
+Most effects use WebGL2; `cylinder()` and `sphere()` use WebGPU. During renders, enable WebGL and WebGPU with:
```ts
import {Config} from '@remotion/cli/config';
diff --git a/packages/studio-shared/src/effect-catalog.ts b/packages/studio-shared/src/effect-catalog.ts
index 888db713c6..eb241654e7 100644
--- a/packages/studio-shared/src/effect-catalog.ts
+++ b/packages/studio-shared/src/effect-catalog.ts
@@ -530,6 +530,17 @@ export const EFFECT_CATALOG: readonly EffectCatalogItem[] = [
},
},
},
+ {
+ id: 'effects-sphere',
+ category: 'Distort',
+ label: 'sphere()',
+ description: 'Wrap content around a rotating sphere',
+ effect: {
+ name: 'sphere',
+ importPath: '@remotion/effects/sphere',
+ config: {},
+ },
+ },
{
id: 'effects-cylinder',
category: 'Distort',