public static void DrawScatterBrush(Vector3 point, Vector3 normal, z_BrushSettings settings, Matrix4x4 localToWorldMatrix) { Vector3 p = localToWorldMatrix.MultiplyPoint3x4(point); Vector3 n = localToWorldMatrix.MultiplyVector(normal).normalized; float r = settings.radius; Vector3 a = Vector3.zero; Quaternion rotation = Quaternion.LookRotation(normal, Vector3.up); for(int i = 0; i < 10; i++) { a.x = Mathf.Cos(Random.Range(0f, 360f)); a.y = Mathf.Sin(Random.Range(0f, 360f)); a = a.normalized * Random.Range(0f, r); Vector3 v = localToWorldMatrix.MultiplyPoint3x4(point + rotation * a); Handles.DrawLine(v, v + (n * .5f)); Handles.CubeCap(i + 2302, v, Quaternion.identity, .01f); } /// radius Handles.DrawWireDisc(p, n, settings.radius); }
public static Bounds GetBounds(PF.Vector3[] points, Matrix4x4 matrix, float minimumHeight) { var origin = matrix.MultiplyPoint3x4(Vector3.zero); var right = matrix.MultiplyPoint3x4(Vector3.right) - origin; var up = matrix.MultiplyPoint3x4(Vector3.up) - origin; var forward = matrix.MultiplyPoint3x4(Vector3.forward) - origin; return(GetBounds(points, right, up, forward, origin, minimumHeight)); }
/** Construct a shape. * \param points Contour of the shape in local space with respect to the matrix (i.e the shape should be in the XZ plane, the Y coordinate will only affect the bounds) * \param convex If true, the convex hull of the points will be calculated. \see #convex * \param matrix local to world space matrix for the points. The matrix determines the up direction of the shape. * \param minimumHeight If the points would be in the XZ plane only, the shape would not have a height and then it might not * include any points inside it (as testing for inclusion is done in 3D space when updating graphs). This ensures * that the shape has at least the minimum height (in the up direction that the matrix specifies). */ public GraphUpdateShape(PF.Vector3[] points, bool convex, Matrix4x4 matrix, float minimumHeight) { this.convex = convex; this.points = points; origin = matrix.MultiplyPoint3x4(Vector3.zero); right = matrix.MultiplyPoint3x4(Vector3.right) - origin; up = matrix.MultiplyPoint3x4(Vector3.up) - origin; forward = matrix.MultiplyPoint3x4(Vector3.forward) - origin; this.minimumHeight = minimumHeight; }
public void Rotate(Matrix4x4 tm) { for ( int i = 0; i < Keys.Length; i++ ) { Keys[i].val = tm.MultiplyPoint3x4(Keys[i].val); Keys[i].intan = tm.MultiplyPoint3x4(Keys[i].intan); Keys[i].outtan = tm.MultiplyPoint3x4(Keys[i].outtan); } InitKeys(); }
/** Recalculate the bounds based on #vertices and #matrix */ public void RecalculateBounds() { Bounds b = new Bounds(matrix.MultiplyPoint3x4(vertices[0]), Vector3.zero); for (int i = 1; i < numVertices; i++) { b.Encapsulate(matrix.MultiplyPoint3x4(vertices[i])); } // Assigned here to avoid changing bounds if vertices would happen to be null bounds = b; }
public void Line(Vector3 a, Vector3 b, Color color) { SetColor(color); if (gizmos) { UnityEngine.Gizmos.DrawLine(matrix.MultiplyPoint3x4(a), matrix.MultiplyPoint3x4(b)); } else { UnityEngine.Debug.DrawLine(matrix.MultiplyPoint3x4(a), matrix.MultiplyPoint3x4(b), color); } }
public static void DrawBrush( Vector3 point, Vector3 normal, z_BrushSettings brushSettings, Matrix4x4 matrix, Color innerColor, Color outerColor) { PushHandleColor(); Vector3 p = matrix.MultiplyPoint3x4(point); Vector3 n = matrix.MultiplyVector(normal).normalized; /// radius Handles.color = outerColor; Handles.DrawWireDisc(p, n, brushSettings.radius); /// falloff Handles.color = innerColor; Handles.DrawWireDisc(p, n, brushSettings.radius * brushSettings.falloff); Handles.color = new Color( Mathf.Abs(n.x), Mathf.Abs(n.y), Mathf.Abs(n.z), 1f); Handles.DrawLine(p, p + n.normalized * HandleUtility.GetHandleSize(p)); PopHandleColor(); }
/// <summary> /// Update the min/max values based on the transformed bounding box. /// </summary> /// <param name="bounds"></param> /// <param name="transform"></param> /// <param name="maxPos"></param> /// <param name="minPos"></param> private void UpdateMinMax(Bounds bounds, Matrix4x4 transform, ref Vector3 maxPos, ref Vector3 minPos) { var tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.min.x, bounds.min.y, bounds.min.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.min.x, bounds.min.y, bounds.max.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.min.x, bounds.max.y, bounds.min.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.min.x, bounds.max.y, bounds.max.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.max.x, bounds.min.y, bounds.min.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.max.x, bounds.min.y, bounds.max.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.max.x, bounds.max.y, bounds.min.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); tmpVec = transform.MultiplyPoint3x4(new Vector3(bounds.max.x, bounds.max.y, bounds.max.z)); maxPos = Vector3.Max(maxPos, tmpVec); minPos = Vector3.Min(minPos, tmpVec); }
private static void DrawPushPlane(Matrix4x4 trs, Vector3 point, Vector3 dir) { point = trs.MultiplyPoint3x4(point); dir = trs.MultiplyVector(dir); Vector3 vector3 = point + (dir.normalized * 0.1f); Gizmos.DrawLine(point, vector3); Matrix4x4 matrix4x4 = Gizmos.matrix; Gizmos.matrix = matrix4x4 * Matrix4x4.TRS(point, Quaternion.LookRotation(dir), Vector3.one); Gizmos.DrawWireCube(Vector3.zero, new Vector3(1f, 1f, 0.0001f)); Gizmos.matrix = matrix4x4; }
/// <summary> /// - Debugs a circle. /// </summary> /// <param name='position'> /// - Where the center of the circle will be positioned. /// </param> /// <param name='up'> /// - The direction perpendicular to the surface of the circle. /// </param> /// <param name='color'> /// - The color of the circle. /// </param> /// <param name='radius'> /// - The radius of the circle. /// </param> /// <param name='duration'> /// - How long to draw the circle. /// </param> /// <param name='depthTest'> /// - Whether or not the circle should be faded when behind other objects. /// </param> public static void DebugArch(Vector3 position, Color color, float start = 0, float end = 360, float radius = 1.0f, float duration = 0, bool depthTest = true) { Vector3 _up = Vector3.up.normalized * radius; Vector3 _forward = Vector3.Slerp(_up, -_up, 0.5f); Vector3 _right = Vector3.Cross(_up, _forward).normalized*radius; Matrix4x4 matrix = new Matrix4x4(); matrix[0] = _right.x; matrix[1] = _right.y; matrix[2] = _right.z; matrix[4] = _up.x; matrix[5] = _up.y; matrix[6] = _up.z; matrix[8] = _forward.x; matrix[9] = _forward.y; matrix[10] = _forward.z; Vector3 _lastPoint = position + matrix.MultiplyPoint3x4(new Vector3(Mathf.Cos(0), 0, Mathf.Sin(0))); Vector3 _nextPoint = Vector3.zero; color = (color == default(Color)) ? Color.white : color; for(var i = 0; i < 91; i++) { _nextPoint.x = Mathf.Cos((i*4)*Mathf.Deg2Rad); _nextPoint.z = Mathf.Sin((i*4)*Mathf.Deg2Rad); _nextPoint.y = 0; _nextPoint = position + matrix.MultiplyPoint3x4(_nextPoint); if (i*4 >= start && i*4 <= end) { Debug.DrawLine(_lastPoint, _nextPoint, color, duration, depthTest); } _lastPoint = _nextPoint; } }
/// <summary> /// Step 2: Transform the vertices by the provided matrix. /// </summary> public void ApplyTransform (Matrix4x4 widgetToPanel) { if (verts.size > 0) { mRtpVerts.Clear(); for (int i = 0, imax = verts.size; i < imax; ++i) mRtpVerts.Add(widgetToPanel.MultiplyPoint3x4(verts[i])); // Calculate the widget's normal and tangent mRtpNormal = widgetToPanel.MultiplyVector(Vector3.back).normalized; Vector3 tangent = widgetToPanel.MultiplyVector(Vector3.right).normalized; mRtpTan = new Vector4(tangent.x, tangent.y, tangent.z, -1f); } else mRtpVerts.Clear(); }
/// <summary> /// Step 3: Transform the vertices by the provided matrix. /// </summary> public void ApplyTransform(Matrix4x4 widgetToPanel, bool normals) { if (verts.size > 0) { mRtpVerts.Clear(); foreach (Vector3 v in verts) mRtpVerts.Add(widgetToPanel.MultiplyPoint3x4(v)); // Calculate the widget's normal and tangent mRtpNormal = widgetToPanel.MultiplyVector(Vector3.back).normalized; Vector3 tangent = widgetToPanel.MultiplyVector(Vector3.right).normalized; mRtpTan = new Vector4(tangent.x, tangent.y, tangent.z, -1f); } else mRtpVerts.Clear(); }
public static Vector3 RGBtoCIELAB( Color color ) { // a crude (s)rgb -> xyz -> l*a*b* transform Matrix4x4 xyzmat = new Matrix4x4(); xyzmat.SetRow( 0, new Vector4( 0.412453f, 0.357580f, 0.180423f, 0.0f ) ); xyzmat.SetRow( 1, new Vector4( 0.212671f, 0.715160f, 0.072169f, 0.0f ) ); xyzmat.SetRow( 2, new Vector4( 0.019334f, 0.119193f, 0.950227f, 0.0f ) ); xyzmat.SetRow( 3, new Vector4( 0.0f, 0.0f, 0.0f, 1.0f ) ); Vector3 rgb = new Vector3( color.r, color.g, color.b ); Vector3 xyz = xyzmat.MultiplyPoint3x4( rgb ); float epsilon = 0.008856f; Vector3 white = new Vector3( 95.047f, 100.0f, 108.883f ); // D65 white point in XYZ float L, a, b; if( (xyz.y / white.y) > epsilon ) { L = 116.0f * Mathf.Pow( (xyz.y / white.y), 1.0f/3.0f ) - 16.0f; } else { L = 903.3f * xyz.y / white.y; } Vector3 xyz_fact = new Vector3(); if( (xyz.x / white.x) > epsilon ) { xyz_fact.x = Mathf.Pow( (xyz.x / white.x), 1.0f/3.0f ); } else { xyz_fact.x = 7.787f * (xyz.x / white.x) + 16.0f / 116.0f; } if( (xyz.y / white.y) > epsilon ) { xyz_fact.y = Mathf.Pow( (xyz.y / white.y), 1.0f/3.0f ); } else { xyz_fact.y = 7.787f * (xyz.y / white.y) + 16.0f / 116.0f; } if( (xyz.z / white.z) > epsilon ) { xyz_fact.z = Mathf.Pow( (xyz.z / white.z), 1.0f/3.0f ); } else { xyz_fact.z = 7.787f * (xyz.z / white.z) + 16.0f / 116.0f; } a = 500.0f * (xyz_fact.x - xyz_fact.y); b = 200.0f * (xyz_fact.y - xyz_fact.z); return new Vector3( L, a, b ); }
static public int MultiplyPoint3x4(IntPtr l) { try{ UnityEngine.Matrix4x4 self = (UnityEngine.Matrix4x4)checkSelf(l); UnityEngine.Vector3 a1; checkType(l, 2, out a1); UnityEngine.Vector3 ret = self.MultiplyPoint3x4(a1); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public static Rect Transform(this Rect rect, UnityEngine.Matrix4x4 mat) { Rect result = rect; Vector3[] points = new Vector3[] { new Vector3(rect.xMin, rect.yMin, 0f), new Vector3(rect.xMax, rect.yMax, 0f) }; for (int i = 0; i < 2; i++) { points[i] = mat.MultiplyPoint3x4(points[i]); } result.xMin = points[0].x; result.xMax = points[1].x; result.yMin = points[0].y; result.yMax = points[1].y; return(result); }
static int MultiplyPoint3x4(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); UnityEngine.Matrix4x4 obj = (UnityEngine.Matrix4x4)ToLua.CheckObject(L, 1, typeof(UnityEngine.Matrix4x4)); UnityEngine.Vector3 arg0 = ToLua.ToVector3(L, 2); UnityEngine.Vector3 o = obj.MultiplyPoint3x4(arg0); ToLua.Push(L, o); ToLua.SetBack(L, 1, obj); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public override Vector3 Map(int i, Vector3 p) { p = tm.MultiplyPoint3x4(p); // Dont need either, so saving 3 vector mat mults but gaining a mat mult float alpha; if ( UseStretchCurve ) { float str = stretchCurve.Evaluate(Mathf.Repeat(p.z * ovlen + usepercent, 1.0f)) * stretch; alpha = (p.z * ovlen * str) + usepercent; //(percent / 100.0f); // can precalc this } else alpha = (p.z * ovlen * stretch) + usepercent; //(percent / 100.0f); // can precalc this Vector3 ps = path.InterpCurve3D(0, alpha, path.normalizedInterp); // - start; Vector3 ps1 = path.InterpCurve3D(0, alpha + usetan, path.normalizedInterp); // - start; if ( path.splines[0].closed ) alpha = Mathf.Repeat(alpha, 1.0f); else alpha = Mathf.Clamp01(alpha); Quaternion tw = Quaternion.identity; if ( UseTwistCurve ) { float twst = twistCurve.Evaluate(alpha) * twist; tw = Quaternion.AngleAxis(twst, Vector3.forward); } else tw = Quaternion.AngleAxis(twist * alpha, Vector3.forward); Vector3 relativePos = ps1 - ps; Quaternion rotation = Quaternion.LookRotation(relativePos) * tw; Matrix4x4 wtm = new Matrix4x4(); wtm.SetTRS(ps, rotation, Vector3.one); wtm = mat * wtm; p.z = 0.0f; return wtm.MultiplyPoint3x4(p); }
public void ApplyTransform(Matrix4x4 widgetToPanel) { if (this.verts.size > 0) { this.mRtpVerts.Clear(); int num = 0; int size = this.verts.size; while (num < size) { this.mRtpVerts.Add(widgetToPanel.MultiplyPoint3x4(this.verts[num])); num++; } this.mRtpNormal = widgetToPanel.MultiplyVector(Vector3.back).normalized; Vector3 normalized = widgetToPanel.MultiplyVector(Vector3.right).normalized; this.mRtpTan = new Vector4(normalized.x, normalized.y, normalized.z, -1f); } else { this.mRtpVerts.Clear(); } }
/// <summary> /// - Draws a circle. /// </summary> /// <param name='position'> /// - Where the center of the circle will be positioned. /// </param> /// <param name='up'> /// - The direction perpendicular to the surface of the circle. /// </param> /// <param name='color'> /// - The color of the circle. /// </param> /// <param name='radius'> /// - The radius of the circle. /// </param> public static void DrawCircle(Vector3 position, Vector3 up, Color color, float radius = 1.0f) { up = ((up == Vector3.zero) ? Vector3.up : up).normalized * radius; Vector3 _forward = Vector3.Slerp(up, -up, 0.5f); Vector3 _right = Vector3.Cross(up, _forward).normalized*radius; Matrix4x4 matrix = new Matrix4x4(); matrix[0] = _right.x; matrix[1] = _right.y; matrix[2] = _right.z; matrix[4] = up.x; matrix[5] = up.y; matrix[6] = up.z; matrix[8] = _forward.x; matrix[9] = _forward.y; matrix[10] = _forward.z; Vector3 _lastPoint = position + matrix.MultiplyPoint3x4(new Vector3(Mathf.Cos(0), 0, Mathf.Sin(0))); Vector3 _nextPoint = Vector3.zero; Color oldColor = Gizmos.color; Gizmos.color = (color == default(Color)) ? Color.white : color; for(var i = 0; i < 91; i++){ _nextPoint.x = Mathf.Cos((i*4)*Mathf.Deg2Rad); _nextPoint.z = Mathf.Sin((i*4)*Mathf.Deg2Rad); _nextPoint.y = 0; _nextPoint = position + matrix.MultiplyPoint3x4(_nextPoint); Gizmos.DrawLine(_lastPoint, _nextPoint); _lastPoint = _nextPoint; } Gizmos.color = oldColor; }
static void CalculateRelativeWidgetBounds (Transform content, ConsiderActiveType considerActiveType, bool isRoot, ref Matrix4x4 toLocal, ref Vector3 vMin, ref Vector3 vMax, ref bool isSet) { if (content == null) return; if (considerActiveType == ConsiderActiveType.activeInHierarchy) { if (!NGUITools.GetActive(content.gameObject)) return; } else if (considerActiveType == ConsiderActiveType.activeSelf) { if (!NGUITools.GetActiveSelf(content.gameObject)) return; } if (content.GetComponent<UINoBounds>() != null) return; // If this isn't a root node, check to see if there is a panel present UIPanel p = isRoot ? null : content.GetComponent<UIPanel>(); // Ignore disabled panels as a disabled panel means invisible children if (p != null && !p.enabled) return; // If there is a clipped panel present simply include its dimensions if (p != null && p.clipping != UIDrawCall.Clipping.None) { Vector3[] corners = p.worldCorners; for (int j = 0; j < 4; ++j) { Vector3 v = toLocal.MultiplyPoint3x4(corners[j]); if (v.x > vMax.x) vMax.x = v.x; if (v.y > vMax.y) vMax.y = v.y; if (v.z > vMax.z) vMax.z = v.z; if (v.x < vMin.x) vMin.x = v.x; if (v.y < vMin.y) vMin.y = v.y; if (v.z < vMin.z) vMin.z = v.z; isSet = true; } } else // No panel present { // If there is a widget present, include its bounds UIWidget w = content.GetComponent<UIWidget>(); if (w != null && w.enabled) { Vector3[] corners = w.worldCorners; for (int j = 0; j < 4; ++j) { Vector3 v = toLocal.MultiplyPoint3x4(corners[j]); if (v.x > vMax.x) vMax.x = v.x; if (v.y > vMax.y) vMax.y = v.y; if (v.z > vMax.z) vMax.z = v.z; if (v.x < vMin.x) vMin.x = v.x; if (v.y < vMin.y) vMin.y = v.y; if (v.z < vMin.z) vMin.z = v.z; isSet = true; } } // Iterate through children including their bounds in turn for (int i = 0, imax = content.childCount; i < imax; ++i) CalculateRelativeWidgetBounds(content.GetChild(i), considerActiveType, false, ref toLocal, ref vMin, ref vMax, ref isSet); } }
public static Vector2 GetUV (Matrix4x4 aTransform, UVType aUVType, Vector3 aPt, Vector3 aNorm, float aUStart, float aUEnd, float aPercentX, float aPercentY) { Vector2 uv = new Vector2(aPercentX + 0.5f, (aPercentY) + 0.5f); if (aUVType == UVType.WallSlide) { uv = new Vector2(Mathf.Lerp(aUEnd, aUStart, aPercentX + 0.5f), aPercentY + 0.5f); } else if (aUVType == UVType.WorldCoordinates) { uv = PosToUV(aTransform.MultiplyPoint3x4(aPt), aTransform.MultiplyVector(aNorm)); } else if (aUVType == UVType.LocalCoordinates) { uv = PosToUV(aPt, aNorm); } return uv; }
public static void AddFace (Matrix4x4 aObjTransform, Matrix4x4 aTransform, float aOffset, UVType aUVType, Vector2 aUVOffset, Vector2 aUVTiling, float aUStart, float aUEnd, int aXSlices, int aYSlices, ref List<Vector3> aVerts, ref List<Vector2> aUVs, ref List<Vector3> aNormals, ref List<Vector4> aTangents, ref List<int> aIndices) { aXSlices = Mathf.Max(aXSlices, 2); aYSlices = Mathf.Max(aYSlices, 2); int startID = aVerts.Count; Vector3 normal = aTransform.MultiplyVector(new Vector3(0, 0, -1)).normalized; for (int y = 0; y < aYSlices; y++) { float percentY = (float)y / (aYSlices-1) - 0.5f; for (int x = 0; x < aXSlices; x++) { float percentX = (float)x / (aXSlices-1) - 0.5f; Vector3 pt = new Vector3(percentX, percentY, -aOffset); aVerts .Add(aTransform.MultiplyPoint3x4(pt)); aNormals .Add(normal); aUVs .Add(aUVOffset + Vector2.Scale(aUVTiling, GetUV(aObjTransform, aUVType, aVerts[aVerts.Count-1], aNormals[aVerts.Count-1], aUStart, aUEnd, percentX, percentY))); if (x > 0 && y > 0) { if ((x+y)%2==0) { aIndices.Add(startID + (x ) + (y ) * aXSlices); aIndices.Add(startID + (x ) + (y-1) * aXSlices); aIndices.Add(startID + (x-1) + (y-1) * aXSlices); aIndices.Add(startID + (x-1) + (y ) * aXSlices); aIndices.Add(startID + (x ) + (y ) * aXSlices); aIndices.Add(startID + (x-1) + (y-1) * aXSlices); } else { aIndices.Add(startID + (x ) + (y ) * aXSlices); aIndices.Add(startID + (x ) + (y-1) * aXSlices); aIndices.Add(startID + (x-1) + (y ) * aXSlices); aIndices.Add(startID + (x-1) + (y ) * aXSlices); aIndices.Add(startID + (x ) + (y-1) * aXSlices); aIndices.Add(startID + (x-1) + (y-1) * aXSlices); } } } } // calculate the tangent! Vector3 dir1 = aTransform.MultiplyVector(new Vector3(1, 0, 0)); Vector3 dir2 = aTransform.MultiplyVector(new Vector3(0, -1, 0)); Vector2 uv1 = aUVs[startID+1 ] - aUVs[startID]; Vector2 uv2 = aUVs[startID+aXSlices] - aUVs[startID]; float r = 1.0f / uv1.x * uv2.y - uv2.x * uv1.y; Vector3 sDir = new Vector3( (uv2.y * dir1.x - uv1.y * dir2.x) * r, (uv2.y * dir1.y - uv1.y * dir2.y) * r, (uv2.y * dir1.z - uv1.y * dir2.z) * r ); Vector3 tDir = new Vector3( (uv1.x * dir2.x - uv2.x * dir1.x) * r, (uv1.x * dir2.y - uv2.x * dir1.y) * r, (uv1.x * dir2.z - uv2.x * dir1.z) * r ); Vector3 ttan = (sDir - aNormals[startID] * Vector3.Dot(aNormals[startID], sDir)).normalized; Vector4 tan = new Vector4(ttan.x, ttan.y, ttan.z, (Vector3.Dot(Vector3.Cross(aNormals[startID], ttan), tDir) < 0) ? -1 : 1); for (int i = 0; i < aXSlices * aYSlices; ++i) { aTangents.Add(tan); } }
public void Add(string c, OTAtlasData data, Vector3[] verts, Vector2[] uv) { text+=c; int tx = 0; string dx = data.GetMeta("dx"); if (dx=="") tx = (int)(data.offset.x + data.size.x); else tx = System.Convert.ToUInt16(dx); txList.Add(tx); atlasData.Add(data); int tt = 0; for (int i=0; i<txList.Count-1; i++) tt+=txList[i]; Matrix4x4 mx = new Matrix4x4(); mx.SetTRS(new Vector3(tt,0,0), Quaternion.identity, Vector3.one); for (int i=0; i<verts.Length; i++) verts[i] = mx.MultiplyPoint3x4(verts[i]); System.Array.Resize<Vector3>(ref this.verts, this.verts.Length + verts.Length); verts.CopyTo(this.verts, this.verts.Length - verts.Length); System.Array.Resize<Vector2>(ref this.uv, this.uv.Length + uv.Length); uv.CopyTo(this.uv, this.uv.Length - uv.Length); }
public Vector3[] GetVerts(int maxWidth, Vector2 pivot, bool justify) { int tt = 0; float twx = 0; float spacing = 0; if (words.Count>1) spacing = (maxWidth - width)/(words.Count-1); if (maxWidth>0 && !justify) twx = (float)(maxWidth - width) * (pivot.x + 0.5f); Vector3[] _verts = new Vector3[] { new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(1,-lineHeight,0), new Vector3(0,-lineHeight,0) }; for (int w=0; w<words.Count; w++) { Vector3[] wVerts = words[w].verts; if (tt>0 || twx > 0 || yPosition!=0) { Matrix4x4 mx = new Matrix4x4(); mx.SetTRS(new Vector3(tt+twx,yPosition,0), Quaternion.identity, Vector3.one); for (int i=0; i<wVerts.Length; i++) wVerts[i] = mx.MultiplyPoint3x4(wVerts[i]); } tt += words[w].width + words[w].space; if (justify) tt+=(int)spacing; System.Array.Resize<Vector3>(ref _verts, _verts.Length + wVerts.Length); wVerts.CopyTo(_verts, _verts.Length - wVerts.Length); } return _verts; }
static bool Matrix4x4_MultiplyPoint3x4__Vector3(JSVCall vc, int argc) { int len = argc; if (len == 1) { UnityEngine.Vector3 arg0 = (UnityEngine.Vector3)JSApi.getVector3S((int)JSApi.GetType.Arg); UnityEngine.Matrix4x4 argThis = (UnityEngine.Matrix4x4)vc.csObj; JSApi.setVector3S((int)JSApi.SetType.Rval, argThis.MultiplyPoint3x4(arg0)); JSMgr.changeJSObj(vc.jsObjID, argThis); } return(true); }
/** Adds an obstacle described by the vertices. * * \see RemoveObstacle */ public ObstacleVertex AddObstacle (Vector3[] vertices, float height, Matrix4x4 matrix, RVOLayer layer = RVOLayer.DefaultObstacle) { if (vertices == null) throw new System.ArgumentNullException ("Vertices must not be null"); if (vertices.Length < 2) throw new System.ArgumentException ("Less than 2 vertices in an obstacle"); ObstacleVertex first = null; ObstacleVertex prev = null; bool identity = matrix == Matrix4x4.identity; //Don't interfere with ongoing calculations if (Multithreading && doubleBuffering) for (int j=0;j<workers.Length;j++) workers[j].WaitOne(); for (int i=0;i<vertices.Length;i++) { ObstacleVertex v = new ObstacleVertex(); if (first == null) first = v; else prev.next = v; v.prev = prev; v.layer = layer; //Premature optimization ftw! v.position = identity ? vertices[i] : matrix.MultiplyPoint3x4(vertices[i]); //v.thin = thin; v.height = height; prev = v; } prev.next = first; first.prev = prev; ObstacleVertex c = first; do { Vector3 dir = c.next.position - c.position; c.dir = new Vector2 (dir.x,dir.z).normalized; c = c.next; } while (c != first); obstacles.Add (first); UpdateObstacles (); return first; }
/// <summary> /// - Debugs a local cube. /// </summary> /// <param name='space'> /// - The space the cube will be local to. /// </param> /// <param name='size'> /// - The size of the cube. /// </param> /// <param name='color'> /// - Color of the cube. /// </param> /// <param name='center'> /// - The position (relative to transform) where the cube will be debugged. /// </param> /// <param name='duration'> /// - How long to draw the cube. /// </param> /// <param name='depthTest'> /// - Whether or not the cube should be faded when behind other objects. /// </param> public static void DebugLocalCube(Matrix4x4 space, Vector3 size, Color color, Vector3 center = default(Vector3), float duration = 0, bool depthTest = true) { color = (color == default(Color)) ? Color.white : color; Vector3 lbb = space.MultiplyPoint3x4(center+((-size)*0.5f)); Vector3 rbb = space.MultiplyPoint3x4(center+(new Vector3(size.x, -size.y, -size.z)*0.5f)); Vector3 lbf = space.MultiplyPoint3x4(center+(new Vector3(size.x, -size.y, size.z)*0.5f)); Vector3 rbf = space.MultiplyPoint3x4(center+(new Vector3(-size.x, -size.y, size.z)*0.5f)); Vector3 lub = space.MultiplyPoint3x4(center+(new Vector3(-size.x, size.y, -size.z)*0.5f)); Vector3 rub = space.MultiplyPoint3x4(center+(new Vector3(size.x, size.y, -size.z)*0.5f)); Vector3 luf = space.MultiplyPoint3x4(center+((size)*0.5f)); Vector3 ruf = space.MultiplyPoint3x4(center+(new Vector3(-size.x, size.y, size.z)*0.5f)); Debug.DrawLine(lbb, rbb, color, duration, depthTest); Debug.DrawLine(rbb, lbf, color, duration, depthTest); Debug.DrawLine(lbf, rbf, color, duration, depthTest); Debug.DrawLine(rbf, lbb, color, duration, depthTest); Debug.DrawLine(lub, rub, color, duration, depthTest); Debug.DrawLine(rub, luf, color, duration, depthTest); Debug.DrawLine(luf, ruf, color, duration, depthTest); Debug.DrawLine(ruf, lub, color, duration, depthTest); Debug.DrawLine(lbb, lub, color, duration, depthTest); Debug.DrawLine(rbb, rub, color, duration, depthTest); Debug.DrawLine(lbf, luf, color, duration, depthTest); Debug.DrawLine(rbf, ruf, color, duration, depthTest); }
public void VisualizeVoxels(Matrix4x4 vesselLocalToWorldMatrix) { ClearVisualVoxels(); visualVoxels = new DebugVisualVoxel[8, 8, 8]; for(int i = 0; i < 8; i++) for(int j = 0; j < 8; j++) for(int k = 0; k < 8; k++) { DebugVisualVoxel vx; //if(voxelPoints[i,j,k] != null) PartSizePair pair = voxelPoints[i + 8 * j + 64 * k]; if ((object)pair.part != null) { double elementSize = pair.GetSize(); if (elementSize > 1) elementSize = 1; elementSize *= _size * 0.5f; vx = new DebugVisualVoxel(vesselLocalToWorldMatrix.MultiplyPoint3x4(lowerCorner + new Vector3d(i, j, k) * _size), elementSize); visualVoxels[i, j, k] = vx; } } }
private void Line3DDiscrete (int start, int end, Matrix4x4 thisMatrix, bool useTransformMatrix) { if (!cam3D) { LogError ("The 3D camera no longer exists...if you have changed scenes, ensure that SetCamera3D is called in order to set it up."); return; } if (m_1pixelLine) { Vector3 p1; for (int i = start; i <= end; i++) { p1 = useTransformMatrix? cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (points3[i])) : cam3D.WorldToScreenPoint (points3[i]); p1.z = p1.z < cutoff? -zDist : zDist; m_lineVertices[i] = p1; } return; } Vector3 pos1, pos2, perpendicular; float normalizedDistance = 0.0f; int widthIdx = 0; widthIdxAdd = 0; if (m_lineWidths.Length > 1) { widthIdx = start; widthIdxAdd = 1; } int idx = start*2; for (int i = start; i < end; i += 2) { if (useTransformMatrix) { pos1 = cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (points3[i])); pos2 = cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (points3[i+1])); } else { pos1 = cam3D.WorldToScreenPoint (points3[i]); pos2 = cam3D.WorldToScreenPoint (points3[i+1]); } pos1.z = pos1.z < cutoff? -zDist : zDist; if (pos1.x == pos2.x && pos1.y == pos2.y) {Skip (ref idx, ref widthIdx, ref pos1); continue;} pos2.z = pos2.z < cutoff? -zDist : zDist; v1.x = pos2.y; v1.y = pos1.x; v2.x = pos1.y; v2.y = pos2.x; perpendicular = v1 - v2; normalizedDistance = 1.0f / Mathf.Sqrt((perpendicular.x * perpendicular.x) + (perpendicular.y * perpendicular.y)); perpendicular *= normalizedDistance * m_lineWidths[widthIdx]; m_lineVertices[idx] = pos1 - perpendicular; m_lineVertices[idx+1] = pos1 + perpendicular; if (smoothWidth && i < end-2) { perpendicular = v1 - v2; perpendicular *= normalizedDistance * m_lineWidths[widthIdx+1]; } m_lineVertices[idx+2] = pos2 - perpendicular; m_lineVertices[idx+3] = pos2 + perpendicular; idx += 4; widthIdx += widthIdxAdd; } if (m_joins == Joins.Weld) { WeldJoinsDiscrete (start + 1, end, Approximately3 (points3[0], points3[points3.Length-1]) && m_minDrawIndex == 0 && (m_maxDrawIndex == points3.Length-1 || m_maxDrawIndex == 0)); } }
private void Line3D (int start, int end, Matrix4x4 thisMatrix, bool useTransformMatrix) { if (!cam3D) { SetCamera3D(); if (!cam3D) { Debug.LogError ("No camera available...use VectorLine.SetCamera3D to assign a camera"); return; } } var pos1 = Vector3.zero; var pos2 = Vector3.zero; var px = Vector3.zero; float normalizedDistance = 0.0f; int widthIdx = 0; int widthIdxAdd = 0; if (m_lineWidths.Length > 1) { widthIdx = start; widthIdxAdd = 1; } int idx = start * 2; int add = 2; if (m_continuous) { pos2 = useTransformMatrix? cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (m_points3[start])) : cam3D.WorldToScreenPoint (m_points3[start]); idx = start * 4; add = 1; } float sw = Screen.width*2; float sh = Screen.height*2; for (int i = start; i < end; i += add) { if (m_continuous) { pos1.x = pos2.x; pos1.y = pos2.y; pos1.z = pos2.z; pos2 = useTransformMatrix? cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (m_points3[i+1])) : cam3D.WorldToScreenPoint (m_points3[i+1]); } else { if (useTransformMatrix) { pos1 = cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (m_points3[i])); pos2 = cam3D.WorldToScreenPoint (thisMatrix.MultiplyPoint3x4 (m_points3[i+1])); } else { pos1 = cam3D.WorldToScreenPoint (m_points3[i]); pos2 = cam3D.WorldToScreenPoint (m_points3[i+1]); } } if ((pos1.x == pos2.x && pos1.y == pos2.y) || (pos1.z < 0.0f && pos2.z < 0.0f) || (pos1.x > sw || pos2.x > sw || pos1.y > sh || pos2.y > sh)) { SkipQuad (ref idx, ref widthIdx, ref widthIdxAdd); continue; } px.x = pos2.y - pos1.y; px.y = pos1.x - pos2.x; normalizedDistance = 1.0f / (float)System.Math.Sqrt ((px.x * px.x) + (px.y * px.y)); px.x *= normalizedDistance * m_lineWidths[widthIdx]; px.y *= normalizedDistance * m_lineWidths[widthIdx]; m_UIVertices[idx ].position.x = pos1.x - px.x; m_UIVertices[idx ].position.y = pos1.y - px.y; m_UIVertices[idx+3].position.x = pos1.x + px.x; m_UIVertices[idx+3].position.y = pos1.y + px.y; if (smoothWidth && i < end - add) { px.x = pos2.y - pos1.y; px.y = pos1.x - pos2.x; px.x *= normalizedDistance * m_lineWidths[widthIdx+1]; px.y *= normalizedDistance * m_lineWidths[widthIdx+1]; } m_UIVertices[idx+2].position.x = pos2.x + px.x; m_UIVertices[idx+2].position.y = pos2.y + px.y; m_UIVertices[idx+1].position.x = pos2.x - px.x; m_UIVertices[idx+1].position.y = pos2.y - px.y; idx += 4; widthIdx += widthIdxAdd; } if (m_joins == Joins.Weld) { if (m_continuous) { WeldJoins (start*4 + 4, end*4, Approximately (m_points3[0], m_points3[m_pointsCount-1])); } else { WeldJoinsDiscrete (start + 1, end, Approximately (m_points3[0], m_points3[m_pointsCount-1])); } } }
/** Draws some gizmos */ void OnDrawGizmos(bool selected) { Color c = selected ? new Color(227 / 255f, 61 / 255f, 22 / 255f, 1.0f) : new Color(227 / 255f, 61 / 255f, 22 / 255f, 0.9f); if (selected) { Gizmos.color = Color.Lerp(c, new Color(1, 1, 1, 0.2f), 0.9f); Bounds b = GetBounds(); Gizmos.DrawCube(b.center, b.size); Gizmos.DrawWireCube(b.center, b.size); } if (points == null) { return; } if (convex) { c.a *= 0.5f; } Gizmos.color = c; Matrix4x4 matrix = legacyMode && legacyUseWorldSpace ? Matrix4x4.identity : transform.localToWorldMatrix; if (convex) { c.r -= 0.1f; c.g -= 0.2f; c.b -= 0.1f; Gizmos.color = c; } if (selected || !convex) { for (int i = 0; i < points.Length; i++) { Gizmos.DrawLine(matrix.MultiplyPoint3x4(points[i]), matrix.MultiplyPoint3x4(points[(i + 1) % points.Length])); } } if (convex) { if (convexPoints == null) { RecalcConvex(); } Gizmos.color = selected ? new Color(227 / 255f, 61 / 255f, 22 / 255f, 1.0f) : new Color(227 / 255f, 61 / 255f, 22 / 255f, 0.9f); for (int i = 0; i < convexPoints.Length; i++) { Gizmos.DrawLine(matrix.MultiplyPoint3x4(convexPoints[i]), matrix.MultiplyPoint3x4(convexPoints[(i + 1) % convexPoints.Length])); } } // Draw the full 3D shape var pts = convex ? convexPoints : points; if (selected && pts != null && pts.Length > 0) { Gizmos.color = new Color(1, 1, 1, 0.2f); float miny = pts[0].y, maxy = pts[0].y; for (int i = 0; i < pts.Length; i++) { miny = Mathf.Min(miny, pts[i].y); maxy = Mathf.Max(maxy, pts[i].y); } var extraHeight = Mathf.Max(minBoundsHeight - (maxy - miny), 0) * 0.5f; miny -= extraHeight; maxy += extraHeight; for (int i = 0; i < pts.Length; i++) { var next = (i + 1) % pts.Length; var p1 = matrix.MultiplyPoint3x4(pts[i] + PF.Vector3.up * (miny - pts[i].y)); var p2 = matrix.MultiplyPoint3x4(pts[i] + PF.Vector3.up * (maxy - pts[i].y)); var p1n = matrix.MultiplyPoint3x4(pts[next] + PF.Vector3.up * (miny - pts[next].y)); var p2n = matrix.MultiplyPoint3x4(pts[next] + PF.Vector3.up * (maxy - pts[next].y)); Gizmos.DrawLine(p1, p2); Gizmos.DrawLine(p1, p1n); Gizmos.DrawLine(p2, p2n); } } }
/// <summary> /// - Draws a local cube. /// </summary> /// <param name='space'> /// - The space the cube will be local to. /// </param> /// <param name='size'> /// - The local size of the cube. /// </param> /// <param name='center'> /// - The local position of the cube. /// </param> /// <param name='color'> /// - The color of the cube. /// </param> public static void DrawLocalCube(Matrix4x4 space, Vector3 size, Color color, Vector3 center = default(Vector3)) { Color oldColor = Gizmos.color; Gizmos.color = color; Vector3 lbb = space.MultiplyPoint3x4(center+((-size)*0.5f)); Vector3 rbb = space.MultiplyPoint3x4(center+(new Vector3(size.x, -size.y, -size.z)*0.5f)); Vector3 lbf = space.MultiplyPoint3x4(center+(new Vector3(size.x, -size.y, size.z)*0.5f)); Vector3 rbf = space.MultiplyPoint3x4(center+(new Vector3(-size.x, -size.y, size.z)*0.5f)); Vector3 lub = space.MultiplyPoint3x4(center+(new Vector3(-size.x, size.y, -size.z)*0.5f)); Vector3 rub = space.MultiplyPoint3x4(center+(new Vector3(size.x, size.y, -size.z)*0.5f)); Vector3 luf = space.MultiplyPoint3x4(center+((size)*0.5f)); Vector3 ruf = space.MultiplyPoint3x4(center+(new Vector3(-size.x, size.y, size.z)*0.5f)); Gizmos.DrawLine(lbb, rbb); Gizmos.DrawLine(rbb, lbf); Gizmos.DrawLine(lbf, rbf); Gizmos.DrawLine(rbf, lbb); Gizmos.DrawLine(lub, rub); Gizmos.DrawLine(rub, luf); Gizmos.DrawLine(luf, ruf); Gizmos.DrawLine(ruf, lub); Gizmos.DrawLine(lbb, lub); Gizmos.DrawLine(rbb, rub); Gizmos.DrawLine(lbf, luf); Gizmos.DrawLine(rbf, ruf); Gizmos.color = oldColor; }
void DrawGraphLine(int index, Matrix4x4 m, float x1, float x2, float y1, float y2, Color color) { Debug.DrawLine(cam.ScreenToWorldPoint(m.MultiplyPoint3x4(new Vector3(x1, y1))), cam.ScreenToWorldPoint(m.MultiplyPoint3x4(new Vector3(x2, y2))), color); }
static void TransformedPointBounds(Matrix4x4 matrix, Vector3 center, float extX, float extY, float extZ, ref Vector3 lower, ref Vector3 upper) { Vector3 boundPt = new Vector3 (center.x + extX, center.y + extY, center.z + extZ); boundPt = matrix.MultiplyPoint3x4(boundPt); lower = Vector3.Min (lower, boundPt); upper = Vector3.Max (upper, boundPt); }
/** Draws Gizmos */ public void OnDrawGizmos(bool selected) { gizmoDrawing = true; Gizmos.color = new Color(0.615f, 1, 0.06f, selected ? 1.0f : 0.7f); var movementPlane = RVOSimulator.active != null ? RVOSimulator.active.movementPlane : MovementPlane.XZ; var up = movementPlane == MovementPlane.XZ ? Vector3.up : -Vector3.forward; if (gizmoVerts == null || AreGizmosDirty() || _obstacleMode != obstacleMode) { _obstacleMode = obstacleMode; if (gizmoVerts == null) { gizmoVerts = new List <Vector3[]>(); } else { gizmoVerts.Clear(); } CreateObstacles(); } Matrix4x4 m = GetMatrix(); for (int i = 0; i < gizmoVerts.Count; i++) { Vector3[] verts = gizmoVerts[i]; for (int j = 0, q = verts.Length - 1; j < verts.Length; q = j++) { Gizmos.DrawLine(m.MultiplyPoint3x4(verts[j]), m.MultiplyPoint3x4(verts[q])); } if (selected) { for (int j = 0, q = verts.Length - 1; j < verts.Length; q = j++) { Vector3 a = m.MultiplyPoint3x4(verts[q]); Vector3 b = m.MultiplyPoint3x4(verts[j]); if (movementPlane != MovementPlane.XY) { Gizmos.DrawLine(a + up * Height, b + up * Height); Gizmos.DrawLine(a, a + up * Height); } Vector3 avg = (a + b) * 0.5f; Vector3 tang = (b - a).normalized; if (tang == Vector3.zero) { continue; } Vector3 normal = Vector3.Cross(up, tang); Gizmos.DrawLine(avg, avg + normal); Gizmos.DrawLine(avg + normal, avg + normal * 0.5f + tang * 0.5f); Gizmos.DrawLine(avg + normal, avg + normal * 0.5f - tang * 0.5f); } } } gizmoDrawing = false; }
public void DrawGraphLine (int index, Matrix4x4 m, float x1, float x2, float y1, float y2, Color col) { Debug.DrawLine(cam.ScreenToWorldPoint(m.MultiplyPoint3x4(new Vector3(x1, y1))), cam.ScreenToWorldPoint(m.MultiplyPoint3x4(new Vector3(x2, y2))), col); }
Assimp.Mesh FromUnityMesh(UnityEngine.Mesh mesh, string _name, Transform rootGo, Transform[] bones = null) { UnityEngine.Matrix4x4 world = rootGo.transform.localToWorldMatrix; Assimp.Mesh assimpMesh = new Assimp.Mesh(_name, Assimp.PrimitiveType.Triangle); foreach (Vector3 v in mesh.vertices) { Vector3 vW = world.MultiplyPoint3x4(v); assimpMesh.Vertices.Add(new Vector3D(vW.x, vW.y, vW.z)); } foreach (Vector3 n in mesh.normals) { Vector3 nW = world.MultiplyPoint3x4(n); assimpMesh.Normals.Add(new Vector3D(nW.x, nW.y, nW.z)); } foreach (Vector2 u in mesh.uv) { assimpMesh.TextureCoordinateChannels[0].Add(new Vector3D(u.x, 1f - u.y, 0)); } if (mesh.uv2 != null && mesh.uv2.Length > 0) { foreach (Vector2 u in mesh.uv2) { assimpMesh.TextureCoordinateChannels[1].Add(new Vector3D(u.x, 1f - u.y, 0)); } } assimpMesh.SetIndices(mesh.triangles, 3); /*if(bones != null) * { * foreach(Transform b in bones) * { * Bone bone = new Bone(); * bone.Name = b.name; * * UnityEngine.Matrix4x4 m = b.worldToLocalMatrix.inverse; * bone.OffsetMatrix = new Assimp.Matrix4x4( * m.m00, m.m01, m.m02, m.m03, * m.m10, m.m11, m.m12, m.m13, * m.m20, m.m21, m.m22, m.m23, * m.m30, m.m31, m.m32, m.m33 * ); * * assimpMesh.Bones.Add(bone); * } * * int vertId = 0; * * foreach(BoneWeight bw in mesh.boneWeights) * { * assimpMesh.Bones[bw.boneIndex0].VertexWeights.Add(new VertexWeight(vertId, bw.weight0)); * assimpMesh.Bones[bw.boneIndex1].VertexWeights.Add(new VertexWeight(vertId, bw.weight1)); * assimpMesh.Bones[bw.boneIndex2].VertexWeights.Add(new VertexWeight(vertId, bw.weight2)); * assimpMesh.Bones[bw.boneIndex3].VertexWeights.Add(new VertexWeight(vertId, bw.weight3)); * vertId++; * } * }*/ return(assimpMesh); }
/** Updates the vertices of an obstacle. * \param obstacle %Obstacle to update * \param vertices New vertices for the obstacle, must have at least the number of vertices in the original obstacle * \param matrix %Matrix to multiply vertices with before updating obstacle * * The number of vertices in an obstacle cannot be changed, existing vertices can only be moved. */ public void UpdateObstacle (ObstacleVertex obstacle, Vector3[] vertices, Matrix4x4 matrix) { if (vertices == null) throw new System.ArgumentNullException ("Vertices must not be null"); if (obstacle == null) throw new System.ArgumentNullException ("Obstacle must not be null"); if (vertices.Length < 2) throw new System.ArgumentException ("Less than 2 vertices in an obstacle"); if (obstacle.split) throw new System.ArgumentException ("Obstacle is not a start vertex. You should only pass those ObstacleVertices got from AddObstacle method calls"); //Don't interfere with ongoing calculations if (Multithreading && doubleBuffering) for (int j=0;j<workers.Length;j++) workers[j].WaitOne(); //Compact obstacle and count int count = 0; ObstacleVertex c = obstacle; do { while (c.next.split) { c.next = c.next.next; c.next.prev = c; } if (count >= vertices.Length) { Debug.DrawLine (c.prev.position, c.position,Color.red); throw new System.ArgumentException ("Obstacle has more vertices than supplied for updating (" + vertices.Length+ " supplied)"); } c.position = matrix.MultiplyPoint3x4 (vertices[count]); count++; c = c.next; } while (c != obstacle); c = obstacle; do { Vector3 dir = c.next.position - c.position; c.dir = new Vector2 (dir.x,dir.z).normalized; c = c.next; } while (c != obstacle); ScheduleCleanObstacles (); UpdateObstacles(); }
public void OnSceneGUI() { var script = target as GraphUpdateScene; // Don't allow editing unless it is the active object if (Selection.activeGameObject != script.gameObject || script.legacyMode) { return; } // Make sure the points array is not null if (script.points == null) { script.points = new PF.Vector3[0]; EditorUtility.SetDirty(script); } List <PF.Vector3> points = ListPool <PF.Vector3> .Claim(); points.AddRange(script.points); Matrix4x4 invMatrix = script.transform.worldToLocalMatrix; Matrix4x4 matrix = script.transform.localToWorldMatrix; for (int i = 0; i < points.Count; i++) { points[i] = matrix.MultiplyPoint3x4(points[i]); } if (Tools.current != Tool.View && Event.current.type == EventType.Layout) { for (int i = 0; i < script.points.Length; i++) { HandleUtility.AddControl(-i - 1, HandleUtility.DistanceToLine(points[i], points[i])); } } if (Tools.current != Tool.View) { HandleUtility.AddDefaultControl(0); } for (int i = 0; i < points.Count; i++) { if (i == selectedPoint && Tools.current == Tool.Move) { Handles.color = PointSelectedColor; SphereCap(-i - 1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i]) * pointGizmosRadius * 2); Vector3 pre = points[i]; Vector3 post = Handles.PositionHandle(points[i], Quaternion.identity); if (pre != post) { Undo.RecordObject(script, "Moved Point"); script.points[i] = invMatrix.MultiplyPoint3x4(post); } } else { Handles.color = PointColor; SphereCap(-i - 1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i]) * pointGizmosRadius); } } if (Event.current.type == EventType.MouseDown) { int pre = selectedPoint; selectedPoint = -(HandleUtility.nearestControl + 1); if (pre != selectedPoint) { GUI.changed = true; } } if (Event.current.shift && Tools.current == Tool.Move) { HandleUtility.Repaint(); if (((int)Event.current.modifiers & (int)EventModifiers.Alt) != 0) { if (Event.current.type == EventType.MouseDown && selectedPoint >= 0 && selectedPoint < points.Count) { Undo.RecordObject(script, "Removed Point"); var arr = new List <PF.Vector3>(script.points); arr.RemoveAt(selectedPoint); points.RemoveAt(selectedPoint); script.points = arr.ToArray(); GUI.changed = true; } else if (points.Count > 0) { var index = -(HandleUtility.nearestControl + 1); if (index >= 0 && index < points.Count) { Handles.color = Color.red; SphereCap(0, points[index], Quaternion.identity, HandleUtility.GetHandleSize(points[index]) * 2f * pointGizmosRadius); } } } else { // Find the closest segment int insertionIndex = points.Count; float minDist = float.PositiveInfinity; for (int i = 0; i < points.Count; i++) { float dist = HandleUtility.DistanceToLine(points[i], points[(i + 1) % points.Count]); if (dist < minDist) { insertionIndex = i + 1; minDist = dist; } } var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); System.Object hit = HandleUtility.RaySnap(ray); Vector3 rayhit = Vector3.zero; bool didHit = false; if (hit != null) { rayhit = ((RaycastHit)hit).point; didHit = true; } else { var plane = new Plane(script.transform.up, script.transform.position); float distance; plane.Raycast(ray, out distance); if (distance > 0) { rayhit = ray.GetPoint(distance); didHit = true; } } if (didHit) { if (Event.current.type == EventType.MouseDown) { points.Insert(insertionIndex, rayhit); Undo.RecordObject(script, "Added Point"); var arr = new List <PF.Vector3>(script.points); arr.Insert(insertionIndex, invMatrix.MultiplyPoint3x4(rayhit)); script.points = arr.ToArray(); GUI.changed = true; } else if (points.Count > 0) { Handles.color = Color.green; Handles.DrawDottedLine(points[(insertionIndex - 1 + points.Count) % points.Count], rayhit, 8); Handles.DrawDottedLine(points[insertionIndex % points.Count], rayhit, 8); SphereCap(0, rayhit, Quaternion.identity, HandleUtility.GetHandleSize(rayhit) * pointGizmosRadius); // Project point down onto a plane var zeroed = invMatrix.MultiplyPoint3x4(rayhit); zeroed.y = 0; Handles.color = new Color(1, 1, 1, 0.5f); Handles.DrawDottedLine(matrix.MultiplyPoint3x4(zeroed), rayhit, 4); } } } if (Event.current.type == EventType.MouseDown) { Event.current.Use(); } } // Make sure the convex hull stays up to date script.RecalcConvex(); ListPool <PF.Vector3> .Release(ref points); if (GUI.changed) { HandleUtility.Repaint(); } }
private void Line2D (int start, int end, Matrix4x4 thisMatrix, bool useTransformMatrix) { var p1 = Vector3.zero; var p2 = Vector3.zero; var v1 = Vector3.zero; var px = Vector3.zero; Vector2 scaleFactor = new Vector2(Screen.width, Screen.height); int add, idx, widthIdx = 0; int widthIdxAdd = 0; if (m_lineWidths.Length > 1) { widthIdx = start; widthIdxAdd = 1; } if (m_continuous) { add = 1; idx = start*4; } else { add = 2; widthIdx /= 2; idx = start*2; } for (int i = start; i < end; i += add) { if (m_points2[i].x == m_points2[i+1].x && m_points2[i].y == m_points2[i+1].y) { SkipQuad (ref idx, ref widthIdx, ref widthIdxAdd); continue; } if (useTransformMatrix) { p1 = thisMatrix.MultiplyPoint3x4 (m_points2[i]); p2 = thisMatrix.MultiplyPoint3x4 (m_points2[i+1]); } else { p1.x = m_points2[i].x; p1.y = m_points2[i].y; p2.x = m_points2[i+1].x; p2.y = m_points2[i+1].y; } if (useViewportCoords) { p1.x *= scaleFactor.x; p1.y *= scaleFactor.y; p2.x *= scaleFactor.x; p2.y *= scaleFactor.y; } if (m_capLength == 0.0f) { px.x = p2.y - p1.y; px.y = p1.x - p2.x; float normalizedDistance = ( 1.0f / (float)System.Math.Sqrt ((px.x * px.x) + (px.y * px.y)) ); px *= normalizedDistance * m_lineWidths[widthIdx]; m_UIVertices[idx ].position.x = p1.x - px.x; m_UIVertices[idx ].position.y = p1.y - px.y; m_UIVertices[idx+3].position.x = p1.x + px.x; m_UIVertices[idx+3].position.y = p1.y + px.y; if (smoothWidth && i < end-add) { px.x = p2.y - v1.y; px.y = p1.x - p2.x; px *= normalizedDistance * m_lineWidths[widthIdx+1]; } } else { px.x = p2.x - p1.x; px.y = p2.y - p1.y; px *= ( 1.0f / (float)System.Math.Sqrt ((px.x * px.x) + (px.y * px.y)) ); p1 -= px * m_capLength; p2 += px * m_capLength; v1.x = px.y; v1.y = -px.x; px = v1 * m_lineWidths[widthIdx]; m_UIVertices[idx ].position.x = p1.x - px.x; m_UIVertices[idx ].position.y = p1.y - px.y; m_UIVertices[idx+3].position.x = p1.x + px.x; m_UIVertices[idx+3].position.y = p1.y + px.y; if (smoothWidth && i < end-add) { px = v1 * m_lineWidths[widthIdx+1]; } } m_UIVertices[idx+2].position.x = p2.x + px.x; m_UIVertices[idx+2].position.y = p2.y + px.y; m_UIVertices[idx+1].position.x = p2.x - px.x; m_UIVertices[idx+1].position.y = p2.y - px.y; idx += 4; widthIdx += widthIdxAdd; } if (m_joins == Joins.Weld) { if (m_continuous) { WeldJoins (start*4 + (start == 0? 4 : 0), end*4, Approximately (m_points2[0], m_points2[m_pointsCount-1])); } else { WeldJoinsDiscrete (start + 1, end, Approximately (m_points2[0], m_points2[m_pointsCount-1])); } } }
/** Draws some debug lines representing the rect */ public void DebugDraw (Matrix4x4 matrix, Color col) { Vector3 p1 = matrix.MultiplyPoint3x4 (new Vector3(xmin,0,ymin)); Vector3 p2 = matrix.MultiplyPoint3x4 (new Vector3(xmin,0,ymax)); Vector3 p3 = matrix.MultiplyPoint3x4 (new Vector3(xmax,0,ymax)); Vector3 p4 = matrix.MultiplyPoint3x4 (new Vector3(xmax,0,ymin)); Debug.DrawLine (p1,p2,col); Debug.DrawLine (p2,p3,col); Debug.DrawLine (p3,p4,col); Debug.DrawLine (p4,p1,col); }
/** Calculates minimum and maximum points for bounds \a b when multiplied with the matrix */ public void GetBoundsMinMax (Bounds b, Matrix4x4 matrix, out Vector3 min, out Vector3 max) { Vector3[] p = new Vector3[8]; p[0] = matrix.MultiplyPoint3x4 (b.center + new Vector3 ( b.extents.x, b.extents.y, b.extents.z)); p[1] = matrix.MultiplyPoint3x4 (b.center + new Vector3 ( b.extents.x, b.extents.y,-b.extents.z)); p[2] = matrix.MultiplyPoint3x4 (b.center + new Vector3 ( b.extents.x,-b.extents.y, b.extents.z)); p[3] = matrix.MultiplyPoint3x4 (b.center + new Vector3 ( b.extents.x,-b.extents.y,-b.extents.z)); p[4] = matrix.MultiplyPoint3x4 (b.center + new Vector3 (-b.extents.x, b.extents.y, b.extents.z)); p[5] = matrix.MultiplyPoint3x4 (b.center + new Vector3 (-b.extents.x, b.extents.y,-b.extents.z)); p[6] = matrix.MultiplyPoint3x4 (b.center + new Vector3 (-b.extents.x,-b.extents.y, b.extents.z)); p[7] = matrix.MultiplyPoint3x4 (b.center + new Vector3 (-b.extents.x,-b.extents.y,-b.extents.z)); min = p[0]; max = p[0]; for (int i=1;i<8;i++) { min = Vector3.Min (min,p[i]); max = Vector3.Max (max,p[i]); } }
private void Line2D (int start, int end, Matrix4x4 thisMatrix, bool useTransformMatrix) { Vector3 p1, p2; if (m_1pixelLine) { if (m_continuous) { int index = start*2; for (int i = start; i < end; i++) { if (useTransformMatrix) { p1 = thisMatrix.MultiplyPoint3x4 (points2[i]); p2 = thisMatrix.MultiplyPoint3x4 (points2[i+1]); } else { p1 = points2[i]; p2 = points2[i+1]; } p1.z = zDist; p2.z = zDist; m_lineVertices[index ] = p1; m_lineVertices[index+1] = p2; index += 2; } } else { for (int i = start; i <= end; i++) { if (useTransformMatrix) { p1 = thisMatrix.MultiplyPoint3x4 (points2[i]); } else { p1 = points2[i]; } p1.z = zDist; m_lineVertices[i] = p1; } } return; } int add, idx, widthIdx = 0; widthIdxAdd = 0; if (m_lineWidths.Length > 1) { widthIdx = start; widthIdxAdd = 1; } if (m_continuous) { idx = start*4; add = 1; } else { idx = start*2; add = 2; widthIdx /= 2; } if (capLength == 0.0f) { var perpendicular = new Vector3(0.0f, 0.0f, 0.0f); for (int i = start; i < end; i += add) { if (useTransformMatrix) { p1 = thisMatrix.MultiplyPoint3x4 (points2[i]); p2 = thisMatrix.MultiplyPoint3x4 (points2[i+1]); } else { p1 = points2[i]; p2 = points2[i+1]; } p1.z = zDist; if (p1.x == p2.x && p1.y == p2.y) {Skip (ref idx, ref widthIdx, ref p1); continue;} p2.z = zDist; v1.x = p2.y; v1.y = p1.x; v2.x = p1.y; v2.y = p2.x; perpendicular = v1 - v2; float normalizedDistance = ( 1.0f / Mathf.Sqrt ((perpendicular.x * perpendicular.x) + (perpendicular.y * perpendicular.y)) ); perpendicular *= normalizedDistance * m_lineWidths[widthIdx]; m_lineVertices[idx] = p1 - perpendicular; m_lineVertices[idx+1] = p1 + perpendicular; if (smoothWidth && i < end-add) { perpendicular = v1 - v2; perpendicular *= normalizedDistance * m_lineWidths[widthIdx+1]; } m_lineVertices[idx+2] = p2 - perpendicular; m_lineVertices[idx+3] = p2 + perpendicular; idx += 4; widthIdx += widthIdxAdd; } if (m_joins == Joins.Weld) { if (m_continuous) { WeldJoins (start*4 + (start == 0? 4 : 0), end*4, Approximately2 (points2[0], points2[points2.Length-1]) && m_minDrawIndex == 0 && (m_maxDrawIndex == points2.Length-1 || m_maxDrawIndex == 0)); } else { WeldJoinsDiscrete (start + 1, end, Approximately2 (points2[0], points2[points2.Length-1]) && m_minDrawIndex == 0 && (m_maxDrawIndex == points2.Length-1 || m_maxDrawIndex == 0)); } } } else { var thisLine = new Vector3(0.0f, 0.0f, 0.0f); for (int i = m_minDrawIndex; i < end; i += add) { if (useTransformMatrix) { p1 = thisMatrix.MultiplyPoint3x4 (points2[i]); p2 = thisMatrix.MultiplyPoint3x4 (points2[i+1]); } else { p1 = points2[i]; p2 = points2[i+1]; } p1.z = zDist; if (p1.x == p2.x && p1.y == p2.y) {Skip (ref idx, ref widthIdx, ref p1); continue;} p2.z = zDist; thisLine = p2 - p1; thisLine *= ( 1.0f / Mathf.Sqrt ((thisLine.x * thisLine.x) + (thisLine.y * thisLine.y)) ); p1 -= thisLine * capLength; p2 += thisLine * capLength; v1.x = thisLine.y; v1.y = -thisLine.x; thisLine = v1 * m_lineWidths[widthIdx]; m_lineVertices[idx] = p1 - thisLine; m_lineVertices[idx+1] = p1 + thisLine; if (smoothWidth && i < end-add) { thisLine = v1 * m_lineWidths[widthIdx+1]; } m_lineVertices[idx+2] = p2 - thisLine; m_lineVertices[idx+3] = p2 + thisLine; idx += 4; widthIdx += widthIdxAdd; } } }