public void Render(GraphicsDevice device, Camera cam) { bool devicePrepared = false; int id = cam.GetInstanceID(); if (!CameraCullingInfo.ContainsKey(id)) { return; } PooledPriorityQueue cullingInfo = CameraCullingInfo[id]; for (int i = 0; i < cullingInfo.Count; i++) { Transform t = Application.Find <Transform>(cullingInfo[i]); if (t == null) { continue; } if (!t.renderer.enabled || !t.gameObject.active) { continue; } #if DEBUG if (Camera.logRenderCalls) { Debug.LogFormat("Render: {0} for {1} on {2}", this, t.gameObject, cam.gameObject); } #endif Effect e = Material.shader.effect; if (!devicePrepared) { devicePrepared = true; device.SetVertexBuffer(VertexBuffer); device.Indices = IndexBuffer; Material.shader.ApplyPreRenderSettings(Material, UseVertexColor); Material.SetBlendState(device); IEffectMatrices ems = e as IEffectMatrices; if (ems != null) { ems.World = t.world; ems.View = cam.view; ems.Projection = cam.projectionMatrix; } } else { IEffectMatrices ems = e as IEffectMatrices; if (ems != null) { ems.World = t.world; } } foreach (EffectPass pass in e.CurrentTechnique.Passes) { pass.Apply(); device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, VertexBuffer.VertexCount, 0, IndexBuffer.IndexCount / 3 ); } RenderStats.AddDrawCall(batches, VertexBuffer.VertexCount, IndexBuffer.IndexCount / 3); } }
public override void Draw(GraphicsDevice device, Camera cam) { UInt32 tiles = tilesV * tilesU; for (UInt32 i = 0; i < tiles; ++i) { quadTreeTiles[i].visible = !cam.DoFrustumCulling(ref quadTreeTiles[i].boundingBox); #if DEBUG if (Camera.logRenderCalls) { if (!quadTreeTiles[i].visible) { Debug.LogFormat("VP Cull Static batch: Tile {0} on {1} on {2}", i, gameObject, cam.gameObject); } } #endif } for (int i = 0; i < _sharedMaterials.Length; i++) { Material mat = _sharedMaterials[i]; Effect effect = cam.BasicEffect; if (useLightMap) { effect = dtEffect; } #if DEBUG if (Camera.logRenderCalls) { Debug.LogFormat("Static batch RQ({6}): Tile {0} go {1} cam {2}. Using {3} and material {4} and shader {5}", i, gameObject, cam.gameObject, effect.GetType().Name, mat, mat.shaderName, mat.finalRenderQueue); } #endif (effect as IEffectMatrices).World = Matrix.Identity; (effect as IEffectMatrices).View = cam.view; (effect as IEffectMatrices).Projection = cam.projectionMatrix; if (effect is IEffectLights) { (effect as IEffectLights).LightingEnabled = Light.HasLights; } if (effect is DualTextureEffect) { (effect as DualTextureEffect).DiffuseColor = new Microsoft.Xna.Framework.Vector3(mat.color.r / 2, mat.color.g / 2, mat.color.b / 2); (effect as DualTextureEffect).Alpha = mat.color.a; (effect as DualTextureEffect).Texture = mat.mainTexture; (effect as DualTextureEffect).Texture2 = LightmapSettings.lightmaps[lightmapIndex].lightmapFar; } if (effect is BasicEffect) { mat.SetTextureState(effect as BasicEffect); } mat.SetBlendState(device); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); for (UInt32 j = 0; j < tiles; ++j) { if (!quadTreeTiles[j].visible || quadTreeTiles[j].vertexBuffer == null || quadTreeTiles[j].indexBuffer == null) { continue; } device.SetVertexBuffer(quadTreeTiles[j].vertexBuffer); device.Indices = quadTreeTiles[j].indexBuffer[i]; device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, quadTreeTiles[j].vertexBuffer.VertexCount, 0, quadTreeTiles[j].indexBuffer[i].IndexCount / 3 ); RenderStats.AddDrawCall(quadTreeTiles[j].batches, quadTreeTiles[j].vertexBuffer.VertexCount, quadTreeTiles[j].indexBuffer[i].IndexCount / 3); } } } }