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); }
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); }