void CreateRenderTextures() { //Load image file as Texture //Create render texture for plane A's material var textureForA = Resources.Load <Texture2D>(textureAPath) as Texture; CustomRenderTexture rtForA = new CustomRenderTexture(256, 256); rtForA.initializationTexture = textureForA; rtForA.initializationMode = CustomRenderTextureUpdateMode.Realtime; rtForA.updateMode = CustomRenderTextureUpdateMode.Realtime; rtForA.material = planeAmaterial; rtForA.wrapMode = TextureWrapMode.Repeat; rtForA.Create(); planeAmaterial.SetTexture("_MainTex", rtForA); //Create render texture for plane B's material var textureForB = Resources.Load <Texture2D>(textureBPath) as Texture; CustomRenderTexture rtForB = new CustomRenderTexture(256, 256); rtForB.initializationTexture = textureForB; rtForB.initializationMode = CustomRenderTextureUpdateMode.Realtime; rtForB.updateMode = CustomRenderTextureUpdateMode.Realtime; rtForB.material = planeBmaterial; rtForB.wrapMode = TextureWrapMode.Repeat; rtForB.Create(); planeBmaterial.SetTexture("_MainTex", rtForB); }
private void Awake() { m_displayImage = GetComponent <RawImage>(); m_rect = GetComponent <RectTransform>(); m_drawingMaterial = Resources.Load <Material>("SimpleDrawingMaterial"); m_customtex = new CustomRenderTexture((int)(m_rect.rect.width * m_renderMultiplier), (int)(m_rect.rect.height * m_renderMultiplier)); m_drawingMaterial = new Material(m_drawingMaterial); m_customtex.material = m_drawingMaterial; m_drawingMaterial.SetTexture("_Tex", m_customtex); m_drawingMaterial.SetFloat("_heightRatio", (float)m_customtex.height / (float)m_customtex.width); m_customtex.updateZoneSpace = CustomRenderTextureUpdateZoneSpace.Pixel; m_customtex.doubleBuffered = true; m_customtex.initializationMode = CustomRenderTextureUpdateMode.OnDemand; m_customtex.initializationColor = m_initializationColor; m_customtex.updateMode = CustomRenderTextureUpdateMode.OnDemand; m_customtex.Create(); m_customtex.Initialize(); m_displayImage.texture = m_customtex; if (m_colors.Length > 0) { SetColor(0); } // init lastpos / currentpos to some negative value out of the canvas lastpos = NormalizeToElementSpace(-Vector2.one); UpdateCursorPosition(-Vector2.one); }
private void Start() { _paintMaterial = new Material(Shader.Find("Unlit/Painter")); _rt = new CustomRenderTexture(textureSize, textureSize, GraphicsFormat.B8G8R8A8_UNorm) { dimension = TextureDimension.Tex2D, doubleBuffered = true, initializationMode = CustomRenderTextureUpdateMode.OnLoad, initializationColor = windowDirtColor, updateMode = CustomRenderTextureUpdateMode.OnLoad, updateZoneSpace = CustomRenderTextureUpdateZoneSpace.Normalized, filterMode = FilterMode.Point, material = _paintMaterial, shaderPass = 0, }; _rt.material = _paintMaterial; { RenderTexture.active = _rt; Texture2D tex = new Texture2D(textureSize, textureSize); } _rt.Create(); _paintMaterial.SetTexture("_Tex", _rt); _paintMaterial.SetFloat("_DirtRate", 1 - dirtRate * Time.fixedDeltaTime); //update the dirt frequency _paintMaterial.SetFloat("_Radius", spongeRadius); MeshRenderer mr = GetComponent <MeshRenderer>(); mr.material.SetTexture("_BaseMap", _rt); mr.material.SetColor("_BaseColor", Color.white); }
/// <summary> /// ペイント用テクスチャをリセット /// </summary> /// <param name="recreate">テクスチャを再生成するか</param> private void ResetPaintTexture(bool recreate) { if (recreate && paintTexture != null) { // CustomRenderTexture.Initialize()では_SelfTexture2Dが初期化されないため,新たにCustomRenderTextureを作成する var tmpTexture = new CustomRenderTexture(paintTexture.width, paintTexture.height, paintTexture.format, RenderTextureReadWrite.Default) { material = paintTexture.material, initializationSource = paintTexture.initializationSource, initializationColor = paintTexture.initializationColor, initializationTexture = paintTexture.initializationTexture, initializationMode = paintTexture.initializationMode, updateMode = paintTexture.updateMode, doubleBuffered = paintTexture.doubleBuffered }; tmpTexture.Create(); paintTexture.Release(); paintTexture = tmpTexture; } paintTexture.Initialize(); // メインテクスチャにセット paintRenderer.material.mainTexture = paintTexture; }
void Start() { Texture mainTexture = paintRenderer.material.mainTexture; // メインテクスチャの解像度が低い場合は固定値 int width = mainTexture.width; int height = mainTexture.height; if (width < 256 || height < 256) { width = 256; height = 256; } // ペイントするテクスチャを作成 paintTexture = new CustomRenderTexture(width, height, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { material = paintMat, initializationSource = CustomRenderTextureInitializationSource.TextureAndColor, initializationColor = Color.white, initializationTexture = mainTexture, initializationMode = CustomRenderTextureUpdateMode.OnDemand, updateMode = CustomRenderTextureUpdateMode.OnDemand, doubleBuffered = true }; paintTexture.Create(); // 頂点マップを作成 vertexMap = new RenderTexture(width, height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Default) { filterMode = FilterMode.Point }; // 頂点マップの初期化 SetVertexMap(); // 集計用テクスチャの生成 colorCountTexture = new RenderTexture(paintTexture.width, paintTexture.height, 0, RenderTextureFormat.ARGB32) { hideFlags = HideFlags.HideAndDontSave, enableRandomWrite = true, filterMode = FilterMode.Point }; colorCountTexture.Create(); forDebug.material.mainTexture = colorCountTexture; // 集計用シェーダーの設定 kernelId = colorCountShader.FindKernel("CountColor"); int color1Id = Shader.PropertyToID("_Color1"); int color2Id = Shader.PropertyToID("_Color2"); colorCountShader.SetVector(color1Id, color1); colorCountShader.SetVector(color2Id, color2); Clear(false); }
/// <summary> /// Forces the volume map cells to be the closest possible to the specified size /// </summary> /// <param name="size">Uniform size desired for a single cell</param> public void ForceCellSize(float size) { volumeMap.Release(); volumeMap.width = Mathf.CeilToInt(transform.lossyScale.x / size); volumeMap.height = Mathf.CeilToInt(transform.lossyScale.y / size); volumeMap.volumeDepth = Mathf.CeilToInt(transform.lossyScale.z / size); volumeMap.Create(); InitializeVolumeMap(); }
void CreateOutputCustomRenderTexture() { _outputCustomRenderTexture = new CustomRenderTexture(videoOutputSizeInPixels.x, videoOutputSizeInPixels.y, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); _outputCustomRenderTexture.updateMode = CustomRenderTextureUpdateMode.Realtime; _outputCustomRenderTexture.Create(); _outputCustomRenderTexture.material = outputCRTMaterial; _outputCRTMaterial.SetTexture("_MainTex", _outputRenderTexture); }
public IEnumerator Initialize() { if (writableTexture.IsCreated() == false) { writableTexture.Create(); } writableTexture.Initialize(); yield return(null); }
public static CustomRenderTexture GenerateNoiseRT(NoiseType type, float scale, int width, int height, int depth = 128) { CustomRenderTexture noiseRT = new CustomRenderTexture(width, height); noiseRT.format = RenderTextureFormat.ARGB32; noiseRT.wrapMode = TextureWrapMode.Repeat; noiseRT.filterMode = FilterMode.Bilinear; noiseRT.updateMode = CustomRenderTextureUpdateMode.Realtime; noiseRT.initializationMode = CustomRenderTextureUpdateMode.Realtime; noiseRT.initializationSource = CustomRenderTextureInitializationSource.Material; noiseRT.enableRandomWrite = true; switch (type) { case NoiseType.PerilinNoise2D: noiseRT.dimension = UnityEngine.Rendering.TextureDimension.Tex2D; noiseRT.material = m_perlin2D; noiseRT.initializationMaterial = m_perlin2D; noiseRT.shaderPass = 0; noiseRT.material.SetFloat("_Scale", scale); break; case NoiseType.PerilinNoise2D_Tileable: noiseRT.dimension = UnityEngine.Rendering.TextureDimension.Tex2D; noiseRT.material = m_perlin2D_Tile; noiseRT.initializationMaterial = m_perlin2D_Tile; noiseRT.shaderPass = 0; noiseRT.material.SetFloat("_Scale", scale); break; case NoiseType.PerilinNoise3D: noiseRT.dimension = UnityEngine.Rendering.TextureDimension.Tex3D; noiseRT.volumeDepth = depth; noiseRT.material = m_perlin3D; noiseRT.initializationMaterial = m_perlin3D; noiseRT.shaderPass = 0; noiseRT.material.SetFloat("_Scale", scale); break; case NoiseType.PerilinNoise3D_ZLoop: noiseRT.dimension = UnityEngine.Rendering.TextureDimension.Tex3D; noiseRT.volumeDepth = depth; noiseRT.material = m_perlin3D_ZLoop; noiseRT.initializationMaterial = m_perlin3D_ZLoop; noiseRT.shaderPass = 0; noiseRT.material.SetFloat("_Scale", scale); break; } noiseRT.Initialize(); noiseRT.Update(); noiseRT.Create(); return(noiseRT); }
public static CustomRenderTexture CreateTextureForScreen(int width, int height) { var texture = new CustomRenderTexture(width, height, RenderTextureFormat.RGFloat); texture.autoGenerateMips = true; texture.useDynamicScale = true; texture.wrapMode = TextureWrapMode.Mirror; texture.filterMode = FilterMode.Bilinear; texture.anisoLevel = 0; texture.initializationMode = CustomRenderTextureUpdateMode.OnDemand; texture.initializationColor = Color.black; texture.updateMode = CustomRenderTextureUpdateMode.OnDemand; texture.doubleBuffered = true; texture.Create(); return(texture); }
/// <summary> /// Creates a render texture for use with the compute shader /// </summary> private void initRenderTexture() { if (_target == null || _target.width != Screen.width || _target.height != Screen.height) { // Release render texture if we already have one if (_target != null) { _target.Release(); } // Get a render target for Ray Tracing _target = new CustomRenderTexture(Screen.width, Screen.height, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); _target.material = renderMaterial; _target.enableRandomWrite = true; _target.Create(); } }
public static CustomRenderTexture GenerateNoise2DBlendRT(Material blendMat, int width, int height) { CustomRenderTexture noiseRT = new CustomRenderTexture(width, height); noiseRT.format = RenderTextureFormat.ARGB32; noiseRT.wrapMode = TextureWrapMode.Repeat; noiseRT.filterMode = FilterMode.Bilinear; noiseRT.updateMode = CustomRenderTextureUpdateMode.Realtime; noiseRT.initializationMode = CustomRenderTextureUpdateMode.Realtime; noiseRT.initializationSource = CustomRenderTextureInitializationSource.Material; noiseRT.enableRandomWrite = true; noiseRT.dimension = UnityEngine.Rendering.TextureDimension.Tex2D; noiseRT.material = blendMat; noiseRT.initializationMaterial = blendMat; noiseRT.shaderPass = 0; noiseRT.Initialize(); noiseRT.Update(); noiseRT.Create(); return(noiseRT); }
void Start() { render_texture = new CustomRenderTexture(resolution, resolution, 0); render_texture.enableRandomWrite = true; render_texture.Create(); render_texture.updateMode = CustomRenderTextureUpdateMode.Realtime; render_texture.initializationMode = CustomRenderTextureUpdateMode.Realtime; render_texture.initializationMaterial = SourceMaterial; render_texture.initializationSource = CustomRenderTextureInitializationSource.Material; render_texture.material = new Material(Shader.Find("Unlit/Texture")); InternalShader.SetTexture(0, "render_texture", render_texture); compute_buffer = new ComputeBuffer(resolution * resolution, sizeof(float) * 3, ComputeBufferType.Default); InternalShader.SetBuffer(0, "compute_buffer", compute_buffer); InternalShader.SetFloat("brightness", brightness); if (gamma) { InternalShader.SetFloat("color_space", 0.4545f); } else { InternalShader.SetFloat("color_space", 1.0f); } }
public static CustomRenderTexture GenerateNoise2DRT(Noise2DType type, float scale, int width, int height, int depth = 128) { CustomRenderTexture noiseRT = new CustomRenderTexture(width, height); noiseRT.format = RenderTextureFormat.RFloat; noiseRT.wrapMode = TextureWrapMode.Repeat; noiseRT.filterMode = FilterMode.Bilinear; noiseRT.updateMode = CustomRenderTextureUpdateMode.Realtime; noiseRT.initializationMode = CustomRenderTextureUpdateMode.Realtime; noiseRT.initializationSource = CustomRenderTextureInitializationSource.Material; noiseRT.enableRandomWrite = true; noiseRT.dimension = UnityEngine.Rendering.TextureDimension.Tex2D; noiseRT.material = new Material(Shader.Find(noise2DDict[type])); noiseRT.initializationMaterial = noiseRT.material; noiseRT.shaderPass = 0; noiseRT.material.SetFloat("_Scale", scale); noiseRT.Initialize(); noiseRT.Update(); noiseRT.Create(); return(noiseRT); }
public override void Init(int w, int h) { width = w; height = h; updateMat = new Material(Shader.Find("Hidden/MicroSplat/StreamUpdateSRP")); buffer0 = new CustomRenderTexture(w, h, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); buffer1 = new CustomRenderTexture(w, h, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); buffer0.initializationMode = CustomRenderTextureUpdateMode.OnDemand; buffer1.initializationMode = CustomRenderTextureUpdateMode.OnDemand; buffer0.updateMode = CustomRenderTextureUpdateMode.OnDemand; buffer1.updateMode = CustomRenderTextureUpdateMode.OnDemand; buffer0.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor; buffer1.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor; buffer0.initializationTexture = Texture2D.blackTexture; buffer1.initializationTexture = Texture2D.blackTexture; buffer0.depth = 0; buffer1.depth = 0; buffer0.material = updateMat; buffer1.material = updateMat; buffer0.Create(); buffer1.Create(); buffer0.Initialize(); buffer1.Initialize(); }
private void OnValidate() { height = (int)Mathf.Max(height, 1f); if (cachedHeight == height) { return; } if (screen == null) { return; } screen.Release(); screen = new CustomRenderTexture((int)(height * mainCamera.aspect), height) { filterMode = FilterMode.Point }; screen.Create(); cachedHeight = height; }
private void OnRenderImage(RenderTexture src, RenderTexture dest) { if (mainCamera == null) { Graphics.Blit(src, dest); return; } if (screen == null) { screen = new CustomRenderTexture((int)(height * mainCamera.aspect), height) { filterMode = FilterMode.Point }; screen.Create(); Graphics.Blit(src, dest); return; } _applyShader ??= gameObject.GetComponent <ApplyShader>(); if (_applyShader && _applyShader.enabled) { Material _customMat = _applyShader.CurrentMaterial; if (_customMat != null) { screen.material = _customMat; } } Graphics.Blit(src, screen); Graphics.Blit(screen, dest); }
protected bool UpdateTempRenderTexture(ref CustomRenderTexture target, bool hasMips = false, bool autoGenerateMips = false) { if (graph.outputTexture == null) { return(false); } int outputWidth = rtSettings.GetWidth(graph); int outputHeight = rtSettings.GetHeight(graph); int outputDepth = rtSettings.GetDepth(graph); GraphicsFormat targetFormat = rtSettings.GetGraphicsFormat(graph); TextureDimension dimension = rtSettings.GetTextureDimension(graph); if (dimension == TextureDimension.Cube) { outputHeight = outputDepth = outputWidth; // we only use the width for cubemaps } if (targetFormat == GraphicsFormat.None) { targetFormat = graph.outputTexture.graphicsFormat; } if (dimension == TextureDimension.None) { dimension = TextureDimension.Tex2D; } if (target == null) { target = new CustomRenderTexture(outputWidth, outputHeight, targetFormat) { volumeDepth = Math.Max(1, outputDepth), dimension = dimension, name = $"Mixture Temp {name}", updateMode = CustomRenderTextureUpdateMode.OnDemand, doubleBuffered = rtSettings.doubleBuffered, wrapMode = rtSettings.wrapMode, filterMode = rtSettings.filterMode, useMipMap = hasMips, autoGenerateMips = autoGenerateMips, }; target.Create(); return(true); } // TODO: check if format is supported by current system // Warning: here we use directly the settings from the if (target.width != outputWidth || target.height != outputHeight || target.graphicsFormat != targetFormat || target.dimension != dimension || target.volumeDepth != outputDepth || target.filterMode != graph.outputTexture.filterMode || target.doubleBuffered != rtSettings.doubleBuffered || target.wrapMode != rtSettings.wrapMode || target.filterMode != rtSettings.filterMode || target.useMipMap != hasMips || target.autoGenerateMips != autoGenerateMips) { target.Release(); target.width = Math.Max(1, outputWidth); target.height = Math.Max(1, outputHeight); target.graphicsFormat = (GraphicsFormat)targetFormat; target.dimension = (TextureDimension)dimension; target.volumeDepth = outputDepth; target.doubleBuffered = rtSettings.doubleBuffered; target.wrapMode = rtSettings.wrapMode; target.filterMode = rtSettings.filterMode; target.useMipMap = hasMips; target.autoGenerateMips = autoGenerateMips; target.Create(); } return(false); }
protected bool UpdateTempRenderTexture(ref CustomRenderTexture target, bool hasMips = false, bool autoGenerateMips = false, CustomRenderTextureUpdateMode updateMode = CustomRenderTextureUpdateMode.OnDemand, bool depthBuffer = false, GraphicsFormat overrideGraphicsFormat = GraphicsFormat.None) { if (graph.mainOutputTexture == null) { return(false); } bool changed = false; int outputWidth = rtSettings.GetWidth(graph); int outputHeight = rtSettings.GetHeight(graph); int outputDepth = rtSettings.GetDepth(graph); GraphicsFormat targetFormat = overrideGraphicsFormat != GraphicsFormat.None ? overrideGraphicsFormat : rtSettings.GetGraphicsFormat(graph); TextureDimension dimension = GetTempTextureDimension(); outputWidth = Mathf.Max(outputWidth, 1); outputHeight = Mathf.Max(outputHeight, 1); outputDepth = Mathf.Max(outputDepth, 1); if (dimension == TextureDimension.Cube) { outputHeight = outputDepth = outputWidth; // we only use the width for cubemaps } if (targetFormat == GraphicsFormat.None) { targetFormat = graph.mainOutputTexture.graphicsFormat; } if (dimension == TextureDimension.None) { dimension = TextureDimension.Tex2D; } if (target == null) { target = new CustomRenderTexture(outputWidth, outputHeight, targetFormat) { volumeDepth = Math.Max(1, outputDepth), depth = depthBuffer ? 32 : 0, dimension = dimension, name = $"Mixture Temp {name}", updateMode = CustomRenderTextureUpdateMode.OnDemand, doubleBuffered = rtSettings.doubleBuffered, wrapMode = rtSettings.wrapMode, filterMode = rtSettings.filterMode, useMipMap = hasMips, autoGenerateMips = autoGenerateMips, enableRandomWrite = true, hideFlags = HideFlags.HideAndDontSave, updatePeriod = GetUpdatePeriod(), }; target.Create(); target.material = MixtureUtils.dummyCustomRenderTextureMaterial; return(true); } // TODO: check if format is supported by current system // Warning: here we use directly the settings from the if (target.width != Math.Max(1, outputWidth) || target.height != Math.Max(1, outputHeight) || target.graphicsFormat != targetFormat || target.dimension != dimension || target.volumeDepth != outputDepth || target.filterMode != rtSettings.filterMode || target.doubleBuffered != rtSettings.doubleBuffered || target.wrapMode != rtSettings.wrapMode || target.useMipMap != hasMips || target.autoGenerateMips != autoGenerateMips || target.updatePeriod != GetUpdatePeriod()) { target.Release(); target.width = Math.Max(1, outputWidth); target.height = Math.Max(1, outputHeight); target.graphicsFormat = (GraphicsFormat)targetFormat; target.dimension = dimension; target.volumeDepth = outputDepth; target.doubleBuffered = rtSettings.doubleBuffered; target.wrapMode = rtSettings.wrapMode; target.filterMode = rtSettings.filterMode; target.useMipMap = hasMips; target.autoGenerateMips = autoGenerateMips; target.enableRandomWrite = true; target.updatePeriod = GetUpdatePeriod(); target.hideFlags = HideFlags.HideAndDontSave; target.Create(); if (target.material == null) { target.material = MixtureUtils.dummyCustomRenderTextureMaterial; } changed = true; } // Patch update mode based on graph type target.updateMode = updateMode; if (target.doubleBuffered) { target.EnsureDoubleBufferConsistency(); var rt = target.GetDoubleBufferRenderTexture(); if (rt.enableRandomWrite != true) { rt.Release(); rt.enableRandomWrite = true; rt.Create(); } } if (target.IsCreated()) { target.Create(); } return(changed); }