|
1 | | - |
2 | 1 | import { Context } from "../../context"; |
3 | 2 | import * as BABYLON from "@babylonjs/core"; |
4 | 3 | import * as GUI from "@babylonjs/gui"; |
@@ -32,16 +31,6 @@ export class BabylonScene { |
32 | 31 | return this.context.scene = inputs.scene; |
33 | 32 | } |
34 | 33 |
|
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 | | - |
45 | 34 | /** |
46 | 35 | * Activate camera by overwriting currently active camera |
47 | 36 | * @param inputs Activates the camera |
@@ -447,6 +436,161 @@ export class BabylonScene { |
447 | 436 | this.context.scene.enablePhysics(new BABYLON.Vector3(inputs.vector[0], inputs.vector[1], inputs.vector[2]), this.context.havokPlugin); |
448 | 437 | } |
449 | 438 |
|
| 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 | + |
450 | 594 | private getRadians(degrees: number): number { |
451 | 595 | let angle = BABYLON.Angle.FromDegrees(degrees).radians(); |
452 | 596 | if (degrees < 0) { |
|
0 commit comments