Comments (8)
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.
@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);
}
}
from deform.
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.
Sorry I'll try to be clearer 👍 I want to do something like this:
black = before
red = after deform
The line is the deformed mesh and the camera is another independant object.
from deform.
Awesome! Thanks for the help :)
from deform.
Super cool! Thanks a lot, I really appreciate and I'll try it this week :)
from deform.
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:
- 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.
That's great to hear! 👍
from deform.
Related Issues (20)
- Bend not applied to plane or any other "flat" model HOT 1
- Deformer are destroyed on play HOT 2
- How to make Elastic Deformalble work?
- Shading breaks HOT 3
- Undo always crashes the Unity HOT 1
- LatticeDeformer behaves differently in edit mode compared to play mode
- Error `Object reference not set to an instance of an object` on Editor stop HOT 2
- Editor bug in 2021.3.4 / Deform 1.2.1: Cannot add menu item HOT 8
- Unwanted dependencies HOT 2
- How to modify deformer's params in script? HOT 1
- CurveDisplaceDeformer exception
- Object with a lattice deformable are incorrectly translated in the world HOT 11
- Moving the lattice to the root of the scene causes exceptions HOT 2
- Applying a lattice deformer causes lattice point to be beside the mesh to deform.
- After removing from project, my physics are different? HOT 1
- Update the MeshCollider HOT 1
- How to do a mirror
- Access Lattice Points via Script HOT 3
- About LatticeDeformer.controlPoints HOT 1
- The following error occurs when importing in Unity 2022.3 : Unable to import newly created asset HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deform.