Skip to content

Commit 1eac275

Browse files
authored
Merge pull request #20 from sovdeeth/spin-off-shapes-lib
Separate the shapes logic and the skript logic
2 parents 07d0391 + 0efb4f4 commit 1eac275

128 files changed

Lines changed: 3686 additions & 4262 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/gradle.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ jobs:
2222
runs-on: ubuntu-latest
2323

2424
steps:
25-
- uses: actions/checkout@v3
25+
- uses: actions/checkout@v6
2626
- name: Set up JDK 17
27-
uses: actions/setup-java@v3
27+
uses: actions/setup-java@v5
2828
with:
2929
java-version: '17'
3030
distribution: 'temurin'
@@ -34,9 +34,9 @@ jobs:
3434
- name: Build with Gradle
3535
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
3636
with:
37-
arguments: shadowJar
37+
arguments: build
3838
- name: Upload Nightly Build
39-
uses: actions/upload-artifact@v3
39+
uses: actions/upload-artifact@v6
4040
if: success()
4141
with:
4242
name: skript-particle-nightly

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
/build/
66
/.git/
77
/src/test/skriptparticle/
8+
CLAUDE.md
9+
/.claude
10+
/shapes-lib/build
11+
/skript-particle/build

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
version = 1.4.0
2+
jomlVersion = 1.10.5
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
rootProject.name = 'skript-particle'
1+
rootProject.name = 'skript-particle-root'
2+
include 'shapes-lib', 'skript-particle'

