Skip to content

Commit a9ae8c0

Browse files
committed
Added uvgradient and noise examples
1 parent f46f5ce commit a9ae8c0

File tree

12 files changed

+280
-1
lines changed

12 files changed

+280
-1
lines changed

examples/_main/loader.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,15 @@ embedded_components {
412412
data: "collection: \"/examples/gui/get_set_texture/get_set_texture.collection\"\n"
413413
""
414414
}
415+
embedded_components {
416+
id: "material/uvgradient"
417+
type: "collectionproxy"
418+
data: "collection: \"/examples/material/uvgradient/uvgradient.collection\"\n"
419+
""
420+
}
421+
embedded_components {
422+
id: "material/noise"
423+
type: "collectionproxy"
424+
data: "collection: \"/examples/material/noise/noise.collection\"\n"
425+
""
426+
}

examples/_main/menu.gui_script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function init(self)
112112
"get_set_font", "get_set_texture", "get_set_material",
113113
}
114114
self.index["input"] = { "move", "text", "down_duration", "mouse_and_touch" }
115-
self.index["material"] = { "vertexcolor" }
115+
self.index["material"] = { "vertexcolor", "uvgradient", "noise" }
116116
self.index["particles"] = { "particlefx", "modifiers", "fire_and_smoke" }
117117
self.index["sound"] = { "music", "fade_in_out", "panning" }
118118
self.index["render"] = { "camera", "screen_to_world" }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "noise"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "go"
5+
data: "components {\n"
6+
" id: \"drawtoquad\"\n"
7+
" component: \"/examples/material/noise/noise.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"model\"\n"
11+
" type: \"model\"\n"
12+
" data: \"mesh: \\\"/builtins/assets/meshes/quad.dae\\\"\\n"
13+
"name: \\\"{{NAME}}\\\"\\n"
14+
"materials {\\n"
15+
" name: \\\"default\\\"\\n"
16+
" material: \\\"/examples/material/noise/noise.material\\\"\\n"
17+
"}\\n"
18+
"\"\n"
19+
"}\n"
20+
""
21+
position {
22+
x: 360.0
23+
y: 360.0
24+
}
25+
scale3 {
26+
x: 720.0
27+
y: 720.0
28+
z: 50.0
29+
}
30+
}

examples/material/noise/noise.fp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
varying mediump vec2 var_texcoord0;
2+
uniform lowp vec4 time;
3+
4+
// noise shader from https://www.shadertoy.com/view/XXBcDz
5+
6+
// pseudo random generator (white noise)
7+
float rand(vec2 n)
8+
{
9+
return fract(sin(dot(n, vec2(12.9898, 78.233))) * 43758.5453);
10+
}
11+
12+
// value noise
13+
float noise(vec2 p)
14+
{
15+
vec2 ip = floor(p);
16+
vec2 u = fract(p);
17+
u = u * u * (3.0 - 2.0 * u);
18+
19+
float x = mix(rand(ip), rand(ip + vec2(1.0, 0.0)), u.x);
20+
float y = mix(rand(ip + vec2(0.0, 1.0)), rand(ip + vec2(1.0, 1.0)), u.x);
21+
float a = u.y;
22+
float res = mix(x, y, a);
23+
return res * res;
24+
}
25+
26+
// used to rotate domain of noise function
27+
const mat2 rot = mat2( 0.80, 0.60, -0.60, 0.80 );
28+
29+
// fast implementation
30+
float fbm( vec2 p )
31+
{
32+
float f = 0.0;
33+
f += 0.500000 * noise( p ); p = rot * p * 2.02;
34+
f += 0.031250 * noise( p ); p = rot * p * 2.01;
35+
f += 0.250000 * noise( p ); p = rot * p * 2.03;
36+
f += 0.125000 * noise( p + 0.1 * sin(time.x) + 0.8 * time.x ); p = rot * p * 2.01;
37+
f += 0.062500 * noise( p + 0.3 * sin(time.x) ); p = rot * p * 2.04;
38+
f += 0.015625 * noise( p );
39+
return f / 0.96875;
40+
}
41+
42+
void main()
43+
{
44+
float n = fbm(var_texcoord0.xy);
45+
gl_FragColor = vec4(n, n, n, 1.0);
46+
}
47+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "noise"
2+
tags: "model"
3+
vertex_program: "/examples/material/noise/noise.vp"
4+
fragment_program: "/examples/material/noise/noise.fp"
5+
vertex_space: VERTEX_SPACE_LOCAL
6+
vertex_constants {
7+
name: "mtx_worldview"
8+
type: CONSTANT_TYPE_WORLDVIEW
9+
}
10+
vertex_constants {
11+
name: "mtx_view"
12+
type: CONSTANT_TYPE_VIEW
13+
}
14+
vertex_constants {
15+
name: "mtx_proj"
16+
type: CONSTANT_TYPE_PROJECTION
17+
}
18+
vertex_constants {
19+
name: "mtx_normal"
20+
type: CONSTANT_TYPE_NORMAL
21+
}
22+
fragment_constants {
23+
name: "time"
24+
type: CONSTANT_TYPE_USER
25+
value {
26+
x: 1.0
27+
y: 1.0
28+
z: 1.0
29+
w: 1.0
30+
}
31+
}

examples/material/noise/noise.script

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function init(self)
2+
self.time = 0
3+
end
4+
5+
function update(self, dt)
6+
self.time = self.time + dt
7+
go.set("#model", "time.x", self.time)
8+
end

