/// <summary> /// Creates a new AlphaTestEffect by cloning parameter settings from an existing instance. /// </summary> protected AlphaTestEffect(AlphaTestEffect cloneSource) : base(cloneSource) { CacheEffectParameters(); fogEnabled = cloneSource.fogEnabled; vertexColorEnabled = cloneSource.vertexColorEnabled; world = cloneSource.world; view = cloneSource.view; projection = cloneSource.projection; diffuseColor = cloneSource.diffuseColor; alpha = cloneSource.alpha; fogStart = cloneSource.fogStart; fogEnd = cloneSource.fogEnd; alphaFunction = cloneSource.alphaFunction; referenceAlpha = cloneSource.referenceAlpha; }
void InitializeGraphicsDevice() { SpriteBatch = new SpriteBatch(graphicsDevice); defaultEffect = new BasicEffect(graphicsDevice); AlphaTestEffect = new AlphaTestEffect(graphicsDevice); PrimitiveEffect = new BasicEffect(graphicsDevice) { TextureEnabled = false, VertexColorEnabled = true }; depthEnableStencilState = new DepthStencilState { DepthBufferEnable = true, DepthBufferWriteEnable = true, TwoSidedStencilMode = true }; depthDisableStencilState = new DepthStencilState { DepthBufferEnable = false }; #if !WINDOWS_PHONE && !XBOX && !WINDOWS && !NETFX_CORE List <string> extensions = CCUtils.GetGLExtensions(); foreach (string s in extensions) { switch (s) { case "GL_OES_depth24": platformDepthFormat = CCDepthFormat.Depth24; break; case "GL_IMG_texture_npot": allowNonPower2Textures = true; break; case "GL_NV_depth_nonlinear": // nVidia Depth 16 non-linear platformDepthFormat = CCDepthFormat.Depth16; break; case "GL_NV_texture_npot_2D_mipmap": // nVidia - nPot textures and mipmaps allowNonPower2Textures = true; break; } } #endif projectionMatrix = Matrix.Identity; viewMatrix = Matrix.Identity; worldMatrix = Matrix.Identity; matrix = Matrix.Identity; worldMatrixChanged = viewMatrixChanged = projectionMatrixChanged = true; // Need to change DrawPrim to no longer be static !!! CCDrawingPrimitives.Initialize(graphicsDevice, this); graphicsDevice.Disposing += GraphicsDeviceDisposing; graphicsDevice.DeviceLost += GraphicsDeviceDeviceLost; graphicsDevice.DeviceReset += GraphicsDeviceDeviceReset; graphicsDevice.DeviceResetting += GraphicsDeviceDeviceResetting; graphicsDevice.ResourceCreated += GraphicsDeviceResourceCreated; graphicsDevice.ResourceDestroyed += GraphicsDeviceResourceDestroyed; }
public void DrawSky(GraphicsDeviceManager graphics, BasicEffect effect, AlphaTestEffect alpha) { normal.Draw(graphics, effect, alpha); }
public void Draw(GraphicsDeviceManager gcm, AlphaTestEffect at, BasicEffect be, RasterizerState rs) { if (Program.game.Collision < 1) return; List<VertexPositionNormalTexture> vpnt1 = new List<VertexPositionNormalTexture>(0); for (int i = 0; i < MainGame.ResourceFiles.Count + Program.game.Map.Supp.Count; i++) { Model mdl = null; if (i >= MainGame.ResourceFiles.Count) mdl = Program.game.Map.Supp[i - MainGame.ResourceFiles.Count] as Model; else mdl = MainGame.ResourceFiles[i] as Model; if (mdl == null) continue; if (mdl.Collision == null) continue; int start_ = mdl.Collision.StartEnds[0][0]; int end_ = mdl.Collision.StartEnds[0][1]; for (int j = start_; j < end_; j++) { Matrix mt = mdl.Skeleton.Bones[mdl.Skeleton.RootBone].LocalMatrix; if (Vector3.Distance(mdl.Location, Vector3.Zero) > 0) { mt = Matrix.Identity; } Vector3 v1 = Vector3.Transform( Vector3.Transform(mdl.Collision.cols[mdl.Collision.indices[j][0][0]], mt) , mdl.Rotate_matrix) + mdl.Location; Vector3 v2 = Vector3.Transform( Vector3.Transform(mdl.Collision.cols[mdl.Collision.indices[j][1][0]], mt), mdl.Rotate_matrix) + mdl.Location; Vector3 v3 = Vector3.Transform( Vector3.Transform(mdl.Collision.cols[mdl.Collision.indices[j][2][0]], mt), mdl.Rotate_matrix) + mdl.Location; var mt_mat = Matrix.CreateFromQuaternion(mt.Rotation); Vector3 n1 = Vector3.Transform(mdl.Collision.norms[mdl.Collision.indices[j][0][1]], mt_mat); Vector3 n2 = Vector3.Transform(mdl.Collision.norms[mdl.Collision.indices[j][1][1]], mt_mat); Vector3 n3 = Vector3.Transform(mdl.Collision.norms[mdl.Collision.indices[j][2][1]], mt_mat); VertexPositionNormalTexture vp1 = new VertexPositionNormalTexture(); VertexPositionNormalTexture vp2 = new VertexPositionNormalTexture(); VertexPositionNormalTexture vp3 = new VertexPositionNormalTexture(); vp1.Position = v1; vp2.Position = v2; vp3.Position = v3; vp1.Normal = n1; vp2.Normal = n2; vp3.Normal = n3; vp1.TextureCoordinate = new Vector2(0.5f, 0.5f); vp2.TextureCoordinate = new Vector2(0.5f, 0.5f); vp3.TextureCoordinate = new Vector2(0.5f, 0.5f); vpnt1.Add(vp3); vpnt1.Add(vp2); vpnt1.Add(vp1); } } VertexPositionNormalTexture[] vpnt = new VertexPositionNormalTexture[this.indices.Count*3]; Model target = Program.game.mainCamera.Target; if (target!=null) { UpdateIndex(target.Location); int start = this.StartEnds[this.CurrIndex][0]; int end = this.StartEnds[this.CurrIndex][1]; for (int i = start; i < end; i++) { for (int j = 0; j < 3; j++) { vpnt[i * 3 + j] = new VertexPositionNormalTexture { Position = this.cols[this.indices[i][2 - j][0]], TextureCoordinate = new Vector2(0.5f, 0.5f), Normal = this.norms[this.indices[i][j][1]] }; } } } be.EnableDefaultLighting(); be.LightingEnabled = true; be.PreferPerPixelLighting = true; //be.AmbientLightColor = Color.White.ToVector3(); be.DiffuseColor = new Color(100, 100, 100,255).ToVector3(); //be.EmissiveColor = Color.White.ToVector3(); be.Texture = ResourceLoader.EmptyT2D; be.CurrentTechnique.Passes[0].Apply(); be.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vpnt, 0, vpnt.Length / 3); vpnt = vpnt1.ToArray(); be.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vpnt, 0, vpnt.Length / 3); be.LightingEnabled = false; be.PreferPerPixelLighting = false; }
public void Draw() { Memory.graphics.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; Memory.graphics.GraphicsDevice.BlendState = BlendState.AlphaBlend; Memory.graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default; Memory.graphics.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; //Memory.graphics.GraphicsDevice.RasterizerState = RasterizerState.CullNone; using (AlphaTestEffect ate = new AlphaTestEffect(Memory.graphics.GraphicsDevice) { Projection = projectionMatrix, View = viewMatrix, World = worldMatrix, }) using (BasicEffect effect = new BasicEffect(Memory.graphics.GraphicsDevice) { TextureEnabled = true, }) { //var models = (from modelgroup in modelGroups // where (modelgroup?.Count ?? 0) > 0 // from model in modelgroup // where model.quads != null && model.triangles != null && model.vertices != null // select model); int[] order = new int[] { 3, 0, 1, 2 }; foreach (int n in order.Where(x => x < (modelGroups?.Count ?? 0))) { foreach (Model b in modelGroups[n]) { GeometryVertexPosition vpt = GetVertexBuffer(b); if (n == 3 && skyRotators[Memory.Encounters.Scenario] != 0) { CreateRotation(vpt); } if (vpt == null) { continue; } int localVertexIndex = 0; foreach (GeometryInfoSupplier gis in vpt.GeometryInfoSupplier.Where(x => !x.GPU.HasFlag(GPU.v2_add))) { Memory.graphics.GraphicsDevice.BlendState = BlendState.AlphaBlend; process(gis); } //BlendState bs = new BlendState //{ // //ColorWriteChannels = ColorWriteChannels.Blue | ColorWriteChannels.Green | ColorWriteChannels.Red, // ColorSourceBlend = Blend.One, // AlphaSourceBlend = Blend.One, // ColorDestinationBlend = Blend.InverseSourceColor, // AlphaDestinationBlend = Blend.One, // ColorBlendFunction = BlendFunction.Max //}; foreach (GeometryInfoSupplier gis in vpt.GeometryInfoSupplier.Where(x => x.GPU.HasFlag(GPU.v2_add))) { Memory.graphics.GraphicsDevice.BlendState = Memory.blendState_Add;//bs; process(gis); } // bs?.Dispose(); void process(GeometryInfoSupplier gis) { ate.Texture = (Texture2D)textures[gis.clut]; //provide texture per-face foreach (EffectPass pass in ate.CurrentTechnique.Passes) { pass.Apply(); if (gis.bQuad) { Memory.graphics.GraphicsDevice.DrawUserPrimitives(primitiveType: PrimitiveType.TriangleList, vertexData: vpt.VertexPositionTexture, vertexOffset: localVertexIndex, primitiveCount: 2); localVertexIndex += 6; } else { Memory.graphics.GraphicsDevice.DrawUserPrimitives(primitiveType: PrimitiveType.TriangleList, vertexData: vpt.VertexPositionTexture, vertexOffset: localVertexIndex, primitiveCount: 1); localVertexIndex += 3; } } } } } } }
public void Render(IRenderArgs args, PlayerLocation position, Matrix characterMatrix) { if (Buffer == null) { return; } args.GraphicsDevice.Indices = Buffer; int idx = 0; for (var index = 0; index < Parts.Length; index++) { var part = Parts[index]; AlphaTestEffect effect = part.Effect; if (effect == null) { continue; } var headYaw = part.ApplyHeadYaw ? MathUtils.ToRadians(-(position.HeadYaw - position.Yaw)) : 0f; var pitch = part.ApplyPitch ? MathUtils.ToRadians(position.Pitch) : 0f; var rot = _rotation + part.Rotation; /*Matrix rotMatrix = Matrix.CreateTranslation(-part.Pivot) * Matrix.CreateFromYawPitchRoll( * MathUtils.ToRadians(rot.Y), * MathUtils.ToRadians(rot.X), * MathUtils.ToRadians(rot.Z) * ); * rotMatrix *= Matrix.CreateTranslation(part.Pivot); * * if (part.ApplyYaw) * rotMatrix *= Matrix.CreateRotationY(yaw);*/ Matrix rotMatrix = Matrix.CreateTranslation(-part.Pivot) * Matrix.CreateFromYawPitchRoll( MathUtils.ToRadians(rot.Y), MathUtils.ToRadians(rot.X), MathUtils.ToRadians(rot.Z) ) * Matrix.CreateTranslation(part.Pivot); var rotMatrix2 = Matrix.CreateTranslation(-part.Pivot) * Matrix.CreateFromYawPitchRoll(headYaw, pitch, 0f) * Matrix.CreateTranslation(part.Pivot); var rotateMatrix = Matrix.CreateTranslation(part.Origin) * (rotMatrix2 * rotMatrix); RotationMatrix = rotateMatrix * characterMatrix; effect.World = rotateMatrix * characterMatrix; effect.View = args.Camera.ViewMatrix; effect.Projection = args.Camera.ProjectionMatrix; foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); } args.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, idx, part.Indexes.Length / 3); idx += part.Indexes.Length; } foreach (var attach in Attachables.ToArray()) { attach.Render(args); } }
public void Update(IUpdateArgs args, Matrix characterMatrix, Vector3 diffuseColor, PlayerLocation modelLocation) { var device = args.GraphicsDevice; if (Effect == null) { Effect = new AlphaTestEffect(device); Effect.Texture = Texture; } else { if (CurrentAnim == null && Animations.TryDequeue(out var animation)) { animation.Setup(); animation.Start(); CurrentAnim = animation; } if (CurrentAnim != null) { CurrentAnim.Update(args.GameTime); if (CurrentAnim.IsFinished()) { CurrentAnim.Reset(); CurrentAnim = null; } } Matrix yawPitchMatrix = Matrix.Identity; if (ApplyHeadYaw || ApplyPitch) { var headYaw = ApplyHeadYaw ? MathUtils.ToRadians(-(modelLocation.HeadYaw - modelLocation.Yaw)) : 0f; var pitch = ApplyPitch ? MathUtils.ToRadians(modelLocation.Pitch) : 0f; yawPitchMatrix = Matrix.CreateTranslation(-EntityModelBone.Pivot) * Matrix.CreateFromYawPitchRoll(headYaw, pitch, 0f) * Matrix.CreateTranslation(EntityModelBone.Pivot); } var userRotationMatrix = Matrix.CreateTranslation(-EntityModelBone.Pivot) * Matrix.CreateRotationX(MathUtils.ToRadians(Rotation.X)) * Matrix.CreateRotationY(MathUtils.ToRadians(Rotation.Y)) * Matrix.CreateRotationZ(MathUtils.ToRadians(Rotation.Z)) * Matrix.CreateTranslation(EntityModelBone.Pivot); Effect.World = yawPitchMatrix * userRotationMatrix * DefaultMatrix * Matrix.CreateTranslation(_position) * characterMatrix; Effect.DiffuseColor = diffuseColor; var children = Children.ToArray(); if (children.Length > 0) { foreach (var child in children) { child.Update(args, userRotationMatrix * characterMatrix, diffuseColor, modelLocation); } } if (_isDirty) { UpdateVertexBuffer(args.GraphicsDevice); } } }
public void Draw(GraphicsDeviceManager gcm, AlphaTestEffect at, BasicEffect be, RasterizerState rs, RasterizerState rsNoCull) { if (this is MAP) { (this as MAP).Draw(gcm, at, be, rs, rsNoCull); } if (this.AllZeroOpacity) { return; } if (this.Eyes.Set == 0x584976) { this.Eyes.AnimateEye(); } if (this.Mouth.Set == 0x584976 && this.NPC && this.Links.Count > 0) { var mset = (this.Links[0] as Moveset); //if (mset.PlayingIndex == mset.chat1_) int ind = -1; if (BulleSpeecher.CurrentBulle != null) { ind = BulleSpeecher.bulles.IndexOf(BulleSpeecher.CurrentBulle); if (ind > -1 && BulleSpeecher.bulleEmmiters[ind] != null && BulleSpeecher.bulleEmmiters[ind].ResourceIndex == this.ResourceIndex) { this.Mouth.AnimateMouth(); } } if (ind < 0 && this.Mouth.lastIndex > -1) { this.Mouth.GetPatch(-1); } } if (rs != null) { if (this.NoCull) { gcm.GraphicsDevice.RasterizerState = rsNoCull; } else { gcm.GraphicsDevice.RasterizerState = rs; } } if (this.ModelType == MDType.Specular) { Vector3 camPos = Program.game.mainCamera.LookAt + Vector3.Transform(Vector3.Backward, Program.game.mainCamera.YawPitch_matrix) * Program.game.mainCamera.Zoom; double hypo = Vector3.Distance(camPos + new Vector3(0, this.AverageVertex.Y - camPos.Y, 0), this.AverageVertex); double angle = Math.Atan2((camPos.Z - this.AverageVertex.Z) / hypo, ((camPos.X - this.AverageVertex.X) / hypo)); float scale = 600f / (float)hypo; float xOff = (float)((camPos.X - this.AverageVertex.X) / hypo) * 0.3f; float yOff = (float)((camPos.Y - this.AverageVertex.Y) / hypo) * 0.3f; if (xOff > 0.5) { xOff = 0.5f; } if (yOff > 0.5) { yOff = 0.5f; } Matrix scaleMat = Matrix.CreateScale(0.73f * scale); float width = (this.MaxVertex.X - this.MinVertex.X); float height = (this.MaxVertex.Y - this.MinVertex.Y); float ratio = height / width; for (int i = 0; i < this.VertexBufferColor.Length; i++) { this.VertexBufferColor[i].TextureCoordinate.X = this.VertexBufferColor[i].Position.X; this.VertexBufferColor[i].TextureCoordinate.Y = this.VertexBufferColor[i].Position.Y; this.VertexBufferColor[i].TextureCoordinate.X -= this.MinVertex.X; this.VertexBufferColor[i].TextureCoordinate.Y -= this.MinVertex.Y; this.VertexBufferColor[i].TextureCoordinate.X /= width; this.VertexBufferColor[i].TextureCoordinate.Y /= height; this.VertexBufferColor[i].TextureCoordinate.Y = 1 - this.VertexBufferColor[i].TextureCoordinate.Y; this.VertexBufferColor[i].TextureCoordinate.X -= 0.5f; this.VertexBufferColor[i].TextureCoordinate.Y -= 0.5f; this.VertexBufferColor[i].TextureCoordinate.Y *= ratio; this.VertexBufferColor[i].TextureCoordinate = Vector2.Transform(this.VertexBufferColor[i].TextureCoordinate, scaleMat); this.VertexBufferColor[i].TextureCoordinate.X += 0.5f; this.VertexBufferColor[i].TextureCoordinate.Y += 0.5f; this.VertexBufferColor[i].TextureCoordinate.X += -xOff; this.VertexBufferColor[i].TextureCoordinate.Y += yOff; } } this.vBuffer.SetData <VertexPositionColorTexture>(this.VertexBufferColor); gcm.GraphicsDevice.SetVertexBuffer(this.vBuffer); gcm.GraphicsDevice.DepthStencilState = DepthStencilState.Default; if (this.ModelType == MDType.Sky || this.ModelType == MDType.Specular || (this.Opacity > 0.001f && this.Opacity < 1)) { if (be != null) { be.VertexColorEnabled = true; // this.HasColor; if (this.HasColor) { be.DiffuseColor = DefaultDiffuseColor; } else { be.DiffuseColor = this.DiffuseColor; } //be.Texture = ResourceLoader.EmptyT2D; //be.CurrentTechnique.Passes[0].Apply(); for (int i = 0; i < this.MeshesOffsets.Count; i++) { if (this.MaterialIndices[i] < this.Textures.Count) { be.Texture = this.Textures[this.MaterialIndices[i]]; } else { be.Texture = ResourceLoader.EmptyT2D; } be.CurrentTechnique.Passes[0].Apply(); gcm.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, this.MeshesOffsets[i][0], this.MeshesOffsets[i][1] / 3); } //be.Texture = KHDebug.ResourceLoader.GetT2D(this.materialFileNames[this.MaterialIndices[ind]]); } } else { if (at != null) { at.VertexColorEnabled = this.HasColor; if (this.HasColor) { at.DiffuseColor = DefaultDiffuseColor; } else { at.DiffuseColor = this.DiffuseColor; } //at.Texture = ResourceLoader.EmptyT2D; //at.CurrentTechnique.Passes[0].Apply(); for (int i = 0; i < this.MeshesOffsets.Count; i++) { if (this.MaterialIndices[i] < this.Textures.Count) { at.Texture = this.Textures[this.MaterialIndices[i]]; } else { at.Texture = ResourceLoader.EmptyT2D; } at.CurrentTechnique.Passes[0].Apply(); gcm.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, this.MeshesOffsets[i][0], this.MeshesOffsets[i][1] / 3); } //at.Texture = KHDebug.ResourceLoader.GetT2D(this.materialFileNames[this.MaterialIndices[ind]]); } } //gcm.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, VertexBufferColor,this.MeshesOffsets[i][0], this.MeshesOffsets[i][1] / 3); }
public void LoadResources() { _effect = new AlphaTestEffect(this.graphicsDevice); vertices = new VertexBuffer(this.graphicsDevice, typeof(VertexPositionTexture), 4 * 6, BufferUsage.WriteOnly); VertexPositionTexture[] data = new VertexPositionTexture[4 * 6]; float y = 0; //0.3f; #region Define Vertexes Vector3 vExtents = new Vector3(Camera.worldSize, Camera.worldSize, Camera.worldSize); //back data[0].Position = new Vector3(vExtents.X, -vExtents.Y * y, -vExtents.Z); data[0].TextureCoordinate.X = 1.0f; data[0].TextureCoordinate.Y = 1.0f; data[1].Position = new Vector3(vExtents.X, vExtents.Y, -vExtents.Z); data[1].TextureCoordinate.X = 1.0f; data[1].TextureCoordinate.Y = 0.0f; data[2].Position = new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z); data[2].TextureCoordinate.X = 0.0f; data[2].TextureCoordinate.Y = 0.0f; data[3].Position = new Vector3(-vExtents.X, -vExtents.Y * y, -vExtents.Z); data[3].TextureCoordinate.X = 0.0f; data[3].TextureCoordinate.Y = 1.0f; //front data[4].Position = new Vector3(-vExtents.X, -vExtents.Y * y, vExtents.Z); data[4].TextureCoordinate.X = 1.0f; data[4].TextureCoordinate.Y = 1.0f; data[5].Position = new Vector3(-vExtents.X, vExtents.Y, vExtents.Z); data[5].TextureCoordinate.X = 1.0f; data[5].TextureCoordinate.Y = 0.0f; data[6].Position = new Vector3(vExtents.X, vExtents.Y, vExtents.Z); data[6].TextureCoordinate.X = 0.0f; data[6].TextureCoordinate.Y = 0.0f; data[7].Position = new Vector3(vExtents.X, -vExtents.Y * y, vExtents.Z); data[7].TextureCoordinate.X = 0.0f; data[7].TextureCoordinate.Y = 1.0f; //bottom data[8].Position = new Vector3(-vExtents.X, -vExtents.Y * y, -vExtents.Z); data[8].TextureCoordinate.X = 0.0f; data[8].TextureCoordinate.Y = 0.0f; data[9].Position = new Vector3(-vExtents.X, -vExtents.Y * y, vExtents.Z); data[9].TextureCoordinate.X = 0.0f; data[9].TextureCoordinate.Y = 1.0f; data[10].Position = new Vector3(vExtents.X, -vExtents.Y * y, vExtents.Z); data[10].TextureCoordinate.X = 1.0f; data[10].TextureCoordinate.Y = 1.0f; data[11].Position = new Vector3(vExtents.X, -vExtents.Y * y, -vExtents.Z); data[11].TextureCoordinate.X = 1.0f; data[11].TextureCoordinate.Y = 0.0f; //top data[12].Position = new Vector3(vExtents.X, vExtents.Y, -vExtents.Z); data[12].TextureCoordinate.X = 1.0f; data[12].TextureCoordinate.Y = 1.0f; data[13].Position = new Vector3(vExtents.X, vExtents.Y, vExtents.Z); data[13].TextureCoordinate.X = 1.0f; data[13].TextureCoordinate.Y = 0.0f; data[14].Position = new Vector3(-vExtents.X, vExtents.Y, vExtents.Z); data[14].TextureCoordinate.X = 0.0f; data[14].TextureCoordinate.Y = 0.0f; data[15].Position = new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z); data[15].TextureCoordinate.X = 0.0f; data[15].TextureCoordinate.Y = 1.0f; //left data[16].Position = new Vector3(-vExtents.X, vExtents.Y, -vExtents.Z); data[16].TextureCoordinate.X = 1.0f; data[16].TextureCoordinate.Y = 0.0f; data[17].Position = new Vector3(-vExtents.X, vExtents.Y, vExtents.Z); data[17].TextureCoordinate.X = 0.0f; data[17].TextureCoordinate.Y = 0.0f; data[18].Position = new Vector3(-vExtents.X, -vExtents.Y * y, vExtents.Z); data[18].TextureCoordinate.X = 0.0f; data[18].TextureCoordinate.Y = 1.0f; data[19].Position = new Vector3(-vExtents.X, -vExtents.Y * y, -vExtents.Z); data[19].TextureCoordinate.X = 1.0f; data[19].TextureCoordinate.Y = 1.0f; //right data[20].Position = new Vector3(vExtents.X, -vExtents.Y * y, -vExtents.Z); data[20].TextureCoordinate.X = 0.0f; data[20].TextureCoordinate.Y = 1.0f; data[21].Position = new Vector3(vExtents.X, -vExtents.Y * y, vExtents.Z); data[21].TextureCoordinate.X = 1.0f; data[21].TextureCoordinate.Y = 1.0f; data[22].Position = new Vector3(vExtents.X, vExtents.Y, vExtents.Z); data[22].TextureCoordinate.X = 1.0f; data[22].TextureCoordinate.Y = 0.0f; data[23].Position = new Vector3(vExtents.X, vExtents.Y, -vExtents.Z); data[23].TextureCoordinate.X = 0.0f; data[23].TextureCoordinate.Y = 0.0f; vertices.SetData <VertexPositionTexture>(data); indices = new IndexBuffer(graphicsDevice, typeof(short), 6 * 6, BufferUsage.WriteOnly); short[] ib = new short[6 * 6]; for (int x = 0; x < 6; x++) { ib[x * 6 + 0] = (short)(x * 4 + 0); ib[x * 6 + 2] = (short)(x * 4 + 1); ib[x * 6 + 1] = (short)(x * 4 + 2); ib[x * 6 + 3] = (short)(x * 4 + 2); ib[x * 6 + 5] = (short)(x * 4 + 3); ib[x * 6 + 4] = (short)(x * 4 + 0); } indices.SetData <short>(ib); #endregion }
public void GetShadow(GraphicsDeviceManager gcm, BasicEffect be, AlphaTestEffect at, RasterizerState rs, RasterizerState rsNoCull) { if (this.ShadowT2D != null && this.Master == null) { gcm.GraphicsDevice.SetRenderTarget(this.ShadowT2D); gcm.GraphicsDevice.Clear(Color.Transparent); at.View = Matrix.CreateLookAt( this.Location + new Vector3(0, this.MaxVertex.Y, 1f), this.Location, Vector3.Up); at.Projection = Ortho; this.vBuffer.SetData <VertexPositionColorTexture>(this.VertexBufferColor); gcm.GraphicsDevice.SetVertexBuffer(this.vBuffer); for (int j = 0; j < this.MeshesOffsets.Count; j++) { at.Texture = this.Textures[this.MaterialIndices[j]]; at.CurrentTechnique.Passes[0].Apply(); gcm.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, this.MeshesOffsets[j][0], this.MeshesOffsets[j][1] / 3); } at.View = be.View; at.Projection = be.Projection; } else if (this.Master != null && this.Master.ShadowT2D != null) { gcm.GraphicsDevice.SetRenderTarget(this.Master.ShadowT2D); gcm.GraphicsDevice.Clear(Color.Transparent); at.View = Matrix.CreateLookAt( this.Master.Location + new Vector3(0, this.Master.MaxVertex.Y, 1f), this.Master.Location, Vector3.Up); at.Projection = Ortho; this.Master.vBuffer.SetData <VertexPositionColorTexture>(this.Master.VertexBufferColor); gcm.GraphicsDevice.SetVertexBuffer(this.Master.vBuffer); //at.Texture = ResourceLoader.EmptyT2D; //at.CurrentTechnique.Passes[0].Apply(); for (int j = 0; j < this.Master.MeshesOffsets.Count; j++) { at.Texture = this.Master.Textures[this.Master.MaterialIndices[j]]; at.CurrentTechnique.Passes[0].Apply(); gcm.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, this.Master.MeshesOffsets[j][0], this.Master.MeshesOffsets[j][1] / 3); } this.vBuffer.SetData <VertexPositionColorTexture>(this.VertexBufferColor); gcm.GraphicsDevice.SetVertexBuffer(this.vBuffer); for (int j = 0; j < this.MeshesOffsets.Count; j++) { at.Texture = this.Textures[this.MaterialIndices[j]]; at.CurrentTechnique.Passes[0].Apply(); gcm.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, this.MeshesOffsets[j][0], this.MeshesOffsets[j][1] / 3); } at.View = be.View; at.Projection = be.Projection; } }
public void Draw(GraphicsDeviceManager graphics, BasicEffect effect, AlphaTestEffect alpha) { if (indices != null && verts != null) { if (verts.Count > 0) { effect.TextureEnabled = textureEnabled; if (textureEnabled) { if (Game1.textures.ContainsKey(textureName)) { effect.Texture = Game1.textures[textureName]; if (alpha != null) { alpha.Texture = effect.Texture; } } else { //Console.WriteLine("missing texture: " + textureName); effect.Texture = Game1.textures["test"]; if (alpha != null) { alpha.Texture = effect.Texture; } } } foreach (var pass in (alpha != null ? alpha.CurrentTechnique.Passes : effect.CurrentTechnique.Passes)) { pass.Apply(); graphics.GraphicsDevice.DrawUserIndexedPrimitives( PrimitiveType.TriangleList, verts_sealed, 0, verts_sealed.Length, indices, 0, indices.Length / 3, VertexPositionColorTexture.VertexDeclaration ); } if (Samplers.EnableWireframe) { effect.TextureEnabled = false; Samplers.SetToDevice(graphics, EngineRasterizer.Wireframe); foreach (var pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphics.GraphicsDevice.DrawUserIndexedPrimitives( PrimitiveType.TriangleList, verts_sealed, 0, verts_sealed.Length, indices, 0, indices.Length / 3, VertexPositionColorTexture.VertexDeclaration ); } Samplers.SetToDevice(graphics, EngineRasterizer.Default); } } else { Console.WriteLine("Empty QuadList!"); } } }
public CustomMeshRenderer(BaseGameObject gameObject, GraphicsDevice device) : base(gameObject) { Effect = new AlphaTestEffect(device); }
public static void Update() { if (!_bInitialized) { _fpsCamera = new FPS_Camera(); //init renderer Effect = new BasicEffect(Memory.Graphics.GraphicsDevice); Effect.EnableDefaultLighting(); Effect.TextureEnabled = true; Effect.DirectionalLight0.Enabled = true; Effect.DirectionalLight1.Enabled = false; Effect.DirectionalLight2.Enabled = false; Effect.DirectionalLight0.Direction = new Vector3( -0.349999f, 0.499999f, -0.650000f ); Effect.DirectionalLight0.SpecularColor = new Vector3(0.8500003f, 0.8500003f, 0.8500003f); Effect.DirectionalLight0.DiffuseColor = new Vector3(1.54999f, 1.54999f, 1.54999f); _camTarget = new Vector3(0, 0f, 0f); _camPosition = new Vector3(0, 0f, 0f); _projectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(60), Memory.Graphics.GraphicsDevice.Viewport.AspectRatio, 1f, 10000f); _viewMatrix = Matrix.CreateLookAt(_camPosition, _camTarget, new Vector3(0f, 1f, 0f)); // Y up //worldMatrix = Matrix.CreateWorld(camTarget, Vector3. // Forward, Vector3.Up); _worldMatrix = Matrix.CreateTranslation(0, 0, 0); //temporarily disabling this, because I'm getting more and more tired of this music playing over and over when debugging //Memory.musicIndex = 30; //AV.Music.Play(); Ate = new AlphaTestEffect(Memory.Graphics.GraphicsDevice) { Projection = _projectionMatrix, View = _viewMatrix, World = _worldMatrix, FogEnabled = false, FogColor = Color.CornflowerBlue.ToVector3(), FogStart = 9.75f, FogEnd = RenderCamDistance }; _bInitialized = true; } if (_lastFieldId != Memory.FieldHolder.FieldID) { ReInit(); } _viewMatrix = _fpsCamera.Update(ref _camPosition, ref _camTarget, ref _degrees); if (Input2.Button(MouseButtons.LeftButton, ButtonTrigger.OnRelease)) { _debugModelId++; } if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F1, ButtonTrigger.OnRelease)) { ReInit(); } if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F2)) { Memory.FieldHolder.FieldID++; } if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F3)) { Memory.FieldHolder.FieldID--; } if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F4)) { _animId++; _animFrame = 0; } _timer += Memory.ElapsedGameTime.TotalMilliseconds / 1000.0d; if (_timer > 0.033d) { _animFrame++; _timer = 0f; } }
public void Draw(SpriteBatch spriteBatch, GameObject o) { //Ref.Logger.Warn("GameObject: "+ o.name); foreach (var c in o.GetComponents <Component>()) { //Ref.Logger.Warn("Component: " + c.name); } //var color = BorderColor; bool closeMask = false; var canvasRenderer = o.GetComponent <CanvasRenderer>(); if (canvasRenderer != null) { var rectTransform = o.GetComponent <RectTransform>(); if (rectTransform != null) { var mask = o.GetComponent <UnityEngine.UI.Mask>(); if (mask != null) { var m = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.PresentationParameters.BackBufferWidth, game.GraphicsDevice.PresentationParameters.BackBufferHeight, 0, 0, 1 ); var alphaEffect = new AlphaTestEffect(game.GraphicsDevice) { Projection = m }; spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.Immediate, null, null, stencilMask, null, alphaEffect); UnityEngine.Vector3[] corners = new UnityEngine.Vector3[4]; rectTransform.GetWorldCorners(corners); var oMin = rectTransform.offsetMin; var oMax = rectTransform.offsetMax; Point point1 = new Point((int)corners[0].x, (int)corners[0].y); Point point22 = new Point((int)corners[2].x, (int)corners[2].y); Color32 color = canvasRenderer._mesh._color * canvasRenderer.GetColor(); drawSliced(spriteBatch, point1, point22, panelMask, Microsoft.Xna.Framework.Color.Blue); spriteBatch.End(); closeMask = true; spriteBatch.Begin(SpriteSortMode.Immediate, null, null, stencilMaskCheck, null, null); } else { var text = o.GetComponent <UnityEngine.UI.Text>(); if (text == null) { UnityEngine.Vector3[] corners = new UnityEngine.Vector3[4]; rectTransform.GetWorldCorners(corners); var oMin = rectTransform.offsetMin; var oMax = rectTransform.offsetMax; Point point = new Point((int)corners[0].x, (int)corners[0].y); Point point2 = new Point((int)corners[2].x, (int)corners[2].y); Color32 color = canvasRenderer._mesh._color * canvasRenderer.GetColor(); drawPanel(spriteBatch, point, point2, new Microsoft.Xna.Framework.Color( color.r, color.g, color.b, color.a ) ); } else { UnityEngine.Vector3[] corners = new UnityEngine.Vector3[4]; rectTransform.GetWorldCorners(corners); var bounds = font.MeasureString(text.text); Point point1 = new Point((int)(corners[0].x + (corners[2].x - corners[0].x) / 2 - bounds.X / 2), (int)(corners[0].y + (corners[2].y - corners[0].y) / 2 + bounds.Y / 2)); Point point = new Point(point1.X, game.Window.ClientBounds.Height - point1.Y); spriteBatch.DrawString(font, text.text, new Microsoft.Xna.Framework.Vector2(point.X, point.Y), Microsoft.Xna.Framework.Color.Yellow); } } } } foreach (var g in o.GetActiveChildren()) { Draw(spriteBatch, g); } if (closeMask) { spriteBatch.End(); spriteBatch.Begin(); } }
public void Draw(Camera cam, SpriteBatch batch, BlendState blend) { if (ate == null) { ate = new AlphaTestEffect(batch.GraphicsDevice); ate.ReferenceAlpha = 128; ate.FogEnabled = false; ate.VertexColorEnabled = true; } ate.World = Matrix.Identity; ate.View = cam.ViewMatrix; ate.Projection = cam.ProjectionMatrix; int startX = cam.ViewSpace.X / TileSize; if (startX < 0) { startX = 0; } if (startX >= SizeX) { startX = SizeX - 1; } int startY = cam.ViewSpace.Y / TileSize; if (startY < 0) { startY = 0; } if (startY >= SizeY) { startY = SizeY - 1; } int endX = cam.ViewSpace.Width / TileSize + startX + 2; if (endX >= SizeX) { endX = SizeX - 1; } int endY = cam.ViewSpace.Height / TileSize + startY + 2; if (endY >= SizeY) { endY = SizeY - 1; } batch.Begin(SpriteSortMode.Immediate, blend, null, DepthStencilState.Default, null, ate, Matrix.Identity); for (int layer = 0; layer < 2; layer++) { for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { if (_tiles[layer][x, y] != null) { batch.Draw(_tileSet, _tiles[layer][x, y].TargetRect, _tiles[layer][x, y].SelectRect, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.55f - layer * 0.1f); } } } } for (int x = 0; x < Objects.Count; x++) { Objects[x].Draw(batch); } batch.End(); }