public void SetShaderParam(D3DDevice device, Vector3 lightDirection, ShaderResourceView texture, Vector4 ambientColor, Vector4 diffuseColour, ref Matrix world, Matrix view, Matrix proj) { LightBuffer lightBuffer = new LightBuffer() { ambientColor = ambientColor, diffuseColor = diffuseColour, lightDirection = lightDirection, padding = 0 }; MatrixBuffer matrixBuffer = new MatrixBuffer() { world = world, view = view, proj = proj }; device.ImmediateContext.MapSubresource(mMatrixConstantBuffer, MapMode.WriteDiscard, MapFlags.None, out mMappedResourceMatrix); mMappedResourceMatrix.Write(matrixBuffer); device.ImmediateContext.UnmapSubresource(mMatrixConstantBuffer, 0); device.ImmediateContext.VertexShader.SetConstantBuffer(1, mMatrixConstantBuffer); device.ImmediateContext.MapSubresource(mLightConstantBuffer, MapMode.WriteDiscard, MapFlags.None, out mMappedResourceLight); mMappedResourceLight.Write(lightBuffer); device.ImmediateContext.UnmapSubresource(mLightConstantBuffer, 0); device.ImmediateContext.PixelShader.SetConstantBuffer(0, mLightConstantBuffer); device.ImmediateContext.PixelShader.SetShaderResource(0, texture); }
public override void InitCBuffersFrame(DeviceContext deviceContext, Camera camera, LightClass light) { DataStream mappedResource; #region Constant Camera Buffer deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); var cameraBuffer = new DCameraBuffer() { cameraPosition = camera.Position, padding = 0.0f }; mappedResource.Write(cameraBuffer); deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); int bufferSlotNumber = 1; deviceContext.VertexShader.SetConstantBuffer(bufferSlotNumber, ConstantCameraBuffer); #endregion #region Constant Light Buffer deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); LightBuffer lightbuffer = new LightBuffer() { ambientColor = light.AmbientColor, diffuseColor = light.DiffuseColour, LightDirection = light.Direction, specularColor = light.SpecularColor, specularPower = light.SpecularPower }; mappedResource.Write(lightbuffer); deviceContext.UnmapSubresource(ConstantLightBuffer, 0); bufferSlotNumber = 0; deviceContext.PixelShader.SetConstantBuffer(bufferSlotNumber, ConstantLightBuffer); #endregion }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 diffuseColour) { try { worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream mappedResource); var matrixBuffer = new WorldViewProjectionMatrixBuffer() { world = worldMatrix, view = viewMatrix, projection = projectionMatrix }; mappedResource.Write(matrixBuffer); deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0); var bufferPositionNumber = 0; deviceContext.VertexShader.SetConstantBuffer(0, ConstantMatrixBuffer); deviceContext.PixelShader.SetShaderResource(0, texture); deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out DataStream mappedResourceLight); var lightBuffer = new LightBuffer() { diffuseColour = diffuseColour, lightDirection = lightDirection, padding = 0 }; mappedResourceLight.Write(lightBuffer); deviceContext.UnmapSubresource(ConstantLightBuffer, 0); bufferPositionNumber = 0; deviceContext.PixelShader.SetConstantBuffer(bufferPositionNumber, ConstantLightBuffer); return(true); } catch (Exception ex) { //Log.WriteToFile(ErrorLevel.Error, "LightShader.SetShaderParameters", ex, true); return(false); } }
public Scene(App app) : base(app) { camera = new PathCamera(App); camera.NearPlane = 0.3f; camera.FarPlane = 1024; camera.Rotation.Yaw = 0; camera.Rotation.Pitch = 0; const int m = 25; camera.AddWayPoint(new WayPoint(0 * m, -232, 32, -232, 45, 45, 0)); camera.AddWayPoint(new WayPoint(1 * m, 180, 64, 180, 45, 25, 0)); camera.AddWayPoint(new WayPoint(2 * m, -240, 40, 240, 135, 45, 0)); camera.AddWayPoint(new WayPoint(3 * m, 0, 40, 0, 135, 30, 0)); camera.AddWayPoint(new WayPoint(4 * m, 230, 50, 180, 0, 0, 0)); camera.AddWayPoint(new WayPoint(5 * m, -232, 32, -232, -675, 45, 0)); lightBuffer = new Buffer(Device, 64, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0); worldBuffer = new Buffer(Device, 192, ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0); world = new World(App, worldBuffer, 16, 9); basicShader = ResourceManager.BasicShader; instanceShader = ResourceManager.InstanceShader; geometryShader = ResourceManager.GeometryShader; lightSetup = new LightBuffer(); lightSetup.AmbientColor = Color.White.ToVector4(); lightSetup.AmbientIntensity = 0.1f; lightSetup.LightColor = Color.White.ToVector4(); lightSetup.LightDirection = Vector3.Normalize(new Vector3(0.25f, -1, 2)); using (Heightmap heightmap = new Heightmap(App, 64, "IP_HEIGHTMAP")) using (ColorMap colormap = new ColorMap(App, "IP_COLOR")) { MapGenerator chunkGenerator = new MapGenerator(heightmap, colormap, world.ChunkSize, 8); IEnumerator <BlockInsert[]> enumerator = chunkGenerator.GetEnumerator(); while (enumerator.MoveNext()) { world.SetBlocks(enumerator.Current.Select(insert => { insert.Position -= new Vector3(heightmap.Width, 0, heightmap.Height) / 2; return(insert); })); } } }
private void Awake() { Instance = this; ColorRangeTag = Shader.PropertyToID("_ColorRange"); LightPosTag = Shader.PropertyToID("_LightPos"); LightDirectionAngleTag = Shader.PropertyToID("_LightDirectionAngle"); TintColorTag = Shader.PropertyToID("_TintColor"); Camera = GetComponent <Camera>(); Layer = LayerMask.NameToLayer("Lights"); Mesh = new Mesh(); Mesh.name = "Light Mesh"; Mesh.vertices = new Vector3[4] { new Vector3(-1f, -1f, 0f), new Vector3(-1f, 1f, 0f), new Vector3(1f, -1f, 0f), new Vector3(1f, 1f, 0f) }; Mesh.uv = new Vector2[4] { new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(1f, 0f), new Vector2(1f, 1f) }; Mesh.triangles = new int[6] { 0, 1, 2, 2, 1, 3 }; Mesh.bounds = new Bounds(Vector3.zero, new Vector3(3.40282347E+38f, 3.40282347E+38f, 3.40282347E+38f)); Texture = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGBHalf); Texture.name = "LightBuffer"; Camera.targetTexture = Texture; }
public virtual void InitCBuffersFrame(DeviceContext context, Camera camera, WorldSettings settings) { var cameraBuffer = new DCameraBuffer() { cameraPosition = camera.Position, padding = 0.0f }; ConstantBufferFactory.UpdateVertexBuffer(context, ConstantCameraBuffer, 1, cameraBuffer); if (previousLighting == null || !previousLighting.Equals(settings.Lighting)) { LightBuffer lightbuffer = new LightBuffer() { ambientColor = settings.Lighting.AmbientColor, diffuseColor = settings.Lighting.DiffuseColour, LightDirection = settings.Lighting.Direction, specularColor = settings.Lighting.SpecularColor, specularPower = settings.Lighting.SpecularPower }; previousLighting = settings.Lighting; ConstantBufferFactory.UpdatePixelBuffer(context, ConstantLightBuffer, 0, lightbuffer); } }
public void SetShaderParam(D3DDevice device, Matrix wvp, Vector3 lightDirection, Vector4 ambientColor, Vector4 diffuseColour) { LightBuffer lightBuffer = new LightBuffer() { ambientColor = ambientColor, diffuseColor = diffuseColour, lightDirection = lightDirection, padding = 0 }; device.ImmediateContext.UpdateSubresource(ref wvp, mWVPConstantBuffer); device.ImmediateContext.VertexShader.SetConstantBuffer(0, mWVPConstantBuffer); DataStream mappedResourceLight = default(DataStream); device.ImmediateContext.MapSubresource(mLightConstantBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResourceLight); mappedResourceLight.Write(lightBuffer); device.ImmediateContext.UnmapSubresource(mLightConstantBuffer, 0); device.ImmediateContext.PixelShader.SetConstantBuffer(0, mLightConstantBuffer); }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView[] textures, Vector3 lightDirection, Vector4 diffuseColor, Vector3 cameraPosition, Vector4 specularColor, float specularPower) { try { #region Constant Matrix Buffer // Transpose the matrices to prepare them for shader. worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the constant buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the matrices into the constant buffer. var matrixBuffer = new MatrixBuffer() { world = worldMatrix, view = viewMatrix, projection = projectionMatrix }; mappedResource.Write(matrixBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0); // Set the position of the constant buffer in the vertex shader. var bufferNumber = 0; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantMatrixBuffer); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResources(0, textures); #endregion #region Constant Light Buffer // Lock the light constant buffer so it can be written to. deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var lightBuffer = new LightBuffer() { diffuseColor = diffuseColor, lightDirection = lightDirection, specularColor = specularColor, specularPower = specularPower, }; mappedResource.Write(lightBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantLightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantLightBuffer); #endregion #region Constant Camera Buffer // Lock the camera constant buffer so it can be written to. deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var cameraBuffer = new CameraBuffer() { cameraPosition = cameraPosition, padding = 0.0f }; mappedResource.Write(cameraBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 1; // Now set the camera constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantCameraBuffer); #endregion return true; } catch (Exception) { return false; } }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, ShaderResourceView texture, Vector3 lightDirection, Vector4 ambientColor, Vector4 diffuseColor, Vector3 cameraPosition, Vector4 specularColor, float specularPower) { try { DataStream mappedResource; #region Constant Matrix Buffer // Transpose the matrices to prepare them for shader. worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the matrix constant buffer so it can be written to. deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the matrices into the constant buffer. var matrixBuffer = new MatrixBuffer() { world = worldMatrix, view = viewMatrix, projection = projectionMatrix }; mappedResource.Write(matrixBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0); // Set the position of the constant buffer in the vertex shader. var bufferNumber = 0; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantMatrixBuffer); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResource(0, texture); #endregion #region Constant Camera Buffer // Lock the camera constant buffer so it can be written to. deviceContext.MapSubresource(ConstantCameraBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var cameraBuffer = new CameraBuffer() { cameraPosition = cameraPosition, padding = 0.0f }; mappedResource.Write(cameraBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantCameraBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 1; // Now set the camera constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferNumber, ConstantCameraBuffer); #endregion #region Constant Light Buffer // Lock the light constant buffer so it can be written to. deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var lightBuffer = new LightBuffer() { ambientColor = ambientColor, diffuseColor = diffuseColor, lightDirection = lightDirection, specularColor = specularColor, specularPower = specularPower }; mappedResource.Write(lightBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantLightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferNumber, ConstantLightBuffer); #endregion return(true); } catch (Exception) { return(false); } }
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, Vector4 ambientColour, Vector4 diffuseColour, Vector3 lightDirection, ShaderResourceView texture) { try { // Transpose the matrices to prepare them for shader. worldMatrix.Transpose(); viewMatrix.Transpose(); projectionMatrix.Transpose(); // Lock the matrix constant buffer so it can be written to. DataStream mappedResource; deviceContext.MapSubresource(ConstantMatrixBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); // Copy the matrices into the constant buffer. var matrixBuffer = new WorldViewProjectionMatrixBuffer() { world = worldMatrix, view = viewMatrix, projection = projectionMatrix }; mappedResource.Write(matrixBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantMatrixBuffer, 0); // Set the position of the constant buffer in the vertex shader. int bufferPositionNumber = 0; // Finally set the constant buffer in the vertex shader with the updated values. deviceContext.VertexShader.SetConstantBuffer(bufferPositionNumber, ConstantMatrixBuffer); // Set shader resource in the pixel shader. deviceContext.PixelShader.SetShaderResource(0, texture); // Lock the light constant buffer so it can be written to. deviceContext.MapSubresource(ConstantLightBuffer, MapMode.WriteDiscard, MapFlags.None, out mappedResource); // Copy the lighting variables into the constant buffer. var lightBuffer = new LightBuffer() { ambientColour = ambientColour, diffuseColour = diffuseColour, lightDirection = lightDirection, padding = 0 }; mappedResource.Write(lightBuffer); // Unlock the constant buffer. deviceContext.UnmapSubresource(ConstantLightBuffer, 0); // Set the position of the light constant buffer in the pixel shader. bufferPositionNumber = 0; // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext.PixelShader.SetConstantBuffer(bufferPositionNumber, ConstantLightBuffer); return(true); } catch { return(false); } }
private void OnDestroy() { Instance = null; }
protected override void OnPrefabInit() { Util.Reset(base.transform); base.transform.SetLocalPosition(new Vector3(Grid.WidthInMeters / 2f, Grid.HeightInMeters / 2f, -100f)); targetOrthographicSize = maxOrthographicSize; Instance = this; DisableUserCameraControl = false; baseCamera = CopyCamera(Camera.main, "baseCamera"); mrt = baseCamera.gameObject.AddComponent <MultipleRenderTarget>(); mrt.onSetupComplete += OnMRTSetupComplete; baseCamera.gameObject.AddComponent <LightBufferCompositor>(); baseCamera.transparencySortMode = TransparencySortMode.Orthographic; baseCamera.transform.parent = base.transform; Util.Reset(baseCamera.transform); int mask = LayerMask.GetMask("PlaceWithDepth", "Overlay"); int mask2 = LayerMask.GetMask("Construction"); cameras.Add(baseCamera); baseCamera.cullingMask &= ~mask; baseCamera.cullingMask |= mask2; baseCamera.tag = "Untagged"; baseCamera.gameObject.AddComponent <CameraRenderTexture>().TextureName = "_LitTex"; infraredCamera = CopyCamera(baseCamera, "Infrared"); infraredCamera.cullingMask = 0; infraredCamera.clearFlags = CameraClearFlags.Color; infraredCamera.depth = baseCamera.depth - 1f; infraredCamera.transform.parent = base.transform; infraredCamera.gameObject.AddComponent <Infrared>(); simOverlayCamera = CopyCamera(baseCamera, "SimOverlayCamera"); simOverlayCamera.cullingMask = LayerMask.GetMask("SimDebugView"); simOverlayCamera.clearFlags = CameraClearFlags.Color; simOverlayCamera.depth = baseCamera.depth + 1f; simOverlayCamera.transform.parent = base.transform; simOverlayCamera.gameObject.AddComponent <CameraRenderTexture>().TextureName = "_SimDebugViewTex"; overlayCamera = Camera.main; overlayCamera.name = "Overlay"; overlayCamera.cullingMask = (mask | mask2); overlayCamera.clearFlags = CameraClearFlags.Nothing; overlayCamera.transform.parent = base.transform; overlayCamera.depth = baseCamera.depth + 3f; overlayCamera.transform.SetLocalPosition(Vector3.zero); overlayCamera.transform.localRotation = Quaternion.identity; overlayCamera.renderingPath = RenderingPath.Forward; overlayCamera.allowHDR = false; overlayCamera.tag = "Untagged"; CameraReferenceTexture cameraReferenceTexture = overlayCamera.gameObject.AddComponent <CameraReferenceTexture>(); cameraReferenceTexture.referenceCamera = baseCamera; ColorCorrectionLookup component = overlayCamera.GetComponent <ColorCorrectionLookup>(); component.Convert(dayColourCube, string.Empty); component.Convert2(nightColourCube, string.Empty); cameras.Add(overlayCamera); lightBufferCamera = CopyCamera(overlayCamera, "Light Buffer"); lightBufferCamera.clearFlags = CameraClearFlags.Color; lightBufferCamera.cullingMask = LayerMask.GetMask("Lights"); lightBufferCamera.depth = baseCamera.depth - 1f; lightBufferCamera.transform.parent = base.transform; lightBufferCamera.transform.SetLocalPosition(Vector3.zero); lightBufferCamera.rect = new Rect(0f, 0f, 1f, 1f); LightBuffer lightBuffer = lightBufferCamera.gameObject.AddComponent <LightBuffer>(); lightBuffer.Material = LightBufferMaterial; lightBuffer.CircleMaterial = LightCircleOverlay; lightBuffer.ConeMaterial = LightConeOverlay; overlayNoDepthCamera = CopyCamera(overlayCamera, "overlayNoDepth"); int mask3 = LayerMask.GetMask("Overlay", "Place"); baseCamera.cullingMask &= ~mask3; overlayNoDepthCamera.clearFlags = CameraClearFlags.Depth; overlayNoDepthCamera.cullingMask = mask3; overlayNoDepthCamera.transform.parent = base.transform; overlayNoDepthCamera.transform.SetLocalPosition(Vector3.zero); overlayNoDepthCamera.depth = baseCamera.depth + 4f; overlayNoDepthCamera.tag = "MainCamera"; overlayNoDepthCamera.gameObject.AddComponent <NavPathDrawer>(); uiCamera = CopyCamera(overlayCamera, "uiCamera"); uiCamera.clearFlags = CameraClearFlags.Depth; uiCamera.cullingMask = LayerMask.GetMask("UI"); uiCamera.transform.parent = base.transform; uiCamera.transform.SetLocalPosition(Vector3.zero); uiCamera.depth = baseCamera.depth + 5f; timelapseFreezeCamera = CopyCamera(uiCamera, "timelapseFreezeCamera"); timelapseFreezeCamera.depth = uiCamera.depth + 3f; timelapseFreezeCamera.gameObject.AddComponent <FillRenderTargetEffect>(); timelapseFreezeCamera.enabled = false; Camera camera = CloneCamera(overlayCamera, "timelapseCamera"); Timelapser timelapser = camera.gameObject.AddComponent <Timelapser>(); camera.transparencySortMode = TransparencySortMode.Orthographic; Game.Instance.timelapser = timelapser; GameScreenManager.Instance.SetCamera(GameScreenManager.UIRenderTarget.ScreenSpaceCamera, uiCamera); GameScreenManager.Instance.SetCamera(GameScreenManager.UIRenderTarget.WorldSpace, uiCamera); GameScreenManager.Instance.SetCamera(GameScreenManager.UIRenderTarget.ScreenshotModeCamera, uiCamera); infoText = GameScreenManager.Instance.screenshotModeCanvas.GetComponentInChildren <LocText>(); }