| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Models; |
| | 3 | | using UnityEngine; |
| | 4 | | using Decentraland.Sdk.Ecs6; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | namespace DCL.Components |
| | 8 | | { |
| | 9 | | public class BoxShape : ParametrizedShape<BoxShape.Model> |
| | 10 | | { |
| | 11 | | [System.Serializable] |
| | 12 | | public new class Model : BaseShape.Model |
| | 13 | | { |
| | 14 | | public float[] uvs; |
| | 15 | |
|
| | 16 | | public override BaseModel GetDataFromJSON(string json) => |
| 124 | 17 | | Utils.SafeFromJson<Model>(json); |
| | 18 | |
|
| | 19 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 20 | | { |
| 0 | 21 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.BoxShape) |
| 0 | 22 | | return Utils.SafeUnimplemented<BoxShape, Model>(expected: ComponentBodyPayload.PayloadOneofCase.BoxS |
| | 23 | |
|
| 0 | 24 | | var pb = new Model(); |
| 0 | 25 | | if (pbModel.BoxShape.HasVisible) pb.visible = pbModel.BoxShape.Visible; |
| 0 | 26 | | if (pbModel.BoxShape.HasWithCollisions) pb.withCollisions = pbModel.BoxShape.WithCollisions; |
| 0 | 27 | | if (pbModel.BoxShape.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.BoxShape.IsPointerBlocker; |
| 0 | 28 | | if (pbModel.BoxShape.Uvs is { Count: > 0 }) pb.uvs = pbModel.BoxShape.Uvs.ToArray(); |
| | 29 | |
|
| 0 | 30 | | return pb; |
| | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| 315 | 34 | | public BoxShape() { model = new Model(); } |
| | 35 | |
|
| | 36 | | public static Mesh cubeMesh = null; |
| | 37 | | private static int cubeMeshRefCount = 0; |
| | 38 | |
|
| 0 | 39 | | public override int GetClassId() { return (int) CLASS_ID.BOX_SHAPE; } |
| | 40 | |
|
| | 41 | | public override Mesh GenerateGeometry() |
| | 42 | | { |
| 106 | 43 | | var model = (Model) this.model; |
| | 44 | |
|
| 106 | 45 | | if (cubeMesh == null) |
| 3 | 46 | | cubeMesh = PrimitiveMeshBuilder.BuildCube(1f); |
| | 47 | |
|
| 106 | 48 | | if (model.uvs != null && model.uvs.Length > 0) |
| | 49 | | { |
| 1 | 50 | | cubeMesh.uv = Utils.FloatArrayToV2List(model.uvs); |
| | 51 | | } |
| | 52 | |
|
| 106 | 53 | | cubeMeshRefCount++; |
| 106 | 54 | | return cubeMesh; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | protected override void DestroyGeometry() |
| | 58 | | { |
| 71 | 59 | | cubeMeshRefCount--; |
| | 60 | |
|
| 71 | 61 | | if (cubeMeshRefCount == 0) |
| | 62 | | { |
| 2 | 63 | | GameObject.Destroy(cubeMesh); |
| 2 | 64 | | cubeMesh = null; |
| | 65 | | } |
| 71 | 66 | | } |
| | 67 | |
|
| | 68 | | protected override bool ShouldGenerateNewMesh(BaseShape.Model previousModel) |
| | 69 | | { |
| 128 | 70 | | if (currentMesh == null) |
| 107 | 71 | | return true; |
| | 72 | |
|
| 21 | 73 | | BoxShape.Model newBoxModel = (BoxShape.Model) this.model; |
| 21 | 74 | | BoxShape.Model oldBoxModel = (BoxShape.Model) previousModel; |
| | 75 | |
|
| 21 | 76 | | if (newBoxModel.uvs != null && oldBoxModel.uvs != null) |
| | 77 | | { |
| 14 | 78 | | if (newBoxModel.uvs.Length != oldBoxModel.uvs.Length) |
| 0 | 79 | | return true; |
| | 80 | |
|
| 28 | 81 | | for (int i = 0; i < newBoxModel.uvs.Length; i++) |
| | 82 | | { |
| 0 | 83 | | if (newBoxModel.uvs[i] != oldBoxModel.uvs[i]) |
| 0 | 84 | | return true; |
| | 85 | | } |
| | 86 | | } |
| | 87 | | else |
| | 88 | | { |
| 7 | 89 | | if (newBoxModel.uvs != oldBoxModel.uvs) |
| 3 | 90 | | return true; |
| | 91 | | } |
| | 92 | |
|
| 18 | 93 | | return false; |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |