Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cubemap reflection example #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/_main/examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ examples["gui"] = {
"healthbar"
}
examples["input"] = { "move", "text", "down_duration", "mouse_and_touch" }
examples["model"] = { { name = "cubemap", nobg = true } }
examples["material"] = { "vertexcolor", { name = "unlit", nobg = true }, "uvgradient", "noise" }
examples["particles"] = { "confetti", "particlefx", "modifiers", "fire_and_smoke" }
examples["sound"] = { "music", "fade_in_out", "panning" }
Expand Down
6 changes: 6 additions & 0 deletions examples/_main/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,9 @@ embedded_components {
data: "collection: \"/examples/gui/healthbar/healthbar.collection\"\n"
""
}
embedded_components {
id: "model/cubemap"
type: "collectionproxy"
data: "collection: \"/examples/model/cubemap/cubemap.collection\"\n"
""
}
1 change: 1 addition & 0 deletions examples/model/cubemap/assets/defold_logo.gltf

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions examples/model/cubemap/assets/env.cubemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
right: "/examples/model/cubemap/assets/px.jpg"
left: "/examples/model/cubemap/assets/nx.jpg"
top: "/examples/model/cubemap/assets/py.jpg"
bottom: "/examples/model/cubemap/assets/ny.jpg"
front: "/examples/model/cubemap/assets/pz.jpg"
back: "/examples/model/cubemap/assets/nz.jpg"
Binary file added examples/model/cubemap/assets/nx.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/model/cubemap/assets/ny.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/model/cubemap/assets/nz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/model/cubemap/assets/px.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/model/cubemap/assets/py.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/model/cubemap/assets/pz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions examples/model/cubemap/cubemap.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "cubemap"
scale_along_z: 0
embedded_instances {
id: "logo"
data: "embedded_components {\n"
" id: \"model\"\n"
" type: \"model\"\n"
" data: \"mesh: \\\"/examples/model/cubemap/assets/defold_logo.gltf\\\"\\n"
"materials {\\n"
" name: \\\"Material\\\"\\n"
" material: \\\"/examples/model/cubemap/cubemap_model.material\\\"\\n"
" textures {\\n"
" sampler: \\\"envMap\\\"\\n"
" texture: \\\"/examples/model/cubemap/assets/env.cubemap\\\"\\n"
" }\\n"
"}\\n"
"\"\n"
"}\n"
""
scale3 {
x: 0.5
y: 0.5
z: 0.5
}
}
embedded_instances {
id: "camera"
data: "components {\n"
" id: \"main\"\n"
" component: \"/examples/model/cubemap/cubemap.script\"\n"
"}\n"
"embedded_components {\n"
" id: \"camera\"\n"
" type: \"camera\"\n"
" data: \"aspect_ratio: 1.0\\n"
"fov: 0.7854\\n"
"near_z: 1.0\\n"
"far_z: 1500.0\\n"
"\"\n"
"}\n"
""
position {
z: 10.0
}
}
7 changes: 7 additions & 0 deletions examples/model/cubemap/cubemap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Cubemap Reflection
brief: This example shows how to use a cubemap to draw environment reflections on a model.
scripts: cubemap.script, cubemap_model.fp, cubemap_model.vp
---

This example contains a game object with a model component in the shape of the Defold logo. The model has a special `cubemap_model.material` which uses a cubemap sampler to calculate reflections on the model from the cubemap.
34 changes: 34 additions & 0 deletions examples/model/cubemap/cubemap.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local ZOOM_SPEED = 0.1
local ROTATION_SPEED = 2

function init(self)
msg.post("@render:", "use_camera_projection")
msg.post(".", "acquire_input_focus")
self.yaw = 0 -- for camera rotation
self.pitch = 0 -- for camera rotation
self.zoom = 5 -- default zoom
self.zoom_offset = 0 -- modification from default zoom
end

function update(self, dt)
local camera_yaw = vmath.quat_rotation_y(math.rad(self.yaw))
local camera_pitch = vmath.quat_rotation_x(math.rad(self.pitch))
local camera_rot = camera_yaw * camera_pitch
local camera_position = vmath.rotate(camera_rot, vmath.vector3(0, 0, self.zoom + self.zoom_offset))
go.set_position(camera_position)
go.set_rotation(camera_rot)

local cameraposv4 = vmath.vector4(camera_position.x, camera_position.y, camera_position.z, 1)
go.set("logo#model", "cameraPosition", cameraposv4)
end

function on_input(self, action_id, action)
if action_id == hash("touch") then
self.yaw = self.yaw - action.dx * ROTATION_SPEED
self.pitch = self.pitch + action.dy * ROTATION_SPEED
elseif action_id == hash("wheel_up") then
self.zoom_offset = self.zoom_offset - ZOOM_SPEED
elseif action_id == hash("wheel_down") then
self.zoom_offset = self.zoom_offset + ZOOM_SPEED
end
end
7 changes: 7 additions & 0 deletions examples/model/cubemap/cubemap_model.fp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
varying mediump vec3 vReflect;

uniform samplerCube envMap;

void main() {
gl_FragColor = textureCube(envMap, vReflect);
}
57 changes: 57 additions & 0 deletions examples/model/cubemap/cubemap_model.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: "cubemap_model"
tags: "model"
vertex_program: "/examples/model/cubemap/cubemap_model.vp"
fragment_program: "/examples/model/cubemap/cubemap_model.fp"
vertex_constants {
name: "view_proj"
type: CONSTANT_TYPE_VIEWPROJ
value {
}
}
vertex_constants {
name: "world"
type: CONSTANT_TYPE_WORLD
value {
}
}
vertex_constants {
name: "normal_transform"
type: CONSTANT_TYPE_NORMAL
value {
}
}
vertex_constants {
name: "viewMatrix"
type: CONSTANT_TYPE_VIEW
value {
}
}
vertex_constants {
name: "world_view"
type: CONSTANT_TYPE_WORLDVIEW
value {
}
}
vertex_constants {
name: "cameraPosition"
type: CONSTANT_TYPE_USER
value {
}
}
fragment_constants {
name: "tint"
type: CONSTANT_TYPE_USER
value {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
}
samplers {
name: "envMap"
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
filter_min: FILTER_MODE_MIN_LINEAR
filter_mag: FILTER_MODE_MAG_LINEAR
}
21 changes: 21 additions & 0 deletions examples/model/cubemap/cubemap_model.vp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
uniform mediump mat4 view_proj;
uniform mediump mat4 world;
uniform mediump mat4 normal_transform;
uniform mediump mat4 world_view;
uniform mediump vec4 cameraPosition;

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

varying mediump vec3 vReflect;

void main()
{
vec4 worldP = world * vec4(position, 1.0);
gl_Position = view_proj * worldP;

vec3 worldNormal = normalize(normal);
vec3 cameraToVertex = normalize( worldP.xyz - cameraPosition.xyz );
vReflect = reflect( cameraToVertex, worldNormal );
}
8 changes: 8 additions & 0 deletions input/game.input_binding
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ mouse_trigger {
input: MOUSE_BUTTON_LEFT
action: "touch"
}
mouse_trigger {
input: MOUSE_WHEEL_UP
action: "wheel_up"
}
mouse_trigger {
input: MOUSE_WHEEL_DOWN
action: "wheel_down"
}
touch_trigger {
input: TOUCH_MULTI
action: "multitouch"
Expand Down