/// <summary> /// /// </summary> public void ClearBuffers(WaveOverlayData data) { RTUtility.ClearColor(data.height, m_clearColor); RTUtility.ClearColor(data.normal, m_clearColor); RTUtility.ClearColor(data.foam, m_clearColor); RTUtility.ClearColor(data.clip, m_clearColor); }
public void ClearAll() { RTUtility.ClearColor(transmittanceT); RTUtility.ClearColor(irradianceT_Read); RTUtility.ClearColor(irradianceT_Write); }
private void Awake() { CreateBuffers(); HeightTexture = RTExtensions.CreateRTexture(QuadSettings.VerticesPerSideFull, 0, RenderTextureFormat.ARGB32); NormalTexture = RTExtensions.CreateRTexture(QuadSettings.VerticesPerSideFull, 0, RenderTextureFormat.ARGB32); RTUtility.ClearColor(new RenderTexture[] { HeightTexture, NormalTexture }); }
protected override void InitNode() { CreateBuffers(); HeightTexture = RTExtensions.CreateRTexture(QuadSettings.VerticesPerSideFull, 0, RenderTextureFormat.ARGB32); NormalTexture = RTExtensions.CreateRTexture(QuadSettings.VerticesPerSideFull, 0, RenderTextureFormat.ARGB32); RTUtility.ClearColor(new RenderTexture[] { HeightTexture, NormalTexture }); InitMaterial(); InitUniforms(QuadMaterial); }
void Start() { // Here we can get away with a lower resolution for the render texture as it doesn't need the full float flowMap = RTUtility.CreateRenderTextures(TEX_SIZE, RenderTextureFormat.ARGBHalf); waterMap = RTUtility.CreateRenderTextures(TEX_SIZE, RenderTextureFormat.RFloat); terrainMap = new RenderTexture(TEX_SIZE, TEX_SIZE, 0, RenderTextureFormat.RFloat) { wrapMode = TextureWrapMode.Clamp, filterMode = FilterMode.Point }; GenerateWorld(); // Clear all the render textures and generate the heightmap RTUtility.ClearColor(terrainMap); RTUtility.ClearColor(flowMap); RTUtility.ClearColor(waterMap); terrainMap = HeightmapGenerator.GenerateHeightMap(terrainMap, TERRAIN_HEIGHT, 8, 0.3f, 3f); }
private IEnumerator CalculateViewsRoutine(Action <List <string>, Dictionary <int, Texture2D> > callback) { yield return(Yielders.EndOfFrame); // Finish previous frame... List <string> viewsLines = new List <string>(); Dictionary <int, Texture2D> viewsBilboards = new Dictionary <int, Texture2D>(); int total = 2 * (N_VIEWS * N_VIEWS + N_VIEWS) + 1; int current = 0; double zmax = Math.Abs(Z); double zmin = -Math.Abs(Z); for (int i = -N_VIEWS; i <= N_VIEWS; i++) { for (int j = -N_VIEWS + Math.Abs(i); j <= N_VIEWS - Math.Abs(i); ++j) { double x = (i + j) / (double)N_VIEWS; double y = (j - i) / (double)N_VIEWS; double angle = 90.0 - Math.Max(Math.Abs(x), Math.Abs(y)) * 90.0; double alpha = x.EpsilonEquals(0.0, 0.00000001) && y.EpsilonEquals(0.0, 0.00000001) ? 0.0f : Math.Atan2(y, x) / Math.PI * 180.0; Matrix4x4d cameraToWorld = Matrix4x4d.RotateX(90) * Matrix4x4d.RotateX(angle); Matrix4x4d worldToCamera = cameraToWorld.Inverse(); Box3d b = new Box3d(); b = b.Enlarge((worldToCamera * new Vector4d(-1.0, -1.0, zmin, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(+1.0, -1.0, zmin, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(-1.0, +1.0, zmin, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(+1.0, +1.0, zmin, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(-1.0, -1.0, zmax, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(+1.0, -1.0, zmax, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(-1.0, +1.0, zmax, 1.0)).xyz); b = b.Enlarge((worldToCamera * new Vector4d(+1.0, +1.0, zmax, 1.0)).xyz); Matrix4x4d c2s = Matrix4x4d.Ortho(b.Max.x, b.Min.x, b.Max.y, b.Min.y, -2.0 * b.Max.z, -2.0 * b.Min.z + S); Matrix4x4d w2s = c2s * worldToCamera * Matrix4x4d.RotateZ(-90 - alpha); Vector3d dir = ((Matrix4x4d.RotateZ(90 + alpha) * cameraToWorld) * new Vector4d(0.0, 0.0, 1.0, 0.0)).xyz; ViewMaterial.SetTexture("colorSampler", TreeSampler); ViewMaterial.SetVector("dir", dir.ToVector3()); ViewMaterial.SetMatrix("worldToScreen", w2s.ToMatrix4x4()); Camera.main.projectionMatrix = w2s.ToMatrix4x4(); ViewMaterial.SetPass(0); Graphics.DrawMeshNow(PreProcessMesh, Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one)); ViewMaterial.SetPass(1); Graphics.DrawMeshNow(PreProcessMesh, Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one)); yield return(Yielders.EndOfFrame); // Finish this frame... var texture = new Texture2D(GRIDRES_VIEWS, GRIDRES_VIEWS, TextureFormat.RGBAFloat, false, true); texture.ReadPixels(new Rect(0, 0, GRIDRES_VIEWS, GRIDRES_VIEWS), 0, 0, false); texture.Apply(false); viewsBilboards.Add(current, texture); var view = i * (1 - Math.Abs(i)) + j + 2 * N_VIEWS * i + N_VIEWS * (N_VIEWS + 1); RTUtility.ClearColor(RenderTexture.active); current++; Debug.Log(string.Format("Precomputing Views Step {0} of {1} : View {2}", current, total, view)); viewsLines.Add(string.Format("{0}f,{1}f,{2}f,{3}f,{4}f,{5}f,{6}f,{7}f,{8}f,", (float)w2s.m[0, 0], (float)w2s.m[0, 1], (float)w2s.m[0, 2], (float)w2s.m[1, 0], (float)w2s.m[1, 1], (float)w2s.m[1, 2], (float)w2s.m[2, 0], (float)w2s.m[2, 1], (float)w2s.m[2, 2])); } } if (callback != null) { callback(viewsLines, viewsBilboards); } }
public override void Clear() { RTUtility.ClearColor(Texture); }
/// <summary> /// This function creates the elevations data and is called by the CreateTileTask when the task is run by the schedular /// The functions needs the tiles parent data to have already been created. If it has not the program will abort. /// </summary> public override void DoCreateTile(int level, int tx, int ty, List <Slot> slot) { GPUSlot gpuSlot = slot[0] as GPUSlot; int tileWidth = gpuSlot.Owner.TileSize; int b = Border * 2 + 1; int tileSize = tileWidth - b; GPUSlot parentGpuSlot = null; Tile parentTile = null; if (level > 0) { parentTile = FindTile(level - 1, tx / 2, ty / 2, false, true); if (parentTile != null) { parentGpuSlot = parentTile.GetSlot(0) as GPUSlot; } else { throw new MissingTileException("Find parent tile failed"); } } float rootQuadSize = (float)TerrainNode.Root.Length; Vector4 tileWSD = new Vector4(); tileWSD.x = tileWidth; tileWSD.y = rootQuadSize / (1 << level) / tileSize; tileWSD.z = (tileWidth - b) / (World.GridResolution - 1.0f); tileWSD.w = 0.0f; m_upsampleMat.SetVector(m_uniforms.tileWSD, tileWSD); if (level > 0) { RenderTexture tex = parentGpuSlot.Texture; m_upsampleMat.SetTexture(m_uniforms.coarseLevelSampler, tex); float dx = (tx % 2) * (tileSize / 2); float dy = (ty % 2) * (tileSize / 2); Vector4 coarseLevelOSL = new Vector4(dx / tex.width, dy / tex.height, 1.0f / tex.width, 0.0f); m_upsampleMat.SetVector(m_uniforms.coarseLevelOSL, coarseLevelOSL); } else { m_upsampleMat.SetVector(m_uniforms.coarseLevelOSL, new Vector4(-1.0f, -1.0f, -1.0f, -1.0f)); } if (m_residualProducer != null && m_residualProducer.HasTile(level, tx, ty)) { Tile residualTile = m_residualProducer.FindTile(level, tx, ty, false, true); CPUSlot <float> residualSlot = null; if (residualTile != null) { residualSlot = residualTile.GetSlot(0) as CPUSlot <float>; } else { throw new MissingTileException("Find residual tile failed"); } //Must clear residual tex before use or terrain will have artifacts at the seams. Not sure why. RTUtility.ClearColor(m_residualTex, Color.clear); m_residualBuffer.SetData(residualSlot.Data); CBUtility.WriteIntoRenderTexture(m_residualTex, 1, m_residualBuffer, World.WriteData); m_upsampleMat.SetTexture(m_uniforms.residualSampler, m_residualTex); m_upsampleMat.SetVector(m_uniforms.residualOSH, new Vector4(0.25f / tileWidth, 0.25f / tileWidth, 2.0f / tileWidth, 1.0f)); } else { m_upsampleMat.SetTexture(m_uniforms.residualSampler, null); m_upsampleMat.SetVector(m_uniforms.residualOSH, new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); } float rs = level < m_noiseAmp.Length ? m_noiseAmp[level] : 0.0f; int noiseL = 0; int face = TerrainNode.Face; if (rs != 0.0f) { if (face == 1) { int offset = 1 << level; int bottomB = m_noise.Noise2D(tx + 0.5f, ty + offset) > 0.0f ? 1 : 0; int rightB = (tx == offset - 1 ? m_noise.Noise2D(ty + offset + 0.5f, offset) : m_noise.Noise2D(tx + 1.0f, ty + offset + 0.5f)) > 0.0f ? 2 : 0; int topB = (ty == offset - 1 ? m_noise.Noise2D((3.0f * offset - 1.0f - tx) + 0.5f, offset) : m_noise.Noise2D(tx + 0.5f, ty + offset + 1.0f)) > 0.0f ? 4 : 0; int leftB = (tx == 0 ? m_noise.Noise2D((4.0f * offset - 1.0f - ty) + 0.5f, offset) : m_noise.Noise2D(tx, ty + offset + 0.5f)) > 0.0f ? 8 : 0; noiseL = bottomB + rightB + topB + leftB; } else if (face == 6) { int offset = 1 << level; int bottomB = (ty == 0 ? m_noise.Noise2D((3.0f * offset - 1.0f - tx) + 0.5f, 0) : m_noise.Noise2D(tx + 0.5f, ty - offset)) > 0.0f ? 1 : 0; int rightB = (tx == offset - 1.0f ? m_noise.Noise2D((2.0f * offset - 1.0f - ty) + 0.5f, 0) : m_noise.Noise2D(tx + 1.0f, ty - offset + 0.5f)) > 0.0f ? 2 : 0; int topB = m_noise.Noise2D(tx + 0.5f, ty - offset + 1.0f) > 0.0f ? 4 : 0; int leftB = (tx == 0 ? m_noise.Noise2D(3.0f * offset + ty + 0.5f, 0) : m_noise.Noise2D(tx, ty - offset + 0.5f)) > 0.0f ? 8 : 0; noiseL = bottomB + rightB + topB + leftB; } else { int offset = (1 << level) * (face - 2); int bottomB = m_noise.Noise2D(tx + offset + 0.5f, ty) > 0.0f ? 1 : 0; int rightB = m_noise.Noise2D((tx + offset + 1) % (4 << level), ty + 0.5f) > 0.0f ? 2 : 0; int topB = m_noise.Noise2D(tx + offset + 0.5f, ty + 1.0f) > 0.0f ? 4 : 0; int leftB = m_noise.Noise2D(tx + offset, ty + 0.5f) > 0.0f ? 8 : 0; noiseL = bottomB + rightB + topB + leftB; } } int[] noiseRs = new int[] { 0, 0, 1, 0, 2, 0, 1, 0, 3, 3, 1, 3, 2, 2, 1, 0 }; int noiseR = noiseRs[noiseL]; int[] noiseLs = new int[] { 0, 1, 1, 2, 1, 3, 2, 4, 1, 2, 3, 4, 2, 4, 4, 5 }; noiseL = noiseLs[noiseL]; m_upsampleMat.SetTexture(m_uniforms.noiseSampler, m_noiseTextures[noiseL]); m_upsampleMat.SetVector(m_uniforms.noiseUVLH, new Vector4(noiseR, (noiseR + 1) % 4, 0, rs)); Graphics.Blit(null, gpuSlot.Texture, m_upsampleMat); base.DoCreateTile(level, tx, ty, slot); }
/// <summary> /// This function creates the elevations data and is called by the <see cref="Tile.Tasks.CreateTileTask"/> when the task is run by the <see cref="Utilities.Schedular"/>. /// The functions needs the tiles parent data to have already been created. If it has not the program will abort. /// </summary> /// <param name="level"></param> /// <param name="tx"></param> /// <param name="ty"></param> /// <param name="slot"></param> public override void DoCreateTile(int level, int tx, int ty, List <TileStorage.Slot> slot) { var gpuSlot = slot[0] as GPUTileStorage.GPUSlot; if (gpuSlot == null) { throw new NullReferenceException("gpuSlot"); } var tileWidth = gpuSlot.Owner.TileSize; var tileSize = tileWidth - (1 + GetBorder() * 2); GPUTileStorage.GPUSlot parentGpuSlot = null; var upsample = level > 0; var parentTile = FindTile(level - 1, tx / 2, ty / 2, false, true); // TODO : Make it classwide... var residualTileSize = GetTileSize(0); var residualTexture = RTExtensions.CreateRTexture(residualTileSize, 0, RenderTextureFormat.RFloat, FilterMode.Point, TextureWrapMode.Clamp); var residualBuffer = new ComputeBuffer(residualTileSize * residualTileSize, sizeof(float)); if (ResidualProducer != null) { if (ResidualProducer.HasTile(level, tx, ty)) { if (ResidualProducer.IsGPUProducer) { GPUTileStorage.GPUSlot residualGpuSlot = null; var residualTile = ResidualProducer.FindTile(level, tx, ty, false, true); if (residualTile != null) { residualGpuSlot = residualTile.GetSlot(0) as GPUTileStorage.GPUSlot; } else { throw new MissingTileException("Find residual tile failed"); } if (residualGpuSlot == null) { throw new MissingTileException("Find parent tile failed"); } UpSampleMaterial.SetTexture("_ResidualSampler", residualGpuSlot.Texture); UpSampleMaterial.SetVector("_ResidualOSH", new Vector4(0.25f / (float)tileWidth, 0.25f / (float)tileWidth, 2.0f / (float)tileWidth, 1.0f)); } else { CPUTileStorage.CPUSlot <float> residualCPUSlot = null; var residualTile = ResidualProducer.FindTile(level, tx, ty, false, true); if (residualTile != null) { residualCPUSlot = residualTile.GetSlot(0) as CPUTileStorage.CPUSlot <float>; } else { throw new MissingTileException("Find residual tile failed"); } if (residualCPUSlot == null) { throw new MissingTileException("Find parent tile failed"); } residualBuffer.SetData(residualCPUSlot.Data); RTUtility.ClearColor(residualTexture); CBUtility.WriteIntoRenderTexture(residualTexture, CBUtility.Channels.R, residualBuffer, GodManager.Instance.WriteData); //RTUtility.SaveAs8bit(residualTileSize, residualTileSize, CBUtility.Channels.R, string.Format("Residual_{0}_{1}-{2}-{3}", TerrainNode.name, level, tx, ty), "/Resources/Preprocess/Textures/Debug/", residualCPUSlot.Data); UpSampleMaterial.SetTexture("_ResidualSampler", residualTexture); UpSampleMaterial.SetVector("_ResidualOSH", new Vector4(0.25f / (float)tileWidth, 0.25f / (float)tileWidth, 2.0f / (float)tileWidth, 1.0f)); } } else { UpSampleMaterial.SetTexture("_ResidualSampler", null); UpSampleMaterial.SetVector("_ResidualOSH", new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); } } else { UpSampleMaterial.SetTexture("_ResidualSampler", null); UpSampleMaterial.SetVector("_ResidualOSH", new Vector4(0.0f, 0.0f, 1.0f, 0.0f)); } if (upsample) { if (parentTile != null) { parentGpuSlot = parentTile.GetSlot(0) as GPUTileStorage.GPUSlot; } else { throw new MissingTileException(string.Format("Find parent tile failed! {0}:{1}-{2}", level - 1, tx / 2, ty / 2)); } } if (parentGpuSlot == null && upsample) { throw new NullReferenceException("parentGpuSlot"); } var rootQuadSize = TerrainNode.TerrainQuadRoot.Length; var tileWSD = Vector4.zero; tileWSD.x = (float)tileWidth; tileWSD.y = (float)rootQuadSize / (float)(1 << level) / (float)tileSize; tileWSD.z = (float)tileSize / (float)(TerrainNode.ParentBody.GridResolution - 1); tileWSD.w = 0.0f; var tileScreenSize = (0.5 + (float)GetBorder()) / (tileWSD.x - 1 - (float)GetBorder() * 2); var tileSD = new Vector2d(tileScreenSize, 1.0 + tileScreenSize * 2.0); UpSampleMaterial.SetVector("_TileWSD", tileWSD); UpSampleMaterial.SetVector("_TileSD", tileSD.ToVector2()); if (upsample) { var parentTexture = parentGpuSlot.Texture; var dx = (float)(tx % 2) * (float)(tileSize / 2.0f); var dy = (float)(ty % 2) * (float)(tileSize / 2.0f); var coarseLevelOSL = new Vector4(dx / (float)parentTexture.width, dy / (float)parentTexture.height, 1.0f / (float)parentTexture.width, 0.0f); UpSampleMaterial.SetTexture("_CoarseLevelSampler", parentTexture); UpSampleMaterial.SetVector("_CoarseLevelOSL", coarseLevelOSL); } else { UpSampleMaterial.SetTexture("_CoarseLevelSampler", null); UpSampleMaterial.SetVector("_CoarseLevelOSL", new Vector4(-1.0f, -1.0f, -1.0f, -1.0f)); } var rs = level < NoiseAmplitudes.Length ? NoiseAmplitudes[level] : 0.0f; var offset = new Vector4d(((double)tx / (1 << level) - 0.5) * rootQuadSize, ((double)ty / (1 << level) - 0.5) * rootQuadSize, rootQuadSize / (1 << level), TerrainNode.ParentBody.Size); UpSampleMaterial.SetFloat("_Amplitude", rs / (TerrainNode.ParentBody.Amplitude / 10.0f)); UpSampleMaterial.SetFloat("_Frequency", TerrainNode.ParentBody.Frequency * (1 << level)); UpSampleMaterial.SetVector("_Offset", offset.ToVector4()); UpSampleMaterial.SetMatrix("_LocalToWorld", TerrainNode.FaceToLocal.ToMatrix4x4()); if (TerrainNode.ParentBody.TCCPS != null) { TerrainNode.ParentBody.TCCPS.SetUniforms(UpSampleMaterial); } Graphics.Blit(null, gpuSlot.Texture, UpSampleMaterial); residualTexture.ReleaseAndDestroy(); residualBuffer.ReleaseAndDisposeBuffer(); base.DoCreateTile(level, tx, ty, slot); }
void InitMaps() { RTUtility.ClearColor(m_terrainField); RTUtility.ClearColor(m_waterOutFlow); RTUtility.ClearColor(m_waterVelocity); RTUtility.ClearColor(m_advectSediment); RTUtility.ClearColor(m_waterField); RTUtility.ClearColor(m_sedimentField); RTUtility.ClearColor(m_regolithField); RTUtility.ClearColor(m_regolithOutFlow); RTUtility.ClearColor(m_lavaOutFlow); RTUtility.ClearColor(m_lavaVelocity); RTUtility.ClearColor(m_lavaField); RenderTexture[] noiseTex = new RenderTexture[2]; noiseTex[0] = new RenderTexture(TEX_SIZE, TEX_SIZE, 0, RenderTextureFormat.RFloat); noiseTex[0].wrapMode = TextureWrapMode.Clamp; noiseTex[0].filterMode = FilterMode.Bilinear; noiseTex[1] = new RenderTexture(TEX_SIZE, TEX_SIZE, 0, RenderTextureFormat.RFloat); noiseTex[1].wrapMode = TextureWrapMode.Clamp; noiseTex[1].filterMode = FilterMode.Bilinear; m_noiseMat.SetTexture("_PermTable1D", m_perlin.GetPermutationTable1D()); m_noiseMat.SetTexture("_Gradient2D", m_perlin.GetGradient2D()); for (int j = 0; j < 1; j++) { m_noiseMat.SetFloat("_Offset", m_offset[j]); float amp = 0.5f; float freq = m_frequency[j]; //Must clear noise from last pass RTUtility.ClearColor(noiseTex); //write noise into texture with the settings for this layer for (int i = 0; i < m_octaves[j]; i++) { m_noiseMat.SetFloat("_Frequency", freq); m_noiseMat.SetFloat("_Amp", amp); m_noiseMat.SetFloat("_Pass", (float)i); Graphics.Blit(noiseTex[READ], noiseTex[WRITE], m_noiseMat, (int)m_layerStyle[j]); RTUtility.Swap(noiseTex); freq *= m_lacunarity[j]; amp *= m_gain[j]; } float useAbs = 0.0f; if (m_finalNosieIsAbs[j]) { useAbs = 1.0f; } //Mask the layers that we dont want to write into Vector4 mask = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); mask[j] = 1.0f; m_initTerrainMat.SetFloat("_Amp", m_amp[j]); m_initTerrainMat.SetFloat("_UseAbs", useAbs); m_initTerrainMat.SetVector("_Mask", mask); m_initTerrainMat.SetTexture("_NoiseTex", noiseTex[READ]); m_initTerrainMat.SetFloat("_Height", TERRAIN_HEIGHT); //Apply the noise for this layer to the terrain field Graphics.Blit(m_terrainField[READ], m_terrainField[WRITE], m_initTerrainMat); RTUtility.Swap(m_terrainField); } //dont need this tex anymore noiseTex[0] = null; noiseTex[1] = null; /*Texture2D myTexture2D = new Texture2D(1024,1024,TextureFormat.RGBAFloat,false); * string path = "Assets\\Textures\\terrain.raw"; * myTexture2D.LoadRawTextureData(File.ReadAllBytes(path)); * myTexture2D.Apply(); * m_gridLand[0].AddComponent<GUITexture>(); * m_gridLand[0].GetComponent<GUITexture>().texture = myTexture2D; * Graphics.Blit(myTexture2D, m_terrainField[READ]);*/ }