Ver código fonte

Added character example

Björn Ritzl 6 meses atrás
pai
commit
4a8593267e

BIN
model/character/assets/SourceSansPro-Semibold.ttf


+ 47 - 0
model/character/assets/materials/model_local.material

@@ -0,0 +1,47 @@
+name: "model"
+tags: "model"
+vertex_program: "/builtins/materials/model.vp"
+fragment_program: "/builtins/materials/model.fp"
+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
+}
+vertex_constants {
+  name: "light"
+  type: CONSTANT_TYPE_USER
+  value {
+    x: 1.0
+    y: 1.0
+    z: 10.0
+    w: 1.0
+  }
+}
+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
+}

BIN
model/character/assets/models/Knight.glb


BIN
model/character/assets/models/dungeon_texture.png


BIN
model/character/assets/models/floor_tile_large.gltf.glb


BIN
model/character/assets/models/knight_texture.png


BIN
model/character/assets/models/wall.gltf.glb


+ 5 - 0
model/character/assets/text48.font

@@ -0,0 +1,5 @@
+font: "/assets/SourceSansPro-Semibold.ttf"
+material: "/builtins/fonts/font.material"
+size: 48
+outline_alpha: 0.0
+outline_width: 0.0

BIN
model/character/character.png


+ 10 - 0
model/character/example.md

@@ -0,0 +1,10 @@
+---
+tags: model
+title: Character
+brief: This example shows how to view and play skeletal animations on a glTF model.
+scripts: player.script
+---
+
+![character](character.png)
+
+The setup consists of one `player` game object with a `model`, `camera` and `script` component. The `model` component uses "Knight.glb" and "knight_texture.png". The "Knight.glb" file contains meshes and animation data. The `player.script` is used to play different animations from "Knight.glb".

+ 135 - 0
model/character/example/character/example.collection

@@ -0,0 +1,135 @@
+name: "example"
+scale_along_z: 0
+embedded_instances {
+  id: "floor"
+  data: "embedded_components {\n"
+  "  id: \"model\"\n"
+  "  type: \"model\"\n"
+  "  data: \"mesh: \\\"/assets/models/floor_tile_large.gltf.glb\\\"\\n"
+  "name: \\\"{{NAME}}\\\"\\n"
+  "materials {\\n"
+  "  name: \\\"texture\\\"\\n"
+  "  material: \\\"/builtins/materials/model.material\\\"\\n"
+  "  textures {\\n"
+  "    sampler: \\\"tex0\\\"\\n"
+  "    texture: \\\"/assets/models/dungeon_texture.png\\\"\\n"
+  "  }\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  ""
+}
+embedded_instances {
+  id: "wall"
+  data: "embedded_components {\n"
+  "  id: \"model\"\n"
+  "  type: \"model\"\n"
+  "  data: \"mesh: \\\"/assets/models/wall.gltf.glb\\\"\\n"
+  "name: \\\"{{NAME}}\\\"\\n"
+  "materials {\\n"
+  "  name: \\\"texture\\\"\\n"
+  "  material: \\\"/builtins/materials/model.material\\\"\\n"
+  "  textures {\\n"
+  "    sampler: \\\"tex0\\\"\\n"
+  "    texture: \\\"/assets/models/dungeon_texture.png\\\"\\n"
+  "  }\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    z: -2.0
+  }
+}
+embedded_instances {
+  id: "player"
+  children: "camera"
+  data: "components {\n"
+  "  id: \"player\"\n"
+  "  component: \"/example/character/player.script\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"model\"\n"
+  "  type: \"model\"\n"
+  "  data: \"mesh: \\\"/assets/models/Knight.glb\\\"\\n"
+  "skeleton: \\\"/assets/models/Knight.glb\\\"\\n"
+  "animations: \\\"/assets/models/Knight.glb\\\"\\n"
+  "default_animation: \\\"T-Pose\\\"\\n"
+  "name: \\\"{{NAME}}\\\"\\n"
+  "materials {\\n"
+  "  name: \\\"knight_texture\\\"\\n"
+  "  material: \\\"/assets/materials/model_local.material\\\"\\n"
+  "  textures {\\n"
+  "    sampler: \\\"tex0\\\"\\n"
+  "    texture: \\\"/assets/models/knight_texture.png\\\"\\n"
+  "  }\\n"
+  "}\\n"
+  "materials {\\n"
+  "  name: \\\"knight_texture_no_skin\\\"\\n"
+  "  material: \\\"/assets/materials/model_local.material\\\"\\n"
+  "  textures {\\n"
+  "    sampler: \\\"tex0\\\"\\n"
+  "    texture: \\\"/assets/models/knight_texture.png\\\"\\n"
+  "  }\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  ""
+}
+embedded_instances {
+  id: "camera"
+  children: "instructions"
+  data: "components {\n"
+  "  id: \"orbit_camera\"\n"
+  "  component: \"/example/character/orbit_camera.script\"\n"
+  "  properties {\n"
+  "    id: \"zoom\"\n"
+  "    value: \"13.0\"\n"
+  "    type: PROPERTY_TYPE_NUMBER\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"camera\"\n"
+  "  type: \"camera\"\n"
+  "  data: \"aspect_ratio: 1.0\\n"
+  "fov: 0.7854\\n"
+  "near_z: 0.1\\n"
+  "far_z: 1000.0\\n"
+  "auto_aspect_ratio: 1\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    y: 5.0
+    z: 8.0
+  }
+  rotation {
+    x: -0.21643962
+    w: 0.976296
+  }
+}
+embedded_instances {
+  id: "instructions"
+  data: "embedded_components {\n"
+  "  id: \"label\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 128.0\\n"
+  "  y: 32.0\\n"
+  "}\\n"
+  "text: \\\"1-5 to play animations\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    y: -3.930587
+    z: -12.866642
+  }
+  scale3 {
+    x: 0.01
+    y: 0.01
+    z: 0.01
+  }
+}

