public override void GenerateShader(MaterialGeneratorContext context) { if (DisplacementMap == null) { return; } var materialStage = (MaterialShaderStage)Stage; // reset the displacement streams at the beginning of the stage context.AddStreamInitializer(materialStage, "MaterialDisplacementStream"); // set the blending mode of displacement map to additive (and not default linear blending) context.UseStreamWithCustomBlend(materialStage, DisplacementStream, new ShaderClassSource("MaterialStreamAdditiveBlend", DisplacementStream)); // build the displacement computer var displacement = DisplacementMap; if (ScaleAndBias) // scale and bias should be done by layer { displacement = new ComputeBinaryScalar(displacement, new ComputeFloat(2f), BinaryOperator.Multiply); displacement = new ComputeBinaryScalar(displacement, new ComputeFloat(1f), BinaryOperator.Subtract); } displacement = new ComputeBinaryScalar(displacement, Intensity, BinaryOperator.Multiply); // Workaround to inform compute colors that sampling is occurring from a vertex shader context.IsNotPixelStage = materialStage != MaterialShaderStage.Pixel; context.SetStream(materialStage, DisplacementStream, displacement, MaterialKeys.DisplacementMap, MaterialKeys.DisplacementValue); context.IsNotPixelStage = false; var scaleNormal = materialStage != MaterialShaderStage.Vertex; var positionMember = materialStage == MaterialShaderStage.Vertex ? "Position" : "PositionWS"; var normalMember = materialStage == MaterialShaderStage.Vertex ? "meshNormal" : "normalWS"; context.SetStreamFinalModifier <MaterialDisplacementMapFeature>(materialStage, new ShaderClassSource("MaterialSurfaceDisplacement", positionMember, normalMember, scaleNormal)); }
public void Visit(MaterialGeneratorContext context) { if (DisplacementMap == null) return; var materialStage = (MaterialShaderStage)Stage; // reset the displacement streams at the beginning of the stage context.AddStreamInitializer(materialStage, "MaterialDisplacementStream"); // set the blending mode of displacement map to additive (and not default linear blending) context.UseStreamWithCustomBlend(materialStage, DisplacementStream, new ShaderClassSource("MaterialStreamAdditiveBlend", DisplacementStream)); // build the displacement computer var displacement = DisplacementMap; if (ScaleAndBias) // scale and bias should be done by layer { displacement = new ComputeBinaryScalar(displacement, new ComputeFloat(2f), BinaryOperator.Multiply); displacement = new ComputeBinaryScalar(displacement, new ComputeFloat(1f), BinaryOperator.Subtract); } displacement = new ComputeBinaryScalar(displacement, Intensity, BinaryOperator.Multiply); // Workaround to inform compute colors that sampling is occurring from a vertex shader context.IsNotPixelStage = materialStage != MaterialShaderStage.Pixel; context.SetStream(materialStage, DisplacementStream, displacement, MaterialKeys.DisplacementMap, MaterialKeys.DisplacementValue); context.IsNotPixelStage = false; var scaleNormal = materialStage != MaterialShaderStage.Vertex; var positionMember = materialStage == MaterialShaderStage.Vertex ? "Position" : "PositionWS"; var normalMember = materialStage == MaterialShaderStage.Vertex ? "meshNormal" : "normalWS"; context.SetStreamFinalModifier<MaterialDisplacementMapFeature>(materialStage, new ShaderClassSource("MaterialSurfaceDisplacement", positionMember, normalMember, scaleNormal)); }