//////////////////////////////////////////////////////////////////////////////////////// // Kara Jensen - mail@karajensen.com - shader_hlsl.fx //////////////////////////////////////////////////////////////////////////////////////// cbuffer SceneVertexBuffer : register(b0) { float4x4 viewProjection; float depthNear; float depthFar; ifdef: SPECULAR float3 cameraPosition; endif }; cbuffer MeshVertexBuffer : register(b1) { float4x4 world; }; cbuffer MeshPixelBuffer : register(b2) { float meshAmbience; ifdef: !FLAT float meshDiffuse; endif ifdef: BUMP float meshBump; endif ifdef: SPECULAR float meshSpecular; float meshSpecularity; endif ifdef: CAUSTICS float meshCausticAmount; float meshCausticScale; endif }; cbuffer ScenePixelBuffer : register(b3) { ifdef: !FLAT float lightActive[MAX_LIGHTS]; float3 lightPosition[MAX_LIGHTS]; float3 lightAttenuation[MAX_LIGHTS]; float3 lightDiffuse[MAX_LIGHTS]; ifdef: SPECULAR float lightSpecularity[MAX_LIGHTS]; float3 lightSpecular[MAX_LIGHTS]; endif endif }; SamplerState Sampler; Texture2D DiffuseSampler; ifdef: BUMP Texture2D NormalSampler; endif ifdef: SPECULAR Texture2D SpecularSampler; endif ifdef: CAUSTICS Texture2D CausticsSampler; endif struct Attributes { float4 position : SV_POSITION; float depth : TEXCOORD0; float2 uvs : TEXCOORD1; ifdef: !FLAT float3 normal : NORMAL; float3 positionWorld : TEXCOORD2; endif ifdef: BUMP float3 tangent : TEXCOORD3; float3 bitangent : TEXCOORD4; endif ifdef: SPECULAR|BUMP float3 vertToCamera : TEXCOORD5; elseif: SPECULAR float3 vertToCamera : TEXCOORD3; endif }; struct Outputs { float4 colour : SV_TARGET0; float4 depth : SV_TARGET1; }; Attributes VShader(float4 position : POSITION, ifdef: BUMP float2 uvs : TEXCOORD0, float3 normal : NORMAL, float3 tangent : TEXCOORD1, float3 bitangent : TEXCOORD2) elseif: !FLAT float2 uvs : TEXCOORD0, float3 normal : NORMAL) else: float2 uvs : TEXCOORD0) endif { Attributes output; output.position = mul(mul(viewProjection, world), position); output.uvs = uvs; ifdef: !FLAT output.normal = mul(world, normal); output.positionWorld = mul(world, position).xyz; endif output.depth = ((output.position.z - depthNear) * (-1.0 / (depthFar - depthNear))) + 1.0; ifdef: BUMP output.tangent = mul(world, tangent); output.bitangent = mul(world, bitangent); endif ifdef: SPECULAR output.vertToCamera = cameraPosition - output.positionWorld; endif return output; } Outputs PShader(Attributes input) { float4 diffuseTex = DiffuseSampler.Sample(Sampler, input.uvs); ifdef: !FLAT float3 diffuse = float3(0.0, 0.0, 0.0); float3 normal = normalize(input.normal); endif ifdef: BUMP float4 normalTex = NormalSampler.Sample(Sampler, input.uvs); float2 bump = meshBump * (normalTex.rg - 0.5); normal = normalize(normal + bump.x * normalize(input.tangent) + bump.y * normalize(input.bitangent)); endif ifdef: SPECULAR float3 vertToCamera = normalize(input.vertToCamera); float4 specularTex = SpecularSampler.Sample(Sampler, input.uvs); float3 specular = float3(0.0, 0.0, 0.0); endif ifdef: !FLAT for (int i = 0; i < MAX_LIGHTS; ++i) { float3 lightColour = lightDiffuse[i]; float3 vertToLight = lightPosition[i] - input.positionWorld; float lightLength = length(vertToLight); float attenuation = 1.0 / (lightAttenuation[i].x + lightAttenuation[i].y * lightLength + lightAttenuation[i].z * lightLength * lightLength); vertToLight /= lightLength; lightColour *= ((dot(vertToLight, normal) + 1.0) * ((1.0 - meshDiffuse) * 0.5)) + meshDiffuse; diffuse += lightColour * attenuation * lightActive[i]; ifdef: SPECULAR float specularity = lightSpecularity[i] * meshSpecularity; float3 halfVector = normalize(vertToLight + vertToCamera); float specularFactor = pow(max(dot(normal, halfVector), 0.0), specularity); specular += specularFactor * lightSpecular[i] * attenuation * lightActive[i] * meshSpecular; endif } endif ifdef: CAUSTICS float3 caustics = CausticsSampler.Sample( Sampler, input.uvs * meshCausticScale).rgb * max(normal.y, 0.0); endif Outputs output; output.depth = float4(input.depth, input.depth, input.depth, 1.0); ifdef: FLAT output.colour.rgb = diffuseTex.rgb; else: output.colour.rgb = diffuseTex.rgb * diffuse; endif ifdef: SPECULAR output.colour.rgb += specularTex.rgb * specular; endif ifdef: CAUSTICS output.colour.rgb += caustics * meshCausticAmount; endif output.colour.rgb *= meshAmbience; output.colour.a = 1.0; return output; }
//////////////////////////////////////////////////////////////////////////////////////// // Kara Jensen - mail@karajensen.com - shader_glsl_vert.fx //////////////////////////////////////////////////////////////////////////////////////// #version 150 in vec4 in_Position; in vec2 in_UVs; ifdef: !FLAT in vec3 in_Normal; endif ifdef: BUMP in vec3 in_Tangent; in vec3 in_Bitangent; endif out float ex_Depth; out vec2 ex_UVs; ifdef: !FLAT out vec3 ex_Normal; out vec3 ex_PositionWorld; endif ifdef: BUMP out vec3 ex_Tangent; out vec3 ex_Bitangent; endif ifdef: SPECULAR out vec3 ex_VertToCamera; endif uniform float depthNear; uniform float depthFar; uniform mat4 world; uniform mat4 viewProjection; ifdef: SPECULAR uniform vec3 cameraPosition; endif void main(void) { gl_Position = viewProjection * world * in_Position; ex_UVs = in_UVs; ifdef: !FLAT ex_Normal = (world * vec4(in_Normal, 0.0)).xyz; ex_PositionWorld = (world * in_Position).xyz; endif ifdef: BUMP ex_Tangent = (world * vec4(in_Tangent, 0.0)).xyz; ex_Bitangent = (world * vec4(in_Bitangent, 0.0)).xyz; endif ifdef: SPECULAR ex_VertToCamera = cameraPosition - ex_PositionWorld; endif ex_Depth = ((gl_Position.z - depthNear) * (-1.0 / (depthFar - depthNear))) + 1.0; } //////////////////////////////////////////////////////////////////////////////////////// // Kara Jensen - mail@karajensen.com - shader_glsl_frag.fx //////////////////////////////////////////////////////////////////////////////////////// #version 150 out vec4 out_Color[SCENE_TEXTURES]; in float ex_Depth; in vec2 ex_UVs; ifdef: !FLAT in vec3 ex_PositionWorld; in vec3 ex_Normal; endif ifdef: BUMP in vec3 ex_Tangent; in vec3 ex_Bitangent; endif ifdef: SPECULAR in vec3 ex_VertToCamera; endif ifdef: !FLAT uniform float lightActive[MAX_LIGHTS]; uniform vec3 lightPosition[MAX_LIGHTS]; uniform vec3 lightDiffuse[MAX_LIGHTS]; uniform vec3 lightAttenuation[MAX_LIGHTS]; ifdef: SPECULAR uniform vec3 lightSpecular[MAX_LIGHTS]; uniform float lightSpecularity[MAX_LIGHTS]; endif endif uniform float meshAmbience; ifdef: !FLAT uniform float meshDiffuse; endif ifdef: BUMP uniform float meshBump; endif ifdef: CAUSTICS uniform float meshCausticAmount; uniform float meshCausticScale; endif ifdef: SPECULAR uniform float meshSpecular; uniform float meshSpecularity; endif uniform sampler2D DiffuseSampler; ifdef: BUMP uniform sampler2D NormalSampler; endif ifdef: SPECULAR uniform sampler2D SpecularSampler; endif ifdef: CAUSTICS uniform sampler2D CausticsSampler; endif void main(void) { vec4 diffuseTex = texture(DiffuseSampler, ex_UVs); vec3 diffuse = vec3(0.0, 0.0, 0.0); ifdef: !FLAT vec3 normal = normalize(ex_Normal); endif ifdef: BUMP vec4 normalTex = texture(NormalSampler, ex_UVs); vec2 bump = meshBump * (normalTex.rg - 0.5); normal = normalize(normal + bump.x * normalize(ex_Tangent) + bump.y * normalize(ex_Bitangent)); endif ifdef: SPECULAR vec3 vertToCamera = normalize(ex_VertToCamera); vec4 specularTex = texture(SpecularSampler, ex_UVs); vec3 specular = vec3(0.0, 0.0, 0.0); endif ifdef: !FLAT for (int i = 0; i < MAX_LIGHTS; ++i) { vec3 lightColour = lightDiffuse[i]; vec3 vertToLight = lightPosition[i] - ex_PositionWorld; float lightLength = length(vertToLight); float attenuation = 1.0 / (lightAttenuation[i].x + lightAttenuation[i].y * lightLength + lightAttenuation[i].z * lightLength * lightLength); vertToLight /= lightLength; lightColour *= ((dot(vertToLight, normal) + 1.0) * ((1.0 - meshDiffuse) * 0.5)) + meshDiffuse; diffuse += lightColour * attenuation * lightActive[i]; ifdef: SPECULAR float specularity = lightSpecularity[i] * meshSpecularity; vec3 halfVector = normalize(vertToLight + vertToCamera); float specularFactor = pow(max(dot(normal, halfVector), 0.0), specularity); specular += specularFactor * lightSpecular[i] * attenuation * lightActive[i] * meshSpecular; endif } endif ifdef: CAUSTICS vec3 caustics = texture(CausticsSampler, ex_UVs * meshCausticScale).rgb * max(normal.y, 0.0); endif ifdef: FLAT out_Color[ID_COLOUR].rgb = diffuseTex.rgb; else: out_Color[ID_COLOUR].rgb = diffuseTex.rgb * diffuse; endif ifdef: SPECULAR out_Color[ID_COLOUR].rgb += specularTex.rgb * specular; endif ifdef: CAUSTICS out_Color[ID_COLOUR].rgb += caustics * meshCausticAmount; endif out_Color[ID_COLOUR].rgb *= meshAmbience; out_Color[ID_COLOUR].a = 1.0; out_Color[ID_DEPTH] = vec4(ex_Depth, ex_Depth, ex_Depth, 1.0); }
//////////////////////////////////////////////////////////////////////////////////////// // Kara Jensen - mail@karajensen.com - Generated Shader //////////////////////////////////////////////////////////////////////////////////////// cbuffer SceneVertexBuffer : register(b0) { float4x4 viewProjection; float depthNear; float depthFar; float3 cameraPosition; }; cbuffer MeshVertexBuffer : register(b1) { float4x4 world; }; cbuffer MeshPixelBuffer : register(b2) { float meshAmbience; float meshDiffuse; float meshBump; float meshSpecular; float meshSpecularity; float meshCausticAmount; float meshCausticScale; }; cbuffer ScenePixelBuffer : register(b3) { float lightActive[1]; float3 lightPosition[1]; float3 lightAttenuation[1]; float3 lightDiffuse[1]; float lightSpecularity[1]; float3 lightSpecular[1]; }; SamplerState Sampler; Texture2D DiffuseSampler; Texture2D NormalSampler; Texture2D SpecularSampler; Texture2D CausticsSampler; struct Attributes { float4 position : SV_POSITION; float depth : TEXCOORD0; float2 uvs : TEXCOORD1; float3 normal : NORMAL; float3 positionWorld : TEXCOORD2; float3 tangent : TEXCOORD3; float3 bitangent : TEXCOORD4; float3 vertToCamera : TEXCOORD5; }; struct Outputs { float4 colour : SV_TARGET0; float4 depth : SV_TARGET1; }; Attributes VShader(float4 position : POSITION, float2 uvs : TEXCOORD0, float3 normal : NORMAL, float3 tangent : TEXCOORD1, float3 bitangent : TEXCOORD2) { Attributes output; output.position = mul(mul(viewProjection, world), position); output.uvs = uvs; output.normal = mul(world, normal); output.positionWorld = mul(world, position).xyz; output.depth = ((output.position.z - depthNear) * (-1.0 / (depthFar - depthNear))) + 1.0; output.tangent = mul(world, tangent); output.bitangent = mul(world, bitangent); output.vertToCamera = cameraPosition - output.positionWorld; return output; } Outputs PShader(Attributes input) { float4 diffuseTex = DiffuseSampler.Sample(Sampler, input.uvs); float3 diffuse = float3(0.0, 0.0, 0.0); float3 normal = normalize(input.normal); float4 normalTex = NormalSampler.Sample(Sampler, input.uvs); float2 bump = meshBump * (normalTex.rg - 0.5); normal = normalize(normal + bump.x * normalize(input.tangent) + bump.y * normalize(input.bitangent)); float3 vertToCamera = normalize(input.vertToCamera); float4 specularTex = SpecularSampler.Sample(Sampler, input.uvs); float3 specular = float3(0.0, 0.0, 0.0); for (int i = 0; i < 1; ++i) { float3 lightColour = lightDiffuse[i]; float3 vertToLight = lightPosition[i] - input.positionWorld; float lightLength = length(vertToLight); float attenuation = 1.0 / (lightAttenuation[i].x + lightAttenuation[i].y * lightLength + lightAttenuation[i].z * lightLength * lightLength); vertToLight /= lightLength; lightColour *= ((dot(vertToLight, normal) + 1.0) * ((1.0 - meshDiffuse) * 0.5)) + meshDiffuse; diffuse += lightColour * attenuation * lightActive[i]; float specularity = lightSpecularity[i] * meshSpecularity; float3 halfVector = normalize(vertToLight + vertToCamera); float specularFactor = pow(max(dot(normal, halfVector), 0.0), specularity); specular += specularFactor * lightSpecular[i] * attenuation * lightActive[i] * meshSpecular; } float3 caustics = CausticsSampler.Sample( Sampler, input.uvs * meshCausticScale).rgb * max(normal.y, 0.0); Outputs output; output.depth = float4(input.depth, input.depth, input.depth, 1.0); output.colour.rgb = diffuseTex.rgb * diffuse; output.colour.rgb += specularTex.rgb * specular; output.colour.rgb += caustics * meshCausticAmount; output.colour.rgb *= meshAmbience; output.colour.a = 1.0; return output; }
//////////////////////////////////////////////////////////////////////////////////////// // Kara Jensen - mail@karajensen.com - Generated Vertex Shader //////////////////////////////////////////////////////////////////////////////////////// #version 150 in vec4 in_Position; in vec2 in_UVs; in vec3 in_Normal; in vec3 in_Tangent; in vec3 in_Bitangent; out float ex_Depth; out vec2 ex_UVs; out vec3 ex_Normal; out vec3 ex_PositionWorld; out vec3 ex_Tangent; out vec3 ex_Bitangent; out vec3 ex_VertToCamera; uniform float depthNear; uniform float depthFar; uniform mat4 world; uniform mat4 viewProjection; uniform vec3 cameraPosition; void main(void) { gl_Position = viewProjection * world * in_Position; ex_UVs = in_UVs; ex_Normal = (world * vec4(in_Normal, 0.0)).xyz; ex_PositionWorld = (world * in_Position).xyz; ex_Tangent = (world * vec4(in_Tangent, 0.0)).xyz; ex_Bitangent = (world * vec4(in_Bitangent, 0.0)).xyz; ex_VertToCamera = cameraPosition - ex_PositionWorld; ex_Depth = ((gl_Position.z - depthNear) * (-1.0 / (depthFar - depthNear))) + 1.0; } //////////////////////////////////////////////////////////////////////////////////////// // Kara Jensen - mail@karajensen.com - Generated Fragment Shader //////////////////////////////////////////////////////////////////////////////////////// #version 150 out vec4 out_Color[2]; in float ex_Depth; in vec2 ex_UVs; in vec3 ex_PositionWorld; in vec3 ex_Normal; in vec3 ex_Tangent; in vec3 ex_Bitangent; in vec3 ex_VertToCamera; uniform float lightActive[1]; uniform vec3 lightPosition[1]; uniform vec3 lightDiffuse[1]; uniform vec3 lightAttenuation[1]; uniform vec3 lightSpecular[1]; uniform float lightSpecularity[1]; uniform float meshAmbience; uniform float meshDiffuse; uniform float meshBump; uniform float meshCausticAmount; uniform float meshCausticScale; uniform float meshSpecular; uniform float meshSpecularity; uniform sampler2D DiffuseSampler; uniform sampler2D NormalSampler; uniform sampler2D SpecularSampler; uniform sampler2D CausticsSampler; void main(void) { vec4 diffuseTex = texture(DiffuseSampler, ex_UVs); vec3 diffuse = vec3(0.0, 0.0, 0.0); vec3 normal = normalize(ex_Normal); vec4 normalTex = texture(NormalSampler, ex_UVs); vec2 bump = meshBump * (normalTex.rg - 0.5); normal = normalize(normal + bump.x * normalize(ex_Tangent) + bump.y * normalize(ex_Bitangent)); vec3 vertToCamera = normalize(ex_VertToCamera); vec4 specularTex = texture(SpecularSampler, ex_UVs); vec3 specular = vec3(0.0, 0.0, 0.0); for (int i = 0; i < 1; ++i) { vec3 lightColour = lightDiffuse[i]; vec3 vertToLight = lightPosition[i] - ex_PositionWorld; float lightLength = length(vertToLight); float attenuation = 1.0 / (lightAttenuation[i].x + lightAttenuation[i].y * lightLength + lightAttenuation[i].z * lightLength * lightLength); vertToLight /= lightLength; lightColour *= ((dot(vertToLight, normal) + 1.0) * ((1.0 - meshDiffuse) * 0.5)) + meshDiffuse; diffuse += lightColour * attenuation * lightActive[i]; float specularity = lightSpecularity[i] * meshSpecularity; vec3 halfVector = normalize(vertToLight + vertToCamera); float specularFactor = pow(max(dot(normal, halfVector), 0.0), specularity); specular += specularFactor * lightSpecular[i] * attenuation * lightActive[i] * meshSpecular; } vec3 caustics = texture(CausticsSampler, ex_UVs * meshCausticScale).rgb * max(normal.y, 0.0); out_Color[0].rgb = diffuseTex.rgb * diffuse; out_Color[0].rgb += specularTex.rgb * specular; out_Color[0].rgb += caustics * meshCausticAmount; out_Color[0].rgb *= meshAmbience; out_Color[0].a = 1.0; out_Color[1] = vec4(ex_Depth, ex_Depth, ex_Depth, 1.0); }