Beispiel #1
0
    override protected NPVoxModel CreateProduct(NPVoxModel reuse = null)
    {
        if (Input == null)
        {
            return(NPVoxModel.NewInvalidInstance(reuse, "No Input Setup"));;
        }

        NPVoxModel model = ((NPVoxIModelFactory)Input).GetProduct() as NPVoxModel;

        NPVoxModel newModel = NPVoxModel.NewInstance(model, NPVoxCoord.ZERO, reuse);

        newModel.CopyOver(model);

        bool addVoxelGroups = addVoxelGroup || newModel.HasVoxelGroups();
        byte theVoxelGroup  = (byte)255;

        if (addVoxelGroups)
        {
            if (!newModel.HasVoxelGroups())
            {
                newModel.InitVoxelGroups();
                foreach (NPVoxCoord coord in newModel.EnumerateVoxels())
                {
                    newModel.SetVoxelGroup(coord, 0);
                }
                theVoxelGroup           = 1;
                newModel.NumVoxelGroups = 2;
            }
            else
            {
                theVoxelGroup = newModel.NumVoxelGroups;
                newModel.NumVoxelGroups++;
            }
        }

        Vector3 direction1 = rotation1 * Vector3.forward;
        Vector3 direction2 = rotation2 * Vector3.forward;

        bool[]    usedColors = NPVoxModelUtils.GetUsedColors(newModel);
        Color32[] colorTable = newModel.Colortable;

        Vector3 currentP1 = NPVoxCoordUtil.ToVector(startCoord1);
        Vector3 currentP2 = NPVoxCoordUtil.ToVector(startCoord2);

        for (int i = 0; i < numSteps; i++)
        {
            byte color = NPVoxModelUtils.FindUnusedColor(ref usedColors);
            colorTable[color] = Color32.Lerp(startColor, endColor, (float)i / (float)numSteps);
            NPVoxGeomUtil.DrawLine(newModel, currentP1, currentP2, color, theVoxelGroup);
            currentP1 += direction1 * stepSize1;
            currentP2 += direction2 * stepSize2;
        }
        newModel.Colortable = colorTable;


        newModel.RecalculateNumVoxels();
//
        return(newModel);
    }
