Skip to content

Commit

Permalink
Merge branch 'indiesoftby:main' into blockbuster
Browse files Browse the repository at this point in the history
  • Loading branch information
notbadgun committed Sep 20, 2023
2 parents 1679902 + 48573f3 commit 54e2463
Show file tree
Hide file tree
Showing 23 changed files with 244 additions and 154 deletions.
16 changes: 9 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install Java ♨️
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
distribution: "adopt"
java-version: "11"
distribution: "temurin"
java-version: "17"

- name: Build 🔧
env:
Expand All @@ -26,11 +26,13 @@ jobs:
mkdir -p build/bundle
# Download the latest bob.jar
BOB_SHA1=${DEFOLD_BOB_SHA1:-$(curl -s 'https://d.defold.com/stable/info.json' | jq -r .sha1)}
wget --progress=dot:mega -O build/bob.jar "https://d.defold.com/archive/${BOB_SHA1}/bob/bob.jar"
java -jar build/bob.jar --version
BOB_LOCAL_SHA1=$((java -jar build/bundle/bob.jar --version | cut -d' ' -f6) || true)
if [ "${BOB_LOCAL_SHA1}" != "${BOB_SHA1}" ]; then wget --progress=dot:mega -O build/bundle/bob.jar "https://d.defold.com/archive/${BOB_SHA1}/bob/bob.jar"; fi
java -jar build/bundle/bob.jar --version
java -jar build/bob.jar --email a@b.com --auth 123 --texture-compression true --bundle-output build/bundle/js-web --platform js-web --archive --variant release resolve build bundle
java -jar build/bundle/bob.jar --email a@b.com --auth 123 --texture-compression true --bundle-output build/bundle/js-web --platform js-web --archive --variant release resolve build bundle
(cd build/bundle && cp -r js-web/* public)
- name: Deploy to Pages 🚀
Expand Down
23 changes: 6 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Follow further developments:
* [x] Car physics.
* [x] Character controller.
* [x] Controls: keyboard, mouse look with a pointer lock.
* [x] Frustum culling for 3D meshes.
* [x] Frustum culling for 3D meshes. **Note: now it's better to use Defold's built-in frustum culling.**
* [x] Helper 3D math functions ([docs](#math3dlua)).

### Included Assets (i.e. Prefabs)
Expand Down Expand Up @@ -103,7 +103,7 @@ Scene3D uses the metric system, i.e. metres as its default unit for measuring di

### Installation

Use it in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your `game.project` file and in the dependencies field under project [add the link to **the ZIP file** of a specific release](https://github.com/indiesoftby/defold-scene3d/releases).
Use it in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your `game.project` file and in the dependencies field under project [add the link to **the ZIP file**](https://github.com/indiesoftby/defold-scene3d/archive/refs/heads/main.zip).

#### Required Dependencies

Expand All @@ -122,15 +122,6 @@ Scene3D can use the following dependencies if you include them in your project:
* [x] `basic_3d`: use it as a template to start developing a new 3D project/scene.
* [x] `shadows`: a scene to test shadows.

### The `game.project` Settings

At this moment, Defold supports only Bilinear (Linear Mipmap Nearest) or Trilinear (Linear Mipmap Linear) filtering to apply to textures. Scene3D can enable anisotropic filtering for all textures in the builds for the HTML5 platform:

```ini
[scene3d]
anisotropic_filtering = 8
```

### `math3d.lua`

The table briefly describes the included helper math functions. The source code of [`math3d.lua`](scene3d/helpers/math3d.lua) has more detailed explanation.
Expand All @@ -156,6 +147,10 @@ The table briefly describes the included helper math functions. The source code
| `math3d.repeat_(t, length)` | Loops the value t, so that it is never larger than length and never smaller than 0. | `Mathf.Repeat` |
| `math3d.sign(x)` | Returns the sign of x. | `Mathf.Sign` |
| `math3d.smooth_step(x, min, max)` | Interpolates between min and max with smoothing at the limits. | `Mathf.SmoothStep` |
| `math3d.smooth_damp(a, b, cur_velocity, smooth_time, max_speed, dt)` | Gradually changes a value towards a desired goal over time. | `Mathf.SmoothDamp` |
| `math3d.smooth_damp_angle(a, b, cur_velocity, smooth_time, max_speed, dt)` | Gradually changes an angle given in degrees towards a desired goal angle over time. | `Mathf.SmoothDampAngle` |
| **Camera** |
| `render3d.screen_to_world(x, y, z, [world_coord])` | Transforms a point from screen space into world space. | `Camera.ScreenToWorldPoint` |
| **Noise** |
| `scene3d.simplex_noise2(x, y)` | Generates 2D Perlin noise. Output range is [0, 1]. | `Mathf.PerlinNoise` |

Expand All @@ -175,17 +170,11 @@ The table briefly describes the included helper math functions. The source code
The following issues are critical for the further development of Scene3D. Please put 👍👍👍 on them!

* [Control sort order of rendered primitives](https://github.com/defold/defold/issues/3625) + [Allow sorting front-to-back](https://github.com/defold/defold/issues/5168).
* [Material option to enable anisotropic texture filtering](https://github.com/defold/defold/issues/6155).
* [Optimize file size of Buffer](https://github.com/defold/defold/issues/6272) + [Add option for indexed vertices for buffers to be used with the mesh component](https://github.com/defold/defold/issues/5477).
* [More matrix types as shader constants](https://github.com/defold/defold/issues/3789).
* [Custom vertex format](https://github.com/defold/defold/issues/3403).
* [Frustum and Occlusion culling](https://github.com/defold/defold/issues/3406) + [Add bounding box representation for gameobjects](https://github.com/defold/defold/issues/6292).
* [Create buffer resources at runtime](https://github.com/defold/defold/issues/5648).

#### Editor Issues

* [Copied collision 3d box doesn't show properly in the editor](https://github.com/defold/defold/issues/5049).
* [Editor rounds values when moving objects](https://github.com/defold/defold/issues/6414).
* [Render axis Z and horizonal grid in perspective camera mode](https://github.com/defold/defold/issues/6420).
* [Incremental adjustment of properties values](https://github.com/defold/defold/issues/6417).
* [Material constants are not easy to edit](https://github.com/defold/defold/issues/6413).
Expand Down
44 changes: 44 additions & 0 deletions examples/scenes/basic_3d/basic_3d.collection
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ embedded_instances {
" z: 0.0\n"
" w: 1.0\n"
" }\n"
" property_decls {\n"
" }\n"
"}\n"
""
position {
Expand All @@ -284,3 +286,45 @@ embedded_instances {
z: 1.0
}
}
embedded_instances {
id: "raycast_target"
data: "embedded_components {\n"
" id: \"mesh\"\n"
" type: \"mesh\"\n"
" data: \"material: \\\"/scene3d/materials/primitive_max_yellow_red.material\\\"\\n"
"vertices: \\\"/scene3d/assets/meshes/primitive_sphere.buffer\\\"\\n"
"textures: \\\"/scene3d/assets/textures/grid_10x10.png\\\"\\n"
"primitive_type: PRIMITIVE_TRIANGLES\\n"
"position_stream: \\\"position\\\"\\n"
"normal_stream: \\\"normal\\\"\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 0.5
y: 0.5
z: 0.5
}
}
21 changes: 21 additions & 0 deletions examples/scenes/basic_3d/basic_3d_main.script
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,26 @@ end
function on_message(self, message_id, message, sender)
end

local RAYCAST_GROUPS = { hash("default") }
local CLOSEST = { all = false }

function on_input(self, action_id, action)
if action_id == nil or action_id == hash("touch") then
local from = render3d.screen_to_world(action.screen_x, action.screen_y, 1)
local to = render3d.screen_to_world(action.screen_x, action.screen_y, 50)
-- UNCOMMENT TO DEBUG
-- if action.pressed then
-- go.set("/raycast_target", "position", from)
-- go.animate("/raycast_target", "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_LINEAR, 3)
-- else
local results = physics.raycast(from, to, RAYCAST_GROUPS, CLOSEST)
if results ~= nil then
for _, result in ipairs(results) do
go.cancel_animations("/raycast_target", "position")
go.animate("/raycast_target", "position", go.PLAYBACK_ONCE_FORWARD, result.position, go.EASING_OUTQUAD, 0.1)
break
end
end
-- end
end
end
6 changes: 3 additions & 3 deletions game.project
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ max_contacts = 10000
ray_cast_limit_3d = 1000
trigger_overlap_capacity = 100
world_count = 5
use_fixed_timestep = 1
max_collision_object_count = 2000
max_fixed_timesteps = 1

[html5]
scale_mode = fit
Expand Down Expand Up @@ -85,9 +88,6 @@ output_spirv = 1
[druid]
no_auto_input = 1

[scene3d]
anisotropic_filtering = 4

[collection]
max_instances = 2048

35 changes: 35 additions & 0 deletions scene3d/assets/shaders/basic.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// WebGL 1.0 / OpenGL ES 2.0 don't have "transpose" and "inverse" functions.
// + some versions aren't able to cast mat4 to mat3.
//
#if __VERSION__ < 300
mat3 mat33_transpose(mat3 m) {
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}

mat3 mat43_inverse(mat4 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];

float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;

float det = a00 * b01 + a01 * b11 + a02 * b21;

return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}
#else
mat3 mat33_transpose(mat3 m) {
return transpose(m);
}

mat3 mat43_inverse(mat4 m) {
return inverse(mat3(m));
}
#endif
32 changes: 3 additions & 29 deletions scene3d/assets/shaders/basic_color.vp
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,16 @@ varying mediump vec3 var_normal;
// varying mediump vec2 var_texcoord0;
varying lowp vec3 var_color;

//
// WebGL 1.0 / OpenGL ES 2.0 don't have "transpose" and "inverse" functions.
//
mat3 mat_transpose(mat3 m) {
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}

mat3 mat_inverse(mat3 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];

float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;

float det = a00 * b01 + a01 * b11 + a02 * b21;

return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}
//
//
//
#include "/scene3d/assets/shaders/basic.glsl"

void main()
{
var_world_position = (mtx_world * vec4(position.xyz, 1.0)).xyz;
var_world_normal = normalize(mat_transpose(mat_inverse(mat3(mtx_world))) * normal.xyz);
var_world_normal = normalize(mat33_transpose(mat43_inverse(mtx_world)) * normal.xyz);

vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
var_position = p.xyz;
var_normal = normalize(mat3(mtx_normal) * normal.xyz);
var_normal = normalize((mtx_normal * vec4(normal, 0.0)).xyz);
// var_texcoord0 = texcoord0;
var_color = color;

Expand Down
32 changes: 3 additions & 29 deletions scene3d/assets/shaders/basic_texcoord0.vp
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,16 @@ varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
// varying lowp vec3 var_color;

//
// WebGL 1.0 / OpenGL ES 2.0 don't have "transpose" and "inverse" functions.
//
mat3 mat_transpose(mat3 m) {
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}

mat3 mat_inverse(mat3 m) {
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];

float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;

float det = a00 * b01 + a01 * b11 + a02 * b21;

return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}
//
//
//
#include "/scene3d/assets/shaders/basic.glsl"

void main()
{
var_world_position = (mtx_world * vec4(position.xyz, 1.0)).xyz;
var_world_normal = normalize(mat_transpose(mat_inverse(mat3(mtx_world))) * normal.xyz);
var_world_normal = normalize(mat33_transpose(mat43_inverse(mtx_world)) * normal.xyz);

vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
var_position = p.xyz;
var_normal = normalize(mat3(mtx_normal) * normal.xyz);
var_normal = normalize((mtx_normal * vec4(normal, 0.0)).xyz);
var_texcoord0 = texcoord0;
// var_color = color;

Expand Down
34 changes: 34 additions & 0 deletions scene3d/helpers/math3d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,38 @@ function M.smooth_step(x, min, max)
return -2 * v1 * v1 * v1 + 3 * v2 * v2
end

--- Gradually changes a value towards a desired goal over time.
-- Based on Game Programming Gems 4, pp. 98-101.
function M.smooth_damp(a, b, cur_velocity, smooth_time, max_speed, dt)
smooth_time = math.max(0.0001, smooth_time)
local omega = 2 / smooth_time

local x = omega * dt
local exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x)
local change = a - b
local initial_b = b

if max_speed then
local max_change = max_speed * smooth_time
change = M.clamp(change, -max_change, max_change)
end
b = a - change

local temp = (cur_velocity + omega * change) * dt
cur_velocity = (cur_velocity - omega * temp) * exp
local result = b + (change + temp) * exp

if (initial_b - a > 0) == (result > initial_b) then
result = initial_b
cur_velocity = (result - initial_b) / dt
end

return result, cur_velocity
end

--- Gradually changes an angle (in degrees) towards a desired goal angle over time.
function M.smooth_damp_angle(a, b, cur_velocity, smooth_time, max_speed, dt)
b = a + M.delta_angle(a, b)
return M.smooth_damp(a, b, cur_velocity, smooth_time, max_speed, dt)
end
return M
Loading

0 comments on commit 54e2463

Please sign in to comment.