@erik9994857/cag 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erik9994857/cag",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "CaG — A code library and custom language for building 3D worlds with .cagc files, .bbmodel support, and auto UV mapping",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -1,5 +1,6 @@
1
1
  const path = require("path");
2
2
  const fs = require("fs");
3
+ const UVMapper = require("../models/uv-mapper");
3
4
 
4
5
  class ModelAPI {
5
6
  constructor(resourceMap, defaultModelsPath) {
@@ -122,6 +123,9 @@ class ModelAPI {
122
123
 
123
124
  const uvMappings = [];
124
125
 
126
+ const useBoxUV = !!data.box_uv;
127
+ const uvMapper = new UVMapper(geometry.resolution);
128
+
125
129
  if (data.elements && Array.isArray(data.elements)) {
126
130
  for (const element of data.elements) {
127
131
  const geomElement = {
@@ -133,23 +137,60 @@ class ModelAPI {
133
137
  faces: {}
134
138
  };
135
139
 
140
+ const needsAutoUV = useBoxUV || (element.box_uv !== undefined && element.box_uv);
141
+ let autoUVs = null;
142
+ if (needsAutoUV) {
143
+ autoUVs = uvMapper.autoMap(geomElement.from, geomElement.to);
144
+ }
145
+
136
146
  if (element.faces) {
137
147
  const faceNames = ["north", "south", "east", "west", "up", "down"];
138
148
  for (const faceName of faceNames) {
139
149
  if (element.faces[faceName]) {
140
150
  const face = element.faces[faceName];
151
+ let faceUV = face.uv;
152
+
153
+ if (!faceUV || !Array.isArray(faceUV) || faceUV.length < 4) {
154
+ faceUV = autoUVs ? autoUVs[faceName] : [0, 0, 16, 16];
155
+ }
156
+
141
157
  geomElement.faces[faceName] = {
142
- uv: face.uv || [0, 0, 16, 16],
158
+ uv: faceUV,
143
159
  texture: face.texture !== undefined ? face.texture : 0
144
160
  };
145
161
  uvMappings.push({
146
162
  element: geomElement.name,
147
163
  face: faceName,
148
- uv: face.uv || [0, 0, 16, 16],
164
+ uv: faceUV,
149
165
  textureIndex: face.texture !== undefined ? face.texture : 0
150
166
  });
167
+ } else if (needsAutoUV && autoUVs) {
168
+ geomElement.faces[faceName] = {
169
+ uv: autoUVs[faceName],
170
+ texture: 0
171
+ };
172
+ uvMappings.push({
173
+ element: geomElement.name,
174
+ face: faceName,
175
+ uv: autoUVs[faceName],
176
+ textureIndex: 0
177
+ });
151
178
  }
152
179
  }
180
+ } else if (needsAutoUV && autoUVs) {
181
+ const faceNames = ["north", "south", "east", "west", "up", "down"];
182
+ for (const faceName of faceNames) {
183
+ geomElement.faces[faceName] = {
184
+ uv: autoUVs[faceName],
185
+ texture: 0
186
+ };
187
+ uvMappings.push({
188
+ element: geomElement.name,
189
+ face: faceName,
190
+ uv: autoUVs[faceName],
191
+ textureIndex: 0
192
+ });
193
+ }
153
194
  }
154
195
 
155
196
  geometry.elements.push(geomElement);
@@ -258,10 +258,10 @@ class UVMapper {
258
258
  var sizeZ = Math.abs(elementTo[2] - elementFrom[2]);
259
259
 
260
260
  return {
261
- north: [sizeZ, sizeY + sizeZ, sizeZ + sizeX, sizeY + sizeZ + sizeY],
262
- south: [sizeZ + sizeX + sizeZ, sizeY + sizeZ, sizeZ + sizeX + sizeZ + sizeX, sizeY + sizeZ + sizeY],
263
- east: [0, sizeY + sizeZ, sizeZ, sizeY + sizeZ + sizeY],
264
- west: [sizeZ + sizeX, sizeY + sizeZ, sizeZ + sizeX + sizeZ, sizeY + sizeZ + sizeY],
261
+ north: [sizeZ, sizeZ, sizeZ + sizeX, sizeZ + sizeY],
262
+ south: [sizeZ + sizeX + sizeZ, sizeZ, sizeZ + sizeX + sizeZ + sizeX, sizeZ + sizeY],
263
+ east: [0, sizeZ, sizeZ, sizeZ + sizeY],
264
+ west: [sizeZ + sizeX, sizeZ, sizeZ + sizeX + sizeZ, sizeZ + sizeY],
265
265
  up: [sizeZ, 0, sizeZ + sizeX, sizeZ],
266
266
  down: [sizeZ + sizeX, 0, sizeZ + sizeX + sizeX, sizeZ]
267
267
  };