Skip to content

Commit 4b28b2b

Browse files
added gradients and background image
1 parent c807c03 commit 4b28b2b

File tree

3 files changed

+415
-11
lines changed

3 files changed

+415
-11
lines changed

packages/dev/babylonjs/lib/api/bitbybit/babylon/scene.ts

Lines changed: 155 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { Context } from "../../context";
32
import * as BABYLON from "@babylonjs/core";
43
import * as GUI from "@babylonjs/gui";
@@ -32,16 +31,6 @@ export class BabylonScene {
3231
return this.context.scene = inputs.scene;
3332
}
3433

35-
/**
36-
* Changes the scene background colour for 3D space
37-
* @param inputs Describes the colour of the scene background
38-
* @group environment
39-
* @shortname colour
40-
*/
41-
backgroundColour(inputs: Inputs.BabylonScene.SceneBackgroundColourDto): void {
42-
this.context.scene.clearColor = BABYLON.Color4.FromColor3(BABYLON.Color3.FromHexString(inputs.colour));
43-
}
44-
4534
/**
4635
* Activate camera by overwriting currently active camera
4736
* @param inputs Activates the camera
@@ -447,6 +436,161 @@ export class BabylonScene {
447436
this.context.scene.enablePhysics(new BABYLON.Vector3(inputs.vector[0], inputs.vector[1], inputs.vector[2]), this.context.havokPlugin);
448437
}
449438

439+
/**
440+
* Changes the scene background to a css background image for 3D space
441+
* @param inputs Describes the css of the scene background or image
442+
* @group background
443+
* @shortname css background image
444+
*/
445+
canvasCSSBackgroundImage(inputs: Inputs.BabylonScene.SceneCanvasCSSBackgroundImageDto): { backgroundImage: string } {
446+
this.context.scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
447+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
448+
const styleObject = { backgroundImage: inputs.cssBackgroundImage };
449+
if (canvas) {
450+
canvas.style.backgroundImage = inputs.cssBackgroundImage;
451+
}
452+
return styleObject;
453+
}
454+
455+
/**
456+
* Creates a two-color linear gradient background for 3D space
457+
* @param inputs Describes the two-color linear gradient parameters
458+
* @group background
459+
* @shortname two color linear gradient
460+
*/
461+
twoColorLinearGradient(inputs: Inputs.BabylonScene.SceneTwoColorLinearGradientDto): { backgroundImage: string } {
462+
this.context.scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
463+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
464+
const gradient = `linear-gradient(${inputs.direction}, ${inputs.colorFrom} ${inputs.stopFrom}%, ${inputs.colorTo} ${inputs.stopTo}%)`;
465+
const styleObject = { backgroundImage: gradient };
466+
if (canvas) {
467+
canvas.style.backgroundImage = gradient;
468+
}
469+
return styleObject;
470+
}
471+
472+
/**
473+
* Creates a two-color radial gradient background for 3D space
474+
* @param inputs Describes the two-color radial gradient parameters
475+
* @group background
476+
* @shortname two color radial gradient
477+
*/
478+
twoColorRadialGradient(inputs: Inputs.BabylonScene.SceneTwoColorRadialGradientDto): { backgroundImage: string } {
479+
this.context.scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
480+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
481+
const gradient = `radial-gradient(${inputs.shape} at ${inputs.position}, ${inputs.colorFrom} ${inputs.stopFrom}%, ${inputs.colorTo} ${inputs.stopTo}%)`;
482+
const styleObject = { backgroundImage: gradient };
483+
if (canvas) {
484+
canvas.style.backgroundImage = gradient;
485+
}
486+
return styleObject;
487+
}
488+
489+
/**
490+
* Creates a multi-color linear gradient background for 3D space
491+
* @param inputs Describes the multi-color linear gradient parameters
492+
* @group background
493+
* @shortname multi color linear gradient
494+
*/
495+
multiColorLinearGradient(inputs: Inputs.BabylonScene.SceneMultiColorLinearGradientDto): { backgroundImage: string } | { error: string } {
496+
this.context.scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
497+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
498+
if (inputs.colors.length !== inputs.stops.length) {
499+
const errorObj = { error: "Colors and stops arrays must have the same length" };
500+
console.warn(errorObj.error);
501+
return errorObj;
502+
}
503+
const colorStops = inputs.colors.map((color, index) => `${color} ${inputs.stops[index]}%`).join(", ");
504+
const gradient = `linear-gradient(${inputs.direction}, ${colorStops})`;
505+
const styleObject = { backgroundImage: gradient };
506+
if (canvas) {
507+
canvas.style.backgroundImage = gradient;
508+
}
509+
return styleObject;
510+
}
511+
512+
/**
513+
* Creates a multi-color radial gradient background for 3D space
514+
* @param inputs Describes the multi-color radial gradient parameters
515+
* @group background
516+
* @shortname multi color radial gradient
517+
*/
518+
multiColorRadialGradient(inputs: Inputs.BabylonScene.SceneMultiColorRadialGradientDto): { backgroundImage: string } | { error: string } {
519+
this.context.scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
520+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
521+
if (inputs.colors.length !== inputs.stops.length) {
522+
const errorObj = { error: "Colors and stops arrays must have the same length" };
523+
console.warn(errorObj.error);
524+
return errorObj;
525+
}
526+
const colorStops = inputs.colors.map((color, index) => `${color} ${inputs.stops[index]}%`).join(", ");
527+
const gradient = `radial-gradient(${inputs.shape} at ${inputs.position}, ${colorStops})`;
528+
const styleObject = { backgroundImage: gradient };
529+
if (canvas) {
530+
canvas.style.backgroundImage = gradient;
531+
}
532+
return styleObject;
533+
}
534+
535+
/**
536+
* Sets a background image with various customization options for 3D space
537+
* @param inputs Describes the background image parameters
538+
* @group background
539+
* @shortname background image
540+
*/
541+
canvasBackgroundImage(inputs: Inputs.BabylonScene.SceneCanvasBackgroundImageDto): {
542+
backgroundImage: string;
543+
backgroundRepeat: string;
544+
backgroundSize: string;
545+
backgroundPosition: string;
546+
backgroundAttachment: string;
547+
backgroundOrigin: string;
548+
backgroundClip: string;
549+
} {
550+
this.context.scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
551+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
552+
const styleObject = {
553+
backgroundImage: `url(${inputs.imageUrl})`,
554+
backgroundRepeat: inputs.repeat,
555+
backgroundSize: inputs.size,
556+
backgroundPosition: inputs.position,
557+
backgroundAttachment: inputs.attachment,
558+
backgroundOrigin: inputs.origin,
559+
backgroundClip: inputs.clip
560+
};
561+
if (canvas) {
562+
canvas.style.backgroundImage = styleObject.backgroundImage;
563+
canvas.style.backgroundRepeat = styleObject.backgroundRepeat;
564+
canvas.style.backgroundSize = styleObject.backgroundSize;
565+
canvas.style.backgroundPosition = styleObject.backgroundPosition;
566+
canvas.style.backgroundAttachment = styleObject.backgroundAttachment;
567+
canvas.style.backgroundOrigin = styleObject.backgroundOrigin;
568+
canvas.style.backgroundClip = styleObject.backgroundClip;
569+
}
570+
return styleObject;
571+
}
572+
573+
/**
574+
* Changes the scene background colour for 3D space
575+
* @param inputs Describes the colour of the scene background
576+
* @group background
577+
* @shortname colour
578+
*/
579+
backgroundColour(inputs: Inputs.BabylonScene.SceneBackgroundColourDto): void {
580+
this.context.scene.clearColor = BABYLON.Color4.FromColor3(BABYLON.Color3.FromHexString(inputs.colour));
581+
const canvas = this.context.scene.getEngine().getRenderingCanvas();
582+
if (canvas) {
583+
// Reset all background properties to ensure no conflicts with gradients/images
584+
canvas.style.backgroundImage = "none";
585+
canvas.style.backgroundRepeat = "repeat";
586+
canvas.style.backgroundSize = "auto";
587+
canvas.style.backgroundPosition = "0% 0%";
588+
canvas.style.backgroundAttachment = "scroll";
589+
canvas.style.backgroundOrigin = "padding-box";
590+
canvas.style.backgroundClip = "border-box";
591+
}
592+
}
593+
450594
private getRadians(degrees: number): number {
451595
let angle = BABYLON.Angle.FromDegrees(degrees).radians();
452596
if (degrees < 0) {

packages/dev/babylonjs/lib/api/inputs/base-inputs.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,66 @@ export namespace Base {
1212
exponentialSquared = "exponentialSquared",
1313
linear = "linear",
1414
}
15+
export enum gradientDirectionEnum {
16+
toTop = "to top",
17+
toTopRight = "to top right",
18+
toRight = "to right",
19+
toBottomRight = "to bottom right",
20+
toBottom = "to bottom",
21+
toBottomLeft = "to bottom left",
22+
toLeft = "to left",
23+
toTopLeft = "to top left",
24+
deg0 = "0deg",
25+
deg45 = "45deg",
26+
deg90 = "90deg",
27+
deg135 = "135deg",
28+
deg180 = "180deg",
29+
deg225 = "225deg",
30+
deg270 = "270deg",
31+
deg315 = "315deg",
32+
}
33+
export enum gradientPositionEnum {
34+
center = "center",
35+
top = "top",
36+
topLeft = "top left",
37+
topRight = "top right",
38+
bottom = "bottom",
39+
bottomLeft = "bottom left",
40+
bottomRight = "bottom right",
41+
left = "left",
42+
right = "right",
43+
centerTop = "50% 0%",
44+
centerBottom = "50% 100%",
45+
leftCenter = "0% 50%",
46+
rightCenter = "100% 50%",
47+
}
48+
export enum gradientShapeEnum {
49+
circle = "circle",
50+
ellipse = "ellipse",
51+
}
52+
export enum backgroundRepeatEnum {
53+
repeat = "repeat",
54+
repeatX = "repeat-x",
55+
repeatY = "repeat-y",
56+
noRepeat = "no-repeat",
57+
space = "space",
58+
round = "round",
59+
}
60+
export enum backgroundSizeEnum {
61+
auto = "auto",
62+
cover = "cover",
63+
contain = "contain",
64+
}
65+
export enum backgroundAttachmentEnum {
66+
scroll = "scroll",
67+
fixed = "fixed",
68+
local = "local",
69+
}
70+
export enum backgroundOriginClipEnum {
71+
paddingBox = "padding-box",
72+
borderBox = "border-box",
73+
contentBox = "content-box",
74+
}
1575
// Can't use BabylonJS types here as that crashes worker, which tries to include them
1676
export type Color = string;
1777
export type ColorRGB = { r: number, g: number, b: number };

0 commit comments

Comments
 (0)