예제 #1
0
        public static bool GenerateHemisphereVertices(Vector3 diameterXYZ, Matrix4x4 transform, int horzSegments, int vertSegments, ref Vector3[] vertices)
        {
            var bottomCap     = true;
            var topCap        = false;
            var extraVertices = ((!bottomCap) ? 1 : 0) + ((!topCap) ? 1 : 0);

            var rings       = (vertSegments) + (topCap ? 1 : 0);
            var vertexCount = (horzSegments * rings) + extraVertices;

            var topVertex    = 0;
            var bottomVertex = (!topCap) ? 1 : 0;
            var radius       = new Vector3(diameterXYZ.x * 0.5f,
                                           diameterXYZ.y,
                                           diameterXYZ.z * 0.5f);

            if (vertices == null ||
                vertices.Length != vertexCount)
            {
                vertices = new Vector3[vertexCount];
            }
            if (!topCap)
            {
                vertices[topVertex] = transform.MultiplyPoint(Vector3.up * radius.y);                // top
            }
            if (!bottomCap)
            {
                vertices[bottomVertex] = transform.MultiplyPoint(Vector3.zero);                      // bottom
            }
            var degreePerSegment = (360.0f / horzSegments) * Mathf.Deg2Rad;
            var angleOffset      = ((horzSegments & 1) == 1) ? 0.0f : 0.5f * degreePerSegment;
            var vertexIndex      = extraVertices;

            {
                for (int h = 0; h < horzSegments; h++, vertexIndex++)
                {
                    var hRad = (h * degreePerSegment) + angleOffset;
                    vertices[vertexIndex] = transform.MultiplyPoint(new Vector3(Mathf.Cos(hRad) * radius.x,
                                                                                0.0f,
                                                                                Mathf.Sin(hRad) * radius.z));
                }
            }
            for (int v = 1; v < rings; v++)
            {
                var segmentFactor = ((v - (rings / 2.0f)) / rings) + 0.5f;                          // [0.0f ... 1.0f]
                var segmentDegree = (segmentFactor * 90);                                           // [0 .. 90]
                var segmentHeight = Mathf.Sin(segmentDegree * Mathf.Deg2Rad) * radius.y;
                var segmentRadius = Mathf.Cos(segmentDegree * Mathf.Deg2Rad);                       // [0 .. 0.707 .. 1 .. 0.707 .. 0]
                for (int h = 0; h < horzSegments; h++, vertexIndex++)
                {
                    vertices[vertexIndex].x = vertices[h + extraVertices].x * segmentRadius;
                    vertices[vertexIndex].y = segmentHeight;
                    vertices[vertexIndex].z = vertices[h + extraVertices].z * segmentRadius;
                }
            }
            return(true);
        }
예제 #2
0
        private bool RayCastMulti(MeshQuery[]                                           meshQuery, // TODO: add meshquery support here
                                  Vector3 worldRayStart,
                                  Vector3 worldRayEnd,
                                  Matrix4x4 treeLocalToWorldMatrix,
                                  int filterLayerParameter0,
                                  out CSGTreeBrushIntersection[]        intersections,
                                  CSGTreeNode[]                                         ignoreNodes = null)
        {
            intersections = null;
            Int32 intersectionCount = 0;

            var ignoreNodeIDsHandle = (ignoreNodes != null) ? GCHandle.Alloc(ignoreNodes, GCHandleType.Pinned) : new GCHandle();

            {
                var ignoreNodeIDsPtr = (ignoreNodes != null) ? ignoreNodeIDsHandle.AddrOfPinnedObject() : IntPtr.Zero;

                var workAround = Matrix4x4.identity;

                intersectionCount = RayCastIntoTreeMultiCount(treeNodeID,
                                                              ref worldRayStart,
                                                              ref worldRayEnd,
                                                              ref workAround,
                                                              filterLayerParameter0, // TODO: implement this properly with MeshQuery
                                                              true,                  // TODO: implement this properly with MeshQuery
                                                              ignoreNodeIDsPtr,
                                                              (ignoreNodes == null) ? 0 : ignoreNodes.Length);
            }
            if (ignoreNodeIDsHandle.IsAllocated)
            {
                ignoreNodeIDsHandle.Free();
            }

            if (intersectionCount > 0)
            {
                var outputIntersections       = new CSGTreeBrushIntersection[intersectionCount];
                var outputIntersectionsHandle = GCHandle.Alloc(outputIntersections, GCHandleType.Pinned);
                {
                    var outputIntersectionsPtr = outputIntersectionsHandle.AddrOfPinnedObject();
                    var result = RayCastMultiGet(intersectionCount, outputIntersectionsPtr);
                    if (result)
                    {
                        intersections = outputIntersections;
                    }
                    else
                    {
                        intersections = null;
                    }
                }
                for (int i = 0; i < intersectionCount; i++)
                {
                    outputIntersections[i].surfaceIntersection.worldPlane        = treeLocalToWorldMatrix.TransformPlane(outputIntersections[i].surfaceIntersection.worldPlane);
                    outputIntersections[i].surfaceIntersection.worldIntersection = treeLocalToWorldMatrix.MultiplyPoint(outputIntersections[i].surfaceIntersection.worldIntersection);
                }
                outputIntersectionsHandle.Free();
            }
            return(intersections != null);
        }