+ 49 - 0
model/character/example/character/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("wheel_up") then
+		self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
+	elseif action_id == hash("wheel_down") then
+		self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
+	end
+end

+ 2 - 0
model/character/example/character/player.collection

@@ -0,0 +1,2 @@
+name: "player"
+scale_along_z: 0

+ 40 - 0
model/character/example/character/player.script

@@ -0,0 +1,40 @@
+-- play a model animation unless already playing the animation
+local function play_animation(self, animation_id, playback, callback)
+	if self.animation_id ~= animation_id then
+		self.animation_id = animation_id
+		model.play_anim("#model", animation_id, playback, nil, callback)
+	end
+end
+
+-- play the Idle animation if the player is not moving
+local function idle(self)
+	
+end
+
+-- play the Walk animation
+local function walk(self)
+	
+end
+
+local function attack(self)
+	
+end
+
+function init(self)
+	msg.post(".", "acquire_input_focus")
+	play_animation(self, "T-Pose", go.PLAYBACK_LOOP_FORWARD)
+end
+
+function on_input(self, action_id, action)
+	if action_id == hash("idle") then
+		play_animation(self, "Idle", go.PLAYBACK_LOOP_FORWARD)
+	elseif action_id == hash("walk") then
+		play_animation(self, "Walking_A", go.PLAYBACK_LOOP_FORWARD)
+	elseif action_id == hash("attack") then
+		play_animation(self, "1H_Melee_Attack_Chop", go.PLAYBACK_LOOP_FORWARD)
+	elseif action_id == hash("block") then
+		play_animation(self, "Block", go.PLAYBACK_LOOP_FORWARD)
+	elseif action_id == hash("cheer") then
+		play_animation(self, "Cheer", go.PLAYBACK_LOOP_FORWARD)
+	end
+end

+ 33 - 0
model/character/game.project

@@ -0,0 +1,33 @@
+[bootstrap]
+main_collection = /example/character/example.collectionc
+render = /builtins/render/default.renderc
+
+[script]
+shared_state = 1
+
+[display]
+width = 960
+height = 640
+
+[android]
+input_method = HiddenInputField
+package = com.defold.example.basic3d
+
+[project]
+dependencies = 
+title = character
+
+[physics]
+type = 3D
+
+[osx]
+bundle_identifier = com.defold.example.basic3d
+
+[ios]
+bundle_identifier = com.defold.example.basic3d
+
+[render]
+clear_color_red = 0.1
+clear_color_green = 0.1
+clear_color_blue = 0.1
+

+ 32 - 0
model/character/input/game.input_binding

@@ -0,0 +1,32 @@
+key_trigger {
+  input: KEY_1
+  action: "idle"
+}
+key_trigger {
+  input: KEY_2
+  action: "walk"
+}
+key_trigger {
+  input: KEY_3
+  action: "attack"
+}
+key_trigger {
+  input: KEY_4
+  action: "block"
+}
+key_trigger {
+  input: KEY_5
+  action: "cheer"
+}
+mouse_trigger {
+  input: MOUSE_BUTTON_1
+  action: "touch"
+}
+mouse_trigger {
+  input: MOUSE_WHEEL_UP
+  action: "wheel_up"
+}
+mouse_trigger {
+  input: MOUSE_WHEEL_DOWN
+  action: "wheel_down"
+}