//public static Material CreateStaticPlane(GraphicsDevice device, Color color, float intensity) //{ // return CreateInternal(device, color, intensity, true); //} private static Material CreateInternal(GraphicsDevice device, Color color, float intensity, bool recenterMesh) { IComputeColor diffuseMap = new ComputeColor(); // TODO: Implement the shader and enable this //if (recenterMesh) //{ // var meshRecenterEffect = new ComputeShaderClassColor { MixinReference = "PhysicsStaticPlaneDebugDiffuse" }; // diffuseMap = new ComputeBinaryColor(new ComputeColor(), meshRecenterEffect, BinaryOperator.Multiply); //} var material = Material.New(device, new MaterialDescriptor { Attributes = { Diffuse = new MaterialDiffuseMapFeature(diffuseMap), DiffuseModel = new MaterialDiffuseLambertModelFeature(), Emissive = new MaterialEmissiveMapFeature(new ComputeColor()), }, }); // set the color to the material var materialColor = new Color4(color).ToColorSpace(device.ColorSpace); material.Passes[0].Parameters.Set(MaterialKeys.DiffuseValue, ref materialColor); material.Passes[0].Parameters.Set(MaterialKeys.EmissiveIntensity, intensity); material.Passes[0].Parameters.Set(MaterialKeys.EmissiveValue, ref materialColor); return(material); }
/// <summary> /// Polls the shader generator if the shader code has changed and has to be reloaded /// </summary> /// <param name="graphicsDevice">The current <see cref="GraphicsDevice"/></param> private void UpdateShaders(GraphicsDevice graphicsDevice) { // TODO Part of the graphics improvement XK-3052 // Don't do this every frame, we have to propagate changes better if (--shadersUpdateCounter > 0) { return; } shadersUpdateCounter = 10; if (ComputeColor != null) { var shaderGeneratorContext = new ShaderGeneratorContext(graphicsDevice) { Parameters = Parameters, ColorSpace = graphicsDevice.ColorSpace }; var newShaderSource = ComputeColor.GenerateShaderSource(shaderGeneratorContext, new MaterialComputeColorKeys(ParticleBaseKeys.EmissiveMap, ParticleBaseKeys.EmissiveValue, Color.White)); // Check if shader code has changed if (!newShaderSource.Equals(shaderSource)) { shaderSource = newShaderSource; Parameters.Set(ParticleBaseKeys.BaseColor, shaderSource); // TODO: Is this necessary? HasVertexLayoutChanged = true; } } }
private void UpdateShaders(GraphicsDevice graphicsDevice) { if (ComputeColor != null && ComputeScalar != null) { var shaderGeneratorContext = new ShaderGeneratorContext(graphicsDevice) { Parameters = Parameters, ColorSpace = graphicsDevice.ColorSpace }; // Don't forget to set the proper color space! shaderGeneratorContext.ColorSpace = graphicsDevice.ColorSpace; var newShaderBaseColor = ComputeColor.GenerateShaderSource(shaderGeneratorContext, new MaterialComputeColorKeys(ParticleCustomShaderKeys.EmissiveMap, ParticleCustomShaderKeys.EmissiveValue, Color.White)); var newShaderBaseScalar = ComputeScalar.GenerateShaderSource(shaderGeneratorContext, new MaterialComputeColorKeys(ParticleCustomShaderKeys.IntensityMap, ParticleCustomShaderKeys.IntensityValue, Color.White)); // Check if shader code has changed if (!newShaderBaseColor.Equals(shaderBaseColor) || !newShaderBaseScalar.Equals(shaderBaseScalar)) { shaderBaseColor = newShaderBaseColor; shaderBaseScalar = newShaderBaseScalar; Parameters.Set(ParticleCustomShaderKeys.BaseColor, shaderBaseColor); Parameters.Set(ParticleCustomShaderKeys.BaseIntensity, shaderBaseScalar); // TODO: Is this necessary? HasVertexLayoutChanged = true; } } }
private Material CreateSolidMaterial(Color color) { var descriptor = new MaterialDescriptor(); var computeColor = new ComputeColor(color); var transparency = new MaterialTransparencyBlendFeature(); transparency.Tint = new ComputeColor(Color.White); transparency.Alpha = new ComputeFloat(0.25f); descriptor.Attributes.Emissive = new MaterialEmissiveMapFeature(computeColor); descriptor.Attributes.Transparency = transparency; return(Material.New(graphicsDevice, descriptor)); }
public void GlobalSetup() { device = GraphicsDevice.New(); IComputeColor diffuseMap = new ComputeColor(); material = Material.New(device, new MaterialDescriptor { Attributes = { Diffuse = new MaterialDiffuseMapFeature(diffuseMap), DiffuseModel = new MaterialDiffuseLambertModelFeature(), Emissive = new MaterialEmissiveMapFeature(new ComputeColor()), }, }); }
} = new ComputeColor(new Color4(1.0f, 0.37f, 0.3f, 1.0f)); // Default falloff for skin. public ShaderSource Generate(MaterialGeneratorContext context) { var shaderSource = new ShaderMixinSource(); MaterialComputeColorKeys materialComputeColorKeys = new MaterialComputeColorKeys(FalloffTexture, FalloffValue, DefaultProfileColor); // Add the shader for computing the transmittance using the custom scattering profile: if (FalloffMap is ComputeTextureColor || FalloffMap is ComputeBinaryColor || FalloffMap is ComputeShaderClassColor || FalloffMap is ComputeVertexStreamColor) { var computeColorSource = FalloffMap.GenerateShaderSource(context, materialComputeColorKeys); shaderSource.AddComposition("FalloffMap", computeColorSource); // Use the expensive pixel shader because the scattering falloff can vary per pixel because we're using a texture: shaderSource.Mixins.Add(new ShaderClassSource("MaterialSubsurfaceScatteringScatteringProfileCustomVarying")); } else { Vector3 falloff = new Vector3(1.0f); ComputeColor falloffComputeColor = FalloffMap as ComputeColor; if (falloffComputeColor != null) { falloff.X = falloffComputeColor.Value.R; falloff.Y = falloffComputeColor.Value.G; falloff.Z = falloffComputeColor.Value.B; } ComputeFloat4 falloffComputeFloat4 = FalloffMap as ComputeFloat4; if (falloffComputeFloat4 != null) { falloff.X = falloffComputeFloat4.Value.X; falloff.Y = falloffComputeFloat4.Value.Y; falloff.Z = falloffComputeFloat4.Value.Z; } // Use the precomputed pixel shader because the scattering falloff is constant across pixels because we're using a texture: Vector4[] scatteringProfile = SubsurfaceScatteringKernelGenerator.CalculateTransmittanceProfile(falloff); // Applied during forward pass. context.MaterialPass.Parameters.Set(MaterialSubsurfaceScatteringScatteringProfileCustomUniformKeys.ScatteringProfile, scatteringProfile); shaderSource.Mixins.Add(new ShaderClassSource("MaterialSubsurfaceScatteringScatteringProfileCustomUniform")); } return(shaderSource); }
public MaterialClearCoatFeature() { BasePaintGlossinessMap = new ComputeFloat(); BasePaintDiffuseMap = new ComputeColor(); MetalFlakesDiffuseMap = new ComputeColor(); MetalFlakesNormalMap = new ComputeColor(); MetalFlakesGlossinessMap = new ComputeFloat(); MetalFlakesMetalnessMap = new ComputeFloat(); OrangePeelNormalMap = new ComputeColor(); ClearCoatGlossinessMap = new ComputeFloat(); ClearCoatMetalnessMap = new ComputeFloat(); LODDistance = new ComputeFloat(1.000f); }
public static Material CreateDebugMaterial(Color color, bool emissive, GraphicsDevice device) { var descriptor = new MaterialDescriptor(); var computeColor = new ComputeColor(color); if (emissive) { descriptor.Attributes.Emissive = new MaterialEmissiveMapFeature(computeColor); } else { descriptor.Attributes.Diffuse = new MaterialDiffuseMapFeature(computeColor); descriptor.Attributes.DiffuseModel = new MaterialDiffuseLambertModelFeature(); } return(Material.New(device, descriptor)); }
/// <summary> /// Polls the shader generator if the shader code has changed and has to be reloaded /// </summary> /// <param name="graphicsDevice">The current <see cref="GraphicsDevice"/></param> private void UpdateShaders(GraphicsDevice graphicsDevice) { // TODO Part of the graphics improvement XK-3052 // Don't do this every frame, we have to propagate changes better if (--shadersUpdateCounter > 0) { return; } shadersUpdateCounter = 10; // TODO Part of the graphics improvement XK-3052 // Weird bug? If the shaderGeneratorContext.Parameters stay the same the particles disappear if (shaderGeneratorContext != null) { ParameterCollections.Remove(shaderGeneratorContext.Parameters); shaderGeneratorContext = null; } if (shaderGeneratorContext == null) { shaderGeneratorContext = new ShaderGeneratorContext(graphicsDevice); ParameterCollections.Add(shaderGeneratorContext.Parameters); } shaderGeneratorContext.Parameters.Clear(); if (ComputeColor != null) { // Don't forget to set the proper color space! shaderGeneratorContext.ColorSpace = graphicsDevice.ColorSpace; var shaderBaseColor = ComputeColor.GenerateShaderSource(shaderGeneratorContext, new MaterialComputeColorKeys(ParticleBaseKeys.EmissiveMap, ParticleBaseKeys.EmissiveValue, Color.White)); shaderGeneratorContext.Parameters.Set(ParticleBaseKeys.BaseColor, shaderBaseColor); // Check if shader code has changed if (!shaderBaseColor.Equals(shaderSource)) { shaderSource = shaderBaseColor; VertexLayoutHasChanged = true; } } }
/// <summary> /// Polls the shader generator if the shader code has changed and has to be reloaded /// </summary> /// <param name="graphicsDevice">The current <see cref="GraphicsDevice"/></param> private void UpdateShaders(GraphicsDevice graphicsDevice) { // TODO Part of the graphics improvement XK-3052 // Don't do this every frame, we have to propagate changes better if (--shadersUpdateCounter > 0) { return; } shadersUpdateCounter = 10; if (ComputeColor != null) { if (ComputeColor.HasChanged) { var shaderGeneratorContext = new ShaderGeneratorContext(graphicsDevice) { Parameters = Parameters, ColorSpace = graphicsDevice.ColorSpace }; shaderSource = ComputeColor.GenerateShaderSource(shaderGeneratorContext, new MaterialComputeColorKeys(ParticleBaseKeys.EmissiveMap, ParticleBaseKeys.EmissiveValue, Color.White)); if (Parameters.Get(ParticleBaseKeys.BaseColor)?.Equals(shaderSource) ?? false) { shaderSource = Parameters.Get(ParticleBaseKeys.BaseColor); } else { Parameters.Set(ParticleBaseKeys.BaseColor, shaderSource); } HasVertexLayoutChanged = true; } } else { shaderSource = null; } }
/// <summary> /// Initializes a new instance of the <see cref="MaterialTransparencyAdditiveFeature"/> class. /// </summary> public MaterialTransparencyAdditiveFeature() { Alpha = new ComputeFloat(0.5f); Tint = new ComputeColor(Color.White); }
/// <summary> /// Initializes a new instance of the <see cref="MaterialTransparencyBlendFeature"/> class. /// </summary> public MaterialTransparencyBlendFeature() { Alpha = new ComputeFloat(1f); Tint = new ComputeColor(Color.White); }
public void LoadAssetTest(Vector3 position) { // Create a new entity and add it to the scene. var entity = new Entity(); var rootScene = SceneSystem.SceneInstance.RootScene; entity.Transform.RotationEulerXYZ = new Vector3(0, 20, 0); entity.Transform.Scale = new Vector3(0.2f, 0.2f, 0.2f); entity.Transform.Position = position; // Create a new model from code // https://doc.xenko.com/latest/en/manual/scripts/create-a-model-from-code.html // Create a model and assign it to the model component. var model = new Stride.Rendering.Model(); entity.GetOrCreate <ModelComponent>().Model = model; // Add one or more meshes using geometric primitives (eg spheres or cubes). //var meshDraw = Stride.Graphics.GeometricPrimitives.GeometricPrimitive.Sphere.New(GraphicsDevice).ToMeshDraw(); //var mesh = new Stride.Rendering.Mesh { Draw = meshDraw }; //model.Meshes.Add(mesh); // create the Mesh // https://github.com/stride3d/stride/blob/master/sources/editor/Stride.Assets.Presentation/AssetEditors/Gizmos/LightSpotGizmo.cs#L168 var CWD = System.IO.Directory.GetCurrentDirectory(); var assetBase = System.IO.Path.GetFullPath(System.IO.Path.Combine(CWD, @"..\..\..\DynLoadAssets\drone2\")); var assetPath = (System.IO.Path.Combine(assetBase, "Drone2.obj")); if (!System.IO.File.Exists(assetPath)) { // not sure why, but DebugText.Print isn't working at the time of testing this... DebugText.Print("Cannot find wavefront OBJ file at : " + assetPath, new Int2(50, 50)); return; } // load the wavefront OBJ file... var wfData = new SimpleScene.Util3d.WavefrontObjLoader(assetPath); // TODO: iterate over materials / multiple materials on the same mesh... { VertexPositionNormalTexture[] vertices; UInt32[] triIndices; Wavefront_VertexSoup_Stride3d.generateDrawIndexBuffer(wfData, wfData.materials[0], out triIndices, out vertices); // convert into a graphics VB / IB pair var vertexBuffer = Stride.Graphics.Buffer.Vertex.New(GraphicsDevice, vertices, GraphicsResourceUsage.Dynamic); var indexBuffer = Stride.Graphics.Buffer.Index.New(GraphicsDevice, triIndices); // add them to the drawing var meshDraw = new Stride.Rendering.MeshDraw { /* Vertex buffer and index buffer setup */ PrimitiveType = Stride.Graphics.PrimitiveType.TriangleList, DrawCount = triIndices.Length, VertexBuffers = new[] { new VertexBufferBinding(vertexBuffer, VertexPositionNormalTexture.Layout, vertexBuffer.ElementCount) }, IndexBuffer = new IndexBufferBinding(indexBuffer, true, triIndices.Length), }; // GenerateTangentBinormal() won't work on a GPU buffer. It has to be run when the data is attached to a // fake CPU buffer. (see build pipeline code) // meshDraw.GenerateTangentBinormal(); var customMesh = new Stride.Rendering.Mesh { Draw = meshDraw }; // set the material index for this mesh // customMesh.MaterialIndex = 0; // add the mesh to the model model.Meshes.Add(customMesh); } // load a texture from a file // var diffuseTextureFilename = System.IO.Path.Combine(assetBase,wfData.materials[0].mtl.diffuseTextureResourceName); // var diffTexStream = System.IO.File.Open(diffuseTextureFilename,System.IO.FileMode.Open,System.IO.FileAccess.Read); // var diffuseTexture = Texture.Load(GraphicsDevice,diffTexStream); // TODO: handle null textures var diffuseTexture = textureObjectForWfTex(System.IO.Path.Combine(assetBase, wfData.materials[0].mtl.diffuseTextureResourceName)); var specularTexture = textureObjectForWfTex(System.IO.Path.Combine(assetBase, wfData.materials[0].mtl.specularTextureResourceName)); var emissiveTexture = textureObjectForWfTex(System.IO.Path.Combine(assetBase, wfData.materials[0].mtl.ambientTextureResourceName)); // note: bump/normal mapping won't won't work until Bitangents are calculated // var bumpTexture = textureObjectForWfTex(System.IO.Path.Combine(assetBase,wfData.materials[0].mtl.bumpTextureResourceName)); var cc = new ComputeColor(); #if (true) { // load textures var materialDescription = new Stride.Rendering.Materials.MaterialDescriptor { Attributes = { DiffuseModel = new MaterialDiffuseLambertModelFeature(), Diffuse = new MaterialDiffuseMapFeature(new ComputeTextureColor(diffuseTexture)), SpecularModel = new MaterialSpecularMicrofacetModelFeature { }, Specular = new MaterialSpecularMapFeature { SpecularMap = new ComputeTextureColor(specularTexture) }, MicroSurface = new MaterialGlossinessMapFeature { GlossinessMap = new ComputeFloat(0.7f) }, Emissive = new MaterialEmissiveMapFeature(new ComputeTextureColor(emissiveTexture)), // note: normal maps won't work until bitangents are calculated // https://gist.github.com/johang88/3f175b045c8e8b55fb815cc19e6128ba // see TNBExtensions.GenerateTangentBinormal(this MeshDraw meshData) //Surface = new MaterialNormalMapFeature { // NormalMap = new ComputeTextureColor(bumpTexture), // IsXYNormal = true, // ScaleAndBias = true, // }, // this is for a solid color rendering... // Diffuse = new MaterialDiffuseMapFeature(new ComputeColor { Key = MaterialKeys.DiffuseValue }), } }; var material = Material.New(GraphicsDevice, materialDescription); material.Passes[0].Parameters.Set(MaterialKeys.EmissiveIntensity, 5.0f); model.Materials.Add(material); } #else // this is for solid color rendering... var material = Material.New(GraphicsDevice, new MaterialDescriptor()); material.Passes[0].Parameters.Set(MaterialKeys.DiffuseValue, Color.Red); model.Materials.Add(material); #endif SceneSystem.SceneInstance.RootScene.Entities.Add(entity); }
/// <summary> /// Initializes a new instance of the <see cref="ComputeTextureColor" /> class. /// </summary> /// <param name="texture">The texture.</param> /// <param name="texcoordIndex">Index of the texcoord.</param> /// <param name="scale">The scale.</param> /// <param name="offset">The offset.</param> public ComputeTextureColor(Texture texture, TextureCoordinate texcoordIndex, Vector2 scale, Vector2 offset) : base(texture, texcoordIndex, scale, offset) { FallbackValue = new ComputeColor(Color4.White); }
public MaterialSubsurfaceScatteringScatteringProfileCustom() { FalloffMap = new ComputeColor(Color.Red); }