|
| 1 | +import { Vector } from "@minecraft/server"; |
| 2 | +import { BlockVolume } from "@minecraft/server-editor"; |
| 3 | +import { Direction, getRotationCorrectedDirection } from "editor-utilities/index"; |
| 4 | +/** |
| 5 | + * @beta |
| 6 | + */ |
| 7 | +var AxisPlanes; |
| 8 | +(function (AxisPlanes) { |
| 9 | + AxisPlanes[AxisPlanes["XZ"] = 0] = "XZ"; |
| 10 | + AxisPlanes[AxisPlanes["XY"] = 1] = "XY"; |
| 11 | + AxisPlanes[AxisPlanes["YZ"] = 2] = "YZ"; |
| 12 | +})(AxisPlanes || (AxisPlanes = {})); |
| 13 | +; |
| 14 | +/** |
| 15 | + * @beta |
| 16 | + */ |
| 17 | +const axisNormalLookup = { |
| 18 | + [AxisPlanes.XZ]: Vector.up, |
| 19 | + [AxisPlanes.XY]: Vector.forward, |
| 20 | + [AxisPlanes.YZ]: Vector.left, |
| 21 | +}; |
| 22 | +/** |
| 23 | + * @beta |
| 24 | + */ |
| 25 | +export function getRelativeXYAxisAsNormal(rotation) { |
| 26 | + const direction = getRotationCorrectedDirection(rotation, Direction.Forward); |
| 27 | + switch (direction) { |
| 28 | + case Direction.Forward: |
| 29 | + case Direction.Back: |
| 30 | + return axisNormalLookup[AxisPlanes.XY]; |
| 31 | + case Direction.Right: |
| 32 | + case Direction.Left: |
| 33 | + return axisNormalLookup[AxisPlanes.YZ]; |
| 34 | + default: |
| 35 | + throw 'Invalid quadrant'; |
| 36 | + } |
| 37 | +} |
| 38 | +/** |
| 39 | + * @beta |
| 40 | + */ |
| 41 | +function VectorDot(a, b) { |
| 42 | + return a.x * b.x + a.y * b.y + a.z * b.z; |
| 43 | +} |
| 44 | +/** |
| 45 | + * @beta |
| 46 | + */ |
| 47 | +function VectorScale(a, s) { |
| 48 | + const v = new Vector(a.x, a.y, a.z); |
| 49 | + v.x *= s; |
| 50 | + v.y *= s; |
| 51 | + v.z *= s; |
| 52 | + return v; |
| 53 | +} |
| 54 | +/** |
| 55 | + * @beta |
| 56 | + */ |
| 57 | +export function intersectRayPlane(rayLocation, rayDirection, planeNormal, planeDistance) { |
| 58 | + const denominator = VectorDot(rayDirection, planeNormal); |
| 59 | + if (denominator !== 0) { |
| 60 | + const t = -(VectorDot(rayLocation, planeNormal) + planeDistance) / denominator; |
| 61 | + if (t < 0) { |
| 62 | + return undefined; |
| 63 | + } |
| 64 | + const scaledDirection = VectorScale(rayDirection, t); |
| 65 | + const result = Vector.add(rayLocation, scaledDirection); |
| 66 | + return result; |
| 67 | + } |
| 68 | + else if (VectorDot(planeNormal, rayLocation) + planeDistance === 0) { |
| 69 | + return rayLocation; |
| 70 | + } |
| 71 | + return undefined; |
| 72 | +} |
| 73 | +/** |
| 74 | + * @beta |
| 75 | + */ |
| 76 | +export function shrinkVolumeAlongViewAxis(volume, rotationY, direction, amount) { |
| 77 | + const relativeDirection = getRotationCorrectedDirection(rotationY, direction); |
| 78 | + return shrinkVolumeAlongAbsoluteAxis(volume, relativeDirection, amount); |
| 79 | +} |
| 80 | +export function growVolumeAlongViewAxis(volume, rotationY, direction, amount) { |
| 81 | + const relativeDirection = getRotationCorrectedDirection(rotationY, direction); |
| 82 | + return growVolumeAlongAbsoluteAxis(volume, relativeDirection, amount); |
| 83 | +} |
| 84 | +function growVolumeAlongAbsoluteAxis(volume, direction, amount) { |
| 85 | + const maxAxialLength = 32; |
| 86 | + if (amount > maxAxialLength) { |
| 87 | + amount = maxAxialLength; |
| 88 | + } |
| 89 | + const bounds = volume.boundingBox; |
| 90 | + const min = bounds.min; |
| 91 | + const max = bounds.max; |
| 92 | + const spanX = bounds.spanX; |
| 93 | + const spanY = bounds.spanY; |
| 94 | + const spanZ = bounds.spanZ; |
| 95 | + switch (direction) { |
| 96 | + case Direction.Up: // +Y Axis |
| 97 | + if (spanY + amount > maxAxialLength) { |
| 98 | + amount = maxAxialLength - spanY; |
| 99 | + } |
| 100 | + max.y += amount; |
| 101 | + break; |
| 102 | + case Direction.Down: // -Y Axis |
| 103 | + if (spanY + amount > maxAxialLength) { |
| 104 | + amount = maxAxialLength - spanY; |
| 105 | + } |
| 106 | + min.y -= amount; |
| 107 | + break; |
| 108 | + case Direction.Forward: // +Z Axis |
| 109 | + if (spanZ + amount > maxAxialLength) { |
| 110 | + amount = maxAxialLength - spanZ; |
| 111 | + } |
| 112 | + max.z += amount; |
| 113 | + break; |
| 114 | + case Direction.Back: // -Z Axis |
| 115 | + if (spanZ + amount > maxAxialLength) { |
| 116 | + amount = maxAxialLength - spanZ; |
| 117 | + } |
| 118 | + min.z -= amount; |
| 119 | + break; |
| 120 | + case Direction.Left: // +X Axis |
| 121 | + if (spanX + amount > maxAxialLength) { |
| 122 | + amount = maxAxialLength - spanX; |
| 123 | + } |
| 124 | + max.x += amount; |
| 125 | + break; |
| 126 | + case Direction.Right: // -X Axis |
| 127 | + if (spanX + amount > maxAxialLength) { |
| 128 | + amount = maxAxialLength - spanX; |
| 129 | + } |
| 130 | + min.x -= amount; |
| 131 | + break; |
| 132 | + } |
| 133 | + const newVolume = new BlockVolume(min, max); |
| 134 | + return newVolume; |
| 135 | +} |
| 136 | +function shrinkVolumeAlongAbsoluteAxis(volume, direction, amount) { |
| 137 | + const bounds = volume.boundingBox; |
| 138 | + const min = bounds.min; |
| 139 | + const max = bounds.max; |
| 140 | + const spanX = bounds.spanX; |
| 141 | + const spanY = bounds.spanY; |
| 142 | + const spanZ = bounds.spanZ; |
| 143 | + switch (direction) { |
| 144 | + case Direction.Up: // +Y Axis |
| 145 | + if (spanY > amount) { |
| 146 | + max.y -= amount; |
| 147 | + } |
| 148 | + break; |
| 149 | + case Direction.Down: // -Y Axis |
| 150 | + if (spanY > amount) { |
| 151 | + min.y += amount; |
| 152 | + } |
| 153 | + break; |
| 154 | + case Direction.Forward: // +Z Axis |
| 155 | + if (spanZ > amount) { |
| 156 | + max.z -= amount; |
| 157 | + } |
| 158 | + break; |
| 159 | + case Direction.Back: // -Z Axis |
| 160 | + if (spanZ > amount) { |
| 161 | + min.z += amount; |
| 162 | + } |
| 163 | + break; |
| 164 | + case Direction.Left: // +X Axis |
| 165 | + if (spanX > amount) { |
| 166 | + max.x -= amount; |
| 167 | + } |
| 168 | + break; |
| 169 | + case Direction.Right: // -X Axis |
| 170 | + if (spanX > amount) { |
| 171 | + min.x += amount; |
| 172 | + } |
| 173 | + break; |
| 174 | + } |
| 175 | + const newVolume = new BlockVolume(min, max); |
| 176 | + return newVolume; |
| 177 | +} |
0 commit comments