// main generation function called on to generate each point in the mesh // experiment with noise here! Vector3 GeneratePoint(float x, float y) { Vector3 position = new Vector3(x, 0.0f, y); float mountains = Noise.Ridged(position + offset1, 6, 0.0075f); mountains *= .5f; mountains += .5f; //float hills = Noise.Fractal(position + offset1, 5, 0.005f); float hills = Noise.Billow(position + offset1, 5, 0.005f); hills *= .25f; hills -= .25f; float blendNoise = Noise.Fractal(position + offset2, 5, 0.003f); float total = Noise.Blend(mountains, hills, blendNoise, -0.5f, 0.5f); total = (total + 1) / 2.0f; //Color c = grad.Evaluate(total); total = total * 0.8f; //float colnoise = (Noise.Billow(position, 4, 0.02f) + 1.0f) / 2.0f; //Color c = new Color(0.2f, 0.1f, colnoise); //c = new Color(.6f, .6f, .1f + colnoise / 3.0f); return(new Vector3(x, total * 60.0f, y)); //float xf = x * 0.04f + offset1.x; //float yf = y * 0.04f + offset1.y; //WorleySample ws = Noise.Worley3(xf, yf, 0, 2, DistanceFunction.EUCLIDIAN); //float h = 1.0f - (float)ws.F[0]; //Vector3 pos = new Vector3(x, h * 25.0f, y); //return new TerrainPoint(pos, grad.Evaluate(h)); }
protected override void ApplyAlpha(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if (this.Radius == 0f) { this.Radius = this.extents.x; } TextureData textureDatum = new TextureData(this.alphamap); Vector3 vector32 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, -this.Radius)); Vector3 vector33 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, -this.Radius)); Vector3 vector34 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, this.Radius)); Vector3 vector35 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, this.Radius)); TerrainMeta.AlphaMap.ForEachParallel(vector32, vector33, vector34, vector35, (int x, int z) => { float single = TerrainMeta.AlphaMap.Coordinate(z); Vector3 vector3 = new Vector3(TerrainMeta.DenormalizeX(TerrainMeta.AlphaMap.Coordinate(x)), 0f, TerrainMeta.DenormalizeZ(single)); Vector3 vector31 = worldToLocal.MultiplyPoint3x4(vector3) - this.offset; float single1 = Noise.Billow(vector3.x, vector3.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f); float single2 = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + single1, vector31.Magnitude2D()); if (single2 == 0f) { return; } float interpolatedVector = textureDatum.GetInterpolatedVector((vector31.x + this.extents.x) / this.size.x, (vector31.z + this.extents.z) / this.size.z).w; TerrainMeta.AlphaMap.SetAlpha(x, z, interpolatedVector, single2); }); }
public void AdjustTerrainHeight() { TerrainHeightMap heightMap = TerrainMeta.HeightMap; TerrainTopologyMap topologyMap = TerrainMeta.TopologyMap; float single6 = 1f; float randomScale = this.RandomScale; float outerPadding = this.OuterPadding; float innerPadding = this.InnerPadding; float outerFade = this.OuterFade; float innerFade = this.InnerFade; float terrainOffset = this.TerrainOffset * TerrainMeta.OneOverSize.y; float width = this.Width * 0.5f; Vector3 startPoint = this.Path.GetStartPoint(); Vector3 endPoint = this.Path.GetEndPoint(); Vector3 startTangent = this.Path.GetStartTangent(); Vector3 vector32 = PathList.rot90 * startTangent; Vector3 vector33 = startPoint - (vector32 * (width + outerPadding + outerFade)); Vector3 vector34 = startPoint + (vector32 * (width + outerPadding + outerFade)); float length = this.Path.Length + single6; for (float i = 0f; i < length; i += single6) { Vector3 vector35 = (this.Spline ? this.Path.GetPointCubicHermite(i) : this.Path.GetPoint(i)); float single7 = (startPoint - vector35).Magnitude2D(); float single8 = (endPoint - vector35).Magnitude2D(); float single9 = Mathf.InverseLerp(0f, width, Mathf.Min(single7, single8)); float single10 = Mathf.Lerp(width, width * randomScale, Noise.Billow(vector35.x, vector35.z, 2, 0.005f, 1f, 2f, 0.5f)); Vector3 vector36 = this.Path.GetTangent(i).XZ3D(); startTangent = vector36.normalized; vector32 = PathList.rot90 * startTangent; Ray ray = new Ray(vector35, startTangent); Vector3 vector37 = vector35 - (vector32 * (single10 + outerPadding + outerFade)); Vector3 vector38 = vector35 + (vector32 * (single10 + outerPadding + outerFade)); float single11 = TerrainMeta.NormalizeY(vector35.y); heightMap.ForEach(vector33, vector34, vector37, vector38, (int x, int z) => { float single = heightMap.Coordinate(x); float single1 = heightMap.Coordinate(z); if ((topologyMap.GetTopology(single, single1) & this.Topology) != 0) { return; } Vector3 vector3 = TerrainMeta.Denormalize(new Vector3(single, single11, single1)); Vector3 vector31 = ray.ClosestPoint(vector3); float single2 = (vector3 - vector31).Magnitude2D(); float single3 = Mathf.InverseLerp(single10 + outerPadding + outerFade, single10 + outerPadding, single2); float single4 = Mathf.InverseLerp(single10 - innerPadding, single10 - innerPadding - innerFade, single2); float single5 = TerrainMeta.NormalizeY(vector31.y); single3 = Mathf.SmoothStep(0f, 1f, single3); single4 = Mathf.SmoothStep(0f, 1f, single4); heightMap.SetHeight(x, z, single5 + terrainOffset * single4, single9 * single3); }); vector33 = vector37; vector34 = vector38; } }
protected override void ApplyBiome(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if (this.Radius == 0f) { this.Radius = this.extents.x; } bool flag = base.ShouldBiome(1); bool flag1 = base.ShouldBiome(2); bool flag2 = base.ShouldBiome(4); bool flag3 = base.ShouldBiome(8); if (!flag && !flag1 && !flag2 && !flag3) { return; } TextureData textureDatum = new TextureData(this.biomemap); Vector3 vector32 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, -this.Radius)); Vector3 vector33 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, -this.Radius)); Vector3 vector34 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, this.Radius)); Vector3 vector35 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, this.Radius)); TerrainMeta.BiomeMap.ForEachParallel(vector32, vector33, vector34, vector35, (int x, int z) => { float single = TerrainMeta.BiomeMap.Coordinate(z); Vector3 vector3 = new Vector3(TerrainMeta.DenormalizeX(TerrainMeta.BiomeMap.Coordinate(x)), 0f, TerrainMeta.DenormalizeZ(single)); Vector3 vector31 = worldToLocal.MultiplyPoint3x4(vector3) - this.offset; float single1 = Noise.Billow(vector3.x, vector3.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f); float single2 = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + single1, vector31.Magnitude2D()); if (single2 == 0f) { return; } Vector4 interpolatedVector = textureDatum.GetInterpolatedVector((vector31.x + this.extents.x) / this.size.x, (vector31.z + this.extents.z) / this.size.z); if (!flag) { interpolatedVector.x = 0f; } if (!flag1) { interpolatedVector.y = 0f; } if (!flag2) { interpolatedVector.z = 0f; } if (!flag3) { interpolatedVector.w = 0f; } TerrainMeta.BiomeMap.SetBiomeRaw(x, z, interpolatedVector, single2); }); }
//public RenderTexture density; public void Start() { int size = 64; volume = new Texture3D(size, size, size, TextureFormat.RFloat, false); volume.filterMode = FilterMode.Point; //volume.filterMode = FilterMode.Bilinear; volume.wrapMode = TextureWrapMode.Clamp; Color[] pixels = new Color[size * size * size]; int i = 0; for (int x = 0; x < size; ++x) { for (int y = 0; y < size; ++y) { for (int z = 0; z < size; ++z, ++i) { //return length(worldPos - origin) - radius; Vector3 wp = new Vector3(x, y, z); Vector3 or = new Vector3(32, 32, 32); float rad = 20.0f; float dist = Vector3.Magnitude(wp - or) - rad; dist += Noise.Billow(wp, 4, 0.01f) * 5.0f; // not really a true distance field this way (above might not be either but closer at least) // basically the worse the distance field the more work for raymarching i think //dist = Noise.Billow(wp, 4, 0.01f) * -1.0f; //dist = Mathf.Floor(dist); //dist = Mathf.Ceil(dist); pixels[i] = new Color(dist, 1.0f, 1.0f, 1.0f); } } } volume.SetPixels(pixels); volume.Apply(); // 64 MB. just a test //int dres = 256; //density = new RenderTexture(dres, dres, 0, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear) { // dimension = UnityEngine.Rendering.TextureDimension.Tex3D, // volumeDepth = dres, // wrapMode = TextureWrapMode.Clamp, // filterMode = FilterMode.Point, // enableRandomWrite = true, // autoGenerateMips = false //}; }
public void AdjustTerrainTopology() { if (this.Topology == 0) { return; } TerrainTopologyMap topologyMap = TerrainMeta.TopologyMap; float single3 = 1f; float randomScale = this.RandomScale; float outerPadding = this.OuterPadding; float innerPadding = this.InnerPadding; float width = this.Width * 0.5f; Vector3 startPoint = this.Path.GetStartPoint(); Vector3 endPoint = this.Path.GetEndPoint(); Vector3 startTangent = this.Path.GetStartTangent(); Vector3 vector31 = PathList.rot90 * startTangent; Vector3 vector32 = startPoint - (vector31 * (width + outerPadding)); Vector3 vector33 = startPoint + (vector31 * (width + outerPadding)); float length = this.Path.Length + single3; for (float i = 0f; i < length; i += single3) { Vector3 vector34 = (this.Spline ? this.Path.GetPointCubicHermite(i) : this.Path.GetPoint(i)); float single4 = (startPoint - vector34).Magnitude2D(); float single5 = (endPoint - vector34).Magnitude2D(); float single6 = Mathf.InverseLerp(0f, width, Mathf.Min(single4, single5)); float single7 = Mathf.Lerp(width, width * randomScale, Noise.Billow(vector34.x, vector34.z, 2, 0.005f, 1f, 2f, 0.5f)); Vector3 vector35 = this.Path.GetTangent(i).XZ3D(); startTangent = vector35.normalized; vector31 = PathList.rot90 * startTangent; Ray ray = new Ray(vector34, startTangent); Vector3 vector36 = vector34 - (vector31 * (single7 + outerPadding)); Vector3 vector37 = vector34 + (vector31 * (single7 + outerPadding)); float single8 = TerrainMeta.NormalizeY(vector34.y); topologyMap.ForEach(vector32, vector33, vector36, vector37, (int x, int z) => { float single = topologyMap.Coordinate(x); float single1 = topologyMap.Coordinate(z); Vector3 vector3 = TerrainMeta.Denormalize(new Vector3(single, single8, single1)); float single2 = (vector3 - ray.ClosestPoint(vector3)).Magnitude2D(); if (Mathf.InverseLerp(single7 + outerPadding, single7 - innerPadding, single2) * single6 > 0.3f) { topologyMap.SetTopology(x, z, this.Topology); } }); vector32 = vector36; vector33 = vector37; } }
protected override void ApplyHeight(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if (this.Radius == 0f) { this.Radius = this.extents.x; } bool flag = this.blendmap != null; Vector3 vector32 = localToWorld.MultiplyPoint3x4(Vector3.zero); TextureData textureDatum = new TextureData(this.heightmap); TextureData textureDatum1 = (flag ? new TextureData(this.blendmap) : new TextureData()); Vector3 vector33 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, -this.Radius)); Vector3 vector34 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, -this.Radius)); Vector3 vector35 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, this.Radius)); Vector3 vector36 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, this.Radius)); TerrainMeta.HeightMap.ForEachParallel(vector33, vector34, vector35, vector36, (int x, int z) => { float single = TerrainMeta.HeightMap.Coordinate(z); Vector3 vector3 = new Vector3(TerrainMeta.DenormalizeX(TerrainMeta.HeightMap.Coordinate(x)), 0f, TerrainMeta.DenormalizeZ(single)); Vector3 vector31 = worldToLocal.MultiplyPoint3x4(vector3) - this.offset; float interpolatedVector = 1f; if (!flag) { float single1 = Noise.Billow(vector3.x, vector3.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f); interpolatedVector = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + single1, vector31.Magnitude2D()); } else { interpolatedVector = textureDatum1.GetInterpolatedVector((vector31.x + this.extents.x) / this.size.x, (vector31.z + this.extents.z) / this.size.z).w; } if (interpolatedVector == 0f) { return; } float single2 = TerrainMeta.NormalizeY(vector32.y + this.offset.y + textureDatum.GetInterpolatedHalf((vector31.x + this.extents.x) / this.size.x, (vector31.z + this.extents.z) / this.size.z) * this.size.y); single2 = Mathf.SmoothStep(TerrainMeta.HeightMap.GetHeight01(x, z), single2, interpolatedVector); TerrainMeta.HeightMap.SetHeight(x, z, single2); }); }
void Awake() { volume = new Texture3D(resolution, resolution, resolution, TextureFormat.RFloat, false); volume.filterMode = FilterMode.Point; //volume.filterMode = FilterMode.Bilinear; volume.wrapMode = TextureWrapMode.Clamp; Color[] pixels = new Color[resolution * resolution * resolution]; int i = 0; for (int x = 0; x < resolution; ++x) { for (int y = 0; y < resolution; ++y) { for (int z = 0; z < resolution; ++z, ++i) { //return length(worldPos - origin) - radius; Vector3 wp = new Vector3(x, y, z); Vector3 or = new Vector3(32, 32, 32); float rad = 20.0f; float dist = Vector3.Magnitude(wp - or) - rad; dist += Noise.Billow(wp, 4, 0.01f) * 5.0f; // not really a true distance field this way (above might not be either but closer at least) // basically the worse the distance field the more work for raymarching i think //dist = Noise.Billow(wp, 4, 0.01f) * -1.0f; //dist = Mathf.Floor(dist); //dist = Mathf.Ceil(dist); pixels[i] = new Color(dist, 1.0f, 1.0f, 1.0f); } } } volume.SetPixels(pixels); volume.Apply(); }
protected override void ApplySplat(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if (this.Radius == 0f) { this.Radius = this.extents.x; } bool flag = base.ShouldSplat(1); bool flag1 = base.ShouldSplat(2); bool flag2 = base.ShouldSplat(4); bool flag3 = base.ShouldSplat(8); bool flag4 = base.ShouldSplat(16); bool flag5 = base.ShouldSplat(32); bool flag6 = base.ShouldSplat(64); bool flag7 = base.ShouldSplat(128); if (!flag && !flag1 && !flag2 && !flag3 && !flag4 && !flag5 && !flag6 && !flag7) { return; } TextureData textureDatum = new TextureData(this.splatmap0); TextureData textureDatum1 = new TextureData(this.splatmap1); Vector3 vector32 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, -this.Radius)); Vector3 vector33 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, -this.Radius)); Vector3 vector34 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(-this.Radius, 0f, this.Radius)); Vector3 vector35 = localToWorld.MultiplyPoint3x4(this.offset + new Vector3(this.Radius, 0f, this.Radius)); TerrainMeta.SplatMap.ForEachParallel(vector32, vector33, vector34, vector35, (int x, int z) => { GenerateCliffSplat.Process(x, z); float single = TerrainMeta.SplatMap.Coordinate(z); Vector3 vector3 = new Vector3(TerrainMeta.DenormalizeX(TerrainMeta.SplatMap.Coordinate(x)), 0f, TerrainMeta.DenormalizeZ(single)); Vector3 vector31 = worldToLocal.MultiplyPoint3x4(vector3) - this.offset; float single1 = Noise.Billow(vector3.x, vector3.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f); float single2 = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + single1, vector31.Magnitude2D()); if (single2 == 0f) { return; } Vector4 interpolatedVector = textureDatum.GetInterpolatedVector((vector31.x + this.extents.x) / this.size.x, (vector31.z + this.extents.z) / this.size.z); Vector4 vector4 = textureDatum1.GetInterpolatedVector((vector31.x + this.extents.x) / this.size.x, (vector31.z + this.extents.z) / this.size.z); if (!flag) { interpolatedVector.x = 0f; } if (!flag1) { interpolatedVector.y = 0f; } if (!flag2) { interpolatedVector.z = 0f; } if (!flag3) { interpolatedVector.w = 0f; } if (!flag4) { vector4.x = 0f; } if (!flag5) { vector4.y = 0f; } if (!flag6) { vector4.z = 0f; } if (!flag7) { vector4.w = 0f; } TerrainMeta.SplatMap.SetSplatRaw(x, z, interpolatedVector, vector4, single2); }); }
protected override void ApplySplat(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if ((double)this.Radius == 0.0) { this.Radius = (float)this.extents.x; } bool should0 = this.ShouldSplat(1); bool should1 = this.ShouldSplat(2); bool should2 = this.ShouldSplat(4); bool should3 = this.ShouldSplat(8); bool should4 = this.ShouldSplat(16); bool should5 = this.ShouldSplat(32); bool should6 = this.ShouldSplat(64); bool should7 = this.ShouldSplat(128); if (!should0 && !should1 && (!should2 && !should3) && (!should4 && !should5 && (!should6 && !should7))) { return; } TextureData splat0data = new TextureData(this.splatmap0); TextureData splat1data = new TextureData(this.splatmap1); TerrainMeta.SplatMap.ForEachParallel(((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, this.Radius))), (Action <int, int>)((x, z) => { GenerateCliffSplat.Process(x, z); float normZ = TerrainMeta.SplatMap.Coordinate(z); float normX = TerrainMeta.SplatMap.Coordinate(x); Vector3 vector3_1; ((Vector3) ref vector3_1).\u002Ector(TerrainMeta.DenormalizeX(normX), 0.0f, TerrainMeta.DenormalizeZ(normZ)); Vector3 vector3_2 = Vector3.op_Subtraction(((Matrix4x4) ref worldToLocal).MultiplyPoint3x4(vector3_1), this.offset); float opacity = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + Noise.Billow((float)vector3_1.x, (float)vector3_1.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f), Vector3Ex.Magnitude2D(vector3_2)); if ((double)opacity == 0.0) { return; } Vector4 interpolatedVector1 = splat0data.GetInterpolatedVector((float)((vector3_2.x + this.extents.x) / this.size.x), (float)((vector3_2.z + this.extents.z) / this.size.z)); Vector4 interpolatedVector2 = splat1data.GetInterpolatedVector((float)((vector3_2.x + this.extents.x) / this.size.x), (float)((vector3_2.z + this.extents.z) / this.size.z)); if (!should0) { interpolatedVector1.x = (__Null)0.0; } if (!should1) { interpolatedVector1.y = (__Null)0.0; } if (!should2) { interpolatedVector1.z = (__Null)0.0; } if (!should3) { interpolatedVector1.w = (__Null)0.0; } if (!should4) { interpolatedVector2.x = (__Null)0.0; } if (!should5) { interpolatedVector2.y = (__Null)0.0; } if (!should6) { interpolatedVector2.z = (__Null)0.0; } if (!should7) { interpolatedVector2.w = (__Null)0.0; } TerrainMeta.SplatMap.SetSplatRaw(x, z, interpolatedVector1, interpolatedVector2, opacity); })); }
protected override void ApplyHeight(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if ((double)this.Radius == 0.0) { this.Radius = (float)this.extents.x; } bool useBlendMap = Object.op_Inequality((Object)this.blendmap, (Object)null); Vector3 position = ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.get_zero()); TextureData heightdata = new TextureData(this.heightmap); TextureData blenddata = useBlendMap ? new TextureData(this.blendmap) : new TextureData(); TerrainMeta.HeightMap.ForEachParallel(((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, this.Radius))), (Action <int, int>)((x, z) => { float normZ = TerrainMeta.HeightMap.Coordinate(z); float normX = TerrainMeta.HeightMap.Coordinate(x); Vector3 vector3_1; ((Vector3) ref vector3_1).\u002Ector(TerrainMeta.DenormalizeX(normX), 0.0f, TerrainMeta.DenormalizeZ(normZ)); Vector3 vector3_2 = Vector3.op_Subtraction(((Matrix4x4) ref worldToLocal).MultiplyPoint3x4(vector3_1), this.offset); float num1 = !useBlendMap ? Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + Noise.Billow((float)vector3_1.x, (float)vector3_1.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f), Vector3Ex.Magnitude2D(vector3_2)) : (float)blenddata.GetInterpolatedVector((float)((vector3_2.x + this.extents.x) / this.size.x), (float)((vector3_2.z + this.extents.z) / this.size.z)).w; if ((double)num1 == 0.0) { return; } float num2 = TerrainMeta.NormalizeY((float)(position.y + this.offset.y + (double)heightdata.GetInterpolatedHalf((float)((vector3_2.x + this.extents.x) / this.size.x), (float)((vector3_2.z + this.extents.z) / this.size.z)) * this.size.y)); float height = Mathf.SmoothStep(TerrainMeta.HeightMap.GetHeight01(x, z), num2, num1); TerrainMeta.HeightMap.SetHeight(x, z, height); })); }
protected override void ApplyBiome(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if ((double)this.Radius == 0.0) { this.Radius = (float)this.extents.x; } bool should0 = this.ShouldBiome(1); bool should1 = this.ShouldBiome(2); bool should2 = this.ShouldBiome(4); bool should3 = this.ShouldBiome(8); if (!should0 && !should1 && (!should2 && !should3)) { return; } TextureData biomedata = new TextureData(this.biomemap); TerrainMeta.BiomeMap.ForEachParallel(((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, this.Radius))), (Action <int, int>)((x, z) => { float normZ = TerrainMeta.BiomeMap.Coordinate(z); float normX = TerrainMeta.BiomeMap.Coordinate(x); Vector3 vector3_1; ((Vector3) ref vector3_1).\u002Ector(TerrainMeta.DenormalizeX(normX), 0.0f, TerrainMeta.DenormalizeZ(normZ)); Vector3 vector3_2 = Vector3.op_Subtraction(((Matrix4x4) ref worldToLocal).MultiplyPoint3x4(vector3_1), this.offset); float opacity = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + Noise.Billow((float)vector3_1.x, (float)vector3_1.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f), Vector3Ex.Magnitude2D(vector3_2)); if ((double)opacity == 0.0) { return; } Vector4 interpolatedVector = biomedata.GetInterpolatedVector((float)((vector3_2.x + this.extents.x) / this.size.x), (float)((vector3_2.z + this.extents.z) / this.size.z)); if (!should0) { interpolatedVector.x = (__Null)0.0; } if (!should1) { interpolatedVector.y = (__Null)0.0; } if (!should2) { interpolatedVector.z = (__Null)0.0; } if (!should3) { interpolatedVector.w = (__Null)0.0; } TerrainMeta.BiomeMap.SetBiomeRaw(x, z, interpolatedVector, opacity); })); }
protected override void ApplyAlpha(Matrix4x4 localToWorld, Matrix4x4 worldToLocal) { if ((double)this.Radius == 0.0) { this.Radius = (float)this.extents.x; } TextureData alphadata = new TextureData(this.alphamap); TerrainMeta.AlphaMap.ForEachParallel(((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, -this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(-this.Radius, 0.0f, this.Radius))), ((Matrix4x4) ref localToWorld).MultiplyPoint3x4(Vector3.op_Addition(this.offset, new Vector3(this.Radius, 0.0f, this.Radius))), (Action <int, int>)((x, z) => { float normZ = TerrainMeta.AlphaMap.Coordinate(z); float normX = TerrainMeta.AlphaMap.Coordinate(x); Vector3 vector3_1; ((Vector3) ref vector3_1).\u002Ector(TerrainMeta.DenormalizeX(normX), 0.0f, TerrainMeta.DenormalizeZ(normZ)); Vector3 vector3_2 = Vector3.op_Subtraction(((Matrix4x4) ref worldToLocal).MultiplyPoint3x4(vector3_1), this.offset); float opacity = Mathf.InverseLerp(this.Radius, this.Radius - this.Fade + Noise.Billow((float)vector3_1.x, (float)vector3_1.z, 4, 0.005f, 0.25f * this.Fade, 2f, 0.5f), Vector3Ex.Magnitude2D(vector3_2)); if ((double)opacity == 0.0) { return; } float w = (float)alphadata.GetInterpolatedVector((float)((vector3_2.x + this.extents.x) / this.size.x), (float)((vector3_2.z + this.extents.z) / this.size.z)).w; TerrainMeta.AlphaMap.SetAlpha(x, z, w, opacity); })); }
public List <Mesh> CreateMesh() { List <Mesh> meshes = new List <Mesh>(); float single = 8f; float single1 = 64f; float randomScale = this.RandomScale; float meshOffset = this.MeshOffset; float width = this.Width * 0.5f; int length = (int)(this.Path.Length / single) * 2; int num = (int)(this.Path.Length / single) * 3; List <Vector3> vector3s = new List <Vector3>(length); List <Color> colors = new List <Color>(length); List <Vector2> vector2s = new List <Vector2>(length); List <Vector3> vector3s1 = new List <Vector3>(length); List <Vector4> vector4s = new List <Vector4>(length); List <int> nums = new List <int>(num); TerrainHeightMap heightMap = TerrainMeta.HeightMap; Vector2 vector2 = new Vector2(0f, 0f); Vector2 vector21 = new Vector2(1f, 0f); Vector3 vector3 = Vector3.zero; Vector3 vector31 = Vector3.zero; Vector3 vector32 = Vector3.zero; Vector3 vector33 = Vector3.zero; int num1 = -1; int num2 = -1; float length1 = this.Path.Length + single; for (float i = 0f; i < length1; i += single) { Vector3 vector34 = (this.Spline ? this.Path.GetPointCubicHermite(i) : this.Path.GetPoint(i)); float single2 = Mathf.Lerp(width, width * randomScale, Noise.Billow(vector34.x, vector34.z, 2, 0.005f, 1f, 2f, 0.5f)); Vector3 tangent = this.Path.GetTangent(i); Vector3 vector35 = tangent.XZ3D().normalized; Vector3 vector36 = PathList.rot90 * vector35; Vector4 vector4 = new Vector4(vector36.x, vector36.y, vector36.z, 1f); Vector3 vector37 = Vector3.Slerp(Vector3.Cross(tangent, vector36), Vector3.up, 0.1f); Vector3 vector38 = new Vector3(vector34.x - vector36.x * single2, 0f, vector34.z - vector36.z * single2) { y = Mathf.Min(vector34.y, heightMap.GetHeight(vector38)) + meshOffset }; Vector3 vector39 = new Vector3(vector34.x + vector36.x * single2, 0f, vector34.z + vector36.z * single2) { y = Mathf.Min(vector34.y, heightMap.GetHeight(vector39)) + meshOffset }; if (i != 0f) { float single3 = (vector34 - vector32).Magnitude2D() / (2f * single2); vector2.y += single3; vector21.y += single3; if (Vector3.Dot((vector38 - vector3).XZ3D(), vector33) <= 0f) { vector38 = vector3; } if (Vector3.Dot((vector39 - vector31).XZ3D(), vector33) <= 0f) { vector39 = vector31; } } Color color = (i <= 0f || i + single >= length1 ? new Color(1f, 1f, 1f, 0f) : new Color(1f, 1f, 1f, 1f)); vector2s.Add(vector2); colors.Add(color); vector3s.Add(vector38); vector3s1.Add(vector37); vector4s.Add(vector4); int count = vector3s.Count - 1; if (num1 != -1 && num2 != -1) { nums.Add(count); nums.Add(num2); nums.Add(num1); } num1 = count; vector3 = vector38; vector2s.Add(vector21); colors.Add(color); vector3s.Add(vector39); vector3s1.Add(vector37); vector4s.Add(vector4); int count1 = vector3s.Count - 1; if (num1 != -1 && num2 != -1) { nums.Add(count1); nums.Add(num2); nums.Add(num1); } num2 = count1; vector31 = vector39; vector32 = vector34; vector33 = vector35; if (vector3s.Count >= 100 && this.Path.Length - i > single1) { Mesh mesh = new Mesh(); mesh.SetVertices(vector3s); mesh.SetColors(colors); mesh.SetUVs(0, vector2s); mesh.SetTriangles(nums, 0); mesh.SetNormals(vector3s1); mesh.SetTangents(vector4s); meshes.Add(mesh); vector3s.Clear(); colors.Clear(); vector2s.Clear(); vector3s1.Clear(); vector4s.Clear(); nums.Clear(); num1 = -1; num2 = -1; i -= single; } } if (nums.Count > 0) { Mesh mesh1 = new Mesh(); mesh1.SetVertices(vector3s); mesh1.SetColors(colors); mesh1.SetUVs(0, vector2s); mesh1.SetTriangles(nums, 0); mesh1.SetNormals(vector3s1); mesh1.SetTangents(vector4s); meshes.Add(mesh1); } return(meshes); }
GameObject BuildChunk(Vec2 coord) { Vector3[] verts = new Vector3[(GRID_SIZE + 3) * (GRID_SIZE + 3)]; int[] tris = new int[(GRID_SIZE + 2) * (GRID_SIZE + 2) * 6]; //Vector2[] uvs = new Vector2[verts.Length]; GameObject go = new GameObject(string.Format("Chunk ({0},{1})", coord.x, coord.y)); // generate vertices and colors List <Vector3> kelpPoints = new List <Vector3>(); for (int i = 0, y = -1; y <= GRID_SIZE + 1; ++y) { for (int x = -1; x <= GRID_SIZE + 1; ++x, ++i) { float xf = (x + coord.x * GRID_SIZE - GRID_SIZE / 2.0f) * GRID_SCALE; float yf = (y + coord.y * GRID_SIZE - GRID_SIZE / 2.0f) * GRID_SCALE; Vector3 p = GeneratePoint(xf, yf); verts[i] = p; if (Noise.Billow(new Vector3(xf, yf, 0), 3, 0.02f) > -0.2f) { kelpPoints.Add(p); } if (Random.value < 0.02f) { bool isCoral = Random.value < 0.25f; if (isCoral) { GameObject pre = Instantiate(coralPrefab, p, Random.rotation, go.transform); MeshData data = MeshGenerator.GenerateIcosphere(3); for (int j = 0; j < data.vertices.Length; ++j) { data.vertices[j] *= 1.0f + Noise.Ridged(data.vertices[j], 3, Random.Range(0.5f, 1.0f)) * 0.5f; } pre.GetComponent <MeshFilter>().sharedMesh = data.CreateMesh(); pre.transform.localScale = Vector3.one * (2 + Random.value * 5); } else { GameObject pre = Instantiate(rockPrefab, p, Random.rotation, go.transform); MeshData data = MeshGenerator.GenerateIcosphere(1); for (int j = 0; j < data.vertices.Length; ++j) { data.vertices[j] *= 1.0f + Noise.Ridged(data.vertices[j], 3, Random.Range(0.3f, 0.8f)) * 0.3f; } pre.GetComponent <MeshFilter>().sharedMesh = data.CreateMesh(); pre.transform.localScale = Vector3.one * (1 + Random.value * 3); } } if (Random.value < 0.0025f) { GameObject g = Instantiate(recyclerPrefab, p + Vector3.up * 0.8f, Quaternion.identity, go.transform); } if (Random.value < 0.005f) { GameObject creature = Instantiate(creatures[Random.Range(0, creatures.Length)], p + Vector3.up * (2.0f + Random.value * 10.0f), Quaternion.identity, go.transform); } if (Random.value < 0.0015f) { GameObject trash = Instantiate(trashes[Random.Range(0, trashes.Length)], new Vector3(p.x, 70 - Random.Range(1.0f, 10.0f), p.z), Random.rotation, go.transform); } } } Mesh kelps = MeshGenerator.GenerateStrips(kelpPoints); GameObject kelpGo = Instantiate(kelpPrefab, Vector3.zero, Quaternion.identity, go.transform); kelpGo.GetComponent <MeshFilter>().sharedMesh = kelps; // generate triangles GenerateTriangles(tris, GRID_SIZE + 2); // generate the normals Vector3[] normals = new Vector3[verts.Length]; for (int i = 0; i < tris.Length / 3; ++i) { int a = tris[i * 3]; int b = tris[i * 3 + 1]; int c = tris[i * 3 + 2]; Vector3 va = verts[a]; Vector3 vb = verts[b]; Vector3 vc = verts[c]; Vector3 norm = Vector3.Cross(vb - va, vc - va); normals[a] += norm; normals[b] += norm; normals[c] += norm; } for (int i = 0; i < normals.Length; ++i) { normals[i].Normalize(); } // now slice off extra vert layer Vector3[] vs = new Vector3[(GRID_SIZE + 1) * (GRID_SIZE + 1)]; Vector3[] ns = new Vector3[vs.Length]; int[] ts = new int[GRID_SIZE * GRID_SIZE * 6]; for (int y = 0; y < GRID_SIZE + 1; ++y) { for (int x = 0; x < GRID_SIZE + 1; ++x) { int oi = x + y * (GRID_SIZE + 1); int ci = (x + 1) + (y + 1) * (GRID_SIZE + 3); vs[oi] = verts[ci]; ns[oi] = normals[ci]; } } GenerateTriangles(ts, GRID_SIZE); Mesh m = new Mesh(); m.vertices = vs; m.triangles = ts; m.normals = ns; go.transform.parent = transform; // build mesh collider from the shared mesh go.AddComponent <MeshCollider>().sharedMesh = m; m.RecalculateBounds(); //m.RecalculateNormals(); go.AddComponent <MeshFilter>().mesh = m; MeshRenderer mr = go.AddComponent <MeshRenderer>(); mr.material = mat; mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.TwoSided; return(go); }