Particle physics experiment
This commit is contained in:
@@ -140,6 +140,7 @@ class Context {
|
||||
public static var particleHitX = 0.0;
|
||||
public static var particleHitY = 0.0;
|
||||
public static var particleHitZ = 0.0;
|
||||
public static var paintBody: arm.plugin.PhysicsBody = null;
|
||||
#end
|
||||
|
||||
public static var layerFilter = 0;
|
||||
|
||||
@@ -97,7 +97,9 @@ class MakeParticle {
|
||||
vert.add_uniform('mat4 W', '_worldMatrix');
|
||||
vert.write_attrib('wpos = mul(vec4(pos.xyz, 1.0), W);');
|
||||
frag.add_uniform('vec3 particleHit', '_particleHit');
|
||||
frag.write('float str = clamp(1.0 - distance(particleHit, wpos.xyz) * 2.0, 0.0, 1.0);');
|
||||
frag.write('dist = distance(particleHit, wpos.xyz) * 20.0;');
|
||||
frag.write('if (dist > 1.0) discard;');
|
||||
frag.write('float str = 1.0;');
|
||||
frag.write('if (particleHit.x == 0.0 && particleHit.y == 0.0 && particleHit.z == 0.0) str = 0.0;');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,8 +47,8 @@ class PhysicsWorld extends iron.Trait {
|
||||
init();
|
||||
|
||||
// Ensure physics are updated first in the lateUpdate list
|
||||
_lateUpdate = [lateUpdate];
|
||||
@:privateAccess iron.App.traitLateUpdates.insert(0, lateUpdate);
|
||||
// _lateUpdate = [lateUpdate];
|
||||
// @:privateAccess iron.App.traitLateUpdates.insert(0, lateUpdate);
|
||||
}
|
||||
|
||||
public function reset() {
|
||||
@@ -106,7 +106,7 @@ class PhysicsWorld extends iron.Trait {
|
||||
return res;
|
||||
}
|
||||
|
||||
function lateUpdate() {
|
||||
public function lateUpdate() {
|
||||
var t = Time.delta * timeScale;
|
||||
if (t == 0.0) return; // Simulation paused
|
||||
|
||||
|
||||
@@ -122,7 +122,12 @@ class RenderPathPaint {
|
||||
var tid = Context.layer.id;
|
||||
|
||||
if (Context.pdirty > 0) {
|
||||
if (Context.tool == ToolParticle) {
|
||||
#if arm_physics
|
||||
var particlePhysics = Context.particlePhysics;
|
||||
#else
|
||||
var particlePhysics = false;
|
||||
#end
|
||||
if (Context.tool == ToolParticle && !particlePhysics) {
|
||||
path.setTarget("texparticle");
|
||||
path.clearTarget(0x00000000);
|
||||
path.bindTarget("_main", "gbufferD");
|
||||
|
||||
@@ -342,6 +342,11 @@ class UIHeader {
|
||||
if (Context.tool == ToolParticle) {
|
||||
ui._x += 10 * ui.SCALE();
|
||||
var physHandle = Id.handle({ selected: false });
|
||||
Context.particlePhysics = ui.check(physHandle, tr("Physics"));
|
||||
if (physHandle.changed) {
|
||||
arm.util.ParticleUtil.initParticlePhysics();
|
||||
MakeMaterial.parsePaintMaterial();
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
@@ -431,6 +431,51 @@ class UISidebar {
|
||||
borderHandle = null;
|
||||
App.isResizing = false;
|
||||
}
|
||||
|
||||
#if arm_physics
|
||||
if (Context.tool == ToolParticle && Context.particlePhysics && inViewport && !Context.paint2d) {
|
||||
var world = arm.plugin.PhysicsWorld.active;
|
||||
world.lateUpdate();
|
||||
Context.ddirty = 2;
|
||||
Context.rdirty = 2;
|
||||
if (mouse.started()) {
|
||||
Scene.active.spawnObject(".Sphere", null, function(o: Object) {
|
||||
iron.data.Data.getMaterial("Scene", ".Gizmo", function(md: MaterialData) {
|
||||
var mo: MeshObject = cast o;
|
||||
mo.name = ".Bullet";
|
||||
mo.materials[0] = md;
|
||||
mo.visible = true;
|
||||
|
||||
var camera = iron.Scene.active.camera;
|
||||
var ct = camera.transform;
|
||||
mo.transform.loc.set(ct.worldx(), ct.worldy(), ct.worldz());
|
||||
mo.transform.scale.set(Context.brushRadius * 0.2, Context.brushRadius * 0.2, Context.brushRadius * 0.2);
|
||||
mo.transform.buildMatrix();
|
||||
|
||||
var body = new arm.plugin.PhysicsBody();
|
||||
body.shape = arm.plugin.PhysicsBody.ShapeType.ShapeSphere;
|
||||
body.mass = 1.0;
|
||||
mo.addTrait(body);
|
||||
|
||||
var ray = iron.math.RayCaster.getRay(mouse.viewX, mouse.viewY, camera);
|
||||
body.applyImpulse(ray.direction.mult(0.1));
|
||||
|
||||
iron.system.Tween.timer(5, mo.remove);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var pairs = world.getContactPairs(Context.paintBody);
|
||||
if (pairs != null) {
|
||||
for (p in pairs) {
|
||||
Context.particleHitX = p.posA.x;
|
||||
Context.particleHitY = p.posA.y;
|
||||
Context.particleHitZ = p.posA.z;
|
||||
Context.pdirty = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
public function toggleDistractFree() {
|
||||
@@ -534,6 +579,13 @@ class UISidebar {
|
||||
}
|
||||
#end
|
||||
|
||||
|
||||
#if arm_physics
|
||||
if (Context.particlePhysics) {
|
||||
down = false;
|
||||
}
|
||||
#end
|
||||
|
||||
#if krom_ios
|
||||
// No hover on iPad, decals are painted by pen release
|
||||
if (decal) {
|
||||
|
||||
@@ -109,9 +109,9 @@ class ParticleUtil {
|
||||
po.transform.scale.y = po.parent.transform.scale.y;
|
||||
po.transform.scale.z = po.parent.transform.scale.z;
|
||||
|
||||
var paintBody = new arm.plugin.PhysicsBody();
|
||||
paintBody.shape = arm.plugin.PhysicsBody.ShapeType.ShapeMesh;
|
||||
po.addTrait(paintBody);
|
||||
Context.paintBody = new arm.plugin.PhysicsBody();
|
||||
Context.paintBody.shape = arm.plugin.PhysicsBody.ShapeType.ShapeMesh;
|
||||
po.addTrait(Context.paintBody);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user