void OnDrawGizmos( ) { UpdateSplineNodes( ); if( splineNodes == null ) return; DrawSplineGizmo( new Color( 0.5f, 0.5f, 0.5f, 0.5f ) ); Plane screen = new Plane( ); Gizmos.color = new Color( 1f, 1f, 1f, 0.5f ); screen.SetNormalAndPosition( Camera.current.transform.forward, Camera.current.transform.position ); foreach( SplineNode node in splineNodes ) { float sizeMultiplier = 0f; if( Camera.current.orthographic ) sizeMultiplier = Camera.current.orthographicSize * 2.5f; else screen.Raycast( new Ray( node.Position, Camera.current.transform.forward ), out sizeMultiplier ); Gizmos.DrawSphere( node.Position, sizeMultiplier * 0.015f ); } }
static bool Plane_SetNormalAndPosition__Vector3__Vector3(JSVCall vc, int argc) { int len = argc; if (len == 2) { UnityEngine.Vector3 arg0 = (UnityEngine.Vector3)JSApi.getVector3S((int)JSApi.GetType.Arg); UnityEngine.Vector3 arg1 = (UnityEngine.Vector3)JSApi.getVector3S((int)JSApi.GetType.Arg); UnityEngine.Plane argThis = (UnityEngine.Plane)vc.csObj; argThis.SetNormalAndPosition(arg0, arg1); JSMgr.changeJSObj(vc.jsObjID, argThis); } return(true); }
static public int SetNormalAndPosition(IntPtr l) { try{ UnityEngine.Plane self=(UnityEngine.Plane)checkSelf(l); UnityEngine.Vector3 a1; checkType(l,2,out a1); UnityEngine.Vector3 a2; checkType(l,3,out a2); self.SetNormalAndPosition(a1,a2); setBack(l,self); return 0; } catch(Exception e) { LuaDLL.luaL_error(l, e.ToString()); return 0; } }
void OnDrawGizmos( ) { UpdateSpline( ); if( !HasNodes ) return; DrawSplineGizmo( new Color( 0.5f, 0.5f, 0.5f, 0.5f ) ); Plane screen = new Plane( ); Gizmos.color = new Color( 1f, 1f, 1f, 0.5f ); screen.SetNormalAndPosition( Camera.current.transform.forward, Camera.current.transform.position ); foreach( SplineNode node in splineNodesInternal ) Gizmos.DrawSphere( node.Position, GetSizeMultiplier( node ) * 2 ); }
void Start() { UnityEngine.Plane worldPlane = new UnityEngine.Plane(); worldPlane.SetNormalAndPosition(Vector3.back, Vector3.zero); Ray ray; float distance; ray = followCam.ViewportPointToRay(Vector3.zero); worldPlane.Raycast(ray, out distance); Vector3 viewMin = ray.GetPoint(distance); ray = followCam.ViewportPointToRay(Vector3.one); worldPlane.Raycast(ray, out distance); Vector3 viewMax = ray.GetPoint(distance); cameraBounds.min = viewBounds.min - viewMin; cameraBounds.max = viewBounds.max - viewMax; }
public override void OnBrushApply(z_BrushTarget target, z_BrushSettings settings) { Vector3[] vertices = target.editableObject.vertices; Vector3 v, t, avg, dirVec = direction.ToVector3(); Plane plane = new Plane(Vector3.up, Vector3.zero); foreach(KeyValuePair<int, float> weight in target.weights) { if(weight.Value < .0001f || (ignoreNonManifoldIndices && nonManifoldIndices.Contains(weight.Key))) continue; v = vertices[weight.Key]; if(direction == z_Direction.Normal && relax) avg = z_Math.Average(cached_vertices, neighborLookup[weight.Key]); else avg = z_Math.WeightedAverage(cached_vertices, neighborLookup[weight.Key], target.weights); if(direction != z_Direction.Normal || !relax) { if(direction == z_Direction.Normal) dirVec = z_Math.WeightedAverage(cached_normals, neighborLookup[weight.Key], target.weights).normalized; plane.SetNormalAndPosition(dirVec, avg); avg = v - dirVec * plane.GetDistanceToPoint(v); } t = Vector3.Lerp(v, avg, weight.Value); vertices[weight.Key] = v + (t-v) * settings.strength * SMOOTH_STRENGTH_MODIFIER; } target.editableObject.mesh.vertices = vertices; if(tempComponent != null) tempComponent.OnVerticesMoved(); base.OnBrushApply(target, settings); }
/// <summary> /// Create a plane from a list of points at random. /// </summary> /// <returns>A random plane.</returns> /// <param name="cam">The Unity camera so we can make sure the plane orientation is correct.</param> /// <param name="points">The points to compute a random plane from.</param> private Plane MakeRandomPlane(Camera cam, List<int> points) { if (points.Count < 3) { return new Plane(); } // Choose 3 points randomly. int r0 = m_rand.Next(points.Count); int r1 = m_rand.Next(points.Count - 1); // Make sure we handle collisions. if (r1 == r0) { r1++; } else if (r1 < r0) { // We'll make sure to keep r0 and r1 in sorted order. int temp = r0; r0 = r1; r1 = temp; } int r2 = m_rand.Next(points.Count - 2); // Handle collisions. if (r2 == r0) { ++r2; } if (r2 == r1) { ++r2; } int idx0 = points[r0]; int idx1 = points[r1]; int idx2 = points[r2]; Vector3 p0 = m_points[idx0]; Vector3 p1 = m_points[idx1]; Vector3 p2 = m_points[idx2]; // Define the plane Plane plane = new Plane(p0, p1, p2); // Make sure that the normal of the plane points towards the camera. if (Vector3.Dot(cam.transform.forward, plane.normal) > 0) { plane.SetNormalAndPosition(plane.normal * -1.0f, p0); } return plane; }
void UpdateParticles2() { Plane movePlane = new Plane(); for (int i = 1; i < m_Particles.Count; ++i) { Particle p = m_Particles[i]; Particle p0 = m_Particles[p.m_ParentIndex]; float restLen; if (p.m_Transform != null) { restLen = (p0.m_Transform.position - p.m_Transform.position).magnitude; } else { restLen = p0.m_Transform.localToWorldMatrix.MultiplyVector(p.m_EndOffset).magnitude; } // keep shape float stiffness = Mathf.Lerp(1.0f, p.m_Stiffness, m_Weight); if (stiffness > 0 || p.m_Elasticity > 0) { Matrix4x4 m0 = p0.m_Transform.localToWorldMatrix; m0.SetColumn(3, p0.m_Position); Vector3 restPos; if (p.m_Transform != null) { restPos = m0.MultiplyPoint3x4(p.m_Transform.localPosition); } else { restPos = m0.MultiplyPoint3x4(p.m_EndOffset); } Vector3 d = restPos - p.m_Position; p.m_Position += d * p.m_Elasticity; if (stiffness > 0) { d = restPos - p.m_Position; float len = d.magnitude; float maxlen = restLen * (1 - stiffness) * 2; if (len > maxlen) { p.m_Position += d * ((len - maxlen) / len); } } } // collide if (m_Colliders != null) { float particleRadius = p.m_Radius * m_ObjectScale; foreach (DynamicBoneCollider c in m_Colliders) { if (c != null && c.enabled) c.Collide(ref p.m_Position, particleRadius); } } // freeze axis, project to plane if (m_FreezeAxis != FreezeAxis.None) { switch (m_FreezeAxis) { case FreezeAxis.X: movePlane.SetNormalAndPosition(p0.m_Transform.right, p0.m_Position); break; case FreezeAxis.Y: movePlane.SetNormalAndPosition(p0.m_Transform.up, p0.m_Position); break; case FreezeAxis.Z: movePlane.SetNormalAndPosition(p0.m_Transform.forward, p0.m_Position); break; } p.m_Position -= movePlane.normal * movePlane.GetDistanceToPoint(p.m_Position); } // keep length Vector3 dd = p0.m_Position - p.m_Position; float leng = dd.magnitude; if (leng > 0) { p.m_Position += dd * ((leng - restLen) / leng); } } }
private void Split(Plane worldPlane, Vector3 lineStart, Vector3 lineEnd, int casts) { if (!splitting) return; // Sometimes there is null here // Too lazy to find real reason right now if (transform == null) return; float distance = worldPlane.GetDistanceToPoint(transform.position); Plane plane = new Plane(); Vector3 newNormal = transform.InverseTransformDirection(worldPlane.normal); //Vector3 newNormal = Quaternion.Inverse(transform.rotation) * worldPlane.normal; plane.SetNormalAndPosition(newNormal, newNormal * -distance); posNormals.Add(-newNormal); negNormals.Add(newNormal); posNormals.AddRange(mesh.normals); negNormals.AddRange(mesh.normals); for (int i = 0; i < triangles.Length; i += 3) { int i1 = triangles[i];//index 1, 2 and 3 of vertices int i2 = triangles[i + 1]; int i3 = triangles[i + 2]; bool side1 = plane.GetSide(vertices[i1]); bool side2 = plane.GetSide(vertices[i2]); bool side3 = plane.GetSide(vertices[i3]); if (side1 == true && side2 == true && side3 == true) { posTriangles.Add(i1 + 1); //first index is reserved for interior face middle vertex so every index is incremented by 1 posTriangles.Add(i2 + 1); posTriangles.Add(i3 + 1); } else if (side1 == false && side2 == false && side3 == false) { negTriangles.Add(i1 + 1); negTriangles.Add(i2 + 1); negTriangles.Add(i3 + 1); } else { //find odd boolean value(expression found using karnaugh map) bool odd = (!side1 && !side2) || (!side1 && !side3) || (!side2 && !side3); //find vertex with odd boolean value int vertex1, vertex2; if (side1 == odd) { vertex1 = findNewVertex(i1, i2, plane); vertex2 = findNewVertex(i1, i3, plane); if (side1 == true) { // i1 /\ Positive Side posTriangles.Add(i1 + 1); // / \ posTriangles.Add(vertex1 + 1); // vertex1/____\vertex2 posTriangles.Add(vertex2 + 1); // / _-'\ // i2/_.-'____\i3 Negative Side negTriangles.Add(i2 + 1); negTriangles.Add(i3 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(i2 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(vertex1 + 1); } else { // i1 /\ Negative Side negTriangles.Add(i1 + 1); // / \ negTriangles.Add(vertex1 + 1); // vertex1/____\vertex2 negTriangles.Add(vertex2 + 1); // / _-'\ // i2/_.-'____\i3 Positive Side posTriangles.Add(i2 + 1); posTriangles.Add(i3 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(i2 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(vertex1 + 1); } } else if (side2 == odd) { vertex1 = findNewVertex(i2, i3, plane); vertex2 = findNewVertex(i2, i1, plane); if (side2 == true) { // i2 /\ Positive Side posTriangles.Add(i2 + 1); // / \ posTriangles.Add(vertex1 + 1); // vertex1/____\vertex2 posTriangles.Add(vertex2 + 1); // / _-'\ // i3/_.-'____\i1 Negative Side negTriangles.Add(i3 + 1); negTriangles.Add(i1 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(i3 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(vertex1 + 1); } else { // i2 /\ Negative Side negTriangles.Add(i2 + 1); // / \ negTriangles.Add(vertex1 + 1); // vertex1/____\vertex2 negTriangles.Add(vertex2 + 1); // / _-'\ // i3/_.-'____\i1 Positive Side posTriangles.Add(i3 + 1); posTriangles.Add(i1 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(i3 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(vertex1 + 1); } } else { vertex1 = findNewVertex(i3, i1, plane); vertex2 = findNewVertex(i3, i2, plane); if (side3 == true) { // i3 /\ Positive Side posTriangles.Add(i3 + 1); // / \ posTriangles.Add(vertex1 + 1); // vertex1/____\vertex2 posTriangles.Add(vertex2 + 1); // / _-'\ // i1/_.-'____\i2 Negative Side negTriangles.Add(i1 + 1); negTriangles.Add(i2 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(i1 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(vertex1 + 1); } else { // i3 /\ Negative Side negTriangles.Add(i3 + 1); // / \ negTriangles.Add(vertex1 + 1); // vertex1/____\vertex2 negTriangles.Add(vertex2 + 1); // / _-'\ // i1/_.-'____\i2 Positive Side posTriangles.Add(i1 + 1); posTriangles.Add(i2 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(i1 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(vertex1 + 1); } } if (odd == true) { //add inner triangles posInnerTriangles.Add(vertex1 + 2); posInnerTriangles.Add(0); posInnerTriangles.Add(vertex2 + 2); negInnerTriangles.Add(vertex1 + 2); negInnerTriangles.Add(vertex2 + 2); negInnerTriangles.Add(0); } else { negInnerTriangles.Add(vertex1 + 2); negInnerTriangles.Add(0); negInnerTriangles.Add(vertex2 + 2); posInnerTriangles.Add(vertex1 + 2); posInnerTriangles.Add(vertex2 + 2); posInnerTriangles.Add(0); } /*if (odd == true) { //add inner triangles posTriangles.Add(vertex1 + 1); posTriangles.Add(0); posTriangles.Add(vertex2 + 1); negTriangles.Add(vertex1 + 1); negTriangles.Add(vertex2 + 1); negTriangles.Add(0); } else { negTriangles.Add(vertex1 + 1); negTriangles.Add(0); negTriangles.Add(vertex2 + 1); posTriangles.Add(vertex1 + 1); posTriangles.Add(vertex2 + 1); posTriangles.Add(0); }*/ } } //now average all seam vertices to find center of inner face float x = 0; float y = 0; float z = 0; int n = seamVertices.Count; for (int j = 0; j < n; j++) { Vector3 current = seamVertices[j]; x += current.x; y += current.y; z += current.z; } Vector3 center = new Vector3(x / n, y / n, z / n); var newVertices = new List<Vector3>(); newVertices.Add(center); // at index 0 newVertices.AddRange(vertices); // index 1 to vertices.length newVertices.AddRange(seamVertices); // then add the new seam vertices Vector3[] doneVertices = newVertices.ToArray(); Vector2[] uvs = uv.ToArray(); if (posTriangles.Count != 0 && negTriangles.Count != 0)//dont bother creating a gameobject if there are no triangles { Vector3 force = plane.normal * 50f; CreateNewSplit(doneVertices, posTriangles.ToArray(), posInnerTriangles.ToArray(), uvs, posNormals.ToArray(), force); CreateNewSplit(doneVertices, negTriangles.ToArray(), negInnerTriangles.ToArray(), uvs, negNormals.ToArray(), -force); if (OnSplit != null) OnSplit(); } else { return; } splitting = false; PlaneGenerator.OnGeneration -= Split; Destroy(gameObject); }
private Vector3 GetScreenPointInWorldPlane(Vector3 screenPoint, float height) { var ray = Camera.main.ScreenPointToRay(screenPoint); var worldPlane = new Plane(); var dist = 0f; worldPlane.SetNormalAndPosition(Vector3.up, new Vector3(0, height, 0)); worldPlane.Raycast(ray, out dist); return ray.GetPoint(dist); }
public void OnSceneGUI() { // We can only build or do anything if we can link to our libraries. #if !( UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || ( UNITY_METRO && UNITY_EDITOR ) ) return; #pragma warning disable 0162 #endif // !( UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || ( UNITY_METRO && UNITY_EDITOR ) ) if ( myCurve == null ) return; // Enable point size on OpenGL. HoudiniHost.preDrawSetup(); // First Remake and Draw Guide Geometry if necessary. if ( mySelectionMeshColours == null ) buildGuideGeometry(); if ( !myTempCamera && Camera.current ) myTempCamera = Camera.current; Event current_event = Event.current; Vector3 mouse_position = getMousePosition( ref current_event ); // Set appropriate handles matrix. // TODO: Fix. /* Vector3 handle_pos = HAPI_AssetUtility.getPosition( myCurve.transform.localToWorldMatrix ); Quaternion handle_rot = HAPI_AssetUtility.getQuaternion( myCurve.transform.localToWorldMatrix ); Matrix4x4 handle_matrix = Matrix4x4.identity; handle_matrix.SetTRS( handle_pos, handle_rot, new Vector3( 1.0f, 1.0f, 1.0f ) ); //Debug.Log( handle_pos ); //Debug.Log( handle_rot ); //Debug.Log( handle_matrix ); Handles.matrix = handle_matrix; */ Handles.matrix = myCurve.transform.localToWorldMatrix; // Determine key state. getKeyState( current_event ); // Decide modes. if ( current_event.type == EventType.Layout && mySceneWindowHasFocus ) decideModes( ref current_event ); // Draw scene UI. drawSceneUI(); // Add points. if ( !current_event.alt && !( current_event.type == EventType.MouseDown && current_event.button == 1 ) ) { if ( myCurve.prIsAddingPoints ) { Vector3 position = Vector3.zero; float handle_size = HandleUtility.GetHandleSize( position ) * myBigButtonHandleSizeMultiplier; Quaternion rotation = HoudiniAssetUtility.getQuaternion( myTempCamera.transform.localToWorldMatrix ); bool button_press = Handles.Button( position, rotation, handle_size, handle_size, Handles.RectangleCap ); Ray ray = myTempCamera.ScreenPointToRay( mouse_position ); #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 if ( !myTempCamera.isOrthoGraphic ) #else if ( !myTempCamera.orthographic ) #endif // UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 ray.origin = myTempCamera.transform.position; Vector3 intersection = new Vector3(); if ( myTarget != null && myTarget.GetComponent< Collider >() ) { Collider collider = myTarget.GetComponent< Collider >(); RaycastHit hit_info; collider.Raycast( ray, out hit_info, myIntersectionRayLength ); intersection = hit_info.point; } else { Plane plane = new Plane(); plane.SetNormalAndPosition( Vector3.up, Vector3.zero ); float enter = 0.0f; plane.Raycast( ray, out enter ); enter = Mathf.Clamp( enter, myTempCamera.nearClipPlane, myTempCamera.farClipPlane ); intersection = ray.origin + ray.direction * enter; } bool is_mid_point = false; int insert_index = -1; Vector3 new_point_location = intersection; // Draw guide line. if ( myCurve.prPoints.Count > 0 ) { Vector3 anchor1 = myCurve.transform.TransformPoint( myCurve.prPoints[ myCurve.prPoints.Count - 1 ] ); Vector3 anchor2 = Vector3.zero; insert_index = myCurve.prPoints.Count; // See if we're close to another segment. for ( int i = 1; i < myCurve.prPoints.Count; ++i ) { Vector3 p0 = myCurve.transform.TransformPoint( myCurve.prPoints[ i - 1 ] ); Vector3 p1 = myCurve.transform.TransformPoint( myCurve.prPoints[ i ] ); Vector3 closest_point = new Vector3(); float distance = HoudiniGUIUtility.closestDistanceBetweenLineAndLineSegment( p0, p1, ray, out closest_point ); if ( distance < HandleUtility.GetHandleSize( closest_point ) / HoudiniHost.prGuideMinDistanceForMidPointInsertion ) { anchor1 = p0; anchor2 = p1; new_point_location = closest_point; insert_index = i; is_mid_point = true; } } int point_count = ( is_mid_point ? 3 : 2 ); Vector3[] line_vertices = new Vector3[ point_count ]; int[] line_indices = new int[ point_count ]; Vector2[] uvs = new Vector2[ point_count ]; line_vertices[ 0 ] = HoudiniGUIUtility.getCameraNearPlanePoint( anchor1, myTempCamera ); line_vertices[ 1 ] = HoudiniGUIUtility.getCameraNearPlanePoint( new_point_location, myTempCamera ); float length = Vector3.Distance( line_vertices[ 0 ], line_vertices[ 1 ] ) * myGuideLinesDashTilingMultiplier; line_indices[ 0 ] = 0; line_indices[ 1 ] = 1; uvs[ 0 ] = new Vector2(); uvs[ 1 ] = new Vector2( length, length ); if ( is_mid_point ) { line_vertices[ 2 ] = HoudiniGUIUtility.getCameraNearPlanePoint( anchor2, myTempCamera ); line_indices[ 2 ] = 2; length += Vector3.Distance( line_vertices[ 1 ], line_vertices[ 2 ] ) * myGuideLinesDashTilingMultiplier; uvs[ 2 ] = new Vector2( length, length ); } myGuideLinesMesh.Clear(); myGuideLinesMesh.vertices = line_vertices; myGuideLinesMesh.uv = uvs; myGuideLinesMesh.SetIndices( line_indices, MeshTopology.LineStrip, 0 ); myGuideLinesMaterial.SetPass( 0 ); myGuideLinesMaterial.SetColor( "_Color", HoudiniHost.prGuideWireframeColour ); myGuideLinesMaterial.SetTextureScale( "_MainTex", new Vector2( 1.0f, 1.0f ) ); Graphics.DrawMeshNow( myGuideLinesMesh, Matrix4x4.identity ); } // Add points on click. if ( button_press ) { // Once we add a point we are no longer bound to the user holding down the add points key. // Add points mode is now fully activated. myCurve.prModeChangeWait = false; // Need to inverse transform the new point because it is in world space // and we want it to stay in the same location as it is in world space // when it is parented to the curve's transform. new_point_location = myCurve.transform.InverseTransformPoint( new_point_location ); if ( is_mid_point ) myCurve.insertPoint( insert_index, new_point_location ); else myCurve.addPoint( new_point_location ); // Remake and Draw Guide Geometry buildGuideGeometry(); } // Delete last point on backspace. if ( current_event.isKey && current_event.type == EventType.KeyUp && ( current_event.keyCode == KeyCode.Delete || current_event.keyCode == KeyCode.Backspace ) ) { // Once we add a point we are no longer bound to the user holding down the add points key. // Add points mode is now fully activated. myCurve.prModeChangeWait = false; myCurve.deleteLastPoint(); // Remake and Draw Guide Geometry buildGuideGeometry(); } if ( current_event.isKey && current_event.keyCode == KeyCode.Delete || current_event.keyCode == KeyCode.Backspace ) { Event.current.Use(); } } // Add mode. else if ( myCurve.prIsEditingPoints ) { // Track mouse dragging. if ( current_event.type == EventType.MouseDown && current_event.button == 0 && !myIsMouseDown ) { myIsMouseDown = true; myFirstMousePosition = mouse_position; } // I have to also interpret the Ignore event as the mouse up event because that's all I // get if the use lets go of the mouse button while over a different Unity window... else if ( ( ( current_event.type == EventType.MouseUp && current_event.button == 0 ) || ( current_event.type == EventType.Ignore ) ) && myIsMouseDown ) { myIsMouseDown = false; // Deselect all. if ( !current_event.control ) clearSelection(); Ray ray = myTempCamera.ScreenPointToRay( mouse_position ); ray.origin = myTempCamera.transform.position; Vector3 mouse_delta = myFirstMousePosition - mouse_position; Vector3 max_bounds = mouse_position; Vector3 min_bounds = mouse_position; max_bounds.x = Mathf.Max( max_bounds.x, myFirstMousePosition.x ); max_bounds.y = Mathf.Max( max_bounds.y, myFirstMousePosition.y ); min_bounds.x = Mathf.Min( min_bounds.x, myFirstMousePosition.x ); min_bounds.y = Mathf.Min( min_bounds.y, myFirstMousePosition.y ); // Get Picking Information Vector3[] points = myCurve.prPoints.ToArray(); for ( int i = 0; points != null && i < points.Length; ++i ) { Vector3 transformed_point = myCurve.transform.TransformPoint( points[ i ] ); Vector3 proj_pos = myTempCamera.WorldToScreenPoint( transformed_point ); proj_pos.z = 0.0f; if ( Mathf.Abs( mouse_delta.x ) > 1.5f || Mathf.Abs( mouse_delta.y ) > 1.5f ) { if ( proj_pos.x >= min_bounds.x && proj_pos.x <= max_bounds.x && proj_pos.y >= min_bounds.y && proj_pos.y <= max_bounds.y ) { // Once we modify a point we are no longer bound to the user holding down // the point edit key. Edit point mode is now fully activated. myCurve.prModeChangeWait = false; togglePointSelection( i ); } } // drag else { float distance = Vector3.Distance( mouse_position, proj_pos ); if ( distance < HoudiniHost.prMinDistanceForPointSelection ) { // Once we modify a point we are no longer bound to the user holding down // the point edit key. Edit point mode is now fully activated. myCurve.prModeChangeWait = false; togglePointSelection( i ); } // if point hit } // single click } // for all points } // mouse up // Prevent click from being passed lower (this is so stupid!). Vector3 position = Vector3.zero; float handle_size = HandleUtility.GetHandleSize( position ) * myBigButtonHandleSizeMultiplier; Quaternion rotation = HoudiniAssetUtility.getQuaternion( myTempCamera.transform.localToWorldMatrix ); Handles.Button( position, rotation, handle_size, handle_size, Handles.RectangleCap ); // Prevent the delete key from deleting the curve in this mode. if ( current_event.isKey && current_event.keyCode == KeyCode.Delete ) { Event.current.Use(); } } // Edit mode. } // Not currently pressing alt. if ( myForceInspectorRedraw ) { Repaint(); myForceInspectorRedraw = false; } // Create selection area. if ( myCurve.prIsEditingPoints && myIsMouseDown ) { float sel_left = Mathf.Min( myFirstMousePosition.x, mouse_position.x ); float sel_top = myTempCamera.pixelHeight - Mathf.Max( myFirstMousePosition.y, mouse_position.y ); float sel_width = Mathf.Abs( myFirstMousePosition.x - mouse_position.x ); float sel_height = Mathf.Abs( myFirstMousePosition.y - mouse_position.y ); mySelectionArea = new Rect( sel_left, sel_top, sel_width, sel_height ); } else mySelectionArea = new Rect(); // Hide default transform handles. myIsTransformHandleHidden = myCurve.prIsEditingPoints; // Update active control point. if ( mySelectedPoints.Count > 0 ) { if ( myCurrentlyPressedKey == KeyCode.Delete ) { // Handle deletions. myCurve.deletePoints( mySelectedPoints.ToArray() ); clearSelection(); Event.current.Use(); } else { // Create midpoint for the handle. Vector3 max_bounds = myCurve.prPoints[ mySelectedPoints[ 0 ] ]; Vector3 min_bounds = myCurve.prPoints[ mySelectedPoints[ 0 ] ]; for ( int i = 1; i < mySelectedPoints.Count; ++i ) { Vector3 current_pos = myCurve.prPoints[ mySelectedPoints[ i ] ]; max_bounds.x = Mathf.Max( max_bounds.x, current_pos.x ); max_bounds.y = Mathf.Max( max_bounds.y, current_pos.y ); max_bounds.z = Mathf.Max( max_bounds.z, current_pos.z ); min_bounds.x = Mathf.Min( min_bounds.x, current_pos.x ); min_bounds.y = Mathf.Min( min_bounds.y, current_pos.y ); min_bounds.z = Mathf.Min( min_bounds.z, current_pos.z ); } Vector3 mid_pos = ( max_bounds + min_bounds ) / 2.0f; Vector3 new_mid_pos = Handles.PositionHandle( mid_pos, Quaternion.identity ); if ( new_mid_pos != mid_pos ) { myIsMouseDown = false; Vector3 delta = new_mid_pos - mid_pos; for ( int i = 0; i < mySelectedPoints.Count; ++i ) { int point_index = mySelectedPoints[ i ]; Vector3 old_pos = myCurve.prPoints[ point_index ]; Vector3 new_pos = old_pos + delta; myCurve.updatePoint( point_index, new_pos ); } // Remake and Draw Guide Geometry myCurve.updatePoints(); buildGuideGeometry(); } } // Delete? } // Connection Mesh Draws if ( myConnectionMaterial != null && myConnectionMesh != null ) { myConnectionMaterial.SetPass( 0 ); Graphics.DrawMeshNow( myConnectionMesh, myCurve.transform.localToWorldMatrix ); } // Selection Mesh Draws if ( mySelectionMaterial != null ) { mySelectionMaterial.SetFloat( "_PointSize", HoudiniHost.prGuidePointSize ); mySelectionMaterial.SetColor( "_Color", HoudiniHost.prGuideWireframeColour ); if ( mySelectionMaterial.SetPass( 0 ) ) { // TODO: Clean this up! Camera tempCamera = Camera.current; float s = HoudiniHost.prGuidePointSize / 2.0f; float w = tempCamera.pixelWidth; float h = tempCamera.pixelHeight; GL.PushMatrix(); GL.Begin( GL.QUADS ); GL.LoadOrtho(); for ( int i = 0; i < myCurve.prPoints.Count; ++i ) { Vector3 p = myCurve.prPoints[ i ]; p = myCurve.transform.TransformPoint( p ); p = tempCamera.WorldToScreenPoint( p ); if ( p.x < 0.0f || p.x > w ) continue; if ( p.y < 0.0f || p.y > h ) continue; if ( p.z < 0.0f ) continue; p.z = 0.0f; GL.Color( mySelectionMeshColours[ i ] ); GL.Vertex3( ( p.x + s ) / w, ( p.y + s ) / h, p.z ); GL.Vertex3( ( p.x + s ) / w, ( p.y - s ) / h, p.z ); GL.Vertex3( ( p.x - s ) / w, ( p.y - s ) / h, p.z ); GL.Vertex3( ( p.x - s ) / w, ( p.y + s ) / h, p.z ); } GL.End(); GL.PopMatrix(); } mySelectionMaterial.SetFloat( "_PointSize", HoudiniHost.prGuidePointSize - myGuideBorderSize ); mySelectionMaterial.SetColor( "_Color", Color.white ); if ( mySelectionMaterial.SetPass( 1 ) ) { // TODO: Clean this up! Camera tempCamera = Camera.current; float s = ( HoudiniHost.prGuidePointSize - myGuideBorderSize ) / 2.0f; float w = tempCamera.pixelWidth; float h = tempCamera.pixelHeight; GL.PushMatrix(); GL.Begin( GL.QUADS ); GL.LoadOrtho(); for ( int i = 0; i < myCurve.prPoints.Count; ++i ) { Vector3 p = myCurve.prPoints[ i ]; p = myCurve.transform.TransformPoint( p ); p = tempCamera.WorldToScreenPoint( p ); if ( p.x < 0.0f || p.x > w ) continue; if ( p.y < 0.0f || p.y > h ) continue; if ( p.z < 0.0f ) continue; p.z = 0.0f; GL.Color( mySelectionMeshColours[ i ] ); GL.Vertex3( ( p.x + s ) / w, ( p.y + s ) / h, p.z ); GL.Vertex3( ( p.x + s ) / w, ( p.y - s ) / h, p.z ); GL.Vertex3( ( p.x - s ) / w, ( p.y - s ) / h, p.z ); GL.Vertex3( ( p.x - s ) / w, ( p.y + s ) / h, p.z ); } GL.End(); GL.PopMatrix(); } } #if !( UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || ( UNITY_METRO && UNITY_EDITOR ) ) #pragma warning restore 0162 #endif // !( UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || ( UNITY_METRO && UNITY_EDITOR ) ) }
public void SliceMyMesh() { if (MyMesh) { //GameObject SlicedMesh = (GameObject) Instantiate (gameObject, transform.position, Quaternion.identity); // off set the positions after the meshes are created, by finding out the releative bounds difference SlicedCustomMesh1 = (CustomMesh) gameObject.GetComponent("CustomMesh"); //SlicedCustomMesh2 = (CustomMesh) SlicedMesh.GetComponent("CustomMesh"); //SlicedCustomMesh2.IsSingleCube = false; //MyPlaneNormal = MyPlaneRotation.transform.position; MySlicePlane = new Plane(MyPlaneNormal, PlaneDistance); MySlicePlane.SetNormalAndPosition(MyPlaneNormal, new Vector3(0,0.5f,0)); SlicedPoints1 = new List<Vector3>(); SlicedIndicies1 = new List<int>(); SlicedPoints2 = new List<Vector3>(); SlicedIndicies2 = new List<int>(); // Where the plane intersects SlicedIndicies3 = new List<int>(); SlicedIndicies4 = new List<int>(); SlicedPoints4 = new List<Vector3>(); //SlicedMesh.MyCustomMesh // Add all the indicies that intersect with the plane for (int i = 0; i < SlicedCustomMesh1.MyCustomMesh.Indicies.Count; i += 3) { // for all the meshes triangles Vector3 Vertex1 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedCustomMesh1.MyCustomMesh.Indicies[i]]; Vector3 Vertex2 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedCustomMesh1.MyCustomMesh.Indicies[i+1]]; Vector3 Vertex3 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedCustomMesh1.MyCustomMesh.Indicies[i+2]]; if (MySlicePlane.GetSide(Vertex1+MyPlanePosition) && MySlicePlane.GetSide(Vertex2+MyPlanePosition) && MySlicePlane.GetSide(Vertex3+MyPlanePosition)) { // if all 3 verticies are on the same side } else if (!MySlicePlane.GetSide(Vertex1+MyPlanePosition) && !MySlicePlane.GetSide(Vertex2+MyPlanePosition) && !MySlicePlane.GetSide(Vertex3+MyPlanePosition)) { // if they are not on same side, they are intersected by the plane } else { SlicedIndicies3.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i]); SlicedIndicies3.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i+1]); SlicedIndicies3.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i+2]); SlicedPoints3.Add (Vertex1); SlicedPoints3.Add (Vertex2); SlicedPoints3.Add (Vertex3); } } // Now create slices in these triangles for (int i = 0; i < SlicedIndicies3.Count; i += 3) { // for all the meshes triangles Vector3 Vertex1 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]]; Vector3 Vertex2 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i+1]]; Vector3 Vertex3 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i+2]]; // from a slice through a triangle // we can get a maximum of 5 points // break down 5 points into 3 triangles // test this by chose random points along the triangle lines and adding them vertexes //RaycastHit hit1; Ray Ray1 = new Ray(Vertex1, Vertex1-Vertex2); float RayDistance1 = Vector3.Distance (Vertex1, Vertex2); if (MySlicePlane.Raycast(Ray1, out RayDistance1)) { Debug.Log(" Triangles are sliced 1" ); DebugVector3 (SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]]); } else { Debug.Log(i + " Triangles Not Sliced Vertex 1 - "); Debug.Log (" Distance: " + RayDistance1); DebugVector3 (Vertex1); DebugVector3 (Vertex1 + Ray1.direction*RayDistance1); if (RayDistance1 != 0) { SlicedIndicies4.Add (i); SlicedPoints4.Add (Vertex1 + Ray1.direction*RayDistance1); //SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]] = Vertex1 + Ray1.direction*RayDistance1; } } Ray Ray2 = new Ray(Vertex2, Vertex2-Vertex3); float RayDistance2 = Vector3.Distance (Vertex1, Vertex2); if (MySlicePlane.Raycast(Ray2, out RayDistance2)) { Debug.Log(" Triangles are sliced 2" ); SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]] = Vertex2 + Ray2.direction*RayDistance2; } else { Debug.Log(i + " Triangles Not Sliced 2 - "); Debug.Log (" Distance: " + RayDistance2); DebugVector3 (Vertex2); DebugVector3 (Vertex2 + Ray2.direction*RayDistance2); if (RayDistance2 != 0) { SlicedIndicies4.Add (i+1); SlicedPoints4.Add (Vertex2 + Ray2.direction*RayDistance2); //SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]] = Vertex2 + Ray2.direction*RayDistance2; } } Ray Ray3 = new Ray(Vertex3, Vertex3-Vertex1); float RayDistance3 = Vector3.Distance (Vertex3, Vertex1); if (MySlicePlane.Raycast(Ray3, out RayDistance3)) { Debug.Log(" Triangles are sliced 3" ); DebugVector3 (Vertex2); SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]] = Vertex3 + Ray3.direction*RayDistance3; } else { Debug.Log(i + " Triangles a Not Sliced 3 -"); Debug.Log (" Distance: " + RayDistance3); DebugVector3 (Vertex3); DebugVector3 (Vertex3 + Ray3.direction*RayDistance3); if (RayDistance3 != 0) { SlicedIndicies4.Add (i+2); SlicedPoints4.Add (Vertex3 + Ray3.direction*RayDistance3); //SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedIndicies3[i]] = Vertex3 + Ray3.direction*RayDistance3; } } //Mathf.Int } // test slice through the middle, where the plane is .5, .5 ,.5 // Concieve a 2d plane that is the slice (sword action pewpew) // find all the intersecting points with the mesh for (int i = 0; i < SlicedCustomMesh1.MyCustomMesh.Indicies.Count; i += 3) { // for all the meshes triangles //SlicedCustomMesh2.MyCustomMesh.Indicies[i] = 0; Vector3 Vertex1 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedCustomMesh1.MyCustomMesh.Indicies[i]]; Vector3 Vertex2 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedCustomMesh1.MyCustomMesh.Indicies[i+1]]; Vector3 Vertex3 = SlicedCustomMesh1.MyCustomMesh.Verticies[SlicedCustomMesh1.MyCustomMesh.Indicies[i+2]]; // if our vertexes are on the positive side of the plane, add to mesh 1 //if (Vertex1.y > 0.5f) { if (MySlicePlane.GetSide(Vertex1) && MySlicePlane.GetSide(Vertex2) && MySlicePlane.GetSide(Vertex3)) { // if all 3 verticies are on the same side SlicedIndicies1.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i]); SlicedIndicies1.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i+1]); SlicedIndicies1.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i+2]); //} else { } else if (!MySlicePlane.GetSide(Vertex1) && !MySlicePlane.GetSide(Vertex2) && !MySlicePlane.GetSide(Vertex3)){ // if they are not on same side, they are intersected by the plane SlicedIndicies2.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i]); SlicedIndicies2.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i+1]); SlicedIndicies2.Add(SlicedCustomMesh1.MyCustomMesh.Indicies[i+2]); } } //for (int i = 0; i < 0 // Add all the points onto 2 new meshes // Add the rest of the meshes points onto the 2 created ones // Instantiate the new objects in the points it was cut // test this with non moving objects for positioning //SlicedCustomMesh1.MyCustomMesh.Verticies = SlicedPoints1; if (IsSlice) { SlicedCustomMesh1.MyCustomMesh.Indicies.Clear (); //SlicedCustomMesh1.MyCustomMesh.Indicies = SlicedIndicies1; SlicedCustomMesh1.MyCustomMesh.Indicies = SlicedIndicies3; //SlicedCustomMesh2.MyCustomMesh.Indicies.Clear (); //SlicedCustomMesh2.MyCustomMesh.Indicies = SlicedIndicies2; //SlicedCustomMesh1.MyCustomMesh.Refresh(SlicedPoints1, SlicedIndicies1, SlicedCustomMesh1.MyCustomMesh.TextureCoordinates); //SlicedCustomMesh2.MyCustomMesh.Refresh(SlicedPoints2, SlicedIndicies2, SlicedCustomMesh2.MyCustomMesh.TextureCoordinates); SlicedCustomMesh1.UpdateMesh(SlicedCustomMesh1.MyCustomMesh); //SlicedCustomMesh2.UpdateMesh(SlicedCustomMesh2.MyCustomMesh); } } }
// AT THE START OF EACH FRAME: SYNCHRONIZE INPUT EVENTS AND CALL ONVREVENT CALLBACK FUNCTIONS void PreUpdate() { List<VREvent> inputEvents = new List<VREvent> (); // TODO: Add input events generated in Unity to the list of inputEvents to sync them across all clients // ----- FAKE TRACKER INPUT FOR DEBUGGING IN DESKTOP MODE USING MOUSE INPUT ----- if (fakeTrackersForDebugging) { float x = Input.mousePosition.x; float y = Input.mousePosition.y; // first time through if (lastx == -9999) { lastx = x; lasty = y; return; } if (Input.GetKeyDown ("1")) { curTracker = 0; } else if (Input.GetKeyDown ("2")) { curTracker = 1; } if (Input.GetKey ("x")) { float angle = 0.1f * (x - lastx); if (curTracker == 0) { tracker1Rot = Quaternion.AngleAxis (angle, new Vector3 (1f, 0f, 0f)) * tracker1Rot; } else if (curTracker == 1) { tracker2Rot = Quaternion.AngleAxis (angle, new Vector3 (1f, 0f, 0f)) * tracker2Rot; } } else if (Input.GetKey ("y")) { float angle = 0.1f * (x - lastx); if (curTracker == 0) { tracker1Rot = Quaternion.AngleAxis (angle, new Vector3 (0f, 1f, 0f)) * tracker1Rot; } else if (curTracker == 1) { tracker2Rot = Quaternion.AngleAxis (angle, new Vector3 (0f, 1f, 0f)) * tracker2Rot; } } else if (Input.GetKey ("z")) { float angle = 0.1f * (x - lastx); if (curTracker == 0) { tracker1Rot = Quaternion.AngleAxis (angle, new Vector3 (0f, 0f, 1f)) * tracker1Rot; } else if (curTracker == 1) { tracker2Rot = Quaternion.AngleAxis (angle, new Vector3 (0f, 0f, 1f)) * tracker2Rot; } } else if (Input.GetKey ("left shift")) { float depth = 0.005f * (y - lasty); if (curTracker == 0) { tracker1Pos += depth * Camera.main.transform.forward; } else if (curTracker == 1) { tracker2Pos += depth * Camera.main.transform.forward; } } else { Ray ray = Camera.main.ScreenPointToRay (new Vector3 (x, y, 0f)); Plane p = new Plane (); float dist = 0.0f; if (curTracker == 0) { p.SetNormalAndPosition (-Camera.main.transform.forward, tracker1Pos); if (p.Raycast (ray, out dist)) { tracker1Pos = ray.GetPoint (dist); } } else if (curTracker == 1) { p.SetNormalAndPosition (-Camera.main.transform.forward, tracker2Pos); if (p.Raycast (ray, out dist)) { tracker2Pos = ray.GetPoint (dist); } } } Matrix4x4 m1 = Matrix4x4.TRS (tracker1Pos, tracker1Rot, Vector3.one); double[] d1 = VRConvert.ToDoubleArray (m1); VRDataIndex data1 = new VRDataIndex (); data1.AddData ("Transform", d1); inputEvents.Add (new VREvent (fakeTracker1Event, data1)); Matrix4x4 m2 = Matrix4x4.TRS (tracker2Pos, tracker2Rot, Vector3.one); double[] d2 = VRConvert.ToDoubleArray (m2); VRDataIndex data2 = new VRDataIndex (); data2.AddData ("Transform", d2); inputEvents.Add (new VREvent (fakeTracker2Event, data2)); lastx = x; lasty = y; } if (fakeHeadTracking) { if (Input.GetKey("up")) { headTrackerPos += 0.1f * Camera.main.transform.forward; } else if (Input.GetKey("down")) { headTrackerPos -= 0.1f * Camera.main.transform.forward; } else if (Input.GetKey("left")) { headTrackerRot *= Quaternion.AngleAxis (-1.0f, new Vector3 (0f, 1f, 0f)); } else if (Input.GetKey("right")) { headTrackerRot *= Quaternion.AngleAxis (1.0f, new Vector3 (0f, 1f, 0f)); } Matrix4x4 m3 = Matrix4x4.TRS (headTrackerPos, headTrackerRot, Vector3.one); double[] d3 = VRConvert.ToDoubleArray (m3); VRDataIndex data3 = new VRDataIndex (); data3.AddData ("Transform", d3); inputEvents.Add (new VREvent (fakeHeadTrackerEvent, data3)); } // ----- END FAKE TRACKER INPUT ----- // Synchronize with the server if (_netClient != null) { _netClient.SynchronizeInputEventsAcrossAllNodes(ref inputEvents); } // Call any event callback functions that have been registered with the VREventHandler for (int i=0; i<inputEvents.Count; i++) { if (VREventHandler != null) { VREventHandler(inputEvents[i]); } } _state = NetState.PostRenderNext; }
float GetSizeMultiplier( SplineNode node ) { if( !Camera.current.orthographic ) { Plane screen = new Plane( ); float sizeMultiplier = 0f; screen.SetNormalAndPosition( Camera.current.transform.forward, Camera.current.transform.position ); screen.Raycast( new Ray( node.Position, Camera.current.transform.forward ), out sizeMultiplier ); return sizeMultiplier * .0075f; } return Camera.current.orthographicSize * 0.01875f; }