public void OnInitialize() { try { OnInitialize_(); AnimateModel(Frame.BEGIN); frameIndex = 0; frameTime = 0.0f; Vector3 screenPos = Camera.main.WorldToScreenPoint(model.GetPivotPosition()); pivot2D = new IntegerVector(screenPos); unifiedTexBound = new IntegerBound(); studio.samplings.Clear(); ClearAllFrames(); stateMachine.ChangeState(SamplingState.BeginFrame); } catch (Exception e) { Debug.LogException(e); Finish(); } }
public void OnBeginView() { try { if (studio.trimming.isUnifiedForAllViews) { unifiedTexBound = new IntegerBound(); } if (studio.packing.on || studio.trimming.isUnifiedForAllViews) { frameModelTextures = new Texture2D[frames.Length]; framePivots = new IntegerVector[frames.Length]; } studio.view.checkedSubViews[viewIndex].func(model); viewName = studio.view.checkedSubViews[viewIndex].name; vecFromCameraToModel = model.transform.position - Camera.main.transform.position; Vector3 screenPos = Camera.main.WorldToScreenPoint(model.GetPivotPosition()); pivot2D = new IntegerVector(screenPos); frameIndex = 0; stateMachine.ChangeState(BakingState.BeginFrame); } catch (Exception e) { Debug.LogException(e); Finish(); } }
public static void MakeUnifiedBound(IntegerVector pivot, IntegerBound bound, IntegerBound unifiedBound) { if (pivot == null) { return; } unifiedBound.min.x = Mathf.Min(unifiedBound.min.x, bound.min.x); unifiedBound.max.x = Mathf.Max(unifiedBound.max.x, bound.max.x); unifiedBound.min.y = Mathf.Min(unifiedBound.min.y, bound.min.y); unifiedBound.max.y = Mathf.Max(unifiedBound.max.y, bound.max.y); }
public void OnInitialize() { try { if (studio.view.rotationType == RotationType.Model) { CameraHelper.LocateMainCameraToModel(model, studio); } ShadowHelper.LocateShadowToModel(model, studio); if (studio.shadow.type == ShadowType.Simple) { ShadowHelper.ScaleSimpleShadow(model, studio); } else if (studio.shadow.type == ShadowType.TopDown) { Camera camera; GameObject fieldObj; ShadowHelper.GetCameraAndFieldObject(studio.shadow.obj, out camera, out fieldObj); CameraHelper.LookAtModel(camera.transform, model); ShadowHelper.ScaleShadowField(camera, fieldObj); } else if (studio.shadow.type == ShadowType.Matte) { ShadowHelper.ScaleMatteField(meshModel, studio.shadow.obj, studio.lit); } if (studio.packing.on || studio.trimming.isUnifiedForAllViews) { bakingDataList = new BakingData[studio.view.checkedSubViews.Count]; } if (studio.trimming.isUnifiedForAllViews) { unifiedTexBound = new IntegerBound(); } fileBaseName = BuildFileBaseName(); BuildFolderPathAndCreate(modelName); viewIndex = 0; stateMachine.ChangeState(BakingState.BeginView); } catch (Exception e) { Debug.LogException(e); Finish(); } }
public static bool CalcTextureBound(Texture2D tex, IntegerVector pivot, IntegerBound texBound, IntegerBound compactBound) { if (!CalcValidPixelBound(tex, compactBound)) { return(false); } texBound.min.x = Mathf.Min(compactBound.min.x, pivot.x); texBound.min.y = Mathf.Min(compactBound.min.y, pivot.y); texBound.max.x = Mathf.Max(compactBound.max.x, pivot.x); texBound.max.y = Mathf.Max(compactBound.max.y, pivot.y); return(true); }
public void OnBeginView() { try { if (studio.trimming.isUnifiedForAllViews) { unifiedTexBound = new IntegerBound(); } if (studio.packing.on || studio.trimming.isUnifiedForAllViews) { frameModelTextures = new Texture2D[frames.Length]; if (studio.output.normalMapMake) { frameNormalMapTextures = new Texture2D[frames.Length]; } framePivots = new IntegerVector[frames.Length]; } studio.view.checkedSubViews[viewIndex].func(model); viewName = studio.view.checkedSubViews[viewIndex].name; modelBaseSizeForView = model.GetSize(); if (outRootPrefab && model.prefabBuilder.GetBoxCollider2D(outRootPrefab) != null && studio.output.isCompactCollider) { frameCompactBounds = new IntegerBound[frames.Length]; frameCompactVectors = new Vector2[frames.Length]; } if (locationMappings != null) { foreach (LocationMapping mapping in locationMappings) { mapping.frameLocationPositions = new IntegerVector[frames.Length]; mapping.frameRatioPositions = new Vector2[frames.Length]; } } frameIndex = 0; stateMachine.ChangeState(BakingState.BeginFrame); } catch (Exception e) { Debug.LogException(e); Finish(); } }
private void CalcCompactVector(Texture2D tex, IntegerBound bound) { IntegerBound compactBound = frameCompactBounds[frameIndex]; compactBound.min.SubtractWithMargin(bound.min, studio.trimming.margin); compactBound.max.SubtractWithMargin(bound.min, studio.trimming.margin); float textureCenterX = tex.width / 2f; float textureCenterY = tex.height / 2f; float compactCenterX = (float)(compactBound.min.x + compactBound.max.x) / 2f; float compactCenterY = (float)(compactBound.min.y + compactBound.max.y) / 2f; float ratioVectorX = (compactCenterX - textureCenterX) / tex.width; float ratioVectorY = (compactCenterY - textureCenterY) / tex.height; frameCompactVectors[frameIndex] = new Vector2(ratioVectorX, ratioVectorY); }
public void OnCaptureFrame() { try { double deltaTime = EditorApplication.timeSinceStartup - prevTime; if (deltaTime < studio.frame.delay) { return; } prevTime = EditorApplication.timeSinceStartup; OnCaptureFrame_(); Texture2D tex = CapturingHelper.CaptureModelManagingShadow(model, studio); studio.samplings.Add(new Sampling(tex, frameTime)); IntegerBound texBound = new IntegerBound(); if (!TextureHelper.CalcTextureBound(tex, pivot2D, texBound)) { texBound.min.x = pivot2D.x - 1; texBound.max.x = pivot2D.x + 1; texBound.min.y = pivot2D.y - 1; texBound.max.y = pivot2D.y + 1; } TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound); stateMachine.ChangeState(SamplingState.EndFrame); } catch (Exception e) { Debug.LogException(e); Finish(); } }
public void OnCaptureFrame() { try { double deltaTime = EditorApplication.timeSinceStartup - prevTime; if (deltaTime < studio.frame.delay) { return; } prevTime = EditorApplication.timeSinceStartup; if (studio.shadow.type == ShadowType.TopDown) { Camera camera; GameObject fieldObj; ShadowHelper.GetCameraAndFieldObject(studio.shadow.obj, out camera, out fieldObj); ShadowHelper.BakeStaticShadow(camera, fieldObj, particleModel, studio); } Texture2D tex = CapturingHelper.CaptureModelManagingShadow(model, studio); IntegerBound texBound = new IntegerBound(); if (!TextureHelper.CalcTextureBound(tex, pivot2D, texBound)) { texBound.min.x = pivot2D.x - 1; texBound.max.x = pivot2D.x + 1; texBound.min.y = pivot2D.y - 1; texBound.max.y = pivot2D.y + 1; } if (studio.trimming.on) { if (studio.trimming.isUnifiedForAllViews) { TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound); } else { pivot2D.SubtractWithMargin(texBound.min, studio.trimming.margin); tex = TextureHelper.TrimTexture(tex, texBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32); } } if (studio.packing.on || studio.trimming.isUnifiedForAllViews) { frameModelTextures[frameIndex] = tex; framePivots[frameIndex] = pivot2D; } else // !studio.packing.on && !studio.trim.isUnifiedSize { BakeIndividually(tex, pivot2D, viewName, frameIndex); } stateMachine.ChangeState(BakingState.EndFrame); } catch (Exception e) { Debug.LogException(e); Finish(); } }
public static bool CalcTextureBound(Texture2D tex, IntegerVector pivot, IntegerBound texBound) { return(CalcTextureBound(tex, pivot, texBound, new IntegerBound())); }
public static bool CalcValidPixelBound(Texture2D tex, IntegerBound bound) { Color[] colors = tex.GetPixels(); bool validPixelExist = false; for (int x = 0; x < tex.width; x++) { for (int y = 0; y < tex.height; y++) { float alpha = colors[y * tex.width + x].a; if (alpha != 0) { bound.min.x = x; validPixelExist = true; goto ENDMINX; } } } ENDMINX: if (!validPixelExist) { return(false); } validPixelExist = false; for (int y = 0; y < tex.height; y++) { for (int x = bound.min.x; x < tex.width; x++) { float alpha = colors[y * tex.width + x].a; if (alpha != 0) { bound.min.y = y; validPixelExist = true; goto ENDMINY; } } } ENDMINY: if (!validPixelExist) { return(false); } validPixelExist = false; for (int x = tex.width - 1; x >= bound.min.x; x--) { for (int y = bound.min.y; y < tex.height; y++) { float alpha = colors[y * tex.width + x].a; if (alpha != 0) { bound.max.x = x; validPixelExist = true; goto ENDMAXX; } } } ENDMAXX: if (!validPixelExist) { return(false); } validPixelExist = false; for (int y = tex.height - 1; y >= bound.min.y; y--) { for (int x = bound.min.x; x <= bound.max.x; x++) { float alpha = colors[y * tex.width + x].a; if (alpha != 0) { bound.max.y = y; validPixelExist = true; goto ENDMAXY; } } } ENDMAXY: if (!validPixelExist) { return(false); } return(true); }
public static Texture2D TrimTexture(Texture2D tex, IntegerBound bound, int margin, Color32 defaultColor, bool isNormalMap = false) { if (tex == null) { return(Texture2D.whiteTexture); } IntegerBound marginedBound = bound.CopyExtendedBy(margin); int marginedWidth = marginedBound.max.x - marginedBound.min.x + 1; int marginedHeight = marginedBound.max.y - marginedBound.min.y + 1; if (marginedWidth < 0 || marginedHeight < 0) { Debug.LogError("Minus margin is too much."); return(Texture2D.whiteTexture); } Texture2D resultTexture = new Texture2D(marginedWidth, marginedHeight, TextureFormat.ARGB32, false); IntegerBound minCopyBound = (margin < 0) ? marginedBound : bound.Copy(); int minCopyWidth = minCopyBound.max.x - minCopyBound.min.x + 1; int minCopyHeight = minCopyBound.max.y - minCopyBound.min.y + 1; int srcFirstX = minCopyBound.min.x; int srcFirstY = minCopyBound.min.y; int destFirstX = (margin < 0) ? 0 : margin; int destFirstY = (margin < 0) ? 0 : margin; Color32[] srcPixels = tex.GetPixels32(); Color32[] destPixels = Enumerable.Repeat(defaultColor, marginedWidth * marginedHeight).ToArray(); if (EngineGlobal.gpuUse && SystemInfo.supportsComputeShaders && TrimShader != null && !isNormalMap && marginedWidth * marginedHeight > EngineGlobal.GPU_THREAD_SIZE) { int kernelIndex = TrimShader.FindKernel("TrimFunction"); ComputeBuffer srcBuffer = new ComputeBuffer(tex.width * tex.height, 4); srcBuffer.SetData(srcPixels); TrimShader.SetBuffer(kernelIndex, "srcBuffer", srcBuffer); TrimShader.SetInt("srcFirstX", srcFirstX); TrimShader.SetInt("srcFirstY", srcFirstY); TrimShader.SetInt("srcWidth", tex.width); TrimShader.SetInt("destFirstX", destFirstX); TrimShader.SetInt("destFirstY", destFirstY); TrimShader.SetInt("destWidth", marginedWidth); ComputeBuffer destBuffer = new ComputeBuffer(marginedWidth * marginedHeight, 4); TrimShader.SetBuffer(kernelIndex, "destBuffer", destBuffer); TrimShader.Dispatch(kernelIndex, EngineGlobal.GPU_THREAD_SIZE, 1, 1); destBuffer.GetData(destPixels); srcBuffer.Release(); destBuffer.Release(); } else { for (int i = 0; i < minCopyWidth * minCopyHeight; ++i) { int x = i % minCopyWidth; int y = i / minCopyWidth; int srcIndex = (srcFirstY + y) * tex.width + (srcFirstX + x); int destIndex = (destFirstY + y) * marginedWidth + (destFirstX + x); destPixels[destIndex] = srcPixels[srcIndex]; } } resultTexture.SetPixels32(destPixels); resultTexture.Apply(); return(resultTexture); }
public void OnCaptureFrame() { try { double deltaTime = EditorApplication.timeSinceStartup - prevTime; if (deltaTime < studio.frame.delay) { return; } prevTime = EditorApplication.timeSinceStartup; if (studio.shadow.type == ShadowType.Simple && studio.shadow.simple.isDynamicScale) { ShadowHelper.ScaleSimpleShadowDynamically(modelBaseSizeForView, simpleShadowBaseScale, meshModel, studio); } Texture2D modelTexture = CapturingHelper.CaptureModelManagingShadow(model, studio); Texture2D normalMapTexture = null; if (studio.output.normalMapMake) { normalMapTexture = CapturingHelper.CaptureModelForNormalMap(model, studio.output.isGrayscaleMap, studio.shadow.obj); } IntegerBound texBound = new IntegerBound(); IntegerBound compactBound = new IntegerBound(); if (!TextureHelper.CalcTextureBound(modelTexture, pivot2D, texBound, compactBound)) { texBound.min.x = pivot2D.x - 1; texBound.max.x = pivot2D.x + 1; texBound.min.y = pivot2D.y - 1; texBound.max.y = pivot2D.y + 1; } if (frameCompactBounds != null) { frameCompactBounds[frameIndex] = compactBound; } if (studio.trimming.on) { if (studio.trimming.isUnifiedForAllViews) { TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound); } else { pivot2D.SubtractWithMargin(texBound.min, studio.trimming.margin); modelTexture = TextureHelper.TrimTexture(modelTexture, texBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32); if (studio.output.normalMapMake) { Color32 defaultColor = (studio.output.isGrayscaleMap ? EngineGlobal.CLEAR_COLOR32 : EngineGlobal.NORMALMAP_COLOR32); normalMapTexture = TextureHelper.TrimTexture(normalMapTexture, texBound, studio.trimming.margin, defaultColor, true); } if (frameCompactBounds != null) { CalcCompactVector(modelTexture, texBound); } if (locationMappings != null) { foreach (LocationMapping mapping in locationMappings) { mapping.frameLocationPositions[frameIndex].SubtractWithMargin(texBound.min, studio.trimming.margin); } } } } if (studio.packing.on || studio.trimming.isUnifiedForAllViews) { framePivots[frameIndex] = pivot2D; frameModelTextures[frameIndex] = modelTexture; if (studio.output.normalMapMake) { frameNormalMapTextures[frameIndex] = normalMapTexture; } } else // !studio.packing.on && !studio.trim.isUnifiedSize { BakeIndividually(modelTexture, pivot2D, viewName, frameIndex); if (studio.output.normalMapMake) { BakeIndividually(normalMapTexture, pivot2D, viewName, frameIndex, true); } } stateMachine.ChangeState(BakingState.EndFrame); } catch (Exception e) { Debug.LogException(e); Finish(); } }
public void OnCaptureFrame() { try { double deltaTime = EditorApplication.timeSinceStartup - prevTime; if (deltaTime < studio.frame.delay) { return; } prevTime = EditorApplication.timeSinceStartup; Texture2D modelTexture = CapturingHelper.CaptureModelManagingShadow(model, studio); Texture2D normalMapTexture = null; if (studio.output.normalMapMake) { normalMapTexture = CapturingHelper.CaptureModelForNormalMap(model, studio.output.isGrayscaleMap, studio.shadow.obj); } IntegerBound texBound = new IntegerBound(); if (!TextureHelper.CalcTextureBound(modelTexture, pivot2D, texBound)) { texBound.min.x = pivot2D.x - 1; texBound.max.x = pivot2D.x + 1; texBound.min.y = pivot2D.y - 1; texBound.max.y = pivot2D.y + 1; } if (studio.trimming.on) { if (studio.trimming.isUnifiedForAllViews) { TextureHelper.MakeUnifiedBound(pivot2D, texBound, unifiedTexBound); } else { pivot2D.SubtractWithMargin(texBound.min, studio.trimming.margin); modelTexture = TextureHelper.TrimTexture(modelTexture, texBound, studio.trimming.margin, EngineGlobal.CLEAR_COLOR32); if (studio.output.normalMapMake) { Color32 defaultColor = (studio.output.isGrayscaleMap ? EngineGlobal.CLEAR_COLOR32 : EngineGlobal.NORMALMAP_COLOR32); normalMapTexture = TextureHelper.TrimTexture(normalMapTexture, texBound, studio.trimming.margin, defaultColor, true); } } } string viewName = studio.view.checkedSubViews[viewIndex].name; if (studio.packing.on || studio.trimming.isUnifiedForAllViews) { bakingDataList[viewIndex] = new BakingData(viewName, pivot2D, modelTexture, normalMapTexture); } else // !studio.packing.on && !studio.trim.isUnifiedSize { BakeIndividually(modelTexture, pivot2D, viewName, ""); if (studio.output.normalMapMake) { BakeIndividually(normalMapTexture, pivot2D, viewName, "", true); } } stateMachine.ChangeState(BakingState.EndView); } catch (Exception e) { Debug.LogException(e); Finish(); } }