Skip to content

Commit 1a7a4af

Browse files
committed
Add example
1 parent db14465 commit 1a7a4af

File tree

7 files changed

+140
-10
lines changed

7 files changed

+140
-10
lines changed

.defignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.cursorrules
2+
/.clang-format
3+
/.gitignore
4+
/.gitattributes
5+
/.vscode
6+
/.github

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Thumbs.db
88
*.pyc
99
.project
1010
.cproject
11-
builtins
11+
builtins
12+
/.vscode

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ Check out the [API](#api) and the included example for more information.
3333
Sets the seed for the noise generator.
3434

3535
**Parameters:**
36-
* `seed` <kbd>(number): The integer seed value.
36+
* `seed` <kbd>(number)</kbd>: The integer seed value.
3737

3838
### `simplex.noise2(x, y [, octaves [, persistence [, lacunarity]]])`
3939
Computes 2D Simplex noise, optionally with fractal parameters.
4040

4141
**Parameters:**
4242
* `x` <kbd>(number)</kbd>: The x-coordinate.
4343
* `y` <kbd>(number)</kbd>: The y-coordinate.
44-
* `octaves` <kbd>(number, optional)</kbd>: Number of noise layers (must be > 0). Defaults to 1.
45-
* `persistence` <kbd>(number, optional)</kbd>: Amplitude decrease per octave. Defaults to 0.5.
44+
* `octaves` <kbd>(integer, optional)</kbd>: Number of noise layers (must be > 0). Defaults to 1. It describes the number of loops that the code is run - making for finer and finer detail.
45+
* `persistence` <kbd>(number, optional)</kbd>: Amplitude decrease per octave. Defaults to 0.5. Describes the maximum disturbance from the undisturbed position of a wave.
4646
* `lacunarity` <kbd>(number, optional)</kbd>: Frequency increase per octave. Defaults to 2.0.
4747

4848
**Returns:**
@@ -55,8 +55,8 @@ Computes 3D Simplex noise, optionally with fractal parameters.
5555
* `x` <kbd>(number)</kbd>: The x-coordinate.
5656
* `y` <kbd>(number)</kbd>: The y-coordinate.
5757
* `z` <kbd>(number)</kbd>: The z-coordinate.
58-
* `octaves` <kbd>(number, optional)</kbd>: Number of noise layers (must be > 0). Defaults to 1.
59-
* `persistence` <kbd>(number, optional)</kbd>: Amplitude decrease per octave. Defaults to 0.5.
58+
* `octaves` <kbd>(integer, optional)</kbd>: Number of noise layers (must be > 0). Defaults to 1. It describes the number of loops that the code is run - making for finer and finer detail.
59+
* `persistence` <kbd>(number, optional)</kbd>: Amplitude decrease per octave. Defaults to 0.5. Describes the maximum disturbance from the undisturbed position of a wave.
6060
* `lacunarity` <kbd>(number, optional)</kbd>: Frequency increase per octave. Defaults to 2.0.
6161

6262
**Returns:**
@@ -70,8 +70,8 @@ Computes 4D Simplex noise, optionally with fractal parameters.
7070
* `y` <kbd>(number)</kbd>: The y-coordinate.
7171
* `z` <kbd>(number)</kbd>: The z-coordinate.
7272
* `w` <kbd>(number)</kbd>: The w-coordinate.
73-
* `octaves` <kbd>(number, optional)</kbd>: Number of noise layers (must be > 0). Defaults to 1.
74-
* `persistence` <kbd>(number, optional)</kbd>: Amplitude decrease per octave. Defaults to 0.5.
73+
* `octaves` <kbd>(integer, optional)</kbd>: Number of noise layers (must be > 0). Defaults to 1. It describes the number of loops that the code is run - making for finer and finer detail.
74+
* `persistence` <kbd>(number, optional)</kbd>: Amplitude decrease per octave. Defaults to 0.5. Describes the maximum disturbance from the undisturbed position of a wave.
7575
* `lacunarity` <kbd>(number, optional)</kbd>: Frequency increase per octave. Defaults to 2.0.
7676

7777
**Returns:**

example/example.collection

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,27 @@ embedded_instances {
3737
"near_z: -10.0\\n"
3838
"far_z: 10.0\\n"
3939
"orthographic_projection: 1\\n"
40+
"orthographic_zoom: 0.5\\n"
4041
"\"\n"
4142
"}\n"
4243
""
4344
}
45+
embedded_instances {
46+
id: "debug_label"
47+
data: "embedded_components {\n"
48+
" id: \"label\"\n"
49+
" type: \"label\"\n"
50+
" data: \"size {\\n"
51+
" x: 250.0\\n"
52+
" y: 50.0\\n"
53+
"}\\n"
54+
"text: \\\"<fps>\\\"\\n"
55+
"font: \\\"/example/vera_large.font\\\"\\n"
56+
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
57+
"\"\n"
58+
"}\n"
59+
""
60+
position {
61+
y: -290.0
62+
}
63+
}

example/example.script

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,107 @@
1+
--- Calculates and sets camera zoom to maintain aspect ratio
2+
local function camera_update_zoom(self)
3+
local display_width = sys.get_config_int("display.width")
4+
local display_height = sys.get_config_int("display.height")
5+
local window_width,window_height = window.get_size()
6+
local scale = window.get_display_scale()
7+
local zoom = math.min(window_width / scale / display_width, window_height / scale / display_height)
8+
go.set("/camera#camera", "orthographic_zoom", zoom)
9+
end
10+
11+
--- Fills the noise texture buffer with new values based on the current position and scale.
12+
local function noise_fill_stream(data)
13+
local stream = data.stream
14+
local i = 1
15+
local h = data.height
16+
local w = data.width
17+
local scale = data.scale
18+
for y = 1, h do
19+
for x = 1, w do
20+
local v = simplex.noise2(data.x + x * scale, data.y + y * scale) * 255
21+
stream[i + 0] = v -- R
22+
stream[i + 1] = v -- G
23+
stream[i + 2] = v -- B
24+
stream[i + 3] = 255 -- A
25+
26+
i = i + 4
27+
end
28+
end
29+
end
30+
31+
--- Initializes the noise texture. Returns initialized data.
32+
local function noise_init()
33+
local data = {
34+
x = 0,
35+
y = 0,
36+
scale = 0.05,
37+
width = 128,
38+
height = 128,
39+
40+
texture_path = "/example/noise_texture.texturec"
41+
}
42+
43+
local buf = buffer.create(data.width * data.height, { { name=hash("rgba"), type=buffer.VALUE_TYPE_UINT8, count=4 } } )
44+
local stream = buffer.get_stream(buf, hash("rgba"))
45+
data.buffer = buf
46+
data.stream = stream
47+
48+
noise_fill_stream(data)
49+
50+
data.texture_params = {
51+
width = data.width,
52+
height = data.height,
53+
type = resource.TEXTURE_TYPE_2D,
54+
format = resource.TEXTURE_FORMAT_RGBA,
55+
num_mip_maps = 1,
56+
min_filter = graphics.TEXTURE_FILTER_LINEAR,
57+
mag_filter = graphics.TEXTURE_FILTER_LINEAR,
58+
}
59+
60+
local texture_id = resource.create_texture(data.texture_path, data.texture_params, buf)
61+
go.set("/debug_view#model", "texture0", texture_id)
62+
63+
return data
64+
end
65+
66+
--- Updates the noise texture with new values based on time.
67+
local function noise_update(data, dt)
68+
data.x = data.x + 1 * dt
69+
70+
noise_fill_stream(data)
71+
72+
resource.set_texture(data.texture_path, data.texture_params, data.buffer)
73+
end
74+
75+
--- Calculates and displays the current frames per second using a rolling average.
76+
local function fps_update(self)
77+
-- fps meter is from examples/bunnymark
78+
if not self.frames then
79+
self.frames = {}
80+
end
81+
self.frames[#self.frames + 1] = socket.gettime()
82+
local fps = 0
83+
if #self.frames == 61 then
84+
table.remove(self.frames, 1)
85+
fps = 1 / ((self.frames[#self.frames] - self.frames[1]) / (#self.frames - 1))
86+
end
87+
label.set_text("/debug_label#label", ("FPS: %.2f"):format(fps))
88+
end
89+
90+
--
91+
-- Script
92+
--
93+
194
function init(self)
2-
95+
self.data = noise_init()
396
end
497

598
function final(self)
699
end
7100

8101
function update(self, dt)
102+
camera_update_zoom(self)
103+
noise_update(self.data, dt)
104+
fps_update(self)
9105
end
10106

11107
function on_message(self, message_id, message, sender)

example/vera_large.font

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
font: "/builtins/fonts/vera_mo_bd.ttf"
2+
material: "/builtins/fonts/font-df.material"
3+
size: 32
4+
outline_alpha: 1.0
5+
outline_width: 3.0
6+
output_format: TYPE_DISTANCE_FIELD
7+
characters: " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

game.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ game_binding = /builtins/input/all.input_bindingc
2323
[html5]
2424
heap_size = 32
2525
cssfile = /builtins/manifests/web/dark_theme.css
26-
scale_mode = fit
26+
scale_mode = stretch
2727

2828
[render]
2929
clear_color_red = 0.25

0 commit comments

Comments
 (0)