| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.renderscript; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.os.Build; |
| 21 |
|
| 22 |
import java.util.Vector; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
@Deprecated |
| 44 |
public class Mesh extends BaseObj { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public enum Primitive { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
@UnsupportedAppUsage |
| 57 |
POINT (0), |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
LINE (1), |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
LINE_STRIP (2), |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@UnsupportedAppUsage |
| 73 |
TRIANGLE (3), |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
TRIANGLE_STRIP (4), |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
TRIANGLE_FAN (5); |
| 87 |
|
| 88 |
int mID; |
| 89 |
Primitive(int id) { |
| 90 |
mID = id; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
Allocation[] mVertexBuffers; |
| 95 |
Allocation[] mIndexBuffers; |
| 96 |
Primitive[] mPrimitives; |
| 97 |
|
| 98 |
Mesh(long id, RenderScript rs) { |
| 99 |
super(id, rs); |
| 100 |
guard.open("destroy"); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public int getVertexAllocationCount() { |
| 109 |
if(mVertexBuffers == null) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
return mVertexBuffers.length; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
@UnsupportedAppUsage |
| 121 |
public Allocation getVertexAllocation(int slot) { |
| 122 |
return mVertexBuffers[slot]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public int getPrimitiveCount() { |
| 131 |
if(mIndexBuffers == null) { |
| 132 |
return 0; |
| 133 |
} |
| 134 |
return mIndexBuffers.length; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public Allocation getIndexSetAllocation(int slot) { |
| 145 |
return mIndexBuffers[slot]; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public Primitive getPrimitive(int slot) { |
| 154 |
return mPrimitives[slot]; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
void updateFromNative() { |
| 159 |
super.updateFromNative(); |
| 160 |
int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS)); |
| 161 |
int idxCount = mRS.nMeshGetIndexCount(getID(mRS)); |
| 162 |
|
| 163 |
long[] vtxIDs = new long[vtxCount]; |
| 164 |
long[] idxIDs = new long[idxCount]; |
| 165 |
int[] primitives = new int[idxCount]; |
| 166 |
|
| 167 |
mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount); |
| 168 |
mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount); |
| 169 |
|
| 170 |
mVertexBuffers = new Allocation[vtxCount]; |
| 171 |
mIndexBuffers = new Allocation[idxCount]; |
| 172 |
mPrimitives = new Primitive[idxCount]; |
| 173 |
|
| 174 |
for(int i = 0; i < vtxCount; i ++) { |
| 175 |
if(vtxIDs[i] != 0) { |
| 176 |
mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT); |
| 177 |
mVertexBuffers[i].updateFromNative(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
for(int i = 0; i < idxCount; i ++) { |
| 182 |
if(idxIDs[i] != 0) { |
| 183 |
mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT); |
| 184 |
mIndexBuffers[i].updateFromNative(); |
| 185 |
} |
| 186 |
mPrimitives[i] = Primitive.values()[primitives[i]]; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static class Builder { |
| 198 |
RenderScript mRS; |
| 199 |
int mUsage; |
| 200 |
|
| 201 |
class Entry { |
| 202 |
Type t; |
| 203 |
Element e; |
| 204 |
int size; |
| 205 |
Primitive prim; |
| 206 |
int usage; |
| 207 |
} |
| 208 |
|
| 209 |
int mVertexTypeCount; |
| 210 |
Entry[] mVertexTypes; |
| 211 |
Vector mIndexTypes; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public Builder(RenderScript rs, int usage) { |
| 222 |
mRS = rs; |
| 223 |
mUsage = usage; |
| 224 |
mVertexTypeCount = 0; |
| 225 |
mVertexTypes = new Entry[16]; |
| 226 |
mIndexTypes = new Vector(); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public int getCurrentVertexTypeIndex() { |
| 235 |
return mVertexTypeCount - 1; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public int getCurrentIndexSetIndex() { |
| 244 |
return mIndexTypes.size() - 1; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public Builder addVertexType(Type t) throws IllegalStateException { |
| 256 |
if (mVertexTypeCount >= mVertexTypes.length) { |
| 257 |
throw new IllegalStateException("Max vertex types exceeded."); |
| 258 |
} |
| 259 |
|
| 260 |
mVertexTypes[mVertexTypeCount] = new Entry(); |
| 261 |
mVertexTypes[mVertexTypeCount].t = t; |
| 262 |
mVertexTypes[mVertexTypeCount].e = null; |
| 263 |
mVertexTypeCount++; |
| 264 |
return this; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public Builder addVertexType(Element e, int size) throws IllegalStateException { |
| 277 |
if (mVertexTypeCount >= mVertexTypes.length) { |
| 278 |
throw new IllegalStateException("Max vertex types exceeded."); |
| 279 |
} |
| 280 |
|
| 281 |
mVertexTypes[mVertexTypeCount] = new Entry(); |
| 282 |
mVertexTypes[mVertexTypeCount].t = null; |
| 283 |
mVertexTypes[mVertexTypeCount].e = e; |
| 284 |
mVertexTypes[mVertexTypeCount].size = size; |
| 285 |
mVertexTypeCount++; |
| 286 |
return this; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public Builder addIndexSetType(Type t, Primitive p) { |
| 299 |
Entry indexType = new Entry(); |
| 300 |
indexType.t = t; |
| 301 |
indexType.e = null; |
| 302 |
indexType.size = 0; |
| 303 |
indexType.prim = p; |
| 304 |
mIndexTypes.addElement(indexType); |
| 305 |
return this; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public Builder addIndexSetType(Primitive p) { |
| 317 |
Entry indexType = new Entry(); |
| 318 |
indexType.t = null; |
| 319 |
indexType.e = null; |
| 320 |
indexType.size = 0; |
| 321 |
indexType.prim = p; |
| 322 |
mIndexTypes.addElement(indexType); |
| 323 |
return this; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public Builder addIndexSetType(Element e, int size, Primitive p) { |
| 337 |
Entry indexType = new Entry(); |
| 338 |
indexType.t = null; |
| 339 |
indexType.e = e; |
| 340 |
indexType.size = size; |
| 341 |
indexType.prim = p; |
| 342 |
mIndexTypes.addElement(indexType); |
| 343 |
return this; |
| 344 |
} |
| 345 |
|
| 346 |
Type newType(Element e, int size) { |
| 347 |
Type.Builder tb = new Type.Builder(mRS, e); |
| 348 |
tb.setX(size); |
| 349 |
return tb.create(); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public Mesh create() { |
| 358 |
mRS.validate(); |
| 359 |
long[] vtx = new long[mVertexTypeCount]; |
| 360 |
long[] idx = new long[mIndexTypes.size()]; |
| 361 |
int[] prim = new int[mIndexTypes.size()]; |
| 362 |
|
| 363 |
Allocation[] vertexBuffers = new Allocation[mVertexTypeCount]; |
| 364 |
Allocation[] indexBuffers = new Allocation[mIndexTypes.size()]; |
| 365 |
Primitive[] primitives = new Primitive[mIndexTypes.size()]; |
| 366 |
|
| 367 |
for(int ct = 0; ct < mVertexTypeCount; ct ++) { |
| 368 |
Allocation alloc = null; |
| 369 |
Entry entry = mVertexTypes[ct]; |
| 370 |
if (entry.t != null) { |
| 371 |
alloc = Allocation.createTyped(mRS, entry.t, mUsage); |
| 372 |
} else if(entry.e != null) { |
| 373 |
alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage); |
| 374 |
} else { |
| 375 |
|
| 376 |
throw new IllegalStateException("Builder corrupt, no valid element in entry."); |
| 377 |
} |
| 378 |
vertexBuffers[ct] = alloc; |
| 379 |
vtx[ct] = alloc.getID(mRS); |
| 380 |
} |
| 381 |
|
| 382 |
for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 383 |
Allocation alloc = null; |
| 384 |
Entry entry = (Entry)mIndexTypes.elementAt(ct); |
| 385 |
if (entry.t != null) { |
| 386 |
alloc = Allocation.createTyped(mRS, entry.t, mUsage); |
| 387 |
} else if(entry.e != null) { |
| 388 |
alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage); |
| 389 |
} else { |
| 390 |
|
| 391 |
throw new IllegalStateException("Builder corrupt, no valid element in entry."); |
| 392 |
} |
| 393 |
long allocID = (alloc == null) ? 0 : alloc.getID(mRS); |
| 394 |
indexBuffers[ct] = alloc; |
| 395 |
primitives[ct] = entry.prim; |
| 396 |
|
| 397 |
idx[ct] = allocID; |
| 398 |
prim[ct] = entry.prim.mID; |
| 399 |
} |
| 400 |
|
| 401 |
long id = mRS.nMeshCreate(vtx, idx, prim); |
| 402 |
Mesh newMesh = new Mesh(id, mRS); |
| 403 |
newMesh.mVertexBuffers = vertexBuffers; |
| 404 |
newMesh.mIndexBuffers = indexBuffers; |
| 405 |
newMesh.mPrimitives = primitives; |
| 406 |
|
| 407 |
return newMesh; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public static class AllocationBuilder { |
| 419 |
RenderScript mRS; |
| 420 |
|
| 421 |
class Entry { |
| 422 |
Allocation a; |
| 423 |
Primitive prim; |
| 424 |
} |
| 425 |
|
| 426 |
int mVertexTypeCount; |
| 427 |
Entry[] mVertexTypes; |
| 428 |
|
| 429 |
Vector mIndexTypes; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
@UnsupportedAppUsage |
| 435 |
public AllocationBuilder(RenderScript rs) { |
| 436 |
mRS = rs; |
| 437 |
mVertexTypeCount = 0; |
| 438 |
mVertexTypes = new Entry[16]; |
| 439 |
mIndexTypes = new Vector(); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public int getCurrentVertexTypeIndex() { |
| 448 |
return mVertexTypeCount - 1; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public int getCurrentIndexSetIndex() { |
| 457 |
return mIndexTypes.size() - 1; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
@UnsupportedAppUsage |
| 470 |
public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException { |
| 471 |
if (mVertexTypeCount >= mVertexTypes.length) { |
| 472 |
throw new IllegalStateException("Max vertex types exceeded."); |
| 473 |
} |
| 474 |
|
| 475 |
mVertexTypes[mVertexTypeCount] = new Entry(); |
| 476 |
mVertexTypes[mVertexTypeCount].a = a; |
| 477 |
mVertexTypeCount++; |
| 478 |
return this; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
@UnsupportedAppUsage |
| 492 |
public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) { |
| 493 |
Entry indexType = new Entry(); |
| 494 |
indexType.a = a; |
| 495 |
indexType.prim = p; |
| 496 |
mIndexTypes.addElement(indexType); |
| 497 |
return this; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
@UnsupportedAppUsage |
| 509 |
public AllocationBuilder addIndexSetType(Primitive p) { |
| 510 |
Entry indexType = new Entry(); |
| 511 |
indexType.a = null; |
| 512 |
indexType.prim = p; |
| 513 |
mIndexTypes.addElement(indexType); |
| 514 |
return this; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
@UnsupportedAppUsage |
| 523 |
public Mesh create() { |
| 524 |
mRS.validate(); |
| 525 |
|
| 526 |
long[] vtx = new long[mVertexTypeCount]; |
| 527 |
long[] idx = new long[mIndexTypes.size()]; |
| 528 |
int[] prim = new int[mIndexTypes.size()]; |
| 529 |
|
| 530 |
Allocation[] indexBuffers = new Allocation[mIndexTypes.size()]; |
| 531 |
Primitive[] primitives = new Primitive[mIndexTypes.size()]; |
| 532 |
Allocation[] vertexBuffers = new Allocation[mVertexTypeCount]; |
| 533 |
|
| 534 |
for(int ct = 0; ct < mVertexTypeCount; ct ++) { |
| 535 |
Entry entry = mVertexTypes[ct]; |
| 536 |
vertexBuffers[ct] = entry.a; |
| 537 |
vtx[ct] = entry.a.getID(mRS); |
| 538 |
} |
| 539 |
|
| 540 |
for(int ct = 0; ct < mIndexTypes.size(); ct ++) { |
| 541 |
Entry entry = (Entry)mIndexTypes.elementAt(ct); |
| 542 |
long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS); |
| 543 |
indexBuffers[ct] = entry.a; |
| 544 |
primitives[ct] = entry.prim; |
| 545 |
|
| 546 |
idx[ct] = allocID; |
| 547 |
prim[ct] = entry.prim.mID; |
| 548 |
} |
| 549 |
|
| 550 |
long id = mRS.nMeshCreate(vtx, idx, prim); |
| 551 |
Mesh newMesh = new Mesh(id, mRS); |
| 552 |
newMesh.mVertexBuffers = vertexBuffers; |
| 553 |
newMesh.mIndexBuffers = indexBuffers; |
| 554 |
newMesh.mPrimitives = primitives; |
| 555 |
|
| 556 |
return newMesh; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public static class TriangleMeshBuilder { |
| 567 |
float mVtxData[]; |
| 568 |
int mVtxCount; |
| 569 |
int mMaxIndex; |
| 570 |
short mIndexData[]; |
| 571 |
int mIndexCount; |
| 572 |
RenderScript mRS; |
| 573 |
Element mElement; |
| 574 |
|
| 575 |
float mNX = 0; |
| 576 |
float mNY = 0; |
| 577 |
float mNZ = -1; |
| 578 |
float mS0 = 0; |
| 579 |
float mT0 = 0; |
| 580 |
float mR = 1; |
| 581 |
float mG = 1; |
| 582 |
float mB = 1; |
| 583 |
float mA = 1; |
| 584 |
|
| 585 |
int mVtxSize; |
| 586 |
int mFlags; |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public static final int COLOR = 0x0001; |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public static final int NORMAL = 0x0002; |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
public static final int TEXTURE_0 = 0x0100; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 612 |
public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { |
| 613 |
mRS = rs; |
| 614 |
mVtxCount = 0; |
| 615 |
mMaxIndex = 0; |
| 616 |
mIndexCount = 0; |
| 617 |
mVtxData = new float[128]; |
| 618 |
mIndexData = new short[128]; |
| 619 |
mVtxSize = vtxSize; |
| 620 |
mFlags = flags; |
| 621 |
|
| 622 |
if (vtxSize < 2 || vtxSize > 3) { |
| 623 |
throw new IllegalArgumentException("Vertex size out of range."); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
private void makeSpace(int count) { |
| 628 |
if ((mVtxCount + count) >= mVtxData.length) { |
| 629 |
float t[] = new float[mVtxData.length * 2]; |
| 630 |
System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 631 |
mVtxData = t; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
private void latch() { |
| 636 |
if ((mFlags & COLOR) != 0) { |
| 637 |
makeSpace(4); |
| 638 |
mVtxData[mVtxCount++] = mR; |
| 639 |
mVtxData[mVtxCount++] = mG; |
| 640 |
mVtxData[mVtxCount++] = mB; |
| 641 |
mVtxData[mVtxCount++] = mA; |
| 642 |
} |
| 643 |
if ((mFlags & TEXTURE_0) != 0) { |
| 644 |
makeSpace(2); |
| 645 |
mVtxData[mVtxCount++] = mS0; |
| 646 |
mVtxData[mVtxCount++] = mT0; |
| 647 |
} |
| 648 |
if ((mFlags & NORMAL) != 0) { |
| 649 |
makeSpace(4); |
| 650 |
mVtxData[mVtxCount++] = mNX; |
| 651 |
mVtxData[mVtxCount++] = mNY; |
| 652 |
mVtxData[mVtxCount++] = mNZ; |
| 653 |
mVtxData[mVtxCount++] = 0.0f; |
| 654 |
} |
| 655 |
mMaxIndex ++; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 669 |
public TriangleMeshBuilder addVertex(float x, float y) { |
| 670 |
if (mVtxSize != 2) { |
| 671 |
throw new IllegalStateException("add mistmatch with declared components."); |
| 672 |
} |
| 673 |
makeSpace(2); |
| 674 |
mVtxData[mVtxCount++] = x; |
| 675 |
mVtxData[mVtxCount++] = y; |
| 676 |
latch(); |
| 677 |
return this; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public TriangleMeshBuilder addVertex(float x, float y, float z) { |
| 692 |
if (mVtxSize != 3) { |
| 693 |
throw new IllegalStateException("add mistmatch with declared components."); |
| 694 |
} |
| 695 |
makeSpace(4); |
| 696 |
mVtxData[mVtxCount++] = x; |
| 697 |
mVtxData[mVtxCount++] = y; |
| 698 |
mVtxData[mVtxCount++] = z; |
| 699 |
mVtxData[mVtxCount++] = 1.0f; |
| 700 |
latch(); |
| 701 |
return this; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
public TriangleMeshBuilder setTexture(float s, float t) { |
| 714 |
if ((mFlags & TEXTURE_0) == 0) { |
| 715 |
throw new IllegalStateException("add mistmatch with declared components."); |
| 716 |
} |
| 717 |
mS0 = s; |
| 718 |
mT0 = t; |
| 719 |
return this; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
public TriangleMeshBuilder setNormal(float x, float y, float z) { |
| 733 |
if ((mFlags & NORMAL) == 0) { |
| 734 |
throw new IllegalStateException("add mistmatch with declared components."); |
| 735 |
} |
| 736 |
mNX = x; |
| 737 |
mNY = y; |
| 738 |
mNZ = z; |
| 739 |
return this; |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
public TriangleMeshBuilder setColor(float r, float g, float b, float a) { |
| 754 |
if ((mFlags & COLOR) == 0) { |
| 755 |
throw new IllegalStateException("add mistmatch with declared components."); |
| 756 |
} |
| 757 |
mR = r; |
| 758 |
mG = g; |
| 759 |
mB = b; |
| 760 |
mA = a; |
| 761 |
return this; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 775 |
public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) { |
| 776 |
if((idx1 >= mMaxIndex) || (idx1 < 0) || |
| 777 |
(idx2 >= mMaxIndex) || (idx2 < 0) || |
| 778 |
(idx3 >= mMaxIndex) || (idx3 < 0)) { |
| 779 |
throw new IllegalStateException("Index provided greater than vertex count."); |
| 780 |
} |
| 781 |
if ((mIndexCount + 3) >= mIndexData.length) { |
| 782 |
short t[] = new short[mIndexData.length * 2]; |
| 783 |
System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 784 |
mIndexData = t; |
| 785 |
} |
| 786 |
mIndexData[mIndexCount++] = (short)idx1; |
| 787 |
mIndexData[mIndexCount++] = (short)idx2; |
| 788 |
mIndexData[mIndexCount++] = (short)idx3; |
| 789 |
return this; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 808 |
public Mesh create(boolean uploadToBufferObject) { |
| 809 |
Element.Builder b = new Element.Builder(mRS); |
| 810 |
b.add(Element.createVector(mRS, |
| 811 |
Element.DataType.FLOAT_32, |
| 812 |
mVtxSize), "position"); |
| 813 |
if ((mFlags & COLOR) != 0) { |
| 814 |
b.add(Element.F32_4(mRS), "color"); |
| 815 |
} |
| 816 |
if ((mFlags & TEXTURE_0) != 0) { |
| 817 |
b.add(Element.F32_2(mRS), "texture0"); |
| 818 |
} |
| 819 |
if ((mFlags & NORMAL) != 0) { |
| 820 |
b.add(Element.F32_3(mRS), "normal"); |
| 821 |
} |
| 822 |
mElement = b.create(); |
| 823 |
|
| 824 |
int usage = Allocation.USAGE_SCRIPT; |
| 825 |
if (uploadToBufferObject) { |
| 826 |
usage |= Allocation.USAGE_GRAPHICS_VERTEX; |
| 827 |
} |
| 828 |
|
| 829 |
Builder smb = new Builder(mRS, usage); |
| 830 |
smb.addVertexType(mElement, mMaxIndex); |
| 831 |
smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); |
| 832 |
|
| 833 |
Mesh sm = smb.create(); |
| 834 |
|
| 835 |
sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData); |
| 836 |
if(uploadToBufferObject) { |
| 837 |
sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
| 838 |
} |
| 839 |
|
| 840 |
sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData); |
| 841 |
if (uploadToBufferObject) { |
| 842 |
sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT); |
| 843 |
} |
| 844 |
|
| 845 |
return sm; |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
|