Ver código fonte

Added textured mesh example

Björn Ritzl 2 semanas atrás
pai
commit
c3d6c0217b

+ 38 - 0
mesh/textured/example.md

@@ -0,0 +1,38 @@
+---
+tags: mesh
+title: Textured Mesh
+brief: This example shows how to create a textured mesh component in the shape of a rectangle.
+author: Defold Foundation
+scripts: texturedmesh.fp, texturedmesh.vp
+---
+
+This example contains a game object with a mesh component in the shape of a rectangle (quad). The quad is defined in `quad.buffer` as the four points (triangle strip) in the `position` stream. The triangle also defines the texture coordinate (UV) at each point.
+
+```
+[
+    {
+        "name": "position",
+        "type": "float32",
+        "count": 3,
+        "data": [
+            -0.5, -0.5, 0,
+             0.5, -0.5, 0,
+            -0.5,  0.5, 0,
+             0.5,  0.5, 0
+        ]
+    },
+    {
+        "name": "texcoord0",
+        "type": "float32",
+        "count": 2,
+        "data": [
+            0.0, 0.0,
+            1.0, 0.0,
+            0.0, 1.0,
+            1.0, 1.0
+        ]
+    }
+]
+```
+
+Texture by [Kenney.nl](https://kenney.nl/assets/prototype-textures)

BIN
mesh/textured/example/floor_ground_grass.png


+ 49 - 0
mesh/textured/example/orbit_camera.script

@@ -0,0 +1,49 @@
+-- The initial zoom level
+go.property("zoom", 3)
+-- The speed of the zoom
+go.property("zoom_speed", 0.1)
+-- The speed of the rotation
+go.property("rotation_speed", 0.5)
+-- The offset of the camera from the origin
+go.property("offset", vmath.vector3(0, 0, 0))
+
+function init(self)
+	-- Acquire input focus to receive input events
+	msg.post(".", "acquire_input_focus")
+
+	-- Initialize start values
+	self.yaw = go.get(".", "euler.y")
+	self.pitch = go.get(".", "euler.x")
+	self.zoom_offset = 0
+	self.current_yaw = self.yaw
+	self.current_pitch = self.pitch
+	self.current_zoom = self.zoom_offset
+end
+
+function update(self, dt)
+	-- Animate camera rotation and zoom
+	self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
+	self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
+	self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)
+
+	-- Calculate rotation and position
+	local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
+	local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
+	local camera_rotation = camera_yaw * camera_pitch
+	local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))
+
+	-- Set camera position and rotation
+	go.set_position(camera_position)
+	go.set_rotation(camera_rotation)
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("touch") and not action.pressed then
+		self.yaw   = self.yaw   - action.dx * self.rotation_speed
+		self.pitch = self.pitch + action.dy * self.rotation_speed
+	elseif action_id == hash("mouse_wheel_up") then
+		self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
+	elseif action_id == hash("mouse_wheel_down") then
+		self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
+	end
+end

+ 25 - 0
mesh/textured/example/quad.buffer

@@ -0,0 +1,25 @@
+[
+    {
+        "name": "position",
+        "type": "float32",
+        "count": 3,
+        "data": [
+            -0.5, -0.5, 0,
+             0.5, -0.5, 0,
+            -0.5,  0.5, 0,
+             0.5,  0.5, 0
+        ]
+    },
+    {
+        "name": "texcoord0",
+        "type": "float32",
+        "count": 2,
+        "data": [
+            0.0, 0.0,
+            1.0, 0.0,
+            0.0, 1.0,
+            1.0, 1.0
+        ]
+    }
+]
+]

+ 38 - 0
mesh/textured/example/texturedmesh.collection

