Git Product home page Git Product logo

Comments (8)

keenanwoodall avatar keenanwoodall commented on May 10, 2024 1

I gotcha. So the camera needs to be deformed as if it's a vertice on the mesh. There isn't currently a way to do this with deform, but I'll look into parenting objects to vertices as I think that's a useful feature. When I get the time I'll try and make a hacky temporary script you can use in the meantime.

from deform.

keenanwoodall avatar keenanwoodall commented on May 10, 2024 1

@nathanvogel Sorry for the delay. I threw together a quick and dirty script (emphasis on dirty). It's not super easy to use but, just drop it on an object, give it a reference to a Deformable component, and increase the triangle index property until the triangle you want is selected. (sorry I know that's lame) The object will be pinned to that triangle. Do note: I didn't add control for changing rotation/position offsets. They shouldn't be hard to add, but I figured it'd be easier to attach an empty object to the triangle and then add the camera as a child. Then you can change the offsets via the scene transform handles rather than fiddling with offsets in the inspector.

I needed to expose the mesh data to be able to access the triangles (without copying the vertex array every frame). Those changes are currently only on the develop branch but will probably make their way into the next release. You can apply the changes to your installation of Deform manually or work off of the linked commit.

Here's the script:

using UnityEngine;
using Deform;

[ExecuteAlways]
public class AttachToTriangle : MonoBehaviour
{
	public struct Triangle
	{
		public Vector3 a, b, c;

		public void Multiply(Matrix4x4 m)
		{
			a = m.MultiplyPoint3x4(a);
			b = m.MultiplyPoint3x4(b);
			c = m.MultiplyPoint3x4(c);
		}

		public Vector3 GetPosition() => (a + b + c) / 3f;
		public Quaternion GetRotation() => Quaternion.LookRotation((c - b).normalized, (b - a).normalized);
	}

	public Deformable target;
	public int triangleIndex;
#if UNITY_EDITOR
	[SerializeField] private bool drawTriangle = true;
#endif
	private void Update()
	{
		if (target == null) 
			return;

		var data = target.GetDynamicManagedMeshData();
		if (data == null) 
		   return;

		triangleIndex = Mathf.Clamp(triangleIndex, 0, data.Triangles.Length / 3);

		var triangle = GetTriangle(data, triangleIndex);

		triangle.Multiply(target.transform.localToWorldMatrix);
#if UNITY_EDITOR
		if (drawTriangle)
			DrawTriangle(triangle);
#endif

		var newPosition = triangle.GetPosition();
		var newRotation = triangle.GetRotation();

		transform.SetPositionAndRotation(newPosition, newRotation);
	}

	public Triangle GetTriangle(ManagedMeshData data, int triangleIndex)
	{
		var vertexIndex = triangleIndex * 3;
		var triangles = data.Triangles;
		var vertices = data.Vertices;
		return new Triangle
		{
			a = vertices[triangles[vertexIndex + 0]],
			b = vertices[triangles[vertexIndex + 1]],
			c = vertices[triangles[vertexIndex + 2]]
		};
	}

	private void DrawTriangle(Triangle triangle)
	{
		Debug.DrawLine(triangle.a, triangle.b);
		Debug.DrawLine(triangle.b, triangle.c);
		Debug.DrawLine(triangle.c, triangle.a);
	}
}

2019-10-26_12-45-59

from deform.

keenanwoodall avatar keenanwoodall commented on May 10, 2024

I'm not sure if I know exactly what you're trying to do, but if you want an effect to follow the camera perfectly you could just make the deformer object a child of the camera. Feel free to explain your goal further if I'm misunderstanding 👍

from deform.

nathanvogel avatar nathanvogel commented on May 10, 2024

Sorry I'll try to be clearer 👍 I want to do something like this:
example-bend

black = before
red = after deform

The line is the deformed mesh and the camera is another independant object.

from deform.

nathanvogel avatar nathanvogel commented on May 10, 2024

Awesome! Thanks for the help :)

from deform.

nathanvogel avatar nathanvogel commented on May 10, 2024

Super cool! Thanks a lot, I really appreciate and I'll try it this week :)

from deform.

nathanvogel avatar nathanvogel commented on May 10, 2024

Hey! I'm working on another part of the project these days, but I tried your script and just wanted to give you some feedback:

  • it works great 👍
  • I mostly use a setup like this:
    image
    • Original transform is just a 0,0,0 child transform.
    • Deformed triangle is a dummy triangle.
    • Where "Camera space" has a script that uses both the "Original transform" and the "Deformed transform" to compute a position (we need a different behavior outside a certain bounding box, and that behavior requires knowing both the original and the deformed position).
  • We're mostly using index 0, but even when using our (quite simple) terrain mesh, manually setting the index is easy.

Thanks again!

from deform.

keenanwoodall avatar keenanwoodall commented on May 10, 2024

That's great to hear! 👍

from deform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.