/// <summary> /// Instantly update all cloth contraints center render pos. This is used to reduce jerkiness on new rope in its initial state /// </summary> public void setRenderCenterPos() { for (int i = 0; i < Constraints.Count; i++) { ClothConstraint cc = Constraints[i]; Vec3d start = cc.Point1.Pos; Vec3d end = cc.Point2.Pos; double nowx = start.X + (start.X - end.X) / 2; double nowy = start.Y + (start.Y - end.Y) / 2; double nowz = start.Z + (start.Z - end.Z) / 2; cc.renderCenterPos.X = nowx; cc.renderCenterPos.Y = nowy; cc.renderCenterPos.Z = nowz; } }
public int UpdateMesh(MeshData updateMesh, float dt) { var cfloats = updateMesh.CustomFloats; Vec3d campos = capi.World.Player.Entity.CameraPos; int basep = cfloats.Count; Vec4f lightRgba = api.World.BlockAccessor.GetLightRGBs(Constraints[Constraints.Count / 2].Point1.Pos.AsBlockPos); if (clothType == EnumClothType.Rope) { for (int i = 0; i < Constraints.Count; i++) { ClothConstraint cc = Constraints[i]; Vec3d start = cc.Point1.Pos; Vec3d end = cc.Point2.Pos; double dX = start.X - end.X; double dY = start.Y - end.Y; double dZ = start.Z - end.Z; float yaw = (float)Math.Atan2(dX, dZ) + GameMath.PIHALF; float pitch = (float)Math.Atan2(Math.Sqrt(dZ * dZ + dX * dX), dY) + GameMath.PIHALF; double nowx = start.X + (start.X - end.X) / 2; double nowy = start.Y + (start.Y - end.Y) / 2; double nowz = start.Z + (start.Z - end.Z) / 2; cc.renderCenterPos.X += (nowx - cc.renderCenterPos.X) * dt * 20; cc.renderCenterPos.Y += (nowy - cc.renderCenterPos.Y) * dt * 20; cc.renderCenterPos.Z += (nowz - cc.renderCenterPos.Z) * dt * 20; distToCam.Set( (float)(cc.renderCenterPos.X - campos.X), (float)(cc.renderCenterPos.Y - campos.Y), (float)(cc.renderCenterPos.Z - campos.Z) ); Mat4f.Identity(tmpMat); Mat4f.Translate(tmpMat, tmpMat, 0, 1 / 32f, 0); Mat4f.Translate(tmpMat, tmpMat, distToCam.X, distToCam.Y, distToCam.Z); Mat4f.RotateY(tmpMat, tmpMat, yaw); Mat4f.RotateZ(tmpMat, tmpMat, pitch); float roll = i / 5f; Mat4f.RotateX(tmpMat, tmpMat, roll); Mat4f.Scale(tmpMat, tmpMat, new float[] { (float)cc.SpringLength, 1, 1 }); // + (float)Math.Sin(api.World.ElapsedMilliseconds / 1000f) * 0.1f Mat4f.Translate(tmpMat, tmpMat, -1.5f, -1 / 32f, -0.5f); // not sure why the -1.5 here instead of -0.5 int j = basep + i * 20; cfloats.Values[j++] = lightRgba.R; cfloats.Values[j++] = lightRgba.G; cfloats.Values[j++] = lightRgba.B; cfloats.Values[j++] = lightRgba.A; for (int k = 0; k < 16; k++) { cfloats.Values[j + k] = tmpMat[k]; } } } return(Constraints.Count); }
private ClothSystem(ICoreAPI api, ClothManager cm, BlockPos originPos, float xsize, float zsize, EnumClothType clothType, AssetLocation ropeSectionModel = null) { this.clothType = clothType; this.ropeSectionModel = ropeSectionModel; Init(api, cm); Random rand = api.World.Rand; bool hor = rand.NextDouble() < 0.5; int vertexIndex = 0; float step = 1 / Resolution; int numzpoints = (int)Math.Round(zsize * Resolution); int numxpoints = (int)Math.Round(xsize * Resolution); if (clothType == EnumClothType.Rope) { numzpoints = 1; } float roughness = 0.05f; int k = 0; int pointIndex = 0; for (int z = 0; z < numzpoints; z++) { Points2d.Add(new PointList()); for (int x = 0; x < numxpoints; x++) { float dx = x * step; float dy = z * step; float dz = -roughness / 2 + (float)rand.NextDouble() * roughness; if (hor) { dx = x * step; dy = -roughness / 2 + (float)rand.NextDouble() * roughness; dz = z * step; } var point = new ClothPoint(this, pointIndex++, originPos.X + dx, originPos.Y + dy, originPos.Z + dz); Points2d[z].Points.Add(point); int color = (k++ % 2) > 0 ? ColorUtil.WhiteArgb : ColorUtil.BlackArgb; // add a vertical constraint if (z > 0) { ClothPoint p1 = Points2d[z - 1].Points[x]; ClothPoint p2 = Points2d[z].Points[x]; var constraint = new ClothConstraint(p1, p2); Constraints.Add(constraint); if (capi != null) { if (LineDebug) { debugUpdateMesh.AddVertex(0, 0, 0, color); debugUpdateMesh.AddVertex(0, 0, 0, color); debugUpdateMesh.AddIndex(vertexIndex++); debugUpdateMesh.AddIndex(vertexIndex++); } } } // add a new horizontal constraints if (x > 0) { ClothPoint p1 = Points2d[z].Points[x - 1]; ClothPoint p2 = Points2d[z].Points[x]; var constraint = new ClothConstraint(p1, p2); Constraints.Add(constraint); if (capi != null) { if (LineDebug) { debugUpdateMesh.AddVertex(0, 0, 0, color); debugUpdateMesh.AddVertex(0, 0, 0, color); debugUpdateMesh.AddIndex(vertexIndex++); debugUpdateMesh.AddIndex(vertexIndex++); } } } } } if (capi != null && LineDebug) { debugUpdateMesh.mode = EnumDrawMode.Lines; debugMeshRef = capi.Render.UploadMesh(debugUpdateMesh); debugUpdateMesh.Indices = null; debugUpdateMesh.Rgba = null; } }