Beispiel #2
0
    public static byte AddBrightenColor(NPVoxModel model)
    {
        byte color = FindUnusedColor(model);

        if (color != 0)
        {
            Color32 brightenColor32 = model.Colortable[color];
            model.Colortable[color] = NPVoxModelUtils.BrightenColor(brightenColor32);
        }
        return(color);
    }
    override protected NPVoxModel CreateProduct(NPVoxModel reuse = null)
    {
        if (Sources.Length > 0)
        {
            bool hasVoxelGroups = System.Array.FindAll(Sources, s => s.VoxelGroupIndex != 0).Length > 0;
            byte numVoxelGroups = 1;
            var  firstSource    = Sources[0];

            if (!firstSource.Source)
            {
//                foreach (NPipeIImportable input in NPipelineUtils.EachSource(this))
//                {
//                    if (!((UnityEngine.Object)input))
//                    {
//                        return NPVoxModel.NewInvalidInstance(reuse, "XXXXX First Source didn't have a factory set");
//                    }
//                }

//                return NPVoxModel.NewInvalidInstance(reuse, "First Source didn't have a factory set");
                throw new NPipeException("First Source didn't have a factory set");
            }
            NPVoxModel firstProduct = ((NPVoxIModelFactory)firstSource.Source).GetProduct();

            NPVoxModel combinedModel = NPVoxModel.NewInstance(firstProduct, reuse);

            if (firstSource.VoxelGroupIndex >= numVoxelGroups)
            {
                numVoxelGroups = (byte)(firstSource.VoxelGroupIndex + 1);
            }

            if (hasVoxelGroups)
            {
                combinedModel.InitVoxelGroups();

                foreach (NPVoxCoord coord in combinedModel.EnumerateVoxels())
                {
                    combinedModel.SetVoxelGroup(coord, firstSource.VoxelGroupIndex);
                }
            }

            Color32[] colorTable = firstProduct.Colortable;

            bool[] colors = NPVoxModelUtils.GetUsedColors(firstProduct);

            for (int i = 0; i < Sources.Length; i++)
            {
                if (!Sources[i].Source)
                {
//                    foreach (NPipeIImportable input in NPipelineUtils.EachSource(this))
//                    {
//                        if (!((UnityEngine.Object)input))
//                        {
//                            return NPVoxModel.NewInvalidInstance(reuse, "XXXXX Source " + i + " didn't have a factory set");
//                        }
//                    }
//
//
//                    return NPVoxModel.NewInvalidInstance(reuse, "Source " + i + " didn't have a factory set");

                    throw new NPipeException("Source " + i + " didn't have a factory set");
                }

                NPVoxModel voxModel = ((NPVoxIModelFactory)Sources[i].Source).GetProduct();

//                numVoxels += voxModel.NumVoxels;

                Pivot      pivot           = Sources[i].Pivot;
                NPVoxCoord offset          = NPVoxCoord.ZERO;
                byte       voxelGroupIndex = Sources[i].VoxelGroupIndex;

                if ((pivot & Pivot.Right) == Pivot.Right)
                {
                    offset.X = (sbyte)(combinedModel.SizeX - voxModel.SizeX);
                }

                if ((pivot & Pivot.Up) == Pivot.Up)
                {
                    offset.Y = (sbyte)(combinedModel.SizeY - voxModel.SizeY);
                }

                if ((pivot & Pivot.Forward) == Pivot.Forward)
                {
                    offset.Z = (sbyte)(combinedModel.SizeZ - voxModel.SizeZ);
                }

                Dictionary <byte, byte> mapping = new Dictionary <byte, byte>();
                foreach (NPVoxCoord coord in voxModel.EnumerateVoxels())
                {
                    NPVoxCoord targetCoord = coord + offset;
                    byte       sourceColor = voxModel.GetVoxel(coord);
                    byte       targetColor = 0;
                    if (!mapping.ContainsKey(sourceColor))
                    {
                        targetColor             = NPVoxModelUtils.FindUnusedColor(ref colors);
                        colorTable[targetColor] = voxModel.Colortable[sourceColor];
                        mapping.Add(sourceColor, targetColor);
                    }

                    targetColor = mapping[sourceColor];
                    combinedModel.SetVoxel(targetCoord, targetColor);
                    if (hasVoxelGroups)
                    {
                        combinedModel.SetVoxelGroup(targetCoord, voxelGroupIndex);
                    }
                }
                if (Sources[i].VoxelGroupIndex >= numVoxelGroups)
                {
                    numVoxelGroups = (byte)(Sources[i].VoxelGroupIndex + 1);
                }
            }

//            combinedModel.NumVoxels = numVoxels;
            combinedModel.RecalculateNumVoxels();
            combinedModel.NumVoxelGroups = numVoxelGroups;
            combinedModel.Colortable     = colorTable;
            combinedModel.name           = "zzz Combined Model";
            return(combinedModel);
        }
        else
        {
            return(NPVoxModel.NewInvalidInstance(reuse, "No Sources Setup"));
        }
    }
    override protected NPVoxModel CreateProduct(NPVoxModel reuse = null)
    {
        if (Input == null)
        {
            return(NPVoxModel.NewInvalidInstance(reuse, "Input was null"));
        }
        NPVoxModel inputModel = ((NPVoxIModelFactory)Input).GetProduct();

        bool hasVoxelGroups = inputModel.HasVoxelGroups();

        if (AffectedArea.Equals(NPVoxBox.INVALID))
        {
            NPVoxModel model = NPVoxModel.NewInstance(inputModel, reuse);
            model.CopyOver(inputModel);
            return(model);
        }

        NPVoxBox clampedBox = inputModel.Clamp(AffectedArea);

        NPVoxModel transformedModel = null;

        transformedModel = NPVoxModel.NewInstance(inputModel, reuse);
        if (hasVoxelGroups)
        {
            transformedModel.InitVoxelGroups();
        }
        transformedModel.NumVoxelGroups = inputModel.NumVoxelGroups;
        transformedModel.NumVoxels      = inputModel.NumVoxels;
        transformedModel.Colortable     = inputModel.Colortable != null ? (Color32[])inputModel.Colortable.Clone() : null;
        transformedModel.Sockets        = inputModel.Sockets != null ? (NPVoxSocket[])inputModel.Sockets.Clone() : null;

        NPVoxBoneModel transformedBoneModel = transformedModel as NPVoxBoneModel;
        NPVoxBoneModel inputBoneModel       = inputModel as NPVoxBoneModel;
        bool           isBoneModel          = false;

        if (transformedBoneModel != null)
        {
            transformedBoneModel.AllBones = NPVoxBone.CloneBones(inputBoneModel.AllBones);
            isBoneModel = true;
        }

        byte brightenedColor = NPVoxModelUtils.FindUnusedColor(inputModel);

        if (brightenedColor == 0)
        {
            Debug.LogWarning("could not find a free color to brighten the model");
        }

        Color32 brightenColor32 = inputModel.Colortable[brightenedColor];

        foreach (NPVoxCoord coord in inputModel.EnumerateVoxels())
        {
            if (!isBoneModel)
            {
                if (clampedBox.Contains(coord) && brightenedColor != 0)
                {
                    brightenColor32 = inputModel.Colortable[inputModel.GetVoxel(coord)];
                    transformedModel.SetVoxel(coord, brightenedColor);
                }
                else
                {
                    transformedModel.SetVoxel(coord, inputModel.GetVoxel(coord));
                }
            }
            else
            {
                if (hiddenBonesMask == 0 || !inputBoneModel.IsInBoneMask(coord, hiddenBonesMask))
                {
                    if (clampedBox.Contains(coord) && brightenedColor != 0 && inputBoneModel.IsInBoneMask(coord, boneMask))
                    {
                        brightenColor32 = inputModel.Colortable[inputModel.GetVoxel(coord)];
                        transformedModel.SetVoxel(coord, brightenedColor);
                    }
                    else
                    {
                        transformedModel.SetVoxel(coord, inputModel.GetVoxel(coord));
                    }
                }
                transformedBoneModel.SetBoneMask(coord, inputBoneModel.GetBoneMask(coord));
            }

            if (hasVoxelGroups)
            {
                transformedModel.SetVoxelGroup(coord, inputModel.GetVoxelGroup(coord));
            }
        }

        if (brightenedColor != 0)
        {
            transformedModel.Colortable[brightenedColor] = NPVoxModelUtils.BrightenColor(brightenColor32);
        }


        transformedModel.RecalculateNumVoxels(true);
        return(transformedModel);
    }
    override protected NPVoxModel CreateProduct(NPVoxModel reuse = null)
    {
        if (Input == null)
        {
            return(NPVoxModel.NewInvalidInstance(reuse, "No Input Setup"));
        }

        NPVoxModel model = ((NPVoxIModelFactory)Input).GetProduct() as NPVoxModel;

        NPVoxModel targetModel = model;

        if (TargetFrame is NPVoxIModelFactory)
        {
            targetModel = ((NPVoxIModelFactory)TargetFrame).GetProduct() as NPVoxModel;
        }

        if (!(PreviousFrame is NPVoxIModelFactory))
        {
            Debug.LogWarning("previous frame is not a model factory");
            return(NPVoxModel.NewInstance(NPVoxCoord.ZERO, reuse));
        }

        #if UNITY_EDITOR
        if (SocketOffsets.Length < 4 || ControlPointOffsets.Length < 4)
        {
            ResetSceneTools();
        }
        #endif

        NPVoxModel sourceModel = ((NPVoxIModelFactory)PreviousFrame).GetProduct();

        NPVoxSocket sourceSocket1 = sourceModel.GetSocketByName(SocketName1);
        NPVoxSocket sourceSocket2 = sourceModel.GetSocketByName(SocketName2);
        NPVoxSocket targetSocket1 = targetModel.GetSocketByName(SocketName1);
        NPVoxSocket targetSocket2 = targetModel.GetSocketByName(SocketName2);

        if (sourceSocket1.IsInvalid())
        {
            Debug.LogWarning("SocketName1 not found in sourceModel");
            return(NPVoxModel.NewInstance(NPVoxCoord.ZERO, reuse));
        }

        if (sourceSocket2.IsInvalid())
        {
            Debug.LogWarning("SocketName2 not found in sourceModel");
            return(NPVoxModel.NewInstance(NPVoxCoord.ZERO, reuse));
        }

        if (targetSocket1.IsInvalid())
        {
            Debug.LogWarning("SocketName1 not found in newModel");
            return(NPVoxModel.NewInstance(NPVoxCoord.ZERO, reuse));
        }

        if (targetSocket2.IsInvalid())
        {
            Debug.LogWarning("SocketName2 not found in oldModel");
            return(NPVoxModel.NewInstance(NPVoxCoord.ZERO, reuse));
        }

        if (TheStepSize < 0.01f)
        {
            Debug.LogWarning("Stepsize too small");
            return(NPVoxModel.NewInstance(NPVoxCoord.ZERO, reuse));
        }

        NPVoxToUnity sourceN2U = new NPVoxToUnity(sourceModel, Vector3.one);
        NPVoxToUnity targetN2U = new NPVoxToUnity(targetModel, Vector3.one);
        NPVoxToUnity modelN2U  = new NPVoxToUnity(model, Vector3.one);

        // calculate size for our new model
        NPVoxBox requiredBounds = model.BoundingBox;
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(sourceN2U.ToUnityPosition(sourceSocket1.Anchor) + SocketOffsets[INDEX_SOURCE_1]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(sourceN2U.ToUnityPosition(sourceSocket2.Anchor) + SocketOffsets[INDEX_SOURCE_2]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(targetN2U.ToUnityPosition(targetSocket1.Anchor) + SocketOffsets[INDEX_TARGET_1]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(targetN2U.ToUnityPosition(targetSocket2.Anchor) + SocketOffsets[INDEX_TARGET_2]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(sourceN2U.ToUnityPosition(sourceSocket1.Anchor) + SocketOffsets[INDEX_SOURCE_1] + ControlPointOffsets[INDEX_SOURCE_1]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(sourceN2U.ToUnityPosition(sourceSocket2.Anchor) + SocketOffsets[INDEX_SOURCE_2] + ControlPointOffsets[INDEX_SOURCE_2]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(targetN2U.ToUnityPosition(targetSocket1.Anchor) + SocketOffsets[INDEX_TARGET_1] + ControlPointOffsets[INDEX_TARGET_1]));
        requiredBounds.EnlargeToInclude(modelN2U.ToVoxCoord(targetN2U.ToUnityPosition(targetSocket2.Anchor) + SocketOffsets[INDEX_TARGET_2] + ControlPointOffsets[INDEX_TARGET_2]));

        // create our product model
        NPVoxModel productModel = NPVoxModelTransformationUtil.CreateWithNewSize(model, requiredBounds, reuse);

        // prepare voxel groups
        bool addVoxelGroups = SetVoxelGroup > 0 || productModel.HasVoxelGroups() || SetBaseVoxelGroup > 0;
        byte theVoxelGroup  = (byte)SetVoxelGroup;
        if (addVoxelGroups)
        {
            if (!productModel.HasVoxelGroups())
            {
                productModel.InitVoxelGroups();
                foreach (NPVoxCoord coord in productModel.EnumerateVoxels())
                {
                    productModel.SetVoxelGroup(coord, SetBaseVoxelGroup);
                }
            }
            if (theVoxelGroup > productModel.NumVoxelGroups - 1)
            {
                productModel.NumVoxelGroups = (byte)(theVoxelGroup + 1);
            }
            if (SetBaseVoxelGroup > productModel.NumVoxelGroups - 1)
            {
                productModel.NumVoxelGroups = (byte)(SetBaseVoxelGroup + 1);
            }
        }

        // check if we have a circularloop
        #if UNITY_EDITOR
        if (NPipelineUtils.IsPrevious(PreviousFrame as NPipeIImportable, this, true))
        {
            Debug.LogWarning("cycular pipeline detected");
            return(productModel);
        }
        #endif

        NPVoxToUnity productN2U = new NPVoxToUnity(productModel, Vector3.one);

        // build our colortable
        bool[] usedColors = NPVoxModelUtils.GetUsedColors(productModel);

        Color32[] colorTable = productModel.Colortable;
        byte[]    Colors     = new byte[NumColorSteps];

        Color32 startColor = Color1;
        Color32 endColor   = Color2;

        bool takeColorFromModel = ColorNumFromModel > -1;
        if (takeColorFromModel)
        {
            byte color1 = NPVoxModelUtils.FindUsedColor(ref usedColors, ColorNumFromModel);
            startColor = colorTable[color1];
            endColor   = colorTable[color1];
            endColor.a = 15;
        }

//        Debug.Log("Me: " + NPipelineUtils.GetPipelineDebugString(this));
        for (int i = 0; i < NumColorSteps; i++)
        {
            byte color = NPVoxModelUtils.FindUnusedColor(ref usedColors);
//            Debug.Log("Color: " + color);
            colorTable[color] = Color32.Lerp(startColor, endColor, ((float)i / (float)NumColorSteps));
            Colors[i]         = color;
        }


        // calculate mathetmatical constants
        Vector3 unityStartPoint1  = targetN2U.ToUnityPosition(targetSocket1.Anchor) + targetN2U.ToUnityDirection(SocketOffsets[INDEX_TARGET_1]);
        Vector3 bezierStartPoint1 = unityStartPoint1 + targetN2U.ToUnityDirection(ControlPointOffsets[INDEX_TARGET_1]);

        Vector3 unityEndPoint1  = sourceN2U.ToUnityPosition(sourceSocket1.Anchor) + sourceN2U.ToUnityDirection(SocketOffsets[INDEX_SOURCE_1]);
        Vector3 bezierEndPoint1 = unityEndPoint1 + sourceN2U.ToUnityDirection(ControlPointOffsets[INDEX_SOURCE_1]);

        Vector3 direction1 = unityEndPoint1 - unityStartPoint1;
        float   dir1len    = direction1.magnitude;

        Vector3 unityStartPoint2  = targetN2U.ToUnityPosition(targetSocket2.Anchor) + targetN2U.ToUnityDirection(SocketOffsets[INDEX_TARGET_2]);
        Vector3 bezierStartPoint2 = unityStartPoint2 + targetN2U.ToUnityDirection(ControlPointOffsets[INDEX_TARGET_2]);

        Vector3 unityEndPoint2  = sourceN2U.ToUnityPosition(sourceSocket2.Anchor) + sourceN2U.ToUnityDirection(SocketOffsets[INDEX_SOURCE_2]);
        Vector3 bezierEndPoint2 = unityEndPoint2 + sourceN2U.ToUnityDirection(ControlPointOffsets[INDEX_SOURCE_2]);

        Vector3 direction2 = unityEndPoint2 - unityStartPoint2;
        float   dir2len    = direction2.magnitude;

        float travelled = 0.0f;
        float distance  = dir1len > dir2len ? dir1len : dir2len;
        if (distance > MaxDistance)
        {
            distance = MaxDistance;
        }

        float StepSize = TheStepSize / distance;

        // draw the trail
        while (travelled < distance)
        {
            float alpha = (travelled / distance);
            float idx   = alpha * (float)(NumColorSteps - 1);
            byte  color = Colors[(int)Mathf.Round(idx)];

            Vector3 currentP1    = NPVoxGeomUtil.GetBezierPoint(unityStartPoint1, bezierStartPoint1, bezierEndPoint1, unityEndPoint1, alpha);
            Vector3 currentP2    = NPVoxGeomUtil.GetBezierPoint(unityStartPoint2, bezierStartPoint2, bezierEndPoint2, unityEndPoint2, alpha);
            Vector3 currentP1vox = productN2U.ToSaveVoxCoord(currentP1);
            Vector3 currentP2vox = productN2U.ToSaveVoxCoord(currentP2);
            NPVoxGeomUtil.DrawLine(productModel, currentP1vox, currentP2vox, color, theVoxelGroup, false);
//            currentP1 += direction1 * stepSize1;
//            currentP2 += direction2 * stepSize2;

            travelled += StepSize;
        }

        productModel.Colortable = colorTable;
        productModel.RecalculateNumVoxels();
        return(productModel);
    }