Prechádzať zdrojové kódy

Merge pull request #48 from defold/dev-cubemap-reflection

Added cubemap reflection example
Jhonny Göransson 9 mesiacov pred
rodič
commit
05a9c04f4f

+ 1 - 0
.gitignore

@@ -16,3 +16,4 @@ node_modules/
 
 # Mac
 .DS_Store
+/.editor_settings

+ 1 - 0
examples/_main/examples.lua

@@ -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", { name = "screenspace", nobg = true } }
 examples["particles"] = { "confetti", "particlefx", "modifiers", "fire_and_smoke", "fireworks" }
 examples["sound"] = { "music", "fade_in_out", "panning" }

+ 8 - 2
examples/_main/loader.go

@@ -455,9 +455,9 @@ embedded_components {
   ""
 }
 embedded_components {
-  id: "material/screenspace"
+  id: "model/cubemap"
   type: "collectionproxy"
-  data: "collection: \"/examples/material/screenspace/screenspace.collection\"\n"
+  data: "collection: \"/examples/model/cubemap/cubemap.collection\"\n"
   ""
 }
 embedded_components {
@@ -472,3 +472,9 @@ embedded_components {
   data: "collection: \"/examples/movement/look_rotation/look_rotation.collection\"\n"
   ""
 }
+embedded_components {
+  id: "material/screenspace"
+  type: "collectionproxy"
+  data: "collection: \"/examples/material/screenspace/screenspace.collection\"\n"
+  ""
+}

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
examples/model/cubemap/assets/defold_logo.gltf


+ 6 - 0
examples/model/cubemap/assets/env.cubemap

@@ -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"

BIN
examples/model/cubemap/assets/nx.jpg


BIN
examples/model/cubemap/assets/ny.jpg


BIN
examples/model/cubemap/assets/nz.jpg


BIN
examples/model/cubemap/assets/px.jpg


BIN
examples/model/cubemap/assets/py.jpg


BIN
examples/model/cubemap/assets/pz.jpg


+ 45 - 0
examples/model/cubemap/cubemap.collection

@@ -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: 100.0\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    z: 10.0
+  }
+}

+ 7 - 0
examples/model/cubemap/cubemap.md

@@ -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 - 0
examples/model/cubemap/cubemap.script

@@ -0,0 +1,34 @@
+local ZOOM_SPEED = 0.1
+local ROTATION_SPEED = 1
+
+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 - 0
examples/model/cubemap/cubemap_model.fp

@@ -0,0 +1,7 @@
+varying mediump vec3 vReflect;
+
+uniform samplerCube envMap;
+
+void main() {
+	gl_FragColor = textureCube(envMap, vReflect);
+}

+ 25 - 0
examples/model/cubemap/cubemap_model.material

@@ -0,0 +1,25 @@
+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
+}
+vertex_constants {
+  name: "world"
+  type: CONSTANT_TYPE_WORLD
+}
+vertex_constants {
+  name: "cameraPosition"
+  type: CONSTANT_TYPE_USER
+  value {
+  }
+}
+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 - 0
examples/model/cubemap/cubemap_model.vp

@@ -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 );
+}

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov