Skip to content

Commit

Permalink
Added uvgradient and noise examples
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Sep 27, 2024
1 parent f46f5ce commit a9ae8c0
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 1 deletion.
12 changes: 12 additions & 0 deletions examples/_main/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,15 @@ embedded_components {
data: "collection: \"/examples/gui/get_set_texture/get_set_texture.collection\"\n"
""
}
embedded_components {
id: "material/uvgradient"
type: "collectionproxy"
data: "collection: \"/examples/material/uvgradient/uvgradient.collection\"\n"
""
}
embedded_components {
id: "material/noise"
type: "collectionproxy"
data: "collection: \"/examples/material/noise/noise.collection\"\n"
""
}
2 changes: 1 addition & 1 deletion examples/_main/menu.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function init(self)
"get_set_font", "get_set_texture", "get_set_material",
}
self.index["input"] = { "move", "text", "down_duration", "mouse_and_touch" }
self.index["material"] = { "vertexcolor" }
self.index["material"] = { "vertexcolor", "uvgradient", "noise" }
self.index["particles"] = { "particlefx", "modifiers", "fire_and_smoke" }
self.index["sound"] = { "music", "fade_in_out", "panning" }
self.index["render"] = { "camera", "screen_to_world" }
Expand Down
30 changes: 30 additions & 0 deletions examples/material/noise/noise.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "noise"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"drawtoquad\"\n"
" component: \"/examples/material/noise/noise.script\"\n"
"}\n"
"embedded_components {\n"
" id: \"model\"\n"
" type: \"model\"\n"
" data: \"mesh: \\\"/builtins/assets/meshes/quad.dae\\\"\\n"
"name: \\\"{{NAME}}\\\"\\n"
"materials {\\n"
" name: \\\"default\\\"\\n"
" material: \\\"/examples/material/noise/noise.material\\\"\\n"
"}\\n"
"\"\n"
"}\n"
""
position {
x: 360.0
y: 360.0
}
scale3 {
x: 720.0
y: 720.0
z: 50.0
}
}
47 changes: 47 additions & 0 deletions examples/material/noise/noise.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
varying mediump vec2 var_texcoord0;
uniform lowp vec4 time;

// noise shader from https://www.shadertoy.com/view/XXBcDz

// pseudo random generator (white noise)
float rand(vec2 n)
{
return fract(sin(dot(n, vec2(12.9898, 78.233))) * 43758.5453);
}

// value noise
float noise(vec2 p)
{
vec2 ip = floor(p);
vec2 u = fract(p);
u = u * u * (3.0 - 2.0 * u);

float x = mix(rand(ip), rand(ip + vec2(1.0, 0.0)), u.x);
float y = mix(rand(ip + vec2(0.0, 1.0)), rand(ip + vec2(1.0, 1.0)), u.x);
float a = u.y;
float res = mix(x, y, a);
return res * res;
}

// used to rotate domain of noise function
const mat2 rot = mat2( 0.80, 0.60, -0.60, 0.80 );

// fast implementation
float fbm( vec2 p )
{
float f = 0.0;
f += 0.500000 * noise( p ); p = rot * p * 2.02;
f += 0.031250 * noise( p ); p = rot * p * 2.01;
f += 0.250000 * noise( p ); p = rot * p * 2.03;
f += 0.125000 * noise( p + 0.1 * sin(time.x) + 0.8 * time.x ); p = rot * p * 2.01;
f += 0.062500 * noise( p + 0.3 * sin(time.x) ); p = rot * p * 2.04;
f += 0.015625 * noise( p );
return f / 0.96875;
}

void main()
{
float n = fbm(var_texcoord0.xy);
gl_FragColor = vec4(n, n, n, 1.0);
}

