From 1979caf360e62672f76bb85391949290faa0ed0f Mon Sep 17 00:00:00 2001 From: Ronja Date: Mon, 24 Jan 2022 17:48:06 +0100 Subject: [PATCH] WIP --- asset/flag.material.lx | 2 +- game.wren | 4 +- modifiers/trail/trail.modifier.lx | 12 +++-- modifiers/trail/trail.modifier.wren | 58 ++++++++++++++------- modifiers/trail/trail.wren | 81 +++++++++++++++++++++++------ outline/app.wren | 2 +- queue.wren | 10 ++-- 7 files changed, 124 insertions(+), 45 deletions(-) diff --git a/asset/flag.material.lx b/asset/flag.material.lx index 7dd7c9f..47e838e 100644 --- a/asset/flag.material.lx +++ b/asset/flag.material.lx @@ -3,6 +3,6 @@ material = { inputs = { sprite.image = "asset/flag" sprite.color = [1 1 1 1] - sprite.uv = [1 0 0 1] + sprite.uv = [0 0 1 1] } } \ No newline at end of file diff --git a/game.wren b/game.wren index 32c24e3..b35c7bd 100644 --- a/game.wren +++ b/game.wren @@ -45,8 +45,8 @@ class Game is Ready { Trail.reset(_blade) } -// app.color.r = app.color.g = app.color.b = (IO.timestamp()/20 % 1) - System.print(1/delta) + //app.color.r = app.color.g = app.color.b = (IO.timestamp()/20 % 1) + //System.print(1/delta) } //tick destroy() { diff --git a/modifiers/trail/trail.modifier.lx b/modifiers/trail/trail.modifier.lx index 5156330..98eb363 100644 --- a/modifiers/trail/trail.modifier.lx +++ b/modifiers/trail/trail.modifier.lx @@ -4,13 +4,15 @@ modifier = { field = "trail" display = "Trail" //the display in the editor class = "Trail" //The code generated name for the modifier API - block = { //The data for the modifier + block = { //The data for the modifiers fields = [ { name="edge_length" type="number" default=100 } - { name="time_based" type="boolean" default=false } - { name="length" type="number" default=500 } - { name="subdivisions_length" type="number" default=1000 } - { name="subdivisions_width" type="number" default=10 } + { name="time_based" type="boolean" default=true } + { name="normalize_uvs" type="boolean" default=true } + //{ name="interpolate_curves" type="boolean" default=true } //todo + { name="length" type="number" default=1 } + { name="subdivisions_length" type="number" default=30 } + { name="subdivisions_width" type="number" default=5 } { name="material" type="id32" editor="material" default="asset/flag"} ] } diff --git a/modifiers/trail/trail.modifier.wren b/modifiers/trail/trail.modifier.wren index 933f30c..03af015 100644 --- a/modifiers/trail/trail.modifier.wren +++ b/modifiers/trail/trail.modifier.wren @@ -157,10 +157,11 @@ import "luxe: world" for Block construct new() { _edge_length = 100 - _time_based = false - _length = 500 - _subdivisions_length = 1000 - _subdivisions_width = 10 + _time_based = true + _normalize_uvs = true + _length = 1 + _subdivisions_length = 30 + _subdivisions_width = 5 _material = "asset/flag" } //new @@ -170,6 +171,9 @@ import "luxe: world" for Block time_based { _time_based } time_based=(vvv) { _time_based = vvv } + normalize_uvs { _normalize_uvs } + normalize_uvs=(vvv) { _normalize_uvs = vvv } + length { _length } length=(vvv) { _length = vvv } @@ -199,6 +203,8 @@ import "luxe: world" for Block size = size + 4 // time_based + size = size + 4 // normalize_uvs + size = size + 8 // length size = size + 8 // subdivisions_length @@ -221,6 +227,7 @@ import "luxe: world" for Block "luxe.self.modifier": false, //:luxe:internal "edge_length": false, "time_based": false, + "normalize_uvs": false, "length": false, "subdivisions_length": false, "subdivisions_width": false, @@ -254,22 +261,27 @@ import "luxe: world" for Block var time_based = instance["time_based"] - if(time_based == null) time_based = false + if(time_based == null) time_based = true out.write_int32(time_based ? 1 : 0) + var normalize_uvs = instance["normalize_uvs"] + if(normalize_uvs == null) normalize_uvs = true + out.write_int32(normalize_uvs ? 1 : 0) + + var length = instance["length"] - if(length == null) length = 500 + if(length == null) length = 1 out.write_float64(length) var subdivisions_length = instance["subdivisions_length"] - if(subdivisions_length == null) subdivisions_length = 1000 + if(subdivisions_length == null) subdivisions_length = 30 out.write_float64(subdivisions_length) var subdivisions_width = instance["subdivisions_width"] - if(subdivisions_width == null) subdivisions_width = 10 + if(subdivisions_width == null) subdivisions_width = 5 out.write_float64(subdivisions_width) @@ -284,7 +296,7 @@ import "luxe: world" for Block bytes_count_block() { - return 96 + return 108 } //bytes_count_block @@ -294,7 +306,7 @@ import "luxe: world" for Block out.write_uint32(compiler.string.hash("modifiers/trail/trail > data")) //fields count - out.write_int32(6) + out.write_int32(7) // edge_length out.write_uint32(compiler.string.hash("edge_length")) @@ -306,28 +318,35 @@ import "luxe: world" for Block // time_based out.write_uint32(compiler.string.hash("time_based")) out.write_uint32(1710517951) //type boolean - var time_based_default = false + var time_based_default = true out.write_int32(time_based_default ? 1 : 0) + // normalize_uvs + out.write_uint32(compiler.string.hash("normalize_uvs")) + out.write_uint32(1710517951) //type boolean + var normalize_uvs_default = true + out.write_int32(normalize_uvs_default ? 1 : 0) + + // length out.write_uint32(compiler.string.hash("length")) out.write_uint32(467038368) //type number - var length_default = 500 + var length_default = 1 out.write_float64(length_default) // subdivisions_length out.write_uint32(compiler.string.hash("subdivisions_length")) out.write_uint32(467038368) //type number - var subdivisions_length_default = 1000 + var subdivisions_length_default = 30 out.write_float64(subdivisions_length_default) // subdivisions_width out.write_uint32(compiler.string.hash("subdivisions_width")) out.write_uint32(467038368) //type number - var subdivisions_width_default = 10 + var subdivisions_width_default = 5 out.write_float64(subdivisions_width_default) @@ -373,10 +392,11 @@ import "luxe: world" for Block _inst_type = instance_type _inst = _inst_type.new(_block, 0) Block.add(_block, "edge_length", "number", 100) - Block.add(_block, "time_based", "boolean", false) - Block.add(_block, "length", "number", 500) - Block.add(_block, "subdivisions_length", "number", 1000) - Block.add(_block, "subdivisions_width", "number", 10) + Block.add(_block, "time_based", "boolean", true) + Block.add(_block, "normalize_uvs", "boolean", true) + Block.add(_block, "length", "number", 1) + Block.add(_block, "subdivisions_length", "number", 30) + Block.add(_block, "subdivisions_width", "number", 5) Block.add(_block, "material", "id32", "asset/flag") Block.set_type(_block, "modifiers/trail/trail > data") } //new @@ -388,6 +408,8 @@ import "luxe: world" for Block edge_length=(v) { Block.set(_block, "edge_length", _slot, v) } time_based { Block.get(_block, "time_based", _slot) } time_based=(v) { Block.set(_block, "time_based", _slot, v) } + normalize_uvs { Block.get(_block, "normalize_uvs", _slot) } + normalize_uvs=(v) { Block.set(_block, "normalize_uvs", _slot, v) } length { Block.get(_block, "length", _slot) } length=(v) { Block.set(_block, "length", _slot, v) } subdivisions_length { Block.get(_block, "subdivisions_length", _slot) } diff --git a/modifiers/trail/trail.wren b/modifiers/trail/trail.wren index 1fa1747..1a1f988 100644 --- a/modifiers/trail/trail.wren +++ b/modifiers/trail/trail.wren @@ -200,13 +200,15 @@ class TrailSystem is ModifierSystem { } tick(delta) { + //System.print("positions") record_positions(delta) //System.print("buffers") update_buffers() //System.print("debug") - //debug_draw() + debug_draw() //System.print("end") + } //tick record_positions(delta: Num){ @@ -216,7 +218,7 @@ class TrailSystem is ModifierSystem { var instance_data: TrailData = _instance_data[entity] var pos = Transform.get_pos_world(entity) - var up = Transform.local_dir_to_world(entity, 0, data.edge_length * 0.5, 0) + var up = Transform.local_dir_to_world(entity, 0, 1, 0) var prev_pos = instance_data.previous_pos || pos var prev_up = instance_data.previous_up || up @@ -250,6 +252,8 @@ class TrailSystem is ModifierSystem { dist = dist + diff var step = data.length / data.subdivisions_length while(dist > step){ + //interpolate between points + //todo: extrapolate curve instead of linear dist = dist - step var t = 1 - dist / diff var point = Point.new([ @@ -262,6 +266,7 @@ class TrailSystem is ModifierSystem { Math.lerp(prev_up.z, up.z, t) ]) Math.normalize(point.up) + //System.print(point.up) if(instance_data.points.count >= data.subdivisions_length - 1) instance_data.points.dequeue() instance_data.points.enqueue(point) } @@ -274,25 +279,15 @@ class TrailSystem is ModifierSystem { update_buffers(){ each{|entity, data: ModifierData| + if(!Transform.has(entity)) return + var instance_data: TrailData = _instance_data[entity] var positions = Floats.new(Render.vertex_buffer_get_size(instance_data.position_buffer) / 4) Render.vertex_buffer_get_data(instance_data.position_buffer, positions, positions.size * 4, 0) //System.print("positions(%(Render.vertex_buffer_get_size(instance_data.position_buffer))):") //System.print((0...(positions.size/4)).map{|i|"[%(positions[i*4]), %(positions[i*4+1]), %(positions[i*4+2])]"}.join("\n")) var i = -1 - for(p in instance_data.points){ - i = i + 1 - for(ii in 0...data.subdivisions_width){ - var t = ii / (data.subdivisions_width-1) - var offset_x = Math.lerp(-p.up.x, p.up.x, t) - var offset_y = Math.lerp(-p.up.y, p.up.y, t) - var offset_z = Math.lerp(-p.up.z, p.up.z, t) - positions[i*4*data.subdivisions_width + ii*4 + 0] = p.pos.x + offset_x * data.edge_length * 0.5 - positions[i*4*data.subdivisions_width + ii*4 + 1] = p.pos.y + offset_y * data.edge_length * 0.5 - positions[i*4*data.subdivisions_width + ii*4 + 2] = p.pos.z + offset_z * data.edge_length * 0.5 - } - } - if(Transform.has(entity)){ + { i = i + 1 var start = Transform.local_point_to_world(entity, 0, data.edge_length * -0.5, 0) @@ -308,6 +303,62 @@ class TrailSystem is ModifierSystem { positions[i*4*data.subdivisions_width + ii*4 + 2] = pos_z } } + + var step = data.length / data.subdivisions_length + + var point_index = -1 + System.print("------------") + while(true){ + point_index = point_index + 1 + if(point_index >= instance_data.points.count) break + i = i + 1 + var p:Point = instance_data.points[point_index] + + var from: Vec + var from_up: Vec + if(point_index == 0){ + from = Transform.get_pos_world(entity) + from_up = Transform.local_dir_to_world(entity, 0, 1, 0) + } else { + var prev: Point = instance_data.points[point_index-1] + from = prev.pos + from_up = prev.up + } + + System.print(from_up) + + var to = p.pos + var to_up = p.up + + var t = instance_data.progress_since_last_point / step + var pos_x = Math.lerp(from.x, to.x, t) + var pos_y = Math.lerp(from.y, to.y, t) + var pos_z = Math.lerp(from.z, to.z, t) + + var up_x = Math.lerp(from_up.x, to_up.x, t) + var up_y = Math.lerp(from_up.y, to_up.y, t) + var up_z = Math.lerp(from_up.z, to_up.z, t) + + pos_x = to.x + pos_y = to.y + pos_z = to.z + + up_x = to_up.x + up_y = to_up.y + up_z = to_up.z + + var x = instance_data.points.count + 1 - i + for(y in 0...data.subdivisions_width){ + var t = 1 - y / (data.subdivisions_width-1) + var offset_x = Math.lerp(-p.up.x, p.up.x, t) + var offset_y = Math.lerp(-p.up.y, p.up.y, t) + var offset_z = Math.lerp(-p.up.z, p.up.z, t) + positions[x*4*data.subdivisions_width + y*4 + 0] = pos_x + offset_x * data.edge_length * 0.5 + positions[x*4*data.subdivisions_width + y*4 + 1] = pos_y + offset_y * data.edge_length * 0.5 + positions[x*4*data.subdivisions_width + y*4 + 2] = pos_z + offset_z * data.edge_length * 0.5 + } + } + Render.vertex_buffer_replace(instance_data.position_buffer, positions, positions.length) Geometry.set_vert_count(instance_data.geometry, i.max(0) * 6 * (data.subdivisions_width-1)) } diff --git a/outline/app.wren b/outline/app.wren index a0f1768..41d244d 100644 --- a/outline/app.wren +++ b/outline/app.wren @@ -18,7 +18,7 @@ class App { scale { Render.drawable_ratio() } construct new() { - + _color = [1,1,1,1] //create worlds diff --git a/queue.wren b/queue.wren index b027d8f..c24f10f 100644 --- a/queue.wren +++ b/queue.wren @@ -43,6 +43,12 @@ class Queue is Sequence{ empty(){_count == 0} + [index]{ + index = _head - _count + 1 + index + if(index < 0) index = _data.count + index + return _data[index] + } + iterate(iter){ if(!iter) return _count > 0 ? 0 : false if(iter >= _count - 1) return false @@ -50,8 +56,6 @@ class Queue is Sequence{ } iteratorValue(iter){ - var index = _head - _count + 1 + iter - if(index < 0) index = _data.count + index - return _data[index] + return this[iter] } } \ No newline at end of file