فهرست منبع

Added Sprite Local UV example

Björn Ritzl 3 ماه پیش
والد
کامیت
ae87837ec1

+ 18 - 0
material/sprite_local_uv/all.texture_profiles

@@ -0,0 +1,18 @@
+path_settings {
+  path: "**"
+  profile: "Default"
+}
+profiles {
+  name: "Default"
+  platforms {
+    os: OS_ID_GENERIC
+    formats {
+      format: TEXTURE_FORMAT_RGBA
+      compression_level: BEST
+      compression_type: COMPRESSION_TYPE_DEFAULT
+    }
+    mipmaps: false
+    max_texture_size: 0
+    premultiply_alpha: true
+  }
+}

BIN
material/sprite_local_uv/assets/images/elementStone019.png


BIN
material/sprite_local_uv/assets/images/elementStone023.png


+ 7 - 0
material/sprite_local_uv/assets/sprites.atlas

@@ -0,0 +1,7 @@
+images {
+  image: "/assets/images/elementStone019.png"
+}
+images {
+  image: "/assets/images/elementStone023.png"
+}
+extrude_borders: 2

+ 17 - 0
material/sprite_local_uv/example.md

@@ -0,0 +1,17 @@
+---
+tags: sprite
+title: Sprite local UV
+brief: This example shows how to get local UV coordinates of a sprite regardless of sprite size
+author: Defold Foundation
+scripts: sprite_local_uv.script, sprite_local_uv.vp, sprite_local_uv.fp
+---
+
+The example uses two game objects, each with a sprite component and a script.
+
+![example](example.png)
+
+The sprite component uses a custom sprite material `sprite_local_uv.material` with `local_position` and `sprite_size` as two vertex attributes. The `local_position` attribute is of semantic type "Position" and coordinate space "Local" while the `sprite_size` attribute is of semantic type "User" and will be set by the script.
+
+![material](material.png)
+
+The script gets the size of the sprite and sets it as the `sprite_size` vertex attribute.

BIN
material/sprite_local_uv/example.png


+ 52 - 0
material/sprite_local_uv/example/sprite_local_uv.collection

@@ -0,0 +1,52 @@
+name: "sprite_local_uv"
+scale_along_z: 0
+embedded_instances {
+  id: "square"
+  data: "components {\n"
+  "  id: \"size\"\n"
+  "  component: \"/example/sprite_local_uv.script\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"default_animation: \\\"elementStone023\\\"\\n"
+  "material: \\\"/example/sprite_local_uv.material\\\"\\n"
+  "textures {\\n"
+  "  sampler: \\\"texture_sampler\\\"\\n"
+  "  texture: \\\"/assets/sprites.atlas\\\"\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    x: 360.0
+    y: 200.0
+  }
+}
+embedded_instances {
+  id: "rectangle"
+  data: "components {\n"
+  "  id: \"size\"\n"
+  "  component: \"/example/sprite_local_uv.script\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"default_animation: \\\"elementStone019\\\"\\n"
+  "material: \\\"/example/sprite_local_uv.material\\\"\\n"
+  "size {\\n"
+  "  x: 140.0\\n"
+  "  y: 140.0\\n"
+  "}\\n"
+  "textures {\\n"
+  "  sampler: \\\"texture_sampler\\\"\\n"
+  "  texture: \\\"/assets/sprites.atlas\\\"\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    x: 360.0
+    y: 500.0
+  }
+}

+ 29 - 0
material/sprite_local_uv/example/sprite_local_uv.fp

@@ -0,0 +1,29 @@
+#version 140
+
+// from sprite_local_uv.vp
+in mediump vec2 var_texcoord0;
+in highp vec2 var_position_local;
+
+out vec4 out_fragColor;
+
+uniform mediump sampler2D texture_sampler;
+uniform fs_uniforms
+{
+    mediump vec4 tint;
+};
+
+void main()
+{
+    // Pre-multiply alpha since all runtime textures already are
+    mediump vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
+
+    // sample color from sprite texture
+    vec4 color = texture(texture_sampler, var_texcoord0.xy) * tint_pm;
+
+    // mix local position with red and green color of sprite to
+    // create a gradient across the entire sprite
+    out_fragColor.rg = mix(color.rg, var_position_local.xy, 0.3);
+    // use blue and alpha from the sprite
+    out_fragColor.b = color.b;
+    out_fragColor.a = color.a;
+}

+ 38 - 0
material/sprite_local_uv/example/sprite_local_uv.material

@@ -0,0 +1,38 @@
+name: "sprite"
+tags: "tile"
+vertex_program: "/example/sprite_local_uv.vp"
+fragment_program: "/example/sprite_local_uv.fp"
+vertex_constants {
+  name: "view_proj"
+  type: CONSTANT_TYPE_VIEWPROJ
+}
+fragment_constants {
+  name: "tint"
+  type: CONSTANT_TYPE_USER
+  value {
+    x: 1.0
+    y: 1.0
+    z: 1.0
+    w: 1.0
+  }
+}
+samplers {
+  name: "texture_sampler"
+  wrap_u: WRAP_MODE_CLAMP_TO_EDGE
+  wrap_v: WRAP_MODE_CLAMP_TO_EDGE
+  filter_min: FILTER_MODE_MIN_DEFAULT
+  filter_mag: FILTER_MODE_MAG_DEFAULT
+}
+attributes {
+  name: "position_local"
+  semantic_type: SEMANTIC_TYPE_POSITION
+  vector_type: VECTOR_TYPE_VEC2
+}
+attributes {
+  name: "sprite_size"
+  double_values {
+    v: 64.0
+    v: 64.0
+  }
+  vector_type: VECTOR_TYPE_VEC2
+}

+ 10 - 0
material/sprite_local_uv/example/sprite_local_uv.script

@@ -0,0 +1,10 @@
+function init(self)
+	-- get the sprite size from the sprite component propertry 'size'
+	local size = go.get("#sprite", "size")
+
+	-- set the size on the sprite material in the custom vertex attribute 'sprite_size'
+	go.set("#sprite", "sprite_size", size)
+
+	-- rotate the sprite
+	go.animate(".", "euler.z", go.PLAYBACK_LOOP_FORWARD, 360, go.EASING_LINEAR, 5)
+end

+ 26 - 0
material/sprite_local_uv/example/sprite_local_uv.vp

@@ -0,0 +1,26 @@
+#version 140
+
+// positions are in world space
+in highp vec4 position;
+in mediump vec2 texcoord0;
+
+// position in local space
+in highp vec2 position_local;
+// size of sprite in pixels
+in mediump vec2 sprite_size;
+
+out mediump vec2 var_texcoord0;
+out highp vec2 var_position_local;
+
+uniform vs_uniforms
+{
+    highp mat4 view_proj;
+};
+
+void main()
+{
+    gl_Position = view_proj * vec4(position.xyz, 1.0);
+    var_texcoord0 = texcoord0;
+    // calculate normalized local position and pass it on to the fragment program
+    var_position_local = (position_local + sprite_size * 0.5) / sprite_size;
+}

+ 64 - 0
material/sprite_local_uv/game.project

@@ -0,0 +1,64 @@
+[project]
+title = Sprite Local UV
+
+[bootstrap]
+main_collection = /example/sprite_local_uv.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
+
+[graphics]
+texture_profiles = /all.texture_profiles
+
+[collection]
+max_instances = 32765
+
+[particle_fx]
+max_emitter_count = 1024
+
+[render]
+clear_color_blue = 1.0
+clear_color_green = 1.0
+clear_color_red = 1.0
+

BIN
material/sprite_local_uv/material.png


BIN
material/sprite_local_uv/size.png