예제 #1
0
        /*
         * public static bool ContinueTexGenFromSurfaceToSurface(CSGBrush srcBrush, int srcSurfaceIndex, CSGBrush dstBrush, int dstSurfaceIndex)
         * {
         *  if (srcSurfaceIndex < 0 || srcSurfaceIndex >= srcBrush.Shape.Materials.Length ||
         *      srcBrush == null)
         *      return false;
         *
         *  var src_brush_cache = CSGModelManager.GetBrushCache(srcBrush);
         *  if (src_brush_cache == null ||
         *      src_brush_cache.childData == null ||
         *      src_brush_cache.childData.modelTransform == null)
         *      return false;
         *
         *  var dst_brush_cache = CSGModelManager.GetBrushCache(dstBrush);
         *  if (dst_brush_cache == null ||
         *      dst_brush_cache.childData == null ||
         *      dst_brush_cache.childData.modelTransform == null)
         *      return false;
         *
         *  var dstPlane	= dstBrush.Shape.Surfaces[dstSurfaceIndex].Plane;
         *  var srcPlane	= srcBrush.Shape.Surfaces[srcSurfaceIndex].Plane;
         *
         *  // convert planes into worldspace
         *  dstPlane = GeometryUtility.InverseTransformPlane(dstBrush.transform.worldToLocalMatrix, dstPlane);
         *  srcPlane = GeometryUtility.InverseTransformPlane(srcBrush.transform.worldToLocalMatrix, srcPlane);
         *
         *  var dstNormal	= dstPlane.normal;
         *  var srcNormal	= srcPlane.normal;
         *
         *  var srcTexGenIndex = srcBrush.Shape.Surfaces[srcSurfaceIndex].TexGenIndex;
         *  var dstTexGenIndex = dstBrush.Shape.Surfaces[dstSurfaceIndex].TexGenIndex;
         *
         *  var scrShape = srcBrush.Shape;
         *
         *  dstBrush.Shape.Materials[dstSurfaceIndex] = scrShape.Materials[srcSurfaceIndex];
         *  Vector3 srcPoint1, srcPoint2;
         *  Vector3 dstPoint1, dstPoint2;
         *  var edgeDirection = Vector3.Cross(dstNormal, srcNormal);
         *  var det = edgeDirection.sqrMagnitude;
         *  if (det < Constants.AlignmentTestEpsilon)
         *  {
         *      // Find 2 points on intersection of 2 planes
         *      srcPoint1 = srcPlane.pointOnPlane;
         *      srcPoint2 = GeometryUtility.ProjectPointOnPlane(srcPlane, srcPoint1 + MathConstants.oneVector3);
         *
         *      dstPoint1 = GeometryUtility.ProjectPointOnPlane(dstPlane, srcPoint1);
         *      dstPoint2 = GeometryUtility.ProjectPointOnPlane(dstPlane, srcPoint2);
         *  } else
         *  {
         *      // Find 2 points on intersection of 2 planes
         *      srcPoint1 = ((Vector3.Cross(edgeDirection, srcNormal) * -dstPlane.d) +
         *                      (Vector3.Cross(dstNormal, edgeDirection) * -srcPlane.d)) / det;
         *      srcPoint2 = srcPoint1 + edgeDirection;
         *      dstPoint1 = srcPoint1;
         *      dstPoint2 = srcPoint2;
         *  }
         *
         *  Vector3 srcLocalPoint1 = srcBrush.transform.InverseTransformPoint(srcPoint1);
         *  Vector3 srcLocalPoint2 = srcBrush.transform.InverseTransformPoint(srcPoint2);
         *
         *  Vector3 dstLocalPoint1 = dstBrush.transform.InverseTransformPoint(dstPoint1);
         *  Vector3 dstLocalPoint2 = dstBrush.transform.InverseTransformPoint(dstPoint2);
         *
         *  var srcShape	= srcBrush.Shape;
         *  var srcTexGens	= srcShape.TexGens;
         *  var srcSurfaces	= srcShape.Surfaces;
         *  var dstShape	= dstBrush.Shape;
         *  var dstTexGens	= dstShape.TexGens;
         *  var dstSurfaces	= dstShape.Surfaces;
         *
         *
         *  // Reset destination shape to simplify calculations
         *  dstTexGens[dstTexGenIndex].Scale			= scrShape.TexGens[srcTexGenIndex].Scale;
         *  dstTexGens[dstTexGenIndex].Translation		= MathConstants.zeroVector2;
         *  dstTexGens[dstTexGenIndex].RotationAngle	= 0;
         *
         *  if (!CSGModelManager.AlignTextureSpacesInLocalSpace(ref srcTexGens[srcTexGenIndex], ref srcSurfaces[srcSurfaceIndex],
         *                                                      srcLocalPoint1, srcLocalPoint2,
         *                                                      ref dstTexGens[dstTexGenIndex], ref dstSurfaces[dstSurfaceIndex],
         *                                                      dstLocalPoint1, dstLocalPoint2))
         *      return false;
         *  return true;
         * }
         */

        public static bool ContinueTexGenFromSurfaceToSurface(CSGBrush brush, int srcSurfaceIndex, int dstSurfaceIndex)
        {
            if (srcSurfaceIndex < 0 || srcSurfaceIndex >= brush.Shape.TexGens.Length ||
                !brush)
            {
                return(false);
            }

            var shape       = brush.Shape;
            var texGens     = shape.TexGens;
            var texGenFlags = shape.TexGenFlags;
            var surfaces    = shape.Surfaces;

            var srcTexGenIndex = surfaces[srcSurfaceIndex].TexGenIndex;
            var dstTexGenIndex = surfaces[dstSurfaceIndex].TexGenIndex;

//          var src_is_world_space = (texGenFlags[srcTexGenIndex] & TexGenFlags.WorldSpaceTexture) != TexGenFlags.None;
//          var dst_is_world_space = (texGenFlags[dstTexGenIndex] & TexGenFlags.WorldSpaceTexture) != TexGenFlags.None;

//          if (src_is_world_space) SurfaceUtility.SetTextureLock(brush, srcSurfaceIndex, true);
//          if (dst_is_world_space) SurfaceUtility.SetTextureLock(brush, dstSurfaceIndex, true);

            var dstLocalPlane = surfaces[dstSurfaceIndex].Plane;
            var srcLocalPlane = surfaces[srcSurfaceIndex].Plane;

            CSGModel     parentModel;
            CSGOperation parentOp;

            InternalCSGModelManager.FindParentOperationAndModel(brush.transform, out parentOp, out parentModel);

            var localFromWorld = brush.transform.worldToLocalMatrix;
            var worldFromModel = parentModel.transform.localToWorldMatrix;
            var modelFromWorld = parentModel.transform.worldToLocalMatrix;
            var localFromModel = localFromWorld * worldFromModel;
            //var localToWorldMatrix = brush.transform.localToWorldMatrix;

            // convert planes into worldspace
            var dstWorldPlane = InverseTransformPlane(localFromWorld, dstLocalPlane);
            var srcWorldPlane = InverseTransformPlane(localFromWorld, srcLocalPlane);

            var dstWorldNormal = dstWorldPlane.normal;
            var srcWorldNormal = srcWorldPlane.normal;


            shape.TexGens[dstSurfaceIndex].RenderMaterial = shape.TexGens[srcSurfaceIndex].RenderMaterial;
            Vector3 srcWorldPoint1, srcWorldPoint2;
            Vector3 dstWorldPoint1, dstWorldPoint2;
            var     edgeDirection = Vector3.Cross(dstWorldNormal, srcWorldNormal);
            var     det           = edgeDirection.sqrMagnitude;

            if (det < MathConstants.AlignmentTestEpsilon)
            {
                // Find 2 points on intersection of 2 planes
                srcWorldPoint1 = srcWorldPlane.pointOnPlane;
                srcWorldPoint2 = GeometryUtility.ProjectPointOnPlane(srcWorldPlane, srcWorldPoint1 + MathConstants.oneVector3);

                dstWorldPoint1 = GeometryUtility.ProjectPointOnPlane(dstWorldPlane, srcWorldPoint1);
                dstWorldPoint2 = GeometryUtility.ProjectPointOnPlane(dstWorldPlane, srcWorldPoint2);
            }
            else
            {
                // Find 2 points on intersection of 2 planes
                srcWorldPoint1 = ((Vector3.Cross(edgeDirection, srcWorldNormal) * -dstWorldPlane.d) +
                                  (Vector3.Cross(dstWorldNormal, edgeDirection) * -srcWorldPlane.d)) / det;
                srcWorldPoint2 = srcWorldPoint1 + edgeDirection;
                dstWorldPoint1 = srcWorldPoint1;
                dstWorldPoint2 = srcWorldPoint2;
            }

            var srcModelPoint1 = modelFromWorld.MultiplyPoint(srcWorldPoint1);
            var srcModelPoint2 = modelFromWorld.MultiplyPoint(srcWorldPoint2);

            var dstModelPoint1 = modelFromWorld.MultiplyPoint(dstWorldPoint1);
            var dstModelPoint2 = modelFromWorld.MultiplyPoint(dstWorldPoint2);

            var result = SurfaceUtility.AlignTextureSpaces(localFromModel, texGens[srcTexGenIndex], texGenFlags[srcTexGenIndex], ref surfaces[srcSurfaceIndex], srcModelPoint1, srcModelPoint2,
                                                           localFromModel, ref texGens[dstTexGenIndex], texGenFlags[dstTexGenIndex], ref surfaces[dstSurfaceIndex], dstModelPoint1, dstModelPoint2, false, Vector3.one);

//          if (src_is_world_space) SurfaceUtility.SetTextureLock(brush, srcSurfaceIndex, false);
//          if (dst_is_world_space) SurfaceUtility.SetTextureLock(brush, dstSurfaceIndex, false);
            return(result);
        }