void UpdateTexture() { lock ( renderBufferLock ) { if (renderBuffer != null && renderBufferForSize == ViewSize && renderBuffer.Length == ViewSize.X * ViewSize.Y * 4) { try { var gpuTexture = texture.Result; if (gpuTexture != null) { //!!!!sense to copy? //!!!!slowly? var data = (byte[])renderBuffer.Clone(); var d = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) }; gpuTexture.SetData(d); } } catch (Exception ex) { Log.Error("UIWebBrowser: Caught exception in UpdateTexture: " + ex.Message); } } } }
bool CreateTexture() { Vector2I textureSize = new Vector2I(Round2(size.X), Round2(size.Y)); texture = ComponentUtility.CreateComponent <Component_Image>(null, true, false); texture.CreateType = Component_Image.TypeEnum._2D; texture.CreateSize = textureSize; texture.CreateMipmaps = false; // 0; //!!!! texture.CreateFormat = PixelFormat.A8R8G8B8; texture.CreateUsage = Component_Image.Usages.Dynamic | Component_Image.Usages.WriteOnly; texture.Enabled = true; //if( !c.Create( Component_Texture.TypeEnum.Type2D, textureSize, 1, 0, PixelFormat.A8R8G8B8, GpuTexture.Usages.DynamicWriteOnly ) ) //{ // texture.Dispose(); // return false; //} GpuTexture gpuTexture = texture.Result; int totalSize = PixelFormatUtility.GetNumElemBytes(gpuTexture.ResultFormat) * gpuTexture.ResultSize.X * gpuTexture.ResultSize.Y; byte[] data = new byte[totalSize]; var d = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) }; gpuTexture.SetData(d); textureFormat = texture.CreateFormat; //HardwarePixelBuffer pixelBuffer = texture.GetBuffer(); //pixelBuffer.Lock( HardwareBuffer.LockOptions.Discard ); //PixelBox pixelBox = pixelBuffer.GetCurrentLock(); //textureFormat = pixelBox.Format; //NativeUtils.ZeroMemory( pixelBox.Data, pixelBox.SlicePitch * 4 ); //pixelBuffer.Unlock(); if (textureFormat != PixelFormat.A8R8G8B8 && textureFormat != PixelFormat.A8B8G8R8) { Log.Warning("UIVideo: CreateTexture: Lock texture format != PixelFormat.A8R8G8B8 and != PixelFormat.A8B8G8R8 ({0}).", textureFormat); } RenderingSystem.RenderSystemEvent += RenderSystem_RenderSystemEvent; return(true); }
public Component_Image GetImage(out long uniqueMaskDataCounter) { uniqueMaskDataCounter = 0; if (MaskImage.Value != null) { return(MaskImage); } else if (Mask.Value != null && Mask.Value.Length != 0) { if (EnabledInHierarchy) { if (createdMaskImage == null) { int textureSize = (int)Math.Sqrt(Mask.Value.Length); //need set ShowInEditor = false before AddComponent var texture = ComponentUtility.CreateComponent <Component_Image>(null, false, false); texture.DisplayInEditor = false; AddComponent(texture, -1); //var texture = CreateComponent<Component_Image>( enabled: false ); texture.SaveSupport = false; texture.CloneSupport = false; //!!!! bool mipmaps = false; texture.CreateType = Component_Image.TypeEnum._2D; texture.CreateSize = new Vector2I(textureSize, textureSize); texture.CreateMipmaps = mipmaps; texture.CreateFormat = PixelFormat.L8; var usage = Component_Image.Usages.WriteOnly; if (mipmaps) { usage |= Component_Image.Usages.AutoMipmaps; } texture.CreateUsage = usage; texture.Enabled = true; createdMaskImage = texture; createdMaskImageNeedUpdate = true; } //update data if (createdMaskImageNeedUpdate) { GpuTexture gpuTexture = createdMaskImage.Result; if (gpuTexture != null) { var d = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, Mask.Value) }; gpuTexture.SetData(d); } createdMaskImageNeedUpdate = false; } uniqueMaskDataCounter = this.uniqueMaskDataCounter; return(createdMaskImage); } } return(null); }
public unsafe void UpdateTexture(bool clearWholeTexture) { if (size == Vector2I.Zero) { return; } if (RenderingSystem.IsDeviceLost()) { return; } if (texture != null && texture.Disposed) { texture = null; } //create texture if (texture == null) { if (!CreateTexture()) { return; } } if (textureFormat != PixelFormat.A8R8G8B8 && textureFormat != PixelFormat.A8B8G8R8) { return; } GpuTexture gpuTexture = texture.Result; int totalSize = PixelFormatUtility.GetNumElemBytes(gpuTexture.ResultFormat) * gpuTexture.ResultSize.X * gpuTexture.ResultSize.Y; //!!!!можно каждый раз не создавать массив byte[] data = new byte[totalSize]; //HardwarePixelBuffer pixelBuffer = texture.GetBuffer(); //pixelBuffer.Lock( HardwareBuffer.LockOptions.Discard ); //PixelBox pixelBox = pixelBuffer.GetCurrentLock(); //if( clearWholeTexture ) // NativeUtility.ZeroMemory( data, pixelBox.SlicePitch * 4 ); byte *pointer = (byte *)buffer; for (int y = 0; y < size.Y; y++) { //!!!!всегда так? int rowPitch = PixelFormatUtility.GetNumElemBytes(gpuTexture.ResultFormat) * gpuTexture.ResultSize.X; Marshal.Copy((IntPtr)pointer, data, y * rowPitch, size.X * 4); //NativeUtility.CopyMemory( pointer, source, length ); //pixelBox.WriteDataUnmanaged( y * pixelBox.RowPitch * 4, pointer, size.X * 4 ); pointer += size.X * 4; } //pixelBuffer.Unlock(); var d = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) }; gpuTexture.SetData(d); needUpdateTexture = false; }
private void ParentMeshInSpace_GetRenderSceneDataAddToFrameData(Component_MeshInSpace sender, ViewportRenderingContext context, GetRenderSceneDataMode mode, ref Component_RenderingPipeline.RenderSceneData.MeshItem item) { if (!CalculateOnCPU) { Component_Skeleton skeleton = ReplaceSkeleton; if (skeleton == null) { skeleton = ParentMeshInSpace?.Mesh.Value?.Skeleton; } if (skeleton != null) { var animation = PlayAnimation.Value; if (animation != null) { UpdateAnimationTime(); //settings.animationStates = new AnimationStateItem[ 1 ]; //settings.animationStates[ 0 ] = new AnimationStateItem( animation, currentLocalTime, 1 ); var skeletonAnimation = animation as Component_SkeletonAnimation; var track = skeletonAnimation?.Track.Value; if (track != null || CalculateBoneTransforms != null) { Update(skeleton, track, currentAnimationTime); if (transformMatrixRelativeToSkin != null && transformMatrixRelativeToSkin.Length != 0) { item.AnimationData = new Component_RenderingPipeline.RenderSceneData.MeshItem.AnimationDataClass(); bool dualQuaternion = false; // GetSkinningMode( skeleton ) == Component_Skeleton.SkinningModeEnum.DualQuaternion; if (dualQuaternion) { item.AnimationData.Mode = 2; } else { item.AnimationData.Mode = 1; } //create dynamic texture var size = new Vector2I(4, MathEx.NextPowerOfTwo(transformMatrixRelativeToSkin.Length)); var bonesTexture = context.DynamicTexture_Alloc(ViewportRenderingContext.DynamicTextureType.DynamicTexture, Component_Image.TypeEnum._2D, size, PixelFormat.Float32RGBA, 0, false); //try get array from texture to minimize memory allocations var surfaces = bonesTexture.Result.GetData(); if (surfaces == null) { surfaces = new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, new byte[size.X * size.Y * 16]) } } ; var data = surfaces[0].data; //copy data to the texture unsafe { fixed(byte *pData2 = data) { Matrix4F *pData = (Matrix4F *)pData2; for (int n = 0; n < transformMatrixRelativeToSkin.Length; n++) { pData[n] = transformMatrixRelativeToSkin[n]; } } } bonesTexture.Result.SetData(new GpuTexture.SurfaceData[] { new GpuTexture.SurfaceData(0, 0, data) }); item.AnimationData.BonesTexture = bonesTexture; } } } } } } void RenderSkeleton(Viewport viewport) { // ParentMeshInSpace.Transform is automaticaly applyed to ParentMeshInSpace.Mesh, skeleton must be transformed manually var transformMatrix = ParentMeshInSpace?.Transform.Value?.ToMatrix4() ?? Matrix4.Identity; var skeletonArrows = GetCurrentAnimatedSkeletonArrows(); if (skeletonArrows != null) { var color = new ColorValue(0, 0.5, 1, 0.7); //ToDo : Вынести в другое место. viewport.Simple3DRenderer.SetColor(color, color * ProjectSettings.Get.HiddenByOtherObjectsColorMultiplier); foreach (var arrow in skeletonArrows) { viewport.Simple3DRenderer.AddArrow(transformMatrix * arrow.Start, transformMatrix * arrow.End); } } } bool CheckNeedModifiableMesh() { if (CalculateOnCPU) { if (ReplaceSkeleton.ReferenceSpecified) { return(true); } var mesh = ParentMeshInSpace?.Mesh.Value; if (mesh != null && mesh.Skeleton.ReferenceSpecified) { return(true); } if (PlayAnimation.ReferenceSpecified) { return(true); } } return(false); } void UpdateModifiableMesh(ViewportRenderingContext context) { var originalMesh = ParentMeshInSpace.Mesh.Value; var modifiableMesh = ParentMeshInSpace.ModifiableMesh; Component_Skeleton skeleton = ReplaceSkeleton; if (skeleton == null) { skeleton = originalMesh.Skeleton; } if (skeleton != null) { //!!!!сериализовывать var animation = PlayAnimation.Value; if (animation != null) { UpdateAnimationTime(); //settings.animationStates = new AnimationStateItem[ 1 ]; //settings.animationStates[ 0 ] = new AnimationStateItem( animation, currentLocalTime, 1 ); var skeletonAnimation = animation as Component_SkeletonAnimation; var track = skeletonAnimation?.Track.Value; if (track != null || CalculateBoneTransforms != null) { Update(skeleton, track, currentAnimationTime); CalculateCPU(skeleton, originalMesh, modifiableMesh); } } if (needResetToOriginalMesh) { needResetToOriginalMesh = false; if (CalculateOnCPU) { ResetToOriginalMesh(originalMesh, modifiableMesh); } } } } ///////////////////////////////////////// void ResetTime(bool needResetToOriginalMesh) { if (needResetToOriginalMesh) { this.needResetToOriginalMesh = true; } currentEngineTime = EngineApp.EngineTime; currentAnimationTime = (PlayAnimation.Value as Component_SkeletonAnimation)?.TrackStartTime ?? 0; } void UpdateAnimationTime() { double t = EngineApp.EngineTime; double increment = t - currentEngineTime; currentEngineTime = t; currentAnimationTime += increment * Speed; var animation = PlayAnimation.Value as Component_SkeletonAnimation; if (animation != null) { double animationStartTime = animation.TrackStartTime; double animationLength = animation.Length; if (animationLength > 0) { if (AutoRewind) { while (currentAnimationTime > animationStartTime + animationLength) { currentAnimationTime -= animationLength; } while (currentAnimationTime < animationStartTime) { currentAnimationTime += animationLength; } } else { MathEx.Clamp(ref currentAnimationTime, animationStartTime, animationStartTime + animationLength); } } else { currentAnimationTime = animationStartTime; } } else { currentAnimationTime = 0; } }