Skip to content

Commit b70c635

Browse files
committed
add live support
1 parent a8ca433 commit b70c635

23 files changed

+395
-36
lines changed

src/Threejs/src/Camera/Camera.php

+25-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,30 @@
1919
*/
2020
abstract class Camera
2121
{
22-
public Vector3 $position;
22+
public Vector3 $position;
23+
public string $type;
2324

24-
public function __construct(?Vector3 $position = null)
25-
{
26-
$this->position = $position ?? new Vector3(0, 0, 5);
27-
}
25+
public function __construct(?Vector3 $position = null)
26+
{
27+
$this->position = $position ?? new Vector3(0, 0, 5);
28+
}
29+
30+
public static function fromArray(array $camera): self
31+
{
32+
$camera['position'] = Vector3::fromArray($camera['position']);
33+
$type = $camera['type'];
34+
unset($camera['type']);
35+
$cameraObject = new static(...$camera);
36+
$cameraObject->type = $type;
37+
38+
return $cameraObject;
39+
}
40+
41+
public function toArray(): array
42+
{
43+
return [
44+
'type' => $this->type,
45+
'position' => $this->position->toArray(),
46+
];
47+
}
2848
}

src/Threejs/src/Camera/OrthographicCamera.php renamed to src/Threejs/src/Camera/Orthographic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Sylvain Blondeau <[email protected]>
1818
*/
19-
final class OrthographicCamera extends Camera
19+
final class Orthographic extends Camera
2020
{
2121
public string $type = 'Orthographic';
2222

src/Threejs/src/Camera/PerspectiveCamera.php renamed to src/Threejs/src/Camera/Perspective.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Sylvain Blondeau <[email protected]>
1818
*/
19-
final class PerspectiveCamera extends Camera
19+
final class Perspective extends Camera
2020
{
2121
public string $type = 'Perspective';
2222

src/Threejs/src/Geometry/Box.php

+12
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,16 @@ public function __construct(
2828
parent::__construct();
2929
$this->type = self::TYPE;
3030
}
31+
32+
33+
34+
public function toArray(): array
35+
{
36+
return [
37+
'width' => $this->width,
38+
'height' => $this->height,
39+
'depth' => $this->depth,
40+
'type' => $this->type,
41+
];
42+
}
3143
}

src/Threejs/src/Geometry/BufferGeometry.php

+11
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,16 @@ public function __construct(
2323
{
2424
}
2525

26+
public static function fromArray(array $geometry): self
27+
{
28+
$type = $geometry['type'];
29+
unset($geometry['type']);
30+
$geometryObject = new static(...$geometry);
31+
$geometryObject->type = $type;
32+
33+
return $geometryObject;
34+
}
35+
36+
abstract public function toArray(): array;
2637

2738
}

src/Threejs/src/Geometry/Cylinder.php

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ public function __construct(
3333
parent::__construct();
3434
$this->type = self::TYPE;
3535
}
36+
37+
public function toArray(): array
38+
{
39+
return [
40+
'radiusTop' => $this->radiusTop,
41+
'radiusBottom' => $this->radiusBottom,
42+
'height' => $this->height,
43+
'radialSegments' => $this->radialSegments,
44+
'heightSegments' => $this->heightSegments,
45+
'openEnded' => $this->openEnded,
46+
'thetaStart' => $this->thetaStart,
47+
'thetaLength' => $this->thetaLength,
48+
];
49+
}
3650
}
3751

3852

src/Threejs/src/Geometry/Plane.php

+11
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ public function __construct(
2828
parent::__construct();
2929
$this->type = self::TYPE;
3030
}
31+
32+
public function toArray(): array
33+
{
34+
return [
35+
'width' => $this->width,
36+
'height' => $this->height,
37+
'widthSegments' => $this->widthSegments,
38+
'heightSegments' => $this->heightSegments,
39+
'type' => $this->type,
40+
];
41+
}
3142
}

src/Threejs/src/Geometry/Sphere.php

+10
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ public function __construct(
2727
parent::__construct();
2828
$this->type = self::TYPE;
2929
}
30+
31+
public function toArray(): array
32+
{
33+
return [
34+
'radius' => $this->radius,
35+
'widthSegments' => $this->widthSegments,
36+
'heightSegments' => $this->heightSegments,
37+
'type' => $this->type,
38+
];
39+
}
3040
}

