Forum

This forum is for swapping tips or ideas with fellow users, suggesting new features, or showing of what you have created with the system etc, there is no guarantee of an answer to questions posted in the forum. All support questions must be submitted via the Support ticket system

Welcome Guest 

Show/Hide Header

Welcome Guest, posting in this forum requires registration.





Pages: [1]
Author Topic: User contributed modifiers
ecurtz
Newbie
Posts: 4
Permalink
Post User contributed modifiers
on: May 17, 2012, 16:39
Quote

Here is a pretty rough "wire deformer" modifier that I added while I was learning the system. Feel free to do anything you'd like with it (you're encouraged but not required to post improvements.)

Anybody else have any additions they've made?

using UnityEngine;
using System.IO;
using System.Collections.Generic;

public struct MegaWireVert
{
	public int		vert;
	public int		u;
	public float	w;
}

[AddComponentMenu("Modifiers/Wire Deform")]
public class MegaWireDeform : MegaModifier
{
	//public MegaSpline		source		= null;
	//public MegaSpline		target		= null;
	public MegaShape		source		= null;
	public MegaShape		target		= null;
	public int				resolution	= 50;
	public float			falloff		= 1f;
	
	public override string ModName()	{ return "WireDeform"; }

	Vector3[]		sourcePositions;
	Vector3[]		targetOffsets;
	
	MegaWireVert[]	wireVerts;
	
	public override void ModStart(MegaModifiers mc)
	{
	}

	public override bool ModLateUpdate(MegaModContext mc)
	{
		
		return Prepare(mc);
	}

	public override bool Prepare(MegaModContext mc)
	{
		if (source == null)
			return false;
		if (target == null)
			return false;
		
		sourcePositions = new Vector3[resolution];
		targetOffsets = new Vector3[resolution];
		
		// Find [resolution] steps along the source shape
		bool type = false;
		int knot = 0;
		float alpha = 0f;
		float step = 1f / (resolution - 1);
		Matrix4x4 trans = transform.worldToLocalMatrix * source.transform.localToWorldMatrix;
		for (int i = 0; i < resolution; i++) {
			//sourcePositions[i] = trans.MultiplyPoint3x4(source.Interpolate(alpha, type, ref knot));
			sourcePositions[i] = trans.MultiplyPoint3x4(source.InterpCurve3D(0, alpha, type));
			alpha += step;
		}
		
		// Where does this go?
		List<MegaWireVert> vertList = new List<MegaWireVert>();
		int vertCount = verts.Length;
		for (int i = 0; i < vertCount; i++) {
			float dist = Mathf.Infinity;
			int closest = 0;
			for (int j = 0; j < resolution; j++) {
				float sourceDist = Vector3.Distance(verts[i], sourcePositions[j]);
				if (sourceDist < dist) {
					dist = sourceDist;
					closest = j;
				}
			}
			
			if (dist < falloff) {
				MegaWireVert wireVert;
				wireVert.vert = i;
				wireVert.u = closest;
				float portion = dist / falloff;
				wireVert.w = (1f - portion * portion) * (1f - portion * portion);
				vertList.Add(wireVert);
			}
		}
		
		wireVerts = vertList.ToArray();

		return true;
	}
	
	public override void Modify(MegaModifiers mc)
	{
		// Copy the verts to start
		verts.CopyTo(sverts, 0);

		// Find [resolution] offsets from source to target shape
		bool type = false;
		int knot = 0;
		float alpha = 0f;
		float step = 1f / (resolution - 1);
		Matrix4x4 trans = transform.worldToLocalMatrix * target.transform.localToWorldMatrix;
		for (int i = 0; i < resolution; i++) {
			//targetOffsets[i] = trans.MultiplyPoint3x4(target.Interpolate(alpha, type, ref knot)) - sourcePositions[i];
			targetOffsets[i] = trans.MultiplyPoint3x4(target.InterpCurve3D(0, alpha, type)) - sourcePositions[i];
			alpha += step;
		}
		
		int wireVertCount = wireVerts.Length;
		for (int i = 0; i < wireVertCount; i++) {
			sverts[wireVerts[i].vert] += targetOffsets[wireVerts[i].u] * wireVerts[i].w;
		}
	}
}

And the editor class...

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(MegaWireDeform))]
public class MegaWireDeformEditor : MegaModifierEditor
{
	public override string GetHelpString() { return "Wire Deformer by Eli Curtz"; }
	public override Texture LoadImage() { return (Texture)EditorGUIUtility.LoadRequired("MegaFiers\\bend_help.png"); }

	public override bool Inspector()
	{
		MegaWireDeform mod = (MegaWireDeform)target;

		EditorGUIUtility.LookLikeControls();
		mod.resolution = EditorGUILayout.IntField("Resolution", mod.resolution);
		mod.falloff = EditorGUILayout.FloatField("FallOff", mod.falloff);
		mod.source = (MegaShape)EditorGUILayout.ObjectField("Source Spline", mod.source, typeof(MegaShape), true);
		mod.target = (MegaShape)EditorGUILayout.ObjectField("Target Spline", mod.target, typeof(MegaShape), true);
		return false;
	}
}
spookycat
Administrator
Posts: 148
Permalink
Post Re: User contributed modifiers
on: May 18, 2012, 10:36
Quote

Excellent, was really hoping people would start adding their own modifiers to the system, looking forward to trying this one out, keep them coming 🙂

Pages: [1]
Mingle Forum by cartpauj
Version: 1.0.34 ; Page loaded in: 0.021 seconds.