예제 #3
0
        static bool GetExtrudedVertices(Vector2[] shapeVertices, Matrix4x4 matrix0, Matrix4x4 matrix1, out Vector3[] vertices)
        {
            var pathSegments  = 2;
            var shapeSegments = shapeVertices.Length;
            var vertexCount   = shapeSegments * pathSegments;

            vertices = new Vector3[vertexCount];

            for (int s = 0; s < shapeSegments; s++)
            {
                vertices[s] = matrix0.MultiplyPoint(shapeVertices[s]);
                vertices[shapeSegments + s] = matrix1.MultiplyPoint(shapeVertices[s]);
            }
            return(true);
        }
예제 #4
0
        /** Calc
         */
        public void Calc(UnityEngine.Camera a_look_camera, UnityEngine.Transform a_look_transform)
        {
            //worldToCameraMatrix
            this.raw_camera.worldToCameraMatrix = a_look_camera.worldToCameraMatrix * this.matrix;

            //projectionMatrix
            {
                UnityEngine.Matrix4x4 t_mirror_camera_matrix = this.raw_camera.worldToCameraMatrix;
                UnityEngine.Vector4   t_mirror_clip_plane;
                {
                    UnityEngine.Vector3 t_pos             = -this.plane_normal * this.plane_distance;
                    UnityEngine.Vector3 t_mirror_position = t_mirror_camera_matrix.MultiplyPoint(t_pos);
                    UnityEngine.Vector3 t_mirror_normal   = t_mirror_camera_matrix.MultiplyVector(this.plane_normal).normalized;
                    t_mirror_clip_plane = new UnityEngine.Vector4(t_mirror_normal.x, t_mirror_normal.y, t_mirror_normal.z, -UnityEngine.Vector3.Dot(t_mirror_position, t_mirror_normal));
                }
                this.raw_camera.projectionMatrix = a_look_camera.CalculateObliqueMatrix(t_mirror_clip_plane);
            }

            //position
            {
                this.raw_camera.transform.position = this.matrix.MultiplyPoint(a_look_transform.position);
            }

            //rotation
            {
                UnityEngine.Vector3 t_up      = this.matrix_rotate.MultiplyPoint(a_look_transform.up).normalized;
                UnityEngine.Vector3 t_forward = this.matrix_rotate.MultiplyPoint(a_look_transform.forward).normalized;
                UnityEngine.Vector3 t_right   = UnityEngine.Vector3.Cross(t_up, t_forward);

                UnityEngine.Matrix4x4 t_matrix = new UnityEngine.Matrix4x4(
                    new UnityEngine.Vector4(t_right.x, t_right.y, t_right.z, 0.0f),
                    new UnityEngine.Vector4(t_up.x, t_up.y, t_up.z, 0.0f),
                    new UnityEngine.Vector4(t_forward.x, t_forward.y, t_forward.z, 0.0f),
                    new UnityEngine.Vector4(0.0f, 0.0f, 0.0f, 1.0f)
                    );

                this.raw_camera.transform.rotation = t_matrix.rotation;
            }
        }