examples/material/noise/noise.vp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
// Positions can be world or local space, since world and normal
3+
// matrices are identity for world vertex space materials.
4+
// If world vertex space is selected, you can remove the
5+
// normal matrix multiplication for optimal performance.
6+
7+
attribute highp vec4 position;
8+
attribute mediump vec2 texcoord0;
9+
attribute mediump vec3 normal;
10+
11+
uniform mediump mat4 mtx_worldview;
12+
uniform mediump mat4 mtx_view;
13+
uniform mediump mat4 mtx_proj;
14+
uniform mediump mat4 mtx_normal;
15+
16+
varying highp vec4 var_position;
17+
varying mediump vec3 var_normal;
18+
varying mediump vec2 var_texcoord0;
19+
20+
void main()
21+
{
22+
vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
23+
var_texcoord0 = texcoord0;
24+
gl_Position = mtx_proj * p;
25+
}
26+

examples/material/noise/noise_copy.fp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
varying mediump vec2 var_texcoord0;
2+
uniform lowp vec4 time;
3+
4+
// noise shader from https://www.shadertoy.com/view/XXBcDz
5+
6+
// pseudo random generator (white noise)
7+
float rand(vec2 n)
8+
{
9+
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
10+
}
11+
12+
// value noise
13+
float noise(vec2 p)
14+
{
15+
vec2 ip = floor(p);
16+
vec2 u = fract(p);
17+
u = u * u * (3.0 - 2.0 * u);
18+
19+
float x = mix(rand(ip), rand(ip + vec2(1.0,0.0)), u.x);
20+
float y = mix(rand(ip + vec2(0.0, 1.0)), rand(ip + vec2(1.0, 1.0)), u.x);
21+
float a = u.y;
22+
float res = mix(x, y, a);
23+
return res * res;
24+
}
25+
26+
// used to rotate domain of noise function
27+
const mat2 rot = mat2( 0.80, 0.60, -0.60, 0.80 );
28+
29+
// fast implementation
30+
float fbm( vec2 p )
31+
{
32+
float f = 0.0;
33+
f += 0.500000*noise( p ); p = rot*p*2.02;
34+
f += 0.031250*noise( p ); p = rot*p*2.01;
35+
f += 0.250000*noise( p ); p = rot*p*2.03;
36+
f += 0.125000*noise( p + 0.1 * sin(time.x) + 0.8 * time.x ); p = rot*p*2.01;
37+
f += 0.062500*noise( p + 0.3 * sin(time.x) ); p = rot*p*2.04;
38+
f += 0.015625*noise( p );
39+
return f / 0.96875;
40+
}
41+
42+
void main()
43+
{
44+
float n = fbm(var_texcoord0.xy);
45+
gl_FragColor = vec4(n, n, n, 1.0);
46+
}
47+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: "uvgradient"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "go"
5+
data: "embedded_components {\n"
6+
" id: \"model\"\n"
7+
" type: \"model\"\n"
8+
" data: \"mesh: \\\"/builtins/assets/meshes/quad.dae\\\"\\n"
9+
"name: \\\"{{NAME}}\\\"\\n"
10+
"materials {\\n"
11+
" name: \\\"default\\\"\\n"
12+
" material: \\\"/examples/material/uvgradient/uvgradient.material\\\"\\n"
13+
"}\\n"
14+
"\"\n"
15+
"}\n"
16+
""
17+
position {
18+
x: 360.0
19+
y: 360.0
20+
}
21+
scale3 {
22+
x: 720.0
23+
y: 720.0
24+
z: 50.0
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
varying mediump vec2 var_texcoord0;
2+
3+
void main()
4+
{
5+
gl_FragColor = vec4(var_texcoord0.x, var_texcoord0.y, 0.5, 1.0f);
6+
}
7+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: "uvgradient"
2+
tags: "model"
3+
vertex_program: "/examples/material/uvgradient/uvgradient.vp"
4+
fragment_program: "/examples/material/uvgradient/uvgradient.fp"
5+
vertex_space: VERTEX_SPACE_LOCAL
6+
vertex_constants {
7+
name: "mtx_worldview"
8+
type: CONSTANT_TYPE_WORLDVIEW
9+
}
10+
vertex_constants {
11+
name: "mtx_view"
12+
type: CONSTANT_TYPE_VIEW
13+
}
14+
vertex_constants {
15+
name: "mtx_proj"
16+
type: CONSTANT_TYPE_PROJECTION
17+
}
18+
vertex_constants {
19+
name: "mtx_normal"
20+
type: CONSTANT_TYPE_NORMAL
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// Positions can be world or local space, since world and normal
3+
// matrices are identity for world vertex space materials.
4+
// If world vertex space is selected, you can remove the
5+
// normal matrix multiplication for optimal performance.
6+
7+
attribute highp vec4 position;
8+
attribute mediump vec2 texcoord0;
9+
attribute mediump vec3 normal;
10+
11+
uniform mediump mat4 mtx_worldview;
12+
uniform mediump mat4 mtx_view;
13+
uniform mediump mat4 mtx_proj;
14+
uniform mediump mat4 mtx_normal;
15+
16+
varying mediump vec2 var_texcoord0;
17+
18+
void main()
19+
{
20+
vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
21+
var_texcoord0 = texcoord0;
22+
gl_Position = mtx_proj * p;
23+
}
24+

0 commit comments

Comments
 (0)