diff options
Diffstat (limited to 'o3d/samples/shaders')
-rw-r--r-- | o3d/samples/shaders/README | 16 | ||||
-rw-r--r-- | o3d/samples/shaders/binormal.shader | 64 | ||||
-rw-r--r-- | o3d/samples/shaders/bump.shader | 137 | ||||
-rw-r--r-- | o3d/samples/shaders/checker.shader | 117 | ||||
-rw-r--r-- | o3d/samples/shaders/diffuse.shader | 96 | ||||
-rw-r--r-- | o3d/samples/shaders/normal.shader | 64 | ||||
-rw-r--r-- | o3d/samples/shaders/one-channel-texture.shader | 94 | ||||
-rw-r--r-- | o3d/samples/shaders/phong-vertex-anim.shader | 95 | ||||
-rw-r--r-- | o3d/samples/shaders/phong-with-colormult.shader | 83 | ||||
-rw-r--r-- | o3d/samples/shaders/solid-color.shader | 74 | ||||
-rw-r--r-- | o3d/samples/shaders/tangent.shader | 64 | ||||
-rw-r--r-- | o3d/samples/shaders/texture-colormult.shader | 74 | ||||
-rw-r--r-- | o3d/samples/shaders/texture-only.shader | 71 | ||||
-rw-r--r-- | o3d/samples/shaders/vertex-color.shader | 75 | ||||
-rw-r--r-- | o3d/samples/shaders/yuv2rgb.shader | 235 |
15 files changed, 1359 insertions, 0 deletions
diff --git a/o3d/samples/shaders/README b/o3d/samples/shaders/README new file mode 100644 index 0000000..d5367d4d --- /dev/null +++ b/o3d/samples/shaders/README @@ -0,0 +1,16 @@ +README for samples/shaders + +This directory contains files of shader code. In order to use them, they should +be loaded into an html textarea in your application or loaded using +o3djs.effect.loadEffect. NOTE: o3djs.effect.loadEffect can only load +files from the same domain as the application webpage and the files must be at +or below the current directory of the webpage + +The purpose of this directory is not to be *the* library of shaders, but to +provide some sample shaders as examples and starting points for people to build +their own. + +Each shader has its own requirements. Some shaders need different streams or +require certain types of texture samplers so a shader will not work with just +any 3d object. Look at the shader and determine what its requirements are before +using it in your application. diff --git a/o3d/samples/shaders/binormal.shader b/o3d/samples/shaders/binormal.shader new file mode 100644 index 0000000..1be9416 --- /dev/null +++ b/o3d/samples/shaders/binormal.shader @@ -0,0 +1,64 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WorldViewProjection; + +// input parameters for our vertex shader +struct VertexShaderInput { + float3 position : POSITION; + float3 binormal : BINORMAL; +}; + +// input parameters for our pixel shader +// also the output parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float3 binormal : TEXCOORD1; +}; + +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(float4(input.position, 1.0), worldViewProjection); + output.binormal = input.binormal; + return output; +} + +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return float4(normalize(input.binormal) * 0.5 + float3(0.5, 0.5, 0.5), 1); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/bump.shader b/o3d/samples/shaders/bump.shader new file mode 100644 index 0000000..4d9eefa --- /dev/null +++ b/o3d/samples/shaders/bump.shader @@ -0,0 +1,137 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WorldViewProjection; +float4x4 worldInverseTranspose : WorldInverseTranspose; +float4x4 world : World; +float4x4 viewInverse : ViewInverse; + +// whether to use texture +float useTexture; + +float3 lightWorldPos; +float4 lightIntensity; +float4 ambientIntensity; +float4 emissive; +float4 ambient; +float4 diffuse; +float4 specular; +float shininess; + +sampler2D AmbientSampler; +sampler2D DiffuseSampler; +sampler2D BumpSampler; + +// input parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; + float4 normal : NORMAL; + float4 tangent : TANGENT; + float4 binormal : BINORMAL; + float2 texcoord0 : TEXCOORD0; +}; + +// input parameters for our pixel shader +// also the output parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float2 texcoord0 : TEXCOORD0; + float3 normal : TEXCOORD1; + float3 binormal : TEXCOORD2; + float3 tangent : TEXCOORD3; + float3 worldPosition : TEXCOORD4; +}; + +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + + // Transform position into clip space. + output.position = mul(input.position, worldViewProjection); + + // Transform the tangent frame into world space. + output.tangent = mul(input.tangent, worldInverseTranspose).xyz; + output.binormal = mul(input.binormal, worldInverseTranspose).xyz; + output.normal = mul(input.normal, worldInverseTranspose).xyz; + + // Pass through the texture coordinates. + output.texcoord0 = input.texcoord0; + + // Calculate surface position in world space. Used for lighting. + output.worldPosition = mul(input.position, world).xyz; + + return output; +} + +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + // Construct a transform from tangent space into world space. + float3x3 tangentToWorld = float3x3(input.tangent, + input.binormal, + input.normal); + + // Read the tangent space normal from the normal map and remove the bias so + // they are in the range [-0.5,0.5]. There is no need to scale by 2 because + // the vector will soon be normalized. + float3 tangentNormal = tex2D(BumpSampler, input.texcoord0.xy).xyz - + float3(0.5, 0.5, 0.5); + + // Transform the normal into world space. + float3 worldNormal = mul(tangentNormal, tangentToWorld); + worldNormal = normalize(worldNormal); + + // Read the diffuse and ambient colors. + float4 textureAmbient = float4(1, 1, 1, 1); + float4 textureDiffuse = float4(1, 1, 1, 1); + if (useTexture == 1) { + textureAmbient = tex2D(AmbientSampler, input.texcoord0.xy); + textureDiffuse = tex2D(DiffuseSampler, input.texcoord0.xy); + } + + // Apply lighting in world space in case the world transform contains scaling. + float3 surfaceToLight = normalize(lightWorldPos.xyz - + input.worldPosition.xyz); + float3 surfaceToView = normalize(viewInverse[3].xyz - input.worldPosition); + float3 halfVector = normalize(surfaceToLight + surfaceToView); + float4 litResult = lit(dot(worldNormal, surfaceToLight), + dot(worldNormal, halfVector), shininess); + float4 outColor = ambientIntensity * ambient * textureAmbient; + outColor += lightIntensity * (diffuse * textureDiffuse * litResult.y + + specular * litResult.z); + outColor += emissive; + return float4(outColor.rgb, diffuse.a * textureDiffuse.a); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/checker.shader b/o3d/samples/shaders/checker.shader new file mode 100644 index 0000000..d47c954 --- /dev/null +++ b/o3d/samples/shaders/checker.shader @@ -0,0 +1,117 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WORLDVIEWPROJECTION; +float4x4 worldInverseTranspose : WORLDINVERSETRANSPOSE; +float4x4 world : WORLD; + +// Default and light position +float4 ambientIntensity; +float4 ambient; +float4 diffuse; +float3 lightWorldPos; +float3 cameraEye; + +// input parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; + float4 normal : NORMAL; + float2 texcoord : TEXCOORD0; +}; + +// input parameters for our pixel shader +struct PixelShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; + float3 normal : TEXCOORD1; + float3 worldPosition : TEXCOORD2; +}; + +// function for getting the checker pattern +float4 checker(float2 uv) { + float checkSize = 4; + float fmodResult = fmod(floor(checkSize * uv.x) + floor(checkSize * uv.y), + 2.0); + if (fmodResult < 1) { + return float4(0, 1, 1, 1); // turquiose + } else { + return float4(1, 0, 1, 1); // magenta + } +} + +/** + * Our vertex shader. In the vertex shader, we calculate the lighting. + * Then we'll combine it with our checker pattern input the pixel shader. + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + + // Transform position into clip space. + output.position = mul(input.position, worldViewProjection); + + // Transform normal into world space, where we can do lighting + // calculations even if the world transform contains scaling. + output.normal = mul(input.normal, worldInverseTranspose).xyz; + + // Calculate surface position in world space. + output.worldPosition = mul(input.position, world).xyz; + + output.texcoord = input.texcoord; + + return output; +} + +/** + * Our pixel shader. We take the lighting color we got from the vertex sahder + * and combine it with our checker pattern. We only need to use the x + * coordinate of our input.col because we gave it uniform color + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + float3 surfaceToLight = normalize(lightWorldPos - input.worldPosition); + + float3 worldNormal = normalize(input.normal); + + // Apply diffuse lighting in world space in case the world transform + // contains scaling. + float4 check = checker(input.texcoord); + float4 directionalIntensity = saturate(dot(worldNormal, surfaceToLight)); + float4 outColor = (ambientIntensity * ambient + + directionalIntensity * diffuse) * check; + return float4(outColor.rgb, diffuse.a); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/diffuse.shader b/o3d/samples/shaders/diffuse.shader new file mode 100644 index 0000000..fcd03eb --- /dev/null +++ b/o3d/samples/shaders/diffuse.shader @@ -0,0 +1,96 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WorldViewProjection; +float4x4 worldInverseTranspose : WorldInverseTranspose; +float4x4 world : World; + +float4 ambientIntensity; +float4 lightIntensity; +float4 ambient; +float4 diffuse; +float3 lightWorldPos; + +// input parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; // Position vector of vertex + float4 normal : NORMAL; +}; + +// input parameters for our pixel shader +struct PixelShaderInput { + float4 position : POSITION; + float3 normal : TEXCOORD0; + float3 worldPosition : TEXCOORD1; +}; + +/** + * Our vertex shader. + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + + // Transform position into clip space. + output.position = mul(input.position, worldViewProjection); + + // Transform normal into world space, where we can do lighting + // calculations even if the world transform contains scaling. + output.normal = mul(input.normal, worldInverseTranspose).xyz; + + // Calculate surface position in world space. + output.worldPosition = mul(input.position, world).xyz; + + return output; +} + +/** + * Our pixel shader. + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + float3 surfaceToLight = normalize(lightWorldPos - input.worldPosition); + + float3 worldNormal = normalize(input.normal); + + // Apply diffuse lighting in world space in case the world transform + // contains scaling. + float4 directionalIntensity = lightIntensity * + saturate(dot(worldNormal, surfaceToLight)); + float4 outColor = ambientIntensity * ambient + directionalIntensity * diffuse; + return float4(outColor.rgb, diffuse.a); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/normal.shader b/o3d/samples/shaders/normal.shader new file mode 100644 index 0000000..24d52a8 --- /dev/null +++ b/o3d/samples/shaders/normal.shader @@ -0,0 +1,64 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WorldViewProjection; + +// input parameters for our vertex shader +struct VertexShaderInput { + float3 position : POSITION; + float3 normal : NORMAL; +}; + +// input parameters for our pixel shader +// also the output parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float3 normal : TEXCOORD0; +}; + +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(float4(input.position, 1.0), worldViewProjection); + output.normal = input.normal; + return output; +} + +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return float4(normalize(input.normal) * 0.5 + float3(0.5, 0.5, 0.5), 1); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/one-channel-texture.shader b/o3d/samples/shaders/one-channel-texture.shader new file mode 100644 index 0000000..f012b6d --- /dev/null +++ b/o3d/samples/shaders/one-channel-texture.shader @@ -0,0 +1,94 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +float4x4 worldViewProjection : WORLDVIEWPROJECTION; + +// The texture sampler is used to access the texture bitmap in the fragment +// shader. +sampler texSampler0; + +// input parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +// input parameters for our pixel shader +struct VertexShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +/** + * Our vertex shader + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(input.position, worldViewProjection); + output.texcoord = input.texcoord; + return output; +} + +/** + * Given the texture coordinates, our pixel shader grabs the corresponding + * color from the texture. + * + * NOTE: GL and D3D do NOT share compatible 1 channel texture formats. + * + * In D3D the sampler will return + * + * R = channel 0 + * G = const 1 + * B = const 1 + * A = const 1 + * + * In GL the sampler will return + * + * R = channel 0 + * G = channel 0 + * B = channel 0 + * A = channel 0 + * + * What that means is only R works across platforms. G, B and A are undefined + * and if you use them you'll get the wrong results. + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + // ** Use only valid channels. ** ---------+ + // | + // V + return tex2D(texSampler0, input.texcoord).rrrr; +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/phong-vertex-anim.shader b/o3d/samples/shaders/phong-vertex-anim.shader new file mode 100644 index 0000000..b3da37f --- /dev/null +++ b/o3d/samples/shaders/phong-vertex-anim.shader @@ -0,0 +1,95 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// This shader expects to be passed vertices that are part of an x-z plane mesh +// because it assumes the normal for each vertex before being offset by the +// animation is 0, 1, 0. + +uniform float4x4 viewProjection : VIEWPROJECTION; +uniform float3 lightWorldPos; +uniform float4 lightIntensity; +uniform float4x4 world : WORLD; +uniform float4x4 viewInverse : VIEWINVERSE; +uniform float4x4 worldInverseTranspose : WORLDINVERSETRANSPOSE; +uniform float4 ambientIntensity; +uniform float4 emissive; +uniform float4 ambient; +uniform float4 diffuse; +uniform float4 specular; +uniform float shininess; +uniform float time; + +struct VertexShaderInput { + float4 position : POSITION; +}; + +struct PixelShaderInput { + float4 position : POSITION; + float3 normal : TEXCOORD1; + float3 worldPosition : TEXCOORD4; +}; + +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + float4 worldPosition = mul(input.position, world); + float animValue = time + (worldPosition.x + worldPosition.z) * 0.4; + float animSin = sin(animValue); + float animCos = cos(animValue); + float4 position = float4( + worldPosition.x, + worldPosition.y + animSin, + worldPosition.z, + 1); + output.position = mul(position, viewProjection); + float3 normal = normalize(float3(animCos, abs(animSin), animCos)); + output.normal = mul(float4(normal, 0), worldInverseTranspose).xyz; + output.worldPosition = position.xyz; + + return output; +} + +float4 pixelShaderFunction(PixelShaderInput input) : COLOR { + float3 surfaceToLight = normalize(lightWorldPos - input.worldPosition); + float3 worldNormal = normalize(input.normal); + float3 surfaceToView = normalize(viewInverse[3].xyz - input.worldPosition); + float3 halfVector = normalize(surfaceToLight + surfaceToView); + float4 litResult = lit(dot(worldNormal, surfaceToLight), + dot(worldNormal, halfVector), shininess); + float4 outColor = ambientIntensity * ambient; + outColor += lightIntensity * (diffuse * litResult.y + + specular * litResult.z); + outColor += emissive; + return float4(outColor.rgb, diffuse.a); +} + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/phong-with-colormult.shader b/o3d/samples/shaders/phong-with-colormult.shader new file mode 100644 index 0000000..756b6bb --- /dev/null +++ b/o3d/samples/shaders/phong-with-colormult.shader @@ -0,0 +1,83 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +uniform float4x4 worldViewProjection : WORLDVIEWPROJECTION; +uniform float4x4 world : WORLD; +uniform float4x4 viewInverse : VIEWINVERSE; +uniform float4x4 worldInverseTranspose : WORLDINVERSETRANSPOSE; +uniform float3 lightWorldPos; +uniform float4 ambientIntensity; +uniform float4 lightIntensity; +uniform float4 emissive; +uniform float4 ambient; +uniform float4 colorMult; +uniform float4 diffuse; +uniform float4 specular; +uniform float shininess; + +struct VertexShaderInput { + float4 position : POSITION; + float4 normal : NORMAL; +}; + +struct PixelShaderInput { + float4 position : POSITION; + float3 normal : TEXCOORD1; + float3 worldPosition : TEXCOORD4; +}; + +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(input.position, worldViewProjection); + float3 worldPosition = mul(input.position, world).xyz; + output.normal = mul(input.normal, worldInverseTranspose).xyz; + output.worldPosition = worldPosition; + + return output; +} + +float4 pixelShaderFunction(PixelShaderInput input) : COLOR { + float3 surfaceToLight = normalize(lightWorldPos - input.worldPosition); + float3 worldNormal = normalize(input.normal); + float3 surfaceToView = normalize(viewInverse[3].xyz - input.worldPosition); + float3 halfVector = normalize(surfaceToLight + surfaceToView); + float4 litResult = lit(dot(worldNormal, surfaceToLight), + dot(worldNormal, halfVector), shininess); + float4 outColor = ambientIntensity * ambient * colorMult; + outColor += lightIntensity * (diffuse * colorMult * litResult.y + + specular * litResult.z); + outColor += emissive; + return float4(outColor.rgb, diffuse.a * colorMult.a); +} + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/solid-color.shader b/o3d/samples/shaders/solid-color.shader new file mode 100644 index 0000000..78d662f --- /dev/null +++ b/o3d/samples/shaders/solid-color.shader @@ -0,0 +1,74 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WORLDVIEWPROJECTION; +float4 color; + +// input parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; +}; + +// input parameters for our pixel shader +// also the output parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; +}; + +/** + * Vertex Shader + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + /** + * We transform each vertex by the world view projection matrix to bring + * it from world space to projection space. + * + * We return its color unchanged. + */ + PixelShaderInput output; + + output.position = mul(input.position, worldViewProjection); + return output; +} +/** + * Pixel Shader - pixel shader does nothing but return the color. + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return color; +} + +// Here we tell our effect file the functions +// which specify our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/tangent.shader b/o3d/samples/shaders/tangent.shader new file mode 100644 index 0000000..c0e953f --- /dev/null +++ b/o3d/samples/shaders/tangent.shader @@ -0,0 +1,64 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WorldViewProjection; + +// input parameters for our vertex shader +struct VertexShaderInput { + float3 position : POSITION; + float3 tangent : TANGENT; +}; + +// input parameters for our pixel shader +// also the output parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float3 tangent : TEXCOORD2; +}; + +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(float4(input.position, 1.0), worldViewProjection); + output.tangent = input.tangent; + return output; +} + +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return float4(normalize(input.tangent) * 0.5 + float3(0.5, 0.5, 0.5), 1); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/texture-colormult.shader b/o3d/samples/shaders/texture-colormult.shader new file mode 100644 index 0000000..b253e48 --- /dev/null +++ b/o3d/samples/shaders/texture-colormult.shader @@ -0,0 +1,74 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +float4x4 worldViewProjection : WORLDVIEWPROJECTION; + +// This parameter lets us adjust the color and fade things in or out. +float4 colorMult; + +// The texture sampler is used to access the texture bitmap in the fragment +// shader. +sampler texSampler0; + +// input parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +// input parameters for our pixel shader +struct PixelShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +/** + * Our vertex shader. + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(input.position, worldViewProjection); + output.texcoord = input.texcoord; + return output; +} + +/* Given the texture coordinates, our pixel shader grabs the corresponding + * color from the texture. + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return tex2D(texSampler0, input.texcoord) * colorMult; +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/texture-only.shader b/o3d/samples/shaders/texture-only.shader new file mode 100644 index 0000000..2f09dfe --- /dev/null +++ b/o3d/samples/shaders/texture-only.shader @@ -0,0 +1,71 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +float4x4 worldViewProjection : WORLDVIEWPROJECTION; + +// The texture sampler is used to access the texture bitmap in the fragment +// shader. +sampler texSampler0; + +// input parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +// input parameters for our pixel shader +struct VertexShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +/** + * Our vertex shader + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(input.position, worldViewProjection); + output.texcoord = input.texcoord; + return output; +} + +/* Given the texture coordinates, our pixel shader grabs the corresponding + * color from the texture. + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return tex2D(texSampler0, input.texcoord); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/vertex-color.shader b/o3d/samples/shaders/vertex-color.shader new file mode 100644 index 0000000..216f554 --- /dev/null +++ b/o3d/samples/shaders/vertex-color.shader @@ -0,0 +1,75 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// The 4x4 world view projection matrix. +float4x4 worldViewProjection : WORLDVIEWPROJECTION; + +// input parameters for our vertex shader +struct PixelShaderInput { + float4 position : POSITION; + float4 color : COLOR; +}; + +// input parameters for our pixel shader +// also the output parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; + float4 color: COLOR; +}; + +/** + * Vertex Shader - our vertex shader + */ +PixelShaderInput vertexShaderFunction(VertexShaderInput input) { + /** + * Our vertex shader projects the vertices onto the screen. + * We return its color unchanged. + */ + PixelShaderInput output; + + output.position = mul(input.position, worldViewProjection); + output.color = input.color; + return output; +} + +/** + * pixel shader does nothing but return whatever color it was given. + */ +float4 pixelShaderFunction(PixelShaderInput input): COLOR { + return input.color; +} + +// Here we tell our effect file the functions +// which specify our vertex and pixel shaders. + +// #o3d VertexShaderEntryPoint vertexShaderFunction +// #o3d PixelShaderEntryPoint pixelShaderFunction +// #o3d MatrixLoadOrder RowMajor diff --git a/o3d/samples/shaders/yuv2rgb.shader b/o3d/samples/shaders/yuv2rgb.shader new file mode 100644 index 0000000..359c2bc --- /dev/null +++ b/o3d/samples/shaders/yuv2rgb.shader @@ -0,0 +1,235 @@ +/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +// This shader takes a Y'UV420p image as a single greyscale plane, and +// converts it to RGB by sampling the correct parts of the image, and +// by converting the colorspace to RGB on the fly. + +// Projection matrix for the camera. +float4x4 worldViewProjection : WorldViewProjection; + +// These represent the image dimensions of the SOURCE IMAGE (not the +// Y'UV420p image). This is the same as the dimensions of the Y' +// portion of the Y'UV420p image. They are set from JavaScript. +float imageWidth; +float imageHeight; + +// This is the texture sampler where the greyscale Y'UV420p image is +// accessed. +sampler textureSampler; + +// These are the input/output parameters for our vertex shader +struct VertexShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +// These are the input/output parameters for our pixel shader. +struct PixelShaderInput { + float4 position : POSITION; + float2 texcoord : TEXCOORD0; // Texture coordinates +}; + +/** + * This fetches an individual Y pixel from the image, given the current + * texture coordinates (which range from 0 to 1 on the source texture + * image). They are mapped to the portion of the image that contains + * the Y component. + * + * @param position This is the position of the main image that we're + * trying to render, in parametric coordinates. + */ +float getYPixel(float2 position) { + position.y = (position.y * 2.0 / 3.0) + (1.0 / 3.0); + return tex2D(textureSampler, position).x; +} + +/** + * This does the crazy work of calculating the planar position (the + * position in the byte stream of the image) of the U or V pixel, and + * then converting that back to x and y coordinates, so that we can + * account for the fact that V is appended to U in the image, and the + * complications that causes (see below for a diagram). + * + * @param position This is the position of the main image that we're + * trying to render, in pixels. + * + * @param planarOffset This is an offset to add to the planar address + * we calculate so that we can find the U image after the V + * image. + */ +float2 mapCommon(float2 position, float planarOffset) { + planarOffset += (imageWidth * floor(position.y / 2.0)) / 2.0 + + floor((imageWidth - 1.0 - position.x) / 2.0); + float x = int(imageWidth - 1.0 - floor(fmod(planarOffset, imageWidth))); + float y = int(floor(planarOffset / imageWidth)); + return float2((x + 0.5) / imageWidth, (y + 0.5) / (1.5 * imageHeight)); +} + +/** + * This is a helper function for mapping pixel locations to a texture + * coordinate for the U plane. + * + * @param position This is the position of the main image that we're + * trying to render, in pixels. + */ +float2 mapU(float2 position) { + float planarOffset = (imageWidth * imageHeight) / 4.0; + return mapCommon(position, planarOffset); +} + +/** + * This is a helper function for mapping pixel locations to a texture + * coordinate for the V plane. + * + * @param position This is the position of the main image that we're + * trying to render, in pixels. + */ +float2 mapV(float2 position) { + return mapCommon(position, 0.0); +} + +/** + * The vertex shader does nothing but returns the position of the + * vertex using the world view projection matrix. + */ +PixelShaderInput vertexShaderMain(VertexShaderInput input) { + PixelShaderInput output; + output.position = mul(input.position, worldViewProjection); + + output.texcoord = input.texcoord; + return output; +} + +/** + * Given the texture coordinates, our pixel shader grabs the right + * value from each channel of the source image, converts it from Y'UV + * to RGB, and returns the result. + * + * Each U and V pixel provides color information for a 2x2 block of Y + * pixels. The U and V planes are just appended to the Y image. + * + * For images that have a height divisible by 4, things work out nicely. + * For images that are merely divisible by 2, it's not so nice + * (and YUV420 doesn't work for image sizes not divisible by 2). + * + * Here is a 6x6 image, with the layout of the planes of U and V. + * Notice that the V plane starts halfway through the last scanline + * that has U on it. + * + * 1 +---+---+---+---+---+---+ + * | Y | Y | Y | Y | Y | Y | + * +---+---+---+---+---+---+ + * | Y | Y | Y | Y | Y | Y | + * +---+---+---+---+---+---+ + * | Y | Y | Y | Y | Y | Y | + * +---+---+---+---+---+---+ + * | Y | Y | Y | Y | Y | Y | + * +---+---+---+---+---+---+ + * | Y | Y | Y | Y | Y | Y | + * +---+---+---+---+---+---+ + * | Y | Y | Y | Y | Y | Y | + * .3 +---+---+---+---+---+---+ + * | U | U | U | U | U | U | + * +---+---+---+---+---+---+ + * | U | U | U | V | V | V | + * +---+---+---+---+---+---+ + * | V | V | V | V | V | V | + * 0 +---+---+---+---+---+---+ + * 0 1 + * + * Here is a 4x4 image, where the U and V planes are nicely split into + * separable blocks. + * + * 1 +---+---+---+---+ + * | Y | Y | Y | Y | + * +---+---+---+---+ + * | Y | Y | Y | Y | + * +---+---+---+---+ + * | Y | Y | Y | Y | + * +---+---+---+---+ + * | Y | Y | Y | Y | + * .3 +---+---+---+---+ + * | U | U | U | U | + * +---+---+---+---+ + * | V | V | V | V | + * 0 +---+---+---+---+ + * 0 1 + * + */ +float4 pixelShaderMain(PixelShaderInput input): COLOR { + // Calculate what image pixel we're on, since we have to calculate + // the location in the image stream, using floor in several places + // which makes it hard to use parametric coordinates. + float2 pixelPosition = float2(floor(imageWidth * input.texcoord.x), + floor(imageHeight * input.texcoord.y)); + + // We can use the parametric coordinates to get the Y channel, since it's + // a relatively normal image. + float yChannel = getYPixel(input.texcoord); + + // As noted above, the U and V planes are smashed onto the end of + // the image in an odd way (in our 2D texture mapping, at least), so + // these mapping functions take care of that oddness. + float uChannel = tex2D(textureSampler, mapU(pixelPosition)).x; + float vChannel = tex2D(textureSampler, mapV(pixelPosition)).x; + + // This does the colorspace conversion from Y'UV to RGB as a matrix + // multiply. It also does the offset of the U and V channels from + // [0,1] to [-.5,.5] as part of the transform. + float4 channels = float4(yChannel, uChannel, vChannel, 1.0); + float3x4 conversion = float3x4(1.0, 0.0, 1.402, -0.701, + 1.0, -0.344, -0.714, 0.529, + 1.0, 1.772, 0.0, -0.886); + float3 rgb = mul(conversion, channels); + + // This is another Y'UV transform that can be used, but it doesn't + // accurately transform my source image. Your images may well fare + // better with it, however, considering they come from a different + // source, and because I'm not sure that my original was converted + // to Y'UV420p with the same RGB->YUV (or YCrCb) conversion as + // yours. + // + // float4 channels = float4(yChannel, uChannel, vChannel, 1.0); + // float3x4 conversion = float3x4(1.0, 0.0, 1.13983, -0.569915, + // 1.0, -0.39465, -0.58060, 0.487625, + // 1.0, 2.03211, 0.0, -1.016055); + // float3 rgb = mul(conversion, channels); + + return float4(rgb, 1.0); +} + +// Here we tell our effect file *which* functions are +// our vertex and pixel shaders. +// #o3d VertexShaderEntryPoint vertexShaderMain +// #o3d PixelShaderEntryPoint pixelShaderMain +// #o3d MatrixLoadOrder RowMajor |