예제 #5
0
 /// <inheritdoc />
 protected virtual UnityEngine.Vector3 CoordinateToWorldSpace(UnityEngine.Vector3 vector) => worldMatrix.MultiplyPoint(vector);
예제 #6
0
        public static bool GenerateHemisphereSubMesh(CSGBrushSubMesh subMesh, Vector3 diameterXYZ, Matrix4x4 transform, int horzSegments, int vertSegments, CSGSurfaceAsset[] surfaceAssets, SurfaceDescription[] surfaceDescriptions)
        {
            if (diameterXYZ.x == 0 ||
                diameterXYZ.y == 0 ||
                diameterXYZ.z == 0)
            {
                subMesh.Clear();
                return(false);
            }

            var bottomCap     = true;
            var topCap        = false;
            var extraVertices = ((!bottomCap) ? 1 : 0) + ((!topCap) ? 1 : 0);

            var rings       = (vertSegments) + (topCap ? 1 : 0);
            var vertexCount = (horzSegments * rings) + extraVertices;

            var topVertex    = 0;
            var bottomVertex = (!topCap) ? 1 : 0;
            var radius       = new Vector3(diameterXYZ.x * 0.5f,
                                           diameterXYZ.y,
                                           diameterXYZ.z * 0.5f);

            var   heightY = radius.y;
            float topY, bottomY;

            if (heightY < 0)
            {
                topY    = 0;
                bottomY = heightY;
            }
            else
            {
                topY    = heightY;
                bottomY = 0;
            }

            var vertices = new Vector3[vertexCount];

            if (!topCap)
            {
                vertices[topVertex] = transform.MultiplyPoint(Vector3.up * topY);                   // top
            }
            if (!bottomCap)
            {
                vertices[bottomVertex] = transform.MultiplyPoint(Vector3.up * bottomY);             // bottom
            }
            var degreePerSegment = (360.0f / horzSegments) * Mathf.Deg2Rad;
            var angleOffset      = ((horzSegments & 1) == 1) ? 0.0f : 0.5f * degreePerSegment;
            var vertexIndex      = extraVertices;

            if (heightY < 0)
            {
                for (int h = horzSegments - 1; h >= 0; h--, vertexIndex++)
                {
                    var hRad = (h * degreePerSegment) + angleOffset;
                    vertices[vertexIndex] = transform.MultiplyPoint(new Vector3(Mathf.Cos(hRad) * radius.x,
                                                                                0.0f,
                                                                                Mathf.Sin(hRad) * radius.z));
                }
            }
            else
            {
                for (int h = 0; h < horzSegments; h++, vertexIndex++)
                {
                    var hRad = (h * degreePerSegment) + angleOffset;
                    vertices[vertexIndex] = transform.MultiplyPoint(new Vector3(Mathf.Cos(hRad) * radius.x,
                                                                                0.0f,
                                                                                Mathf.Sin(hRad) * radius.z));
                }
            }
            for (int v = 1; v < rings; v++)
            {
                var segmentFactor = ((v - (rings / 2.0f)) / rings) + 0.5f;                      // [0.0f ... 1.0f]
                var segmentDegree = (segmentFactor * 90);                                       // [0 .. 90]
                var segmentHeight = Mathf.Sin(segmentDegree * Mathf.Deg2Rad) * heightY;
                var segmentRadius = Mathf.Cos(segmentDegree * Mathf.Deg2Rad);                   // [0 .. 0.707 .. 1 .. 0.707 .. 0]
                for (int h = 0; h < horzSegments; h++, vertexIndex++)
                {
                    vertices[vertexIndex].x = vertices[h + extraVertices].x * segmentRadius;
                    vertices[vertexIndex].y = segmentHeight;
                    vertices[vertexIndex].z = vertices[h + extraVertices].z * segmentRadius;
                }
            }

            return(GenerateSegmentedSubMesh(subMesh, horzSegments, vertSegments, vertices, bottomCap, topCap, bottomVertex, topVertex, surfaceAssets, surfaceDescriptions));
        }