@@ -0,0 +1,38 @@
+name: "texturedmesh"
+scale_along_z: 0
+embedded_instances {
+  id: "go"
+  data: "embedded_components {\n"
+  "  id: \"mesh\"\n"
+  "  type: \"mesh\"\n"
+  "  data: \"material: \\\"/example/texturedmesh.material\\\"\\n"
+  "vertices: \\\"/example/quad.buffer\\\"\\n"
+  "textures: \\\"/example/floor_ground_grass.png\\\"\\n"
+  "primitive_type: PRIMITIVE_TRIANGLE_STRIP\\n"
+  "position_stream: \\\"position\\\"\\n"
+  "normal_stream: \\\"position\\\"\\n"
+  "\"\n"
+  "}\n"
+  ""
+}
+embedded_instances {
+  id: "camera"
+  data: "components {\n"
+  "  id: \"orbit_camera\"\n"
+  "  component: \"/example/orbit_camera.script\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"camera\"\n"
+  "  type: \"camera\"\n"
+  "  data: \"aspect_ratio: 1.0\\n"
+  "fov: 0.7854\\n"
+  "near_z: 0.01\\n"
+  "far_z: 1000.0\\n"
+  "auto_aspect_ratio: 1\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    z: 6.0
+  }
+}

+ 23 - 0
mesh/textured/example/texturedmesh.fp

@@ -0,0 +1,23 @@
+#version 140
+
+in highp vec4 var_position;
+in mediump vec2 var_texcoord0;
+
+out vec4 out_fragColor;
+
+uniform mediump sampler2D tex0;
+
+uniform fs_uniforms
+{
+	mediump vec4 tint;
+};
+
+void main()
+{
+	// Pre-multiply alpha since all runtime textures already are
+	vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
+
+	vec4 color = texture(tex0, var_texcoord0) * tint_pm;
+
+	out_fragColor = vec4(color.rgb, 1.0);
+}

+ 35 - 0
mesh/textured/example/texturedmesh.material

@@ -0,0 +1,35 @@
+name: "mesh"
+tags: "model"
+vertex_program: "/example/texturedmesh.vp"
+fragment_program: "/example/texturedmesh.fp"
+vertex_space: VERTEX_SPACE_LOCAL
+vertex_constants {
+  name: "mtx_proj"
+  type: CONSTANT_TYPE_PROJECTION
+}
+vertex_constants {
+  name: "mtx_worldview"
+  type: CONSTANT_TYPE_WORLDVIEW
+}
+vertex_constants {
+  name: "mtx_view"
+  type: CONSTANT_TYPE_VIEW
+}
+fragment_constants {
+  name: "tint"
+  type: CONSTANT_TYPE_USER
+  value {
+    x: 1.0
+    y: 1.0
+    z: 1.0
+    w: 1.0
+  }
+}
+samplers {
+  name: "tex0"
+  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
+  max_anisotropy: 0.0
+}

+ 27 - 0
mesh/textured/example/texturedmesh.vp

@@ -0,0 +1,27 @@
+#version 140
+
+// 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.
+
+in highp vec4 position;
+in mediump vec2 texcoord0;
+
+out highp vec4 var_position;
+out mediump vec2 var_texcoord0;
+
+uniform vs_uniforms
+{
+	uniform mediump mat4 mtx_worldview;
+	uniform mediump mat4 mtx_proj;
+	uniform mediump mat4 mtx_view;
+};
+
+void main()
+{
+	vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
+	var_position = p;
+	var_texcoord0 = texcoord0;
+	gl_Position = mtx_proj * p;
+}

+ 61 - 0
mesh/textured/game.project

@@ -0,0 +1,61 @@
+[project]
+title = Textured Mesh
+
+[bootstrap]
+main_collection = /example/texturedmesh.collectionc
+
+[input]
+game_binding = /builtins/input/all.input_bindingc
+repeat_interval = 0.05
+
+[display]
+width = 720
+height = 720
+high_dpi = 1
+
+[physics]
+scale = 0.02
+gravity_y = -500.0
+
+[script]
+shared_state = 1
+
+[collection_proxy]
+max_count = 256
+
+[label]
+subpixels = 1
+
+[sprite]
+subpixels = 1
+max_count = 32765
+
+[windows]
+iap_provider = 
+
+[android]
+package = com.defold.examples
+
+[ios]
+bundle_identifier = com.defold.examples
+
+[osx]
+bundle_identifier = com.defold.examples
+
+[html5]
+show_fullscreen_button = 0
+show_made_with_defold = 0
+scale_mode = no_scale
+heap_size = 64
+
+[collection]
+max_instances = 32765
+
+[particle_fx]
+max_emitter_count = 1024
+
+[render]
+clear_color_blue = 0.2
+clear_color_green = 0.2
+clear_color_red = 0.2
+