| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Components |
| | 7 | | { |
| | 8 | | public class BoxShape : ParametrizedShape<BoxShape.Model> |
| | 9 | | { |
| | 10 | | [System.Serializable] |
| | 11 | | new public class Model : BaseShape.Model |
| | 12 | | { |
| | 13 | | public float[] uvs; |
| | 14 | |
|
| 122 | 15 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 16 | | } |
| | 17 | |
|
| 318 | 18 | | public BoxShape() { model = new Model(); } |
| | 19 | |
|
| | 20 | | public static Mesh cubeMesh = null; |
| | 21 | | private static int cubeMeshRefCount = 0; |
| | 22 | |
|
| 0 | 23 | | public override int GetClassId() { return (int) CLASS_ID.BOX_SHAPE; } |
| | 24 | |
|
| | 25 | | public override Mesh GenerateGeometry() |
| | 26 | | { |
| 107 | 27 | | var model = (Model) this.model; |
| | 28 | |
|
| 107 | 29 | | if (cubeMesh == null) |
| 1 | 30 | | cubeMesh = PrimitiveMeshBuilder.BuildCube(1f); |
| | 31 | |
|
| 107 | 32 | | if (model.uvs != null && model.uvs.Length > 0) |
| | 33 | | { |
| 1 | 34 | | cubeMesh.uv = Utils.FloatArrayToV2List(model.uvs); |
| | 35 | | } |
| | 36 | |
|
| 107 | 37 | | cubeMeshRefCount++; |
| 107 | 38 | | return cubeMesh; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | protected override void DestroyGeometry() |
| | 42 | | { |
| 18 | 43 | | cubeMeshRefCount--; |
| | 44 | |
|
| 18 | 45 | | if (cubeMeshRefCount == 0) |
| | 46 | | { |
| 0 | 47 | | GameObject.Destroy(cubeMesh); |
| 0 | 48 | | cubeMesh = null; |
| | 49 | | } |
| 18 | 50 | | } |
| | 51 | |
|
| | 52 | | protected override bool ShouldGenerateNewMesh(BaseShape.Model previousModel) |
| | 53 | | { |
| 126 | 54 | | if (currentMesh == null) |
| 108 | 55 | | return true; |
| | 56 | |
|
| 18 | 57 | | BoxShape.Model newBoxModel = (BoxShape.Model) this.model; |
| 18 | 58 | | BoxShape.Model oldBoxModel = (BoxShape.Model) previousModel; |
| | 59 | |
|
| 18 | 60 | | if (newBoxModel.uvs != null && oldBoxModel.uvs != null) |
| | 61 | | { |
| 11 | 62 | | if (newBoxModel.uvs.Length != oldBoxModel.uvs.Length) |
| 0 | 63 | | return true; |
| | 64 | |
|
| 22 | 65 | | for (int i = 0; i < newBoxModel.uvs.Length; i++) |
| | 66 | | { |
| 0 | 67 | | if (newBoxModel.uvs[i] != oldBoxModel.uvs[i]) |
| 0 | 68 | | return true; |
| | 69 | | } |
| 11 | 70 | | } |
| | 71 | | else |
| | 72 | | { |
| 7 | 73 | | if (newBoxModel.uvs != oldBoxModel.uvs) |
| 3 | 74 | | return true; |
| | 75 | | } |
| | 76 | |
|
| 15 | 77 | | return false; |
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |