Release() 공개 메소드

Releases the RenderTexture.

public Release ( ) : void
리턴 void
예제 #1
1
    /*****/
    private void CreateDistanceField()
    {
        var size = 128;
        var pdbName = "MA_matrix_G1";
        string path = "Assets/Resources/3D Textures/" + pdbName + ".asset";

        Texture3D tmp = (Texture3D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture3D));

        if (tmp)
        {
            _volumeTexture = tmp;
        }
        else
        {
            RenderTexture _distanceFieldRT;

            _distanceFieldRT = new RenderTexture(size, size, 0, RenderTextureFormat.R8);
            _distanceFieldRT.volumeDepth = size;
            _distanceFieldRT.isVolume = true;
            _distanceFieldRT.isPowerOfTwo = true;
            _distanceFieldRT.enableRandomWrite = true;
            _distanceFieldRT.filterMode = FilterMode.Trilinear;
            _distanceFieldRT.name = pdbName;
            _distanceFieldRT.hideFlags = HideFlags.HideAndDontSave;
            _distanceFieldRT.generateMips = true;
            _distanceFieldRT.useMipMap = true;
            _distanceFieldRT.Create();

            var atomSpheres = PdbLoader.LoadAtomSpheres(pdbName);
            var atomSphereGPUBuffer = new ComputeBuffer(atomSpheres.Count, sizeof(float) * 4, ComputeBufferType.Default);
            atomSphereGPUBuffer.SetData(atomSpheres.ToArray());

            Graphics.SetRenderTarget(_distanceFieldRT);
            GL.Clear(true, true, new Color(0, 0, 0));

            var createDistanceFieldCS = Resources.Load("Compute Shaders/CreateDistanceField") as ComputeShader;
            createDistanceFieldCS.SetInt("_GridSize", size);
            createDistanceFieldCS.SetInt("_NumAtoms", atomSpheres.Count);
            createDistanceFieldCS.SetBuffer(0, "_SpherePositions", atomSphereGPUBuffer);
            createDistanceFieldCS.SetTexture(0, "_VolumeTexture", _distanceFieldRT);
            createDistanceFieldCS.Dispatch(0, Mathf.CeilToInt(size / 10.0f), Mathf.CeilToInt(size / 10.0f), Mathf.CeilToInt(size / 10.0f));

            atomSphereGPUBuffer.Release();

            //****

            var flatSize = size * size * size;
            var voxelGPUBuffer = new ComputeBuffer(flatSize, sizeof(float));

            var readVoxelsCS = Resources.Load("Compute Shaders/ReadVoxels") as ComputeShader;
            readVoxelsCS.SetInt("_VolumeSize", size);
            readVoxelsCS.SetBuffer(0, "_VoxelBuffer", voxelGPUBuffer);
            readVoxelsCS.SetTexture(0, "_VolumeTexture", _distanceFieldRT);
            readVoxelsCS.Dispatch(0, size, size, size);

            var voxelCPUBuffer = new float[flatSize];
            voxelGPUBuffer.GetData(voxelCPUBuffer);

            var volumeColors = new Color[flatSize];
            for (int i = 0; i < flatSize; i++)
            {
                volumeColors[i] = new Color(0, 0, 0, voxelCPUBuffer[i]);
            }

            var texture3D = new Texture3D(size, size, size, TextureFormat.Alpha8, true);
            texture3D.SetPixels(volumeColors);
            texture3D.wrapMode = TextureWrapMode.Clamp;
            texture3D.anisoLevel = 0;
            texture3D.Apply();

            AssetDatabase.CreateAsset(texture3D, path);
            AssetDatabase.SaveAssets();

            // Print the path of the created asset
            Debug.Log(AssetDatabase.GetAssetPath(texture3D));

            voxelGPUBuffer.Release();

            _distanceFieldRT.Release();
            DestroyImmediate(_distanceFieldRT);

            _volumeTexture = texture3D;
        }
    }
예제 #2
0
	void OnRenderImage(RenderTexture source, RenderTexture destination)
	{
		//Debug.Log ("s" + source.GetInstanceID () + " " + destination.GetInstanceID ());
		material.SetFloat("_ValidDepthThreshold", ValidDepthThreshold);
		material.SetFloat("_EdgeLuminanceThreshold", EdgeLuminanceThreshold);
		material.SetFloat("_SharpEdge", SharpEdge);

		//var rt = RenderTexture.GetTemporary (source.width / 2, source.height / 2);
		//Graphics.Blit(source, rt2, material, 0);

		var rt2 = new RenderTexture (source.width, source.height, 0);
		rt2.enableRandomWrite = false;
		//rt2.filterMode = FilterMode.Point;
		rt2.filterMode = FilterMode.Point;
		Graphics.Blit(source, rt2, material, 0);

		//var rt3 = new RenderTexture (source.width/2, source.height/2, 0);
		//rt3.enableRandomWrite = false;
		//rt3.filterMode = FilterMode.Point;
		//Graphics.Blit(rt2, rt3, material, 0);

		Graphics.Blit(rt2, destination, material, 1);
		rt2.Release ();
		//rt3.Release ();

	}
예제 #3
0
 static public int Release(IntPtr l)
 {
     try {
         UnityEngine.RenderTexture self = (UnityEngine.RenderTexture)checkSelf(l);
         self.Release();
         return(0);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
예제 #4
0
 static public int Release(IntPtr l)
 {
     try {
         UnityEngine.RenderTexture self = (UnityEngine.RenderTexture)checkSelf(l);
         self.Release();
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
예제 #5
0
 static int QPYX_Release_YXQP(IntPtr L_YXQP)
 {
     try
     {
         ToLua.CheckArgsCount(L_YXQP, 1);
         UnityEngine.RenderTexture QPYX_obj_YXQP = (UnityEngine.RenderTexture)ToLua.CheckObject <UnityEngine.RenderTexture>(L_YXQP, 1);
         QPYX_obj_YXQP.Release();
         return(0);
     }
     catch (Exception e_YXQP)                {
         return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP));
     }
 }
 static int Release(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         UnityEngine.RenderTexture obj = (UnityEngine.RenderTexture)ToLua.CheckObject(L, 1, typeof(UnityEngine.RenderTexture));
         obj.Release();
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
예제 #7
0
 private static UnityEngine.Texture2D ToTexture2DNoise(this UnityEngine.RenderTexture inTex, bool apply = true, bool release = false, UnityEngine.TextureFormat format = UnityEngine.TextureFormat.RGBA32)
 {
     UnityEngine.Texture2D tex = new UnityEngine.Texture2D(inTex.width, inTex.height, format, false);
     UnityEngine.RenderTexture.active = inTex;
     tex.ReadPixels(new UnityEngine.Rect(0, 0, inTex.width, inTex.height), 0, 0);
     if (release)
     {
         UnityEngine.RenderTexture.active = null;
         inTex.Release();
     }
     if (apply)
     {
         tex.Apply();
     }
     return(tex);
 }
	static int Release(IntPtr L)
	{
#if UNITY_EDITOR
        ToluaProfiler.AddCallRecord("UnityEngine.RenderTexture.Release");
#endif
		try
		{
			ToLua.CheckArgsCount(L, 1);
			UnityEngine.RenderTexture obj = (UnityEngine.RenderTexture)ToLua.CheckObject<UnityEngine.RenderTexture>(L, 1);
			obj.Release();
			return 0;
		}
		catch (Exception e)
		{
			return LuaDLL.toluaL_exception(L, e);
		}
	}
예제 #9
0
    void CreateRenderTexture(Sprite sprite)
    {
        GameObject.Find("background-1").GetComponent<SpriteRenderer>().sprite = sprite;

        int width = (int)GameObject.Find("background-1").GetComponent<Renderer>().bounds.size.x;
        int height = (int)GameObject.Find("background-1").GetComponent<Renderer>().bounds.size.y;
        Vector3 nsize = Camera.main.WorldToScreenPoint(new Vector3(width, height, 0));
        nsize.x *= 2.0f;
        //nsize.x *= 1.33f;
        nsize.y *= 2.0f;
        //nsize.y *= 1.33f;

        tex = new RenderTexture((int)nsize.x, (int)nsize.y, 1);
        tex.useMipMap = false;
        tex.filterMode = FilterMode.Point;
        tex.antiAliasing = 1;
        tex.Create();

        cam = GetComponent<Camera>();
        float previousOrthoSize = cam.orthographicSize;
        float previousAspect = cam.aspect;
        RenderTexture previousRenderTexture = cam.targetTexture;

        Texture2D image = new Texture2D((int)nsize.x - 50, (int)nsize.y);
        cam.targetTexture = tex;
        float scale = 1.0f / (cam.orthographicSize / GameObject.Find("background-1").GetComponent<Renderer>().bounds.size.y / 2.0f);
        cam.orthographicSize = scale;
        cam.aspect = GameObject.Find("background-1").GetComponent<Renderer>().bounds.size.x / GameObject.Find("background-1").GetComponent<Renderer>().bounds.size.y;

        RenderTexture past = RenderTexture.active;
        RenderTexture.active = tex;

        cam.Render();

        image.ReadPixels(new Rect(25, 0, (int)nsize.x - 50, (int)nsize.y), 0, 0);
        image.Apply();

        cam.targetTexture = previousRenderTexture;
        cam.orthographicSize = previousOrthoSize;
        cam.aspect = previousAspect;
        RenderTexture.active = past;
        tex.Release();

        enhancedBackgroundImages.Add(Sprite.Create(image, new Rect(0, 0, image.width, image.height), new Vector2(0.5f, 0.5f)));
    }
예제 #10
0
		private void UpdateFaceTexture() {
			if(_targetCube == null) return;
			if(faceTexture == null || faceTexture.width != _targetCube.width) {
				if(faceTexture) Texture2D.DestroyImmediate(faceTexture);
				faceTexture = new Texture2D(_targetCube.width, _targetCube.width, TextureFormat.ARGB32, true, false);
				
				//attempt to make an HDR render texture for RGBM capture
				RT = RenderTexture.GetTemporary(_targetCube.width, _targetCube.width, 24, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
				RT.Release();
				RT.isCubemap = false;
				RT.useMipMap = false;
				RT.generateMips = false;
				RT.Create();
				if(!RT.IsCreated() && !RT.Create()) {
					Debug.LogWarning("Failed to create HDR RenderTexture, capturing in LDR mode.");
					RenderTexture.ReleaseTemporary(RT);
					RT = null;
				}		
			}
		}
예제 #11
0
	void SafeDestroyRenderTexture( ref RenderTexture rt )
	{
		if ( rt != null )
		{
			RenderTexture.active = null;
			rt.Release();
			DestroyImmediate( rt );
			rt = null;
		}
	}
예제 #12
0
 public static void ReleaseAndDestroy(RenderTexture tex)
 {
     if (tex == null)
     {
         return;
     }
     tex.Release();
     UnityEngine.Object.Destroy(tex);
 }
예제 #13
0
    void InitRenderTexture(ref RenderTexture rt, int width, int height, int depth, RenderTextureFormat format, bool temp = true)
    {
        if (temp)
        {
            rt = RenderTexture.GetTemporary(width, height, depth, format);
        }
        else
        {
            if (rt != null)
            {
                if (rt.width == width && rt.height == height && rt.depth == depth && rt.format == format)
                    return;

                rt.Release();
                DestroyImmediate(rt);
            }

            rt = new RenderTexture(width, height, depth, format);
            rt.hideFlags = HideFlags.HideAndDontSave;
        }
    }
예제 #14
0
		public bool capture(Texture targetCube, Vector3 position, Quaternion rotation, bool HDR, bool linear, bool convolve) {
			if(targetCube == null) return false;

			bool tempRT = false;
			if(cubeRT == null) {
				tempRT = true;

				//everything's captured to an HDR buffer right now
				cubeRT = RenderTexture.GetTemporary(targetCube.width, targetCube.width, 24, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
				cubeRT.Release();
				cubeRT.isCubemap = true;
				cubeRT.useMipMap = true;
				cubeRT.generateMips = true;
				cubeRT.Create();
				if(!cubeRT.IsCreated() && !cubeRT.Create()) {
					cubeRT = RenderTexture.GetTemporary(targetCube.width, targetCube.width, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
					cubeRT.Release();
					cubeRT.isCubemap = true;
					cubeRT.useMipMap = true;
					cubeRT.generateMips = true;
					cubeRT.Create();
				}
			}

			if(!cubeRT.IsCreated() && !cubeRT.Create()) return false;

			GameObject go = null;
			Camera cam = null;
			go = new GameObject("_temp_probe");
			cam = go.AddComponent<Camera>();

			mset.SkyManager mgr = mset.SkyManager.Get();
			if(mgr && mgr.ProbeCamera) {
				cam.CopyFrom (mgr.ProbeCamera);
			} else if(Camera.main) {
				cam.CopyFrom (Camera.main);
			}
			cam.renderingPath = renderPath;
			cam.useOcclusionCulling = false;
			cam.hdr = true;

			go.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideAndDontSave;
			go.SetActive(true);			
			go.transform.position = position;

			//capture entire scene in HDR
			Shader.SetGlobalVector("_UniformOcclusion", this.exposures);
			cam.RenderToCubemap(cubeRT);
			Shader.SetGlobalVector("_UniformOcclusion", Vector4.one);

			Matrix4x4 matrix = Matrix4x4.identity;
			matrix.SetTRS(position, rotation, Vector3.one);

			//render cubeRT converting it to RGBM
			Material skyMat = new Material(Shader.Find("Hidden/Marmoset/RGBM Cube"));

			bool dstRGBM = HDR;
			bool srcRGBM = false;
			copy_internal(targetCube, cubeRT, dstRGBM, srcRGBM, linear, cam, skyMat, matrix);
			if( convolve ) {
				convolve_internal(targetCube, cubeRT, HDR, false, linear, cam, skyMat, matrix);
			}

			//make sure the old sky and matrix vars are bound again
			if(mgr) mgr.GlobalSky = mgr.GlobalSky;
			Material.DestroyImmediate(skyMat);

			GameObject.DestroyImmediate(go);

		#if UNITY_5
			if(tempRT) GameObject.DestroyImmediate(cubeRT);
		#else
			if(tempRT) RenderTexture.ReleaseTemporary(cubeRT);
		#endif
			return true;
		}
예제 #15
0
        protected override IEnumerator workerMethod()
        {
			var textureMerge = umaGenerator.textureMerge;
			for (int atlasIndex = umaData.generatedMaterials.materials.Count-1; atlasIndex >= 0; atlasIndex--)
            {
                var atlas = umaData.generatedMaterials.materials[atlasIndex];

                //Rendering Atlas
                int moduleCount = 0;

                //Process all necessary TextureModules
                for (int i = 0; i < atlas.materialFragments.Count; i++)
                {
                    if (!atlas.materialFragments[i].isRectShared)
                    {
                        moduleCount++;
                        moduleCount = moduleCount + atlas.materialFragments[i].overlays.Length;
                    }
                }
				textureMerge.EnsureCapacity(moduleCount);

                var slotData = atlas.materialFragments[0].slotData;
				resultingTextures = new Texture[slotData.asset.material.channels.Length];
				for (int textureType = slotData.asset.material.channels.Length - 1; textureType >= 0; textureType--)
                {
					switch(slotData.asset.material.channels[textureType].channelType )
					{
						case UMAMaterial.ChannelType.Texture:
						case UMAMaterial.ChannelType.NormalMap:
						{
							textureMerge.Reset();
							for (int i = 0; i < atlas.materialFragments.Count; i++)
							{
								textureMerge.SetupModule(atlas, i, textureType);
							}

							//last element for this textureType
							moduleCount = 0;

							umaGenerator.textureMerge.gameObject.SetActive(true);

							int width = Mathf.FloorToInt(atlas.cropResolution.x);
							int height = Mathf.FloorToInt(atlas.cropResolution.y);
							destinationTexture = new RenderTexture(Mathf.FloorToInt(atlas.cropResolution.x * umaData.atlasResolutionScale), Mathf.FloorToInt(atlas.cropResolution.y * umaData.atlasResolutionScale), 0, slotData.asset.material.channels[textureType].textureFormat, RenderTextureReadWrite.Linear);
							destinationTexture.filterMode = FilterMode.Point;
							destinationTexture.useMipMap = umaGenerator.convertMipMaps && !umaGenerator.convertRenderTexture;
							renderCamera = umaGenerator.textureMerge.myCamera;
							renderCamera.targetTexture = destinationTexture;
							renderCamera.orthographicSize = height >> 1;
							var camTransform = renderCamera.GetComponent<Transform>();
							camTransform.localPosition = new Vector3(width >> 1, height >> 1, 3);
							camTransform.localRotation = Quaternion.Euler(0, 180, 180);
							renderCamera.Render();
							renderCamera.gameObject.SetActive(false);
							renderCamera.targetTexture = null;

							if (umaGenerator.convertRenderTexture)
							{
								#region Convert Render Textures
								yield return 25;
								Texture2D tempTexture;
								tempTexture = new Texture2D(destinationTexture.width, destinationTexture.height, TextureFormat.ARGB32, umaGenerator.convertMipMaps);
								int xblocks = destinationTexture.width / 512;
								int yblocks = destinationTexture.height / 512;
								if (xblocks == 0 || yblocks == 0)
								{
									RenderTexture.active = destinationTexture;
									tempTexture.ReadPixels(new Rect(0, 0, destinationTexture.width, destinationTexture.height), 0, 0, umaGenerator.convertMipMaps);
									RenderTexture.active = null;
								}
								else
								{
									// figures that ReadPixels works differently on OpenGL and DirectX, someday this code will break because Unity fixes this bug!
									if (IsOpenGL())
									{
										for (int x = 0; x < xblocks; x++)
										{
											for (int y = 0; y < yblocks; y++)
											{
												RenderTexture.active = destinationTexture;
												tempTexture.ReadPixels(new Rect(x * 512, y * 512, 512, 512), x * 512, y * 512, umaGenerator.convertMipMaps);
												RenderTexture.active = null;
												yield return 8;
											}
										}
									}
									else
									{
										for (int x = 0; x < xblocks; x++)
										{
											for (int y = 0; y < yblocks; y++)
											{
												RenderTexture.active = destinationTexture;
												tempTexture.ReadPixels(new Rect(x * 512, destinationTexture.height - 512 - y * 512, 512, 512), x * 512, y * 512, umaGenerator.convertMipMaps);
												RenderTexture.active = null;
												yield return 8;
											}
										}
									}
								}
								resultingTextures[textureType] = tempTexture as Texture;

								renderCamera.targetTexture = null;
								RenderTexture.active = null;

								destinationTexture.Release();
								UnityEngine.GameObject.DestroyImmediate(destinationTexture);
								umaGenerator.textureMerge.gameObject.SetActive(false);
								yield return 6;
								tempTexture = resultingTextures[textureType] as Texture2D;
								tempTexture.Apply();
								tempTexture.wrapMode = TextureWrapMode.Repeat;
								tempTexture.filterMode = FilterMode.Bilinear;
								resultingTextures[textureType] = tempTexture;
								atlas.material.SetTexture(slotData.asset.material.channels[textureType].materialPropertyName, tempTexture);
							#endregion
							}
							else
							{
								destinationTexture.filterMode = FilterMode.Bilinear;
								destinationTexture.wrapMode = TextureWrapMode.Repeat;
								resultingTextures[textureType] = destinationTexture;
								atlas.material.SetTexture(slotData.asset.material.channels[textureType].materialPropertyName, destinationTexture);
							}
							umaGenerator.textureMerge.gameObject.SetActive(false);
							break;
						}
						case UMAMaterial.ChannelType.MaterialColor:
						{
							atlas.material.SetColor(slotData.asset.material.channels[textureType].materialPropertyName, atlas.materialFragments[0].baseColor);
							break;
						}
						case UMAMaterial.ChannelType.TintedTexture:
						{
							for (int i = 0; i < atlas.materialFragments.Count; i++)
							{
								var fragment = atlas.materialFragments[i];
								if (fragment.isRectShared) continue;
								for (int j = 0; j < fragment.baseTexture.Length; j++)
								{
									if (fragment.baseTexture[j] != null)
									{
										atlas.material.SetTexture(slotData.asset.material.channels[j].materialPropertyName, fragment.baseTexture[j]);
										if (j == 0)
										{
											atlas.material.color = fragment.baseColor;
										}
									}
								}
								foreach (var overlay in fragment.overlays)
								{
									for (int j = 0; j < overlay.textureList.Length; j++)
									{
										if (overlay.textureList[j] != null)
										{
											atlas.material.SetTexture(slotData.asset.material.channels[j].materialPropertyName, overlay.textureList[j]);
										}
									}
								}
							}
							break;
						}
					}
                }
                atlas.resultingAtlasList = resultingTextures;
            }
        }
    /// <summary>
    /// キャプチャしたイメージを Texture2D として取得します。
    /// </summary>
    /// <returns>
    /// キャプチャされたイメージが与えられた Texture2D 。
    /// </returns>
    private Texture2D GenerateCaptureImage()
    {
        Camera fixedCamera;

        if (this.camera == null)
        {
            fixedCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
        }
        else
        {
            fixedCamera = this.camera;
        }

        int fixedWidth = this.imageWidth;
        int fixedHiehgt = this.imageHeight;
        int bit = 32;

        int[] gameViewResolution = GetGameViewResolution();

        if (fixedWidth == 0)
        {
            fixedWidth = gameViewResolution[0];
        }

        if (fixedHiehgt == 0)
        {
            fixedHiehgt = gameViewResolution[1];
        }

        fixedWidth *= this.imageScale;
        fixedHiehgt *= this.imageScale;

        Color presetBackgroundColor = fixedCamera.backgroundColor;
        CameraClearFlags presetClearFlags = fixedCamera.clearFlags;

        if (this.enableBackgroundAlpha)
        {
            fixedCamera.backgroundColor = Color.clear;
            fixedCamera.clearFlags = CameraClearFlags.SolidColor;
        }

        RenderTexture presetRenderTexture = fixedCamera.targetTexture;

        // カメラに出力用の RenderTexture を設定してレンダリングを実行し、
        // その情報を Texture2D に保存して返す。

        RenderTexture outputRenderTexture
            = new RenderTexture(fixedWidth, fixedHiehgt, bit);
        fixedCamera.targetTexture = outputRenderTexture;

        Texture2D captureImage = new Texture2D(fixedWidth,
                                               fixedHiehgt,
                                               TextureFormat.ARGB32,
                                               false);

        fixedCamera.Render();

        RenderTexture.active = outputRenderTexture;

        captureImage.ReadPixels
            (new Rect(0, 0, fixedWidth, fixedHiehgt), 0, 0);

        // 設定を元に戻します。

        fixedCamera.backgroundColor = presetBackgroundColor;
        fixedCamera.clearFlags = presetClearFlags;
        fixedCamera.targetTexture = presetRenderTexture;

        // 解放してから終了します。

        RenderTexture.active = null;

        outputRenderTexture.Release();

        DestroyImmediate(outputRenderTexture);

        return captureImage;
    }
예제 #17
0
        /// <summary>
        /// Generates a texture containing the given graph's noise output.
        /// If this is being called very often, create a permanent render target and material and
        ///     use the other version of this method instead for much better performance.
        /// If an error occurred, outputs to the Unity debug console and returns "null".
        /// </summary>
        /// <param name="gradientRampName">The name of the gradient ramp texture param.</param>
        public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height,
												  Gradient gradientRamp,
												  TextureFormat format = TextureFormat.RGBAFloat)
        {
            //Generate a shader from the graph and have Unity compile it.
            string shaderPath = Path.Combine(Application.dataPath, "gpuNoiseShaderTemp.shader");
            Shader shader = SaveShader(g, shaderPath, "TempGPUNoiseShader", "_MyGradientRamp14123");
            if (shader == null)
            {
                return null;
            }

            //Generate a texture from the gradient.
            Texture2D myRamp = new Texture2D(1024, 1, TextureFormat.RGBA32, false);
            Color[] cols = new Color[myRamp.width];
            for (int i = 0; i < cols.Length; ++i)
                cols[i] = gradientRamp.Evaluate((float)i / (float)(cols.Length - 1));
            myRamp.SetPixels(cols);
            myRamp.Apply(false, true);

            //Render the shader's output into a render texture and copy the data to a Texture2D.
            RenderTexture target = new RenderTexture(width, height, 16, RenderTextureFormat.ARGBFloat);
            target.Create();
            Texture2D resultTex = new Texture2D(width, height, format, false, true);

            //Create the material and set its parameters.
            Material mat = new Material(shader);
            mat.SetTexture("_MyGradientRamp14123", myRamp);
            c.SetParams(mat);

            GraphUtils.GenerateToTexture(target, mat, resultTex);

            //Clean up.
            target.Release();
            if (!AssetDatabase.DeleteAsset(StringUtils.GetRelativePath(shaderPath, "Assets")))
            {
                Debug.LogError("Unable to delete temp file: " + shaderPath);
            }

            return resultTex;
        }
예제 #18
0
	//This will read the values in tex into data array. Data must be in the range 0 - 0.9999
	static public void ReadFromRenderTexture(RenderTexture tex, int channels, float[] data)
	{
		if(tex == null)
		{
			Debug.Log("EncodeFloat::ReadFromRenderTexture - RenderTexture is null");
			return;
		}
		
		if(data == null)
		{
			Debug.Log("EncodeFloat::ReadFromRenderTexture - Data is null");
			return;
		}
		
		if(channels < 1 || channels > 4)
		{
			Debug.Log("EncodeFloat::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4");
			return;
		}
		
		if(m_encodeToFloat == null)
		{
			Shader shader = Shader.Find("EncodeFloat/EncodeToFloat");
			
			if(shader == null)
			{
				Debug.Log("EncodeFloat::ReadFromRenderTexture - could not find shader EncodeFloat/EncodeToFloat. Did you change the shaders name?");
				return;
			}
			
			m_encodeToFloat = new Material(shader);
		}
		
		int w = tex.width;
		int h = tex.height;
		
		RenderTexture encodeTex = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
		encodeTex.filterMode = FilterMode.Point;
		Texture2D readTex = new Texture2D(w, h, TextureFormat.ARGB32, false, true);
		
		Vector4 factor = new Vector4(1.0f, 1.0f/255.0f, 1.0f/65025.0f, 1.0f/160581375.0f);
		
		for(int i = 0; i < channels; i++)
		{
			//enocde data in tex into encodeTex
			Graphics.Blit(tex, encodeTex, m_encodeToFloat, i);
			//Read encoded values into a normal texture where we can retrive them
			RenderTexture.active = encodeTex;
			readTex.ReadPixels(new Rect(0,0,w,h),0,0);
			readTex.Apply();
			RenderTexture.active = null;
			
			//decode each pixel in readTex into a single float for the current channel
			for(int x = 0; x < w; x++)
			{
				for(int y = 0; y < h; y++)
				{
					data[(x+y*w)*channels+i] = Vector4.Dot(readTex.GetPixel(x,y), factor);
				}
			}
		}
			encodeTex.Release ();
			UnityEngine.Object.Destroy (encodeTex);
			UnityEngine.Object.Destroy (readTex);
	}
예제 #19
0
        /// <summary>
        /// Generates a texture containing the given graph's noise output.
        /// If this is being called very often, create a permanent render target and material and
        ///     use the other version of this method instead for much better performance.
        /// If an error occurred, outputs to the Unity debug console and returns "null".
        /// </summary>
        /// <param name="outputComponents">
        /// The texture output.
        /// For example, pass "rgb" or "xyz" to output the noise into the red, green, and blue channels
        ///     but not the alpha channel.
        /// </param>
        /// <param name="defaultColor">
        /// The color (generally 0-1) of the color components which aren't set by the noise.
        /// </param>
        public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height,
												  string outputComponents, float defaultColor,
												  TextureFormat format = TextureFormat.RGBAFloat)
        {
            //Generate a shader from the graph and have Unity compile it.
            string shaderPath = Path.Combine(Application.dataPath, "gpuNoiseShaderTemp.shader");
            Shader shader = SaveShader(g, shaderPath, "TempGPUNoiseShader", outputComponents, defaultColor);
            if (shader == null)
            {
                return null;
            }

            //Render the shader's output into a render texture and copy the data to a Texture2D.
            RenderTexture target = new RenderTexture(width, height, 16, RenderTextureFormat.ARGBFloat);
            target.Create();
            Texture2D resultTex = new Texture2D(width, height, format, false, true);

            //Create the material and set its parameters.
            Material mat = new Material(shader);
            c.SetParams(mat);

            GraphUtils.GenerateToTexture(target, mat, resultTex);

            //Clean up.
            target.Release();
            if (!AssetDatabase.DeleteAsset(StringUtils.GetRelativePath(shaderPath, "Assets")))
            {
                Debug.LogError("Unable to delete temp file: " + shaderPath);
            }

            return resultTex;
        }
예제 #20
0
    void Update()
    {
        for (int i = 0; i < ripples.Count; i++) {
            Ripple tmp = ripples[i];
            tmp.positionScale.z *= tmp.scaleSpeed;
            tmp.positionScale.w *= tmp.scaleSpeed;
            tmp.rippleStrength *= strengthDecay;

            if(ripples[i].rippleStrength <= 0.1f)
            {
                ripples.Remove(ripples[i]);
            }else{
                ripples[i] = tmp;
            }
        }

        RenderTexture tmpTexture = new RenderTexture (rippleOutput.width, rippleOutput.height, rippleOutput.depth);

        Graphics.SetRenderTarget(rippleOutput);
        if (ripples.Count == 0) {
            Graphics.Blit(rippleOutput,rippleOutput,rippleRenderer.sharedMaterial,0);
        }
        //Graphics.Blit(rippleOutput,rippleOutput,renderer.sharedMaterial,1);

        //TODO: Push this over to computeshader to remove blit calls
        rippleRenderer.sharedMaterial.SetFloat("_Width", 128);
        rippleRenderer.sharedMaterial.SetFloat("_Height", 128);
        for (int i = 0; i < ripples.Count; ++i) {

            rippleRenderer.sharedMaterial.SetVector("_BaseRipplePosition", ripples[i].positionScale);
            rippleRenderer.sharedMaterial.SetFloat("_BaseRippleStrength", ripples[i].rippleStrength);

            Graphics.Blit(rippleOutput,rippleOutput,rippleRenderer.sharedMaterial,1);
        }

        tmpTexture.Release ();
    }
예제 #21
0
 public static void Release(RenderTexture tex)
 {
     if (tex == null)
     {
         return;
     }
     tex.Release();
 }
예제 #22
0
        protected override IEnumerator workerMethod()
        {
            for (int atlasIndex = 0; atlasIndex < umaData.atlasList.atlas.Count; atlasIndex++)
            {
                //Rendering Atlas
                int moduleCount = 0;

                //Process all necessary TextureModules
                for (int i = 0; i < umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; i++)
                {
                    if (!umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[i].isRectShared)
                    {
                        moduleCount++;
                        moduleCount = moduleCount + umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[i].source.overlays.Length;
                    }
                }

                while (umaGenerator.textureMerge.textureModuleList.Count < moduleCount)
                {
                    Transform tempModule = UnityEngine.Object.Instantiate(umaGenerator.textureMerge.textureModule, new Vector3(0, 0, 3), Quaternion.identity) as Transform;
                    tempModule.gameObject.renderer.sharedMaterial = UnityEngine.Object.Instantiate(umaGenerator.textureMerge.material) as Material;
                    umaGenerator.textureMerge.textureModuleList.Add(tempModule);
                }

                textureModuleList = umaGenerator.textureMerge.textureModuleList.ToArray();
                for (int i = 0; i < moduleCount; i++)
                {
                    textureModuleList[i].localEulerAngles = new Vector3(textureModuleList[i].localEulerAngles.x, 180.0f, textureModuleList[i].localEulerAngles.z);
                    textureModuleList[i].parent = umaGenerator.textureMerge.myTransform;
                    textureModuleList[i].name = "tempModule";
                    textureModuleList[i].gameObject.SetActive(true);
                }

                moduleCount = 0;

                resultingTextures = new Texture[umaGenerator.textureNameList.Length];
                Rect nullRect = new Rect(0, 0, 0, 0);

                for (int textureType = 0; textureType < umaGenerator.textureNameList.Length; textureType++)
                {

                    if (umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[0].source.materialSample.HasProperty(umaGenerator.textureNameList[textureType]))
                    {
                        for (int i = 0; i < umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions.Count; i++)
                        {
                            UMAData.AtlasMaterialDefinition atlasElement = umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[i];
                            resolutionScale = umaData.atlasList.atlas[atlasIndex].resolutionScale * umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[i].source.slotData.overlayScale;

                            Vector2 offsetAdjust = new Vector2(umaGenerator.atlasResolution / 1024, umaGenerator.atlasResolution / 1024);

                            if (!atlasElement.isRectShared)
                            {
                                if (textureType == 0)
                                {
                                    textureModuleList[moduleCount].localScale = new Vector3(atlasElement.atlasRegion.width / umaGenerator.atlasResolution, atlasElement.atlasRegion.height / umaGenerator.atlasResolution, 1);

                                    textureModuleList[moduleCount].localPosition = new Vector3(Mathf.Lerp(-1, 1, (offsetAdjust.x + atlasElement.atlasRegion.x + atlasElement.atlasRegion.width * 0.5f) / umaGenerator.atlasResolution),
                                    Mathf.Lerp(-1, 1, (offsetAdjust.y + atlasElement.atlasRegion.y + atlasElement.atlasRegion.height * 0.5f) / umaGenerator.atlasResolution), 3.0f);
                                }

                                //							Material tempMaterial = UnityEngine.Object.Instantiate(umaGenerator.textureMerge.material) as Material;
                                //							textureModuleList[moduleCount].renderer.material = tempMaterial;

                                if (atlasElement.source.baseTexture[textureType])
                                {
                                    atlasElement.source.baseTexture[textureType].filterMode = FilterMode.Point;
                                    atlasElement.source.baseTexture[0].filterMode = FilterMode.Point;
                                }
                                textureModuleList[moduleCount].renderer.sharedMaterial.SetTexture("_MainTex", atlasElement.source.baseTexture[textureType]);
                                textureModuleList[moduleCount].renderer.sharedMaterial.SetTexture("_ExtraTex", atlasElement.source.baseTexture[0]);
                                textureModuleList[moduleCount].renderer.sharedMaterial.SetColor("_Color", atlasElement.source.GetMultiplier(0, textureType));
                                textureModuleList[moduleCount].renderer.sharedMaterial.SetColor("_AdditiveColor", atlasElement.source.GetAdditive(0, textureType));
                                textureModuleList[moduleCount].name = atlasElement.source.baseTexture[textureType].name;

                                Transform tempModule = textureModuleList[moduleCount];
                                moduleCount++;

                                for (int i2 = 0; i2 < atlasElement.source.overlays.Length; i2++)
                                {

                                    if (atlasElement.source.rects[i2] != nullRect)
                                    {
                                        textureModuleList[moduleCount].localScale = new Vector3((atlasElement.source.rects[i2].width / umaGenerator.atlasResolution) * resolutionScale, (atlasElement.source.rects[i2].height / umaGenerator.atlasResolution) * resolutionScale, 1);
                                        textureModuleList[moduleCount].localPosition = new Vector3(Mathf.Lerp(-1, 1, (offsetAdjust.x + atlasElement.atlasRegion.x + atlasElement.source.rects[i2].x * resolutionScale + atlasElement.source.rects[i2].width * 0.5f * resolutionScale) / umaGenerator.atlasResolution),
                                        Mathf.Lerp(-1, 1, (offsetAdjust.y + atlasElement.atlasRegion.y + atlasElement.source.rects[i2].y * resolutionScale + atlasElement.source.rects[i2].height * 0.5f * resolutionScale) / umaGenerator.atlasResolution), tempModule.localPosition.z - 0.1f - 0.1f * i2);
                                    }
                                    else
                                    {
                                        textureModuleList[moduleCount].localScale = tempModule.localScale;
                                        textureModuleList[moduleCount].localPosition = new Vector3(tempModule.localPosition.x, tempModule.localPosition.y, tempModule.localPosition.z - 0.1f - 0.1f * i2);
                                    }

                                    //								Material tempGenMaterial = umaGenerator.textureMerge.GenerateMaterial(umaGenerator.textureMerge.material);
                                    //								textureModuleList[moduleCount].renderer.material = tempGenMaterial;

                                    atlasElement.source.overlays[i2].textureList[textureType].filterMode = FilterMode.Point;
                                    atlasElement.source.overlays[i2].textureList[0].filterMode = FilterMode.Point;

                                    textureModuleList[moduleCount].renderer.sharedMaterial.SetTexture("_MainTex", atlasElement.source.overlays[i2].textureList[textureType]);
                                    textureModuleList[moduleCount].renderer.sharedMaterial.SetTexture("_ExtraTex", atlasElement.source.overlays[i2].textureList[0]);
                                    textureModuleList[moduleCount].renderer.sharedMaterial.SetColor("_Color", atlasElement.source.GetMultiplier(i2 + 1, textureType));
                                    textureModuleList[moduleCount].renderer.sharedMaterial.SetColor("_AdditiveColor", atlasElement.source.GetAdditive(i2 + 1, textureType));

                                    textureModuleList[moduleCount].name = atlasElement.source.overlays[i2].textureList[textureType].name;

                                    moduleCount++;
                                }
                                //							yield return null;
                            }
                        }

                        //last element for this textureType
                        moduleCount = 0;

                        umaGenerator.textureMerge.gameObject.SetActive(true);

                        destinationTexture = new RenderTexture(Mathf.FloorToInt(umaData.atlasList.atlas[atlasIndex].cropResolution.x), Mathf.FloorToInt(umaData.atlasList.atlas[atlasIndex].cropResolution.y), 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
                        destinationTexture.filterMode = FilterMode.Point;
                        renderCamera = umaGenerator.textureMerge.myCamera;
                        Vector3 tempPosition = renderCamera.transform.position;

                        renderCamera.orthographicSize = umaData.atlasList.atlas[atlasIndex].cropResolution.y / umaGenerator.atlasResolution;
                        renderCamera.transform.position = tempPosition + (-Vector3.right * (1 - umaData.atlasList.atlas[atlasIndex].cropResolution.x / umaGenerator.atlasResolution)) + (-Vector3.up * (1 - renderCamera.orthographicSize));

                        renderCamera.targetTexture = destinationTexture;
                        renderCamera.Render();
                        renderCamera.transform.position = tempPosition;
                        renderCamera.active = false;
                        renderCamera.targetTexture = null;
                        yield return 25;

                        if (umaGenerator.convertRenderTexture)
                        {
                            Texture2D tempTexture;
                            tempTexture = new Texture2D(destinationTexture.width, destinationTexture.height, TextureFormat.ARGB32, umaGenerator.convertMipMaps);
                            int xblocks = destinationTexture.width / 512;
                            int yblocks = destinationTexture.height / 512;
                            if (xblocks == 0 || yblocks == 0)
                            {
                                RenderTexture.active = destinationTexture;
                                tempTexture.ReadPixels(new Rect(0, 0, destinationTexture.width, destinationTexture.height), 0, 0, umaGenerator.convertMipMaps);
                                RenderTexture.active = null;
                            }
                            else
                            {
                                // figures that ReadPixels works differently on OpenGL and DirectX, someday this code will break because Unity fixes this bug!
                                if (IsOpenGL())
                                {
                                    for (int x = 0; x < xblocks; x++)
                                    {
                                        for (int y = 0; y < yblocks; y++)
                                        {
                                            RenderTexture.active = destinationTexture;
                                            tempTexture.ReadPixels(new Rect(x * 512, y * 512, 512, 512), x * 512, y * 512, umaGenerator.convertMipMaps);
                                            RenderTexture.active = null;
                                            yield return 8;
                                        }
                                    }
                                }
                                else
                                {
                                    for (int x = 0; x < xblocks; x++)
                                    {
                                        for (int y = 0; y < yblocks; y++)
                                        {
                                            RenderTexture.active = destinationTexture;
                                            tempTexture.ReadPixels(new Rect(x * 512, destinationTexture.height - 512 - y * 512, 512, 512), x * 512, y * 512, umaGenerator.convertMipMaps);
                                            RenderTexture.active = null;
                                            yield return 8;
                                        }
                                    }
                                }
                            }
                            resultingTextures[textureType] = tempTexture as Texture;

                            renderCamera.targetTexture = null;
                            RenderTexture.active = null;

                            destinationTexture.Release();
                            UnityEngine.GameObject.DestroyImmediate(destinationTexture);
                            umaGenerator.textureMerge.gameObject.SetActive(false);
                            yield return 6;
                            tempTexture = resultingTextures[textureType] as Texture2D;
                            tempTexture.Apply();
                            resultingTextures[textureType] = tempTexture;
                        }
                        else
                        {
                            destinationTexture.filterMode = FilterMode.Bilinear;
                            resultingTextures[textureType] = destinationTexture;
                        }
                        umaGenerator.textureMerge.gameObject.SetActive(false);
                    }
                    else
                    {

                    }
                }

                for (int textureModuleIndex = 0; textureModuleIndex < textureModuleList.Length; textureModuleIndex++)
                {
                    textureModuleList[textureModuleIndex].gameObject.SetActive(false);
                    //				UnityEngine.Object.DestroyImmediate(textureModuleList[textureModuleIndex].gameObject.renderer.material);
                    //				UnityEngine.Object.DestroyImmediate(textureModuleList[textureModuleIndex].gameObject);
                }

                umaData.atlasList.atlas[atlasIndex].resultingAtlasList = resultingTextures;
                umaData.atlasList.atlas[atlasIndex].materialSample = UnityEngine.Object.Instantiate(umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[0].source.materialSample) as Material;
                umaData.atlasList.atlas[atlasIndex].materialSample.name = umaData.atlasList.atlas[atlasIndex].atlasMaterialDefinitions[0].source.materialSample.name;
                for (int finalTextureType = 0; finalTextureType < umaGenerator.textureNameList.Length; finalTextureType++)
                {
                    if (umaData.atlasList.atlas[atlasIndex].materialSample.HasProperty(umaGenerator.textureNameList[finalTextureType]))
                    {
                        umaData.atlasList.atlas[atlasIndex].materialSample.SetTexture(umaGenerator.textureNameList[finalTextureType], resultingTextures[finalTextureType]);
                    }
                }
            }
        }
예제 #23
0
 private void SafeDestroyRenderTexture(ref RenderTexture rt)
 {
     if (rt != null)
     {
         RenderTexture.active = null;
         rt.Release();
         UnityEngine.Object.DestroyImmediate(rt);
         rt = null;
     }
 }