shapes-lib/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id 'java-library'
3+
}
4+
5+
group = 'com.sovdee'
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
api "org.joml:joml:${jomlVersion}"
13+
}
14+
15+
java {
16+
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
17+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.sovdee.shapes.sampling;
2+
3+
import com.sovdee.shapes.shapes.Shape;
4+
import org.joml.Quaterniond;
5+
import org.joml.Vector3d;
6+
7+
import java.util.Comparator;
8+
import java.util.LinkedHashSet;
9+
import java.util.Set;
10+
import java.util.TreeSet;
11+
import java.util.UUID;
12+
13+
/**
14+
* Default implementation of {@link PointSampler} with hash-based caching.
15+
*/
16+
public class DefaultPointSampler implements PointSampler {
17+
18+
private SamplingStyle style = SamplingStyle.OUTLINE;
19+
private double density = 0.25;
20+
private Comparator<Vector3d> ordering;
21+
private final UUID uuid;
22+
private DrawContext drawContext;
23+
24+
// Cache
25+
private Set<Vector3d> cachedPoints = new LinkedHashSet<>();
26+
private CacheState lastState;
27+
private boolean needsUpdate = false;
28+
29+
public DefaultPointSampler() {
30+
this.uuid = UUID.randomUUID();
31+
this.lastState = new CacheState(style, 0, 1.0, 0, density, 0);
32+
}
33+
34+
@Override
35+
public Set<Vector3d> getPoints(Shape shape) {
36+
return getPoints(shape, shape.getOrientation());
37+
}
38+
39+
@Override
40+
public Set<Vector3d> getPoints(Shape shape, Quaterniond orientation) {
41+
CacheState state = new CacheState(style, orientation.hashCode(),
42+
shape.getScale(), shape.getOffset().hashCode(), density, shape.getVersion());
43+
if (shape.isDynamic() || needsUpdate || !state.equals(lastState) || cachedPoints.isEmpty()) {
44+
Set<Vector3d> points = (ordering != null) ? new TreeSet<>(ordering) : new LinkedHashSet<>();
45+
46+
shape.beforeSampling(density);
47+
switch (style) {
48+
case OUTLINE -> shape.generateOutline(points, density);
49+
case SURFACE -> shape.generateSurface(points, density);
50+
case FILL -> shape.generateFilled(points, density);
51+
}
52+
shape.afterSampling(points);
53+
54+
for (Vector3d point : points) {
55+
orientation.transform(point);
56+
point.mul(shape.getScale());
57+
point.add(shape.getOffset());
58+
}
59+
cachedPoints = points;
60+
lastState = state;
61+
needsUpdate = false;
62+
}
63+
return cachedPoints;
64+
}
65+
66+
public void markDirty() {
67+
needsUpdate = true;
68+
}
69+
70+
@Override
71+
public SamplingStyle getStyle() { return style; }
72+
73+
@Override
74+
public void setStyle(SamplingStyle style) {
75+
this.style = style;
76+
this.needsUpdate = true;
77+
}
78+
79+
@Override
80+
public double getDensity() { return density; }
81+
82+
@Override
83+
public void setDensity(double density) {
84+
this.density = Math.max(density, Shape.EPSILON);
85+
this.needsUpdate = true;
86+
}
87+
88+
@Override
89+
public Comparator<Vector3d> getOrdering() { return ordering; }
90+
91+
@Override
92+
public void setOrdering(Comparator<Vector3d> ordering) {
93+
this.ordering = ordering;
94+
this.needsUpdate = true;
95+
}
96+
97+
@Override
98+
public UUID getUUID() { return uuid; }
99+
100+
@Override
101+
public DrawContext getDrawContext() { return drawContext; }
102+
103+
@Override
104+
public void setDrawContext(DrawContext context) { this.drawContext = context; }
105+
106+
@Override
107+
public DefaultPointSampler clone() {
108+
try {
109+
DefaultPointSampler copy = (DefaultPointSampler) super.clone();
110+
// Don't share the cache
111+
copy.cachedPoints = new LinkedHashSet<>();
112+
copy.needsUpdate = true;
113+
if (drawContext != null)
114+
copy.drawContext = drawContext.copy();
115+
return copy;
116+
} catch (CloneNotSupportedException e) {
117+
throw new AssertionError(e);
118+
}
119+
}
120+
121+
private record CacheState(SamplingStyle style, int orientationHash, double scale,
122+
int offsetHash, double density, long shapeVersion) {}
123+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.sovdee.shapes.sampling;
2+
3+
/**
4+
* Client-provided rendering metadata. Implementations are opaque to the library.
5+
*/
6+
public interface DrawContext {
7+
DrawContext copy();
8+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.sovdee.shapes.sampling;
2+
3+
import com.sovdee.shapes.shapes.Shape;
4+
import org.joml.Quaterniond;
5+
import org.joml.Vector3d;
6+
7+
import java.util.Comparator;
8+
import java.util.Set;
9+
import java.util.UUID;
10+
11+
/**
12+
* Responsible for sampling points from a {@link Shape}'s geometry.
13+
* Manages sampling configuration (style, density, ordering) and caching.
14+
*/
15+
public interface PointSampler extends Cloneable {
16+
17+
SamplingStyle getStyle();
18+
void setStyle(SamplingStyle style);
19+
20+
double getDensity();
21+
void setDensity(double density);
22+
23+
Comparator<Vector3d> getOrdering();
24+
void setOrdering(Comparator<Vector3d> ordering);
25+
26+
UUID getUUID();
27+
28+
DrawContext getDrawContext();
29+
void setDrawContext(DrawContext context);
30+
31+
/**
32+
* Samples points from the given shape using the shape's own orientation.
33+
*/
34+
Set<Vector3d> getPoints(Shape shape);
35+
36+
/**
37+
* Samples points from the given shape using the given orientation.
38+
*/
39+
Set<Vector3d> getPoints(Shape shape, Quaterniond orientation);
40+
41+
/**
42+
* Computes and sets the density to achieve approximately the given particle count.
43+
*/
44+
default void setParticleCount(Shape shape, int count) {
45+
setDensity(shape.computeDensity(getStyle(), count));
46+
}
47+
48+
PointSampler clone();
49+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sovdee.shapes.sampling;
2+
3+
/**
4+
* Determines how a shape's geometry is sampled into points.
5+
*/
6+
public enum SamplingStyle {
7+
OUTLINE,
8+
SURFACE,
9+
FILL;
10+
11+
@Override
12+
public String toString() {
13+
return name().toLowerCase();
14+
}
15+
}

0 commit comments

Comments
 (0)