Ejemplo n.º 1
0
		protected void init(){
			for (int i=0; i<normals.Count; i++) {
				avgNormal+=normals[i];
			}
			avgNormal /= normals.Count;
			plane = new Plane ();
			plane.Set3Points (positions[0],positions[1],positions[2]);
			bounds = MeshUtils.createBounds (positions/*, avgNormal, centerPos, true*/);
			extendedBounds = new Bounds (bounds.center, bounds.size + MeshUtils.MAX_ROUND_ERROR);
		}
Ejemplo n.º 2
0
    static bool Plane_Set3Points__Vector3__Vector3__Vector3(JSVCall vc, int argc)
    {
        int len = argc;

        if (len == 3)
        {
            UnityEngine.Vector3 arg0    = (UnityEngine.Vector3)JSApi.getVector3S((int)JSApi.GetType.Arg);
            UnityEngine.Vector3 arg1    = (UnityEngine.Vector3)JSApi.getVector3S((int)JSApi.GetType.Arg);
            UnityEngine.Vector3 arg2    = (UnityEngine.Vector3)JSApi.getVector3S((int)JSApi.GetType.Arg);
            UnityEngine.Plane   argThis = (UnityEngine.Plane)vc.csObj;        argThis.Set3Points(arg0, arg1, arg2);
            JSMgr.changeJSObj(vc.jsObjID, argThis);
        }

        return(true);
    }
Ejemplo n.º 3
0
	static public int Set3Points(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);
			UnityEngine.Vector3 a3;
			checkType(l,4,out a3);
			self.Set3Points(a1,a2,a3);
			setBack(l,self);
			return 0;
		}
		catch(Exception e) {
			LuaDLL.luaL_error(l, e.ToString());
			return 0;
		}
	}
Ejemplo n.º 4
0
    public bool DoesLaserCrossesMesh()
    {
        if (!_isEmitting)
        {
            return false;
        }

        Vector3 capsuleNormal = _tr.up;
        Vector3 capsuleOrigin = _tr.position - capsuleNormal * _length;
        float capsuleRadius = 0.01f;

        Mesh bakedMesh = new Mesh();
        _pokeball._content._skinnedMesh.BakeMesh(bakedMesh);

        Vector3[] vertex = new Vector3[3];
        Vector3[] vertexWorldPosition = new Vector3[3];
        Vector3 planeCenter, planeNormal, projection, projectionOnPlane, directionToPlaneCenter, finalPoint, lastCross, cross;
        int[] triangles = bakedMesh.triangles;
        Vector3[] vertices = bakedMesh.vertices;
        Ray ray = new Ray(capsuleOrigin, capsuleNormal);
        float distance;
        Plane plane = new Plane();

        //while(triangleId < triangles.Length - 3)
        {
            _verticesToDraw.Clear();
            // Checking a single triangle.
            for (int i = 0; i < triangles.Length - 3; i += 3)
            {
                /// Steps to check the collision between an length capsule (which is also a cylinder) and a triangle:
                /// 1) calculate the projection of the point on the triangle plane.
                /// 2) bring the point on the plane closer to the center of the triangle by the smallest value between the radius of the capsule and the distance between the projected point and the center of the triangle.
                /// 3) use the final point to check the cross product between one edge of the triangle and the vector between the first point of the edge and the final point. If all the dot products between the 3 cross products are of the same sign, then the final point lies within the triangle.
                /// 4) you now have confirmed the collision between the triangle and the infinite length capsule!

                vertex[0] = vertices[triangles[i]];
                vertex[1] = vertices[triangles[i + 1]];
                vertex[2] = vertices[triangles[i + 2]];

                // The coordinates of the vertices we find in the mesh are relative to the mesh pivot. So we make sure all coordinates are in the same referential which is world coordinates.
                // Are the mesh relative coordinates affected by the scale of the Pokemon? Yes, they are. No need to convert them.
                for (int x = 0; x < 3; x++)
                {
                    vertexWorldPosition[x] = _pokeball._content.transform.position + _pokeball._content.transform.rotation * vertex[x];
                }

                plane.Set3Points(vertices[triangles[i]], vertices[triangles[i + 1]], vertices[triangles[i + 2]]);
                plane.Raycast(ray, out distance);

                /*planeCenter = (vertexWorldPosition[0] + vertexWorldPosition[1] + vertexWorldPosition[2]) / 3f;
                planeNormal = Vector3.Cross((vertexWorldPosition[1] - vertexWorldPosition[0]), (vertexWorldPosition[2] - vertexWorldPosition[1])).normalized;

                // Finding the orthogonal projection of the capsule onto the plane.
                projection = Vector3.Project((planeCenter - capsuleOrigin), planeNormal);
                */
                // If the projection and the capsule normal go the opposite way, that means the plane is behind the ray so we stop the calculations for this triangle.
                //if (Vector3.Dot(projection, capsuleNormal) < 0)
                if (distance < 0)
                {
                    continue;
                }

                // We calculate the projection of the capsule onto the plane.
                //projectionOnPlane = capsuleOrigin + projection;
                projectionOnPlane = capsuleOrigin + capsuleNormal * distance;

                _verticesToDraw.Add(projectionOnPlane);

                // Actually when I said the capsule had an infinite length, I lied. We can give it a length thanks to the following condition:
                //if ((projectionOnPlane - capsuleOrigin).magnitude > _length)
                //{
                //    continue;
                //}

                // We now move the point towards the center of the triangle in order to take into account the radius of the capsule.
                //directionToPlaneCenter = planeCenter - projectionOnPlane;
                //finalPoint = projectionOnPlane + directionToPlaneCenter * Mathf.Min(capsuleRadius, directionToPlaneCenter.magnitude);
                finalPoint = projectionOnPlane;

                // Now we check if the final point is within the triangle by comparing some cross products.
                lastCross = Vector3.zero;
                bool finalPointIsInside = true;
                for (int j = 0; j < 3; j++)
                {
                    cross = Vector3.Cross((vertexWorldPosition[(j + 1) % 3] - vertexWorldPosition[j]), (vertexWorldPosition[(j + 2) % 3] - vertexWorldPosition[(j + 1) % 3]));
                    lastCross = Vector3.Cross((vertexWorldPosition[(j + 1) % 3] - vertexWorldPosition[j]), (finalPoint - vertexWorldPosition[j]));  // A virer.

                    if (Vector3.Dot(lastCross, cross) <= 0)
                    {
                        finalPointIsInside = false;
                        break;
                    }

                    //if (j > 0 && Vector3.Dot(lastCross, cross) < 0)
                    //{
                    //    finalPointIsInside = false;
                    //    break;
                    //}

                    //lastCross = cross;
                }

                // If the final point is outside, we proceed to the next triangle.
                if (finalPointIsInside)
                {
                    Debug.Log("is inside !!!!!!!!!!");
                    return true;
                }
            }
        }

        return false;
    }