src/Threejs/src/Light/AmbientLight.php renamed to src/Threejs/src/Light/Ambient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @author Sylvain Blondeau <[email protected]>
1616
*/
17-
final class AmbientLight extends Light
17+
final class Ambient extends Light
1818
{
1919
public const string TYPE = 'Ambient';
2020

src/Threejs/src/Light/DirectionalLight.php renamed to src/Threejs/src/Light/Directional.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Sylvain Blondeau <[email protected]>
1818
*/
19-
final class DirectionalLight extends Light
19+
final class Directional extends Light
2020
{
2121
public const string TYPE = 'Directional';
2222

@@ -30,5 +30,28 @@ public function __construct(
3030

3131
) {
3232
parent::__construct($color, $intensity);
33+
}
34+
35+
public static function fromArray(array $light): self
36+
{
37+
$light['position'] = Vector3::fromArray($light['position']);
38+
$light['target'] = Vector3::fromArray($light['target']);
39+
$type = $light['type'];
40+
unset($light['type']);
41+
$lightObject = new static(...$light);
42+
$lightObject->type = $type;
43+
44+
return $lightObject;
45+
}
46+
47+
public function toArray(): array
48+
{
49+
return [
50+
'type' => $this->type,
51+
'color' => $this->color,
52+
'intensity' => $this->intensity,
53+
'position' => $this->position->toArray(),
54+
'target' => $this->target->toArray(),
55+
];
3356
}
3457
}

src/Threejs/src/Light/Light.php

+19
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,23 @@ public function __construct(
2222
public string $color = 'white',
2323
public float $intensity = 1,
2424
) {}
25+
26+
public static function fromArray(array $light): self
27+
{
28+
$type = $light['type'];
29+
unset($light['type']);
30+
$lightObject = new static(...$light);
31+
$lightObject->type = $type;
32+
33+
return $lightObject;
34+
}
35+
36+
public function toArray(): array
37+
{
38+
return [
39+
'type' => $this->type,
40+
'color' => $this->color,
41+
'intensity' => $this->intensity,
42+
];
43+
}
2544
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Symfony\UX\Threejs\Live;
15+
16+
use Symfony\UX\Threejs\Three;
17+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
18+
use Symfony\UX\TwigComponent\Attribute\PostMount;
19+
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
20+
21+
/**
22+
* @author Hugo Alliaume <[email protected]>
23+
*
24+
* @experimental
25+
*/
26+
trait ComponentWithThreeTrait
27+
{
28+
/**
29+
* @internal
30+
*/
31+
#[LiveProp(hydrateWith: 'hydrateThree', dehydrateWith: 'dehydrateThree')]
32+
#[ExposeInTemplate(getter: 'getThree')]
33+
public ?Three $three = null;
34+
35+
abstract protected function instantiateThree(): Three;
36+
37+
public function getThree(): Three
38+
{
39+
return $this->three ??= $this->instantiateThree();
40+
}
41+
42+
/**
43+
* @internal
44+
*/
45+
#[PostMount]
46+
public function initializeThree(array $data): array
47+
{
48+
// allow the Three object to be passed into the component() as "three"
49+
if (\array_key_exists('three', $data)) {
50+
$this->three = $data['three'];
51+
unset($data['three']);
52+
}
53+
54+
return $data;
55+
}
56+
57+
/**
58+
* @internal
59+
*/
60+
public function hydrateThree(array $data): Three
61+
{
62+
return Three::fromArray($data);
63+
}
64+
65+
/**
66+
* @internal
67+
*/
68+
public function dehydrateThree(Three $three): array
69+
{
70+
return $three->toArray();
71+
}
72+
}

src/Threejs/src/Material/Material.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
abstract class Material
1919
{
20-
public bool $transparent;
2120
public string $type;
2221

2322
public function __construct(
@@ -26,10 +25,32 @@ public function __construct(
2625
public string $map = '',
2726
public bool $doubleSide = false,
2827
public bool $skybox = false,
29-
28+
public bool $transparent = false,
3029
) {
3130
$this->transparent = $this->opacity < 1;
3231
}
3332

33+
public static function fromArray(array $material): self
34+
{
35+
$material['transparent'] = $material['opacity'] < 1;
36+
$type = $material['type'];
37+
unset($material['type']);
38+
$materialObject = new static(...$material);
39+
$materialObject->type = $type;
40+
41+
return $materialObject;
42+
}
3443

44+
public function toArray(): array
45+
{
46+
return [
47+
'transparent' => $this->transparent,
48+
'type' => $this->type,
49+
'color' => $this->color,
50+
'opacity' => $this->opacity,
51+
'map' => $this->map,
52+
'doubleSide' => $this->doubleSide,
53+
'skybox' => $this->skybox,
54+
];
55+
}
3556
}

src/Threejs/src/Material/MeshBasic.php

-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ final class MeshBasic extends Material
2020
public const string TYPE = 'MeshBasic';
2121

2222
public string $type = self::TYPE;
23-
2423
}

src/Threejs/src/Material/MeshPhong.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
final class MeshPhong extends Material
1919
{
2020
public const string TYPE = 'MeshPhong';
21+
2122
public string $type = self::TYPE;
23+
2224
}

0 commit comments

Comments
 (0)