31 changes: 31 additions & 0 deletions examples/material/noise/noise.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "noise"
tags: "model"
vertex_program: "/examples/material/noise/noise.vp"
fragment_program: "/examples/material/noise/noise.fp"
vertex_space: VERTEX_SPACE_LOCAL
vertex_constants {
name: "mtx_worldview"
type: CONSTANT_TYPE_WORLDVIEW
}
vertex_constants {
name: "mtx_view"
type: CONSTANT_TYPE_VIEW
}
vertex_constants {
name: "mtx_proj"
type: CONSTANT_TYPE_PROJECTION
}
vertex_constants {
name: "mtx_normal"
type: CONSTANT_TYPE_NORMAL
}
fragment_constants {
name: "time"
type: CONSTANT_TYPE_USER
value {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
}
8 changes: 8 additions & 0 deletions examples/material/noise/noise.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function init(self)
self.time = 0
end

function update(self, dt)
self.time = self.time + dt
go.set("#model", "time.x", self.time)
end
26 changes: 26 additions & 0 deletions examples/material/noise/noise.vp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// Positions can be world or local space, since world and normal
// matrices are identity for world vertex space materials.
// If world vertex space is selected, you can remove the
// normal matrix multiplication for optimal performance.

attribute highp vec4 position;
attribute mediump vec2 texcoord0;
attribute mediump vec3 normal;

uniform mediump mat4 mtx_worldview;
uniform mediump mat4 mtx_view;
uniform mediump mat4 mtx_proj;
uniform mediump mat4 mtx_normal;

varying highp vec4 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;

void main()
{
vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
var_texcoord0 = texcoord0;
gl_Position = mtx_proj * p;
}

47 changes: 47 additions & 0 deletions examples/material/noise/noise_copy.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
varying mediump vec2 var_texcoord0;
uniform lowp vec4 time;

// noise shader from https://www.shadertoy.com/view/XXBcDz

// pseudo random generator (white noise)
float rand(vec2 n)
{
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}

// value noise
float noise(vec2 p)
{
vec2 ip = floor(p);
vec2 u = fract(p);
u = u * u * (3.0 - 2.0 * u);

float x = mix(rand(ip), rand(ip + vec2(1.0,0.0)), u.x);
float y = mix(rand(ip + vec2(0.0, 1.0)), rand(ip + vec2(1.0, 1.0)), u.x);
float a = u.y;
float res = mix(x, y, a);
return res * res;
}

// used to rotate domain of noise function
const mat2 rot = mat2( 0.80, 0.60, -0.60, 0.80 );

// fast implementation
float fbm( vec2 p )
{
float f = 0.0;
f += 0.500000*noise( p ); p = rot*p*2.02;
f += 0.031250*noise( p ); p = rot*p*2.01;
f += 0.250000*noise( p ); p = rot*p*2.03;
f += 0.125000*noise( p + 0.1 * sin(time.x) + 0.8 * time.x ); p = rot*p*2.01;
f += 0.062500*noise( p + 0.3 * sin(time.x) ); p = rot*p*2.04;
f += 0.015625*noise( p );
return f / 0.96875;
}

void main()
{
float n = fbm(var_texcoord0.xy);
gl_FragColor = vec4(n, n, n, 1.0);
}

26 changes: 26 additions & 0 deletions examples/material/uvgradient/uvgradient.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: "uvgradient"
scale_along_z: 0
embedded_instances {
id: "go"
data: "embedded_components {\n"
" id: \"model\"\n"
" type: \"model\"\n"
" data: \"mesh: \\\"/builtins/assets/meshes/quad.dae\\\"\\n"
"name: \\\"{{NAME}}\\\"\\n"
"materials {\\n"
" name: \\\"default\\\"\\n"
" material: \\\"/examples/material/uvgradient/uvgradient.material\\\"\\n"
"}\\n"
"\"\n"
"}\n"
""
position {
x: 360.0
y: 360.0
}
scale3 {
x: 720.0
y: 720.0
z: 50.0
}
}
7 changes: 7 additions & 0 deletions examples/material/uvgradient/uvgradient.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
varying mediump vec2 var_texcoord0;

void main()
{
gl_FragColor = vec4(var_texcoord0.x, var_texcoord0.y, 0.5, 1.0f);
}

21 changes: 21 additions & 0 deletions examples/material/uvgradient/uvgradient.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "uvgradient"
tags: "model"
vertex_program: "/examples/material/uvgradient/uvgradient.vp"
fragment_program: "/examples/material/uvgradient/uvgradient.fp"
vertex_space: VERTEX_SPACE_LOCAL
vertex_constants {
name: "mtx_worldview"
type: CONSTANT_TYPE_WORLDVIEW
}
vertex_constants {
name: "mtx_view"
type: CONSTANT_TYPE_VIEW
}
vertex_constants {
name: "mtx_proj"
type: CONSTANT_TYPE_PROJECTION
}
vertex_constants {
name: "mtx_normal"
type: CONSTANT_TYPE_NORMAL
}
24 changes: 24 additions & 0 deletions examples/material/uvgradient/uvgradient.vp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

// Positions can be world or local space, since world and normal
// matrices are identity for world vertex space materials.
// If world vertex space is selected, you can remove the
// normal matrix multiplication for optimal performance.

attribute highp vec4 position;
attribute mediump vec2 texcoord0;
attribute mediump vec3 normal;

uniform mediump mat4 mtx_worldview;
uniform mediump mat4 mtx_view;
uniform mediump mat4 mtx_proj;
uniform mediump mat4 mtx_normal;

varying mediump vec2 var_texcoord0;

void main()
{
vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
var_texcoord0 = texcoord0;
gl_Position = mtx_proj * p;
}

0 comments on commit a9ae8c0

Please sign in to comment.