Skip to content

Commit

Permalink
Xeltalliv/simple3D: fix draw range bounds checking, fix instance XY p…
Browse files Browse the repository at this point in the history
…ositions, packager warning, improve docs (#1500)
  • Loading branch information
Xeltalliv authored Jun 1, 2024
1 parent b4b5c70 commit 6d97253
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
20 changes: 11 additions & 9 deletions docs/Xeltalliv/simple3D.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ If texture coordinates are specified, but texture is not, the default texture is
---
```scratch
set [my mesh] texture () [clamp to edge v] [pixelated v] :: sensing
set [my mesh] cube texture [X+] () [clamp to edge v] [pixelated v] :: sensing
set [my mesh] cube texture (X+ v) () [clamp to edge v] [pixelated v] :: sensing
```
Used to upload texture into the mesh.
Calling this block depending on the type of texture used either uploads texture instantly (e.g. empty or text) or schedules texture to be uploaded into the mesh some time in the future (e.g. load from url or from costume).
Expand Down Expand Up @@ -527,7 +527,7 @@ Decodes a 3D model file and uploads it into a mesh. Block continues instantly, b

File formats:
- [obj](https://en.wikipedia.org/wiki/Wavefront_.obj_file) is a very common and well known 3D model file format. It supports UV texture coordinates, materials with colors and textures. However it does not have a standartized way to do vertex colors. This block implements a non-standart but widely supported way to represent vertex colors as 4th - 7th elements of `v`. The OBJ and MTL specification describes a lot of features, only some of which are currently (or even can be) supported by this importer. In particular, there is currently no way to import models which use multiple textures as this extensions only supports 1 texture per mesh. Normals and anything lighting related isn't and can't be supported. **In case both OBJ and MTL files need to be imported, combine them all into 1 list sequentially, first all of the MTL files and then the OBJ file.**
- [off](https://en.wikipedia.org/wiki/OFF_(file_format)) is a not that well known, but very simple file format. Is is quite neat for the use in scratch in general, as it's simpler than OBJ and unlike it, natively supports both vertex and face colors. It does not support textures or texture coordinates. You can read more about it and find a lot of example models [here](http://web.archive.org/web/20230331211230/https://people.sc.fsu.edu/~jburkardt/data/off/off.html). Since the format is so simple, this importer supports all features of it.
- [off](https://en.wikipedia.org/wiki/OFF_(file_format)) is a not that well known, but very simple file format. Is is quite neat for the use in scratch in general, as it's simpler than OBJ and unlike it, natively supports both vertex and face colors. It does not support textures or texture coordinates. You can read more about it and find a lot of example models [here](http://web.archive.org/web/20230331211230/https://people.sc.fsu.edu/~jburkardt/data/off/off.html).

Imported model is affected by transformation set with:
```scratch
Expand Down Expand Up @@ -585,7 +585,7 @@ Default for mesh is threshold:0 preserve opacity.

---
```scratch
set [my mesh] billboarding [on v] :: sensing
set [my mesh] billboarding (on v) :: sensing
```
Used for enabling and disabling billboarding.
When billboarding is enabled, the mesh is always facing in the opposite direction to your look direction (behind you).
Expand All @@ -595,7 +595,7 @@ Default for mesh is "off".

---
```scratch
set [my mesh] accurate interpolation [on v] :: sensing
set [my mesh] accurate interpolation (on v) :: sensing
```
Used for enabling a more accurate interpolation method which doesn't have issues of texture coordinates extrapolating outside of the specified range on the triangle edges, causing unpleasant looking seams. It is more computationally expensive and should only be used when that is an issue.
Enabling mipmapping and/or anisatropic filtering may prevent it from working and reintroduce seams.
Expand Down Expand Up @@ -657,6 +657,8 @@ Creates texture from costume.

**Do not forget: bitmap costumes have 2x2 subpixels, which here are counted as pixels.** So do not be surprized when your seemingly 8x8 costume turns into 16x16 texture.

**When using TurboWarp Packager, you need to disable "Remove raw asset data after loading to save RAM" in order for this block to work.**

---
```scratch
(texture from text [Hello World!] font [italic bold 32px sans-serif] color (#ffff00) :: sensing)
Expand Down Expand Up @@ -686,7 +688,7 @@ Creates black texture of given size.
### Text measurement <a name="blocks-text-measurement"></a>
```scratch
measure text [Hello World!] font [italic bold 32px sans-serif] :: sensing
(measure [up v] size ::sensing)
(measured (up v) size ::sensing)
```
Used for measuring how the text texture was or will be generated.
Outputs 4 sizes. Sizes can be negative.
Expand All @@ -701,7 +703,7 @@ Used for getting internal names of default and custom fonts. It is not neccesary

### View transformations <a name="blocks-view-transformations"></a>
```scratch
configure [to projected from view space] transformation :: sensing
configure [to projected from view space v] transformation :: sensing
```
Used for switching between which of the few transformations is currently active for editing.
Transformations `to world space from model space`, `to view space from world space`, `to projected from view space` are 3 sequential transformation used for drawing, explained at the top of this page.
Expand Down Expand Up @@ -784,7 +786,7 @@ Overwites 16 elements in list starting from the specified item, with the numbers

---
```scratch
reset transformation's [offset/rotation] :: sensing
reset transformation's (offset v) :: sensing
```
Resets offset or rotation of the transformation.

Expand All @@ -800,8 +802,8 @@ Transforms point using currently selected transformation. It is the fastest way

---
```scratch
transform X (0) Y (0) Z (0) from [world space] to [model space] :: sensing
transform direction X (0) Y (0) Z (0) from [world space] to [model space] :: sensing
transform X (0) Y (0) Z (0) from [world space v] to [model space v] :: sensing
transform direction X (0) Y (0) Z (0) from [world space v] to [model space v] :: sensing
```
Transforms point from specified coordinate system to another. Convenient, but slower.
Transform direction only applies rotations and does not apply offsets.
Expand Down
27 changes: 22 additions & 5 deletions extensions/Xeltalliv/simple3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Description: Make GPU accelerated 3D projects easily.
// By: Vadik1 <https://scratch.mit.edu/users/Vadik1/>
// License: MPL-2.0 AND BSD-3-Clause
// Version: 1.0.2
// Version: 1.0.3

(function (Scratch) {
"use strict";
Expand Down Expand Up @@ -1316,6 +1316,16 @@ void main() {
};
return texture;
}
// requireNonPackagedRuntime by LilyMakesThings
function requireNonPackagedRuntime(blockName) {
if (runtime.isPackaged) {
alert(
`To use the Simple3D ${blockName} block, the creator of the packaged project must uncheck "Remove raw asset data after loading to save RAM" under advanced settings in the packager.`
);
return false;
}
return true;
}
/*
* Profiler has shown that this was the main bottleneck, so:
* - loops were unrolled
Expand Down Expand Up @@ -2695,7 +2705,7 @@ void main() {
if (mesh.data.uvOffset) flags.push("UV_OFFSET");
if (mesh.buffers.instanceTransforms) {
flags.push("INSTANCING");
if (mesh.buffers.instanceTransforms.size == 3)
if (mesh.buffers.instanceTransforms.size <= 3)
flags.push("INSTANCE_POS");
if (mesh.buffers.instanceTransforms.size == 4)
flags.push("INSTANCE_POS_SCALE");
Expand Down Expand Up @@ -2946,8 +2956,11 @@ void main() {
? mesh.buffers.indices.bytesPerEl
: 1;
start = mesh.data.drawRange[0] * size;
const end = Math.min(start + mesh.data.drawRange[1], amount);
amount = end - start;
const end = Math.min(
mesh.data.drawRange[0] + mesh.data.drawRange[1],
amount
);
amount = end - mesh.data.drawRange[0];
}
if (mesh.buffers.instanceTransforms) {
if (mesh.buffers.indices) {
Expand Down Expand Up @@ -3095,6 +3108,10 @@ void main() {
def: function ({ NAME }, { target }) {
imageSourceSync = null;
imageSource = new Promise((resolve, reject) => {
if (!requireNonPackagedRuntime("texture from costume")) {
resolve(null);
return;
}
const costumeIndex = target.getCostumeIndexByName(NAME);
if (costumeIndex == -1) return;
const costume = target.sprite.costumes[costumeIndex];
Expand Down Expand Up @@ -3405,7 +3422,7 @@ void main() {
defaultValue: "Sans Serif",
},
SIZE: {
type: ArgumentType.STRING,
type: ArgumentType.NUMBER,
defaultValue: 32,
},
},
Expand Down
Binary file modified samples/Simple3D template.sb3
Binary file not shown.

0 comments on commit 6d97253

Please sign in to comment.