void RenderHeldItem(bool isShadowPass) { IRenderAPI rapi = capi.Render; ItemStack stack = (entity as IEntityAgent).RightHandItemSlot?.Itemstack; BlendEntityAnimator bea = curAnimator as BlendEntityAnimator; AttachmentPointAndPose apap = null; bea.AttachmentPointByCode.TryGetValue("RightHand", out apap); if (apap == null || stack == null) { return; } AttachmentPoint ap = apap.AttachPoint; ItemRenderInfo renderInfo = rapi.GetItemStackRenderInfo(stack, EnumItemRenderTarget.HandTp); IStandardShaderProgram prog = null; EntityPlayer entityPlayer = capi.World.Player.Entity; ItemModelMat .Set(ModelMat) .Mul(apap.Pose.AnimModelMatrix) .Translate(renderInfo.Transform.Origin.X, renderInfo.Transform.Origin.Y, renderInfo.Transform.Origin.Z) .Scale(renderInfo.Transform.ScaleXYZ.X, renderInfo.Transform.ScaleXYZ.Y, renderInfo.Transform.ScaleXYZ.Z) .Translate(ap.PosX / 16f + renderInfo.Transform.Translation.X, ap.PosY / 16f + renderInfo.Transform.Translation.Y, ap.PosZ / 16f + renderInfo.Transform.Translation.Z) .RotateX((float)(ap.RotationX + renderInfo.Transform.Rotation.X) * GameMath.DEG2RAD) .RotateY((float)(ap.RotationY + renderInfo.Transform.Rotation.Y) * GameMath.DEG2RAD) .RotateZ((float)(ap.RotationZ + renderInfo.Transform.Rotation.Z) * GameMath.DEG2RAD) .Translate(-(renderInfo.Transform.Origin.X), -(renderInfo.Transform.Origin.Y), -(renderInfo.Transform.Origin.Z)) ; if (isShadowPass) { rapi.CurrentActiveShader.BindTexture2D("tex2d", renderInfo.TextureId, 0); float[] mvpMat = Mat4f.Mul(ItemModelMat.Values, capi.Render.CurrentModelviewMatrix, ItemModelMat.Values); Mat4f.Mul(mvpMat, capi.Render.CurrentProjectionMatrix, mvpMat); capi.Render.CurrentActiveShader.UniformMatrix("mvpMatrix", mvpMat); capi.Render.CurrentActiveShader.Uniform("origin", new Vec3f()); } else { prog = rapi.StandardShader; prog.Use(); prog.WaterWave = 0; prog.Tex2D = renderInfo.TextureId; prog.RgbaTint = ColorUtil.WhiteArgbVec; BlockPos pos = entity.Pos.AsBlockPos; Vec4f lightrgbs = capi.World.BlockAccessor.GetLightRGBs(pos.X, pos.Y, pos.Z); int temp = (int)stack.Collectible.GetTemperature(capi.World, stack); float[] glowColor = ColorUtil.GetIncandescenceColorAsColor4f(temp); lightrgbs[0] += 2 * glowColor[0]; lightrgbs[1] += 2 * glowColor[1]; lightrgbs[2] += 2 * glowColor[2]; prog.ExtraGlow = GameMath.Clamp((temp - 500) / 3, 0, 255); prog.RgbaAmbientIn = rapi.AmbientColor; prog.RgbaLightIn = lightrgbs; prog.RgbaBlockIn = ColorUtil.WhiteArgbVec; prog.RgbaFogIn = rapi.FogColor; prog.FogMinIn = rapi.FogMin; prog.FogDensityIn = rapi.FogDensity; prog.ProjectionMatrix = rapi.CurrentProjectionMatrix; prog.ViewMatrix = rapi.CameraMatrixOriginf; prog.ModelMatrix = ItemModelMat.Values; } if (!renderInfo.CullFaces) { rapi.GlDisableCullFace(); } rapi.RenderMesh(renderInfo.ModelRef); if (!renderInfo.CullFaces) { rapi.GlEnableCullFace(); } if (!isShadowPass) { prog.Stop(); } }
private void RenderBackPack(EntityPlayer entity, EntityShapeRenderer rend, bool isShadowPass) { IRenderAPI rpi = api.Render; BlendEntityAnimator bea = rend.curAnimator as BlendEntityAnimator; AttachmentPointAndPose apap = null; bea.AttachmentPointByCode.TryGetValue("Back", out apap); if (apap == null || backPackMeshRef == null) { return; } for (int i = 0; i < 16; i++) { tmpMat[i] = rend.ModelMat[i]; } AttachmentPoint ap = apap.AttachPoint; float[] mat = apap.Pose.AnimModelMatrix; float[] orig = new float[16]; for (int i = 0; i < 16; i++) { orig[i] = (float)api.Render.CameraMatrixOrigin[i]; } if (!isShadowPass) { Mat4f.Mul(tmpMat, orig, tmpMat); } Mat4f.Mul(tmpMat, tmpMat, mat); IStandardShaderProgram prog = null; if (isShadowPass) { rpi.CurrentActiveShader.BindTexture2D("tex2d", backPackTextureId, 0); } else { prog = rpi.PreparedStandardShader((int)entity.Pos.X, (int)entity.Pos.Y, (int)entity.Pos.Z); prog.Tex2D = backPackTextureId; prog.AlphaTest = 0.01f; } Mat4f.Translate(tmpMat, tmpMat, backPackTransform.Origin.X, backPackTransform.Origin.Y, backPackTransform.Origin.Z); Mat4f.Scale(tmpMat, tmpMat, backPackTransform.Scale, backPackTransform.Scale, backPackTransform.Scale); Mat4f.Translate(tmpMat, tmpMat, (float)ap.PosX / 16f + backPackTransform.Translation.X, (float)ap.PosY / 16f + backPackTransform.Translation.Y, (float)ap.PosZ / 16f + backPackTransform.Translation.Z); Mat4f.RotateX(tmpMat, tmpMat, (float)(ap.RotationX + backPackTransform.Rotation.X) * GameMath.DEG2RAD); Mat4f.RotateY(tmpMat, tmpMat, (float)(ap.RotationY + backPackTransform.Rotation.Y) * GameMath.DEG2RAD); Mat4f.RotateZ(tmpMat, tmpMat, (float)(ap.RotationZ + backPackTransform.Rotation.Z) * GameMath.DEG2RAD); Mat4f.Translate(tmpMat, tmpMat, -(backPackTransform.Origin.X), -(backPackTransform.Origin.Y), -(backPackTransform.Origin.Z)); if (isShadowPass) { Mat4f.Mul(tmpMat, api.Render.CurrentShadowProjectionMatrix, tmpMat); api.Render.CurrentActiveShader.UniformMatrix("mvpMatrix", tmpMat); api.Render.CurrentActiveShader.Uniform("origin", rend.OriginPos); } else { prog.ModelViewMatrix = tmpMat; } api.Render.RenderMesh(backPackMeshRef); if (!isShadowPass) { prog.Stop(); } }
public void TesselateShape() { CompositeShape compositeShape = entity.Properties.Client.Shape; Shape entityShape = entity.Properties.Client.LoadedShape; if (entityShape == null) { return; } ShapeElement headElement = null; // Only for player entity for now if (entityShape.Elements != null && HeadControl) { headElement = FindHead(entityShape.Elements); } entityShape.ResolveAndLoadJoints(headElement); ITexPositionSource texSource = GetTextureSource(); MeshData meshdata; if (entity.Properties.Client.Shape.VoxelizeTexture) { int altTexNumber = entity.WatchedAttributes.GetInt("textureIndex", 0); TextureAtlasPosition pos = texSource["all"]; CompositeTexture tex = altTexNumber == 0 ? entity.Properties.Client.FirstTexture : entity.Properties.Client.FirstTexture.Alternates[altTexNumber - 1]; meshdata = capi.Tesselator.VoxelizeTexture(tex, capi.EntityTextureAtlas.Size, pos); for (int i = 0; i < meshdata.xyz.Length; i += 3) { meshdata.xyz[i] -= 0.125f; meshdata.xyz[i + 1] -= 0.5f; meshdata.xyz[i + 2] += 0.125f / 2; } curAnimator = new BlendEntityAnimator(entity, new Animation[0], new ShapeElement[0], new Dictionary <int, AnimationJoint>()); } else { string animDictkey = (entity.Code + entity.Properties.Client.Shape.Base.ToString()); try { capi.Tesselator.TesselateShapeWithJointIds("entity", entityShape, out meshdata, texSource, new Vec3f(), compositeShape.QuantityElements, compositeShape.SelectiveElements); } catch (Exception e) { capi.World.Logger.Fatal("Failed tesselating entity {0} with id {1}. Entity will probably be invisible!. The teselator threw {2}", entity.Code, entity.EntityId, e); curAnimator = new BlendEntityAnimator(entity, entityShape.Animations, entityShape.Elements, entityShape.JointsById, headElement); return; } // We cache animations because they are cpu intensive to calculate if (AnimationsByShape.ContainsKey(animDictkey)) { entityShape.Animations = AnimationsByShape[animDictkey]; } else { for (int i = 0; entityShape.Animations != null && i < entityShape.Animations.Length; i++) { entityShape.Animations[i].GenerateAllFrames(entityShape.Elements, entityShape.JointsById); } AnimationsByShape[animDictkey] = entityShape.Animations; } curAnimator = new BlendEntityAnimator(entity, entityShape.Animations, entityShape.Elements, entityShape.JointsById, headElement); } meshdata.Rgba2 = null; if (meshRefOpaque != null) { capi.Render.DeleteMesh(meshRefOpaque); meshRefOpaque = null; } if (meshRefOit != null) { capi.Render.DeleteMesh(meshRefOit); meshRefOit = null; } MeshData opaqueMesh = meshdata.Clone().Clear(); MeshData oitMesh = meshdata.Clone().Clear(); opaqueMesh.AddMeshData(meshdata, EnumChunkRenderPass.Opaque); oitMesh.AddMeshData(meshdata, EnumChunkRenderPass.Transparent); if (opaqueMesh.VerticesCount > 0) { meshRefOpaque = capi.Render.UploadMesh(opaqueMesh); } if (oitMesh.VerticesCount > 0) { meshRefOit = capi.Render.UploadMesh(oitMesh); } }