Advertisement
Guest User

interiormapping.cg

a guest
Jan 8th, 2020
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float calculateNoise(float3 position, sampler2D noiseTexture)
  2. {
  3.     float3 noises = float3(tex2D(noiseTexture, float2(position.x, position.y) / 32).r,
  4.                                     tex2D(noiseTexture, float2(position.z, position.x) / 32).r,
  5.                                     tex2D(noiseTexture, float2(position.y, position.z) / 32).r);
  6.     float result = noises.x + noises.y + noises.z;
  7.     result /= 3;
  8.     return result;
  9. }
  10.  
  11. float calculateNoiseR(float3 position, sampler1D noiseTexture)
  12. {
  13.     float zNoise = tex1D(noiseTexture, position.z).r;
  14.     float yNoise = tex1D(noiseTexture, position.y + zNoise).r;
  15.     float xNoise = tex1D(noiseTexture, position.x + yNoise).r;
  16.     return xNoise;
  17. }
  18.  
  19.  
  20. float calculateNoiseG(float3 position, sampler1D noiseTexture)
  21. {
  22.     float zNoise = tex1D(noiseTexture, position.z).g;
  23.     float yNoise = tex1D(noiseTexture, position.y + zNoise).g;
  24.     float xNoise = tex1D(noiseTexture, position.x + yNoise).g;
  25.     return xNoise;
  26. }
  27.  
  28.  
  29. float4 mixInteriorWithDiffuseCube
  30.     (
  31.         float4      lighting,
  32.         float2      uv,
  33.         float3      reflection,
  34.         sampler2D   diffuseTexture,
  35.         samplerCUBE cubeTexture,
  36.         float4      interiorColour
  37.     )
  38. {
  39.     float4 diffuseColour = tex2D(diffuseTexture, uv);
  40.     float4 cubeColour = texCUBE(cubeTexture, reflection);
  41.     return lerp(diffuseColour * lighting, cubeColour + interiorColour, diffuseColour.a);
  42. }
  43.  
  44.  
  45. float4 mixInteriorWithDiffuseStrongCube
  46.     (
  47.         float4      lighting,
  48.         float2      uv,
  49.         float3      reflection,
  50.         sampler2D   diffuseTexture,
  51.         samplerCUBE cubeTexture,
  52.         float4      interiorColour
  53.     )
  54. {
  55.     float4 diffuseColour = tex2D(diffuseTexture, uv);
  56.     float4 cubeColour = texCUBE(cubeTexture, reflection) * 1.5;
  57.     float cubeAverage = (cubeColour.r + cubeColour.g + cubeColour.b) / 3;
  58.     float4 windowColour = lerp(interiorColour * 1.6f, cubeColour, cubeAverage);
  59.  
  60.     return lerp(diffuseColour * lighting, windowColour, diffuseColour.a);
  61. }
  62.  
  63.  
  64. void IM_VP
  65.     (
  66.         float4 position : POSITION,
  67.         float3 normal   : NORMAL,
  68.         float2 uv       : TEXCOORD0,
  69.  
  70.         out float4 oPosition        : POSITION,
  71.         out float4 oLighting        : COLOR0,
  72.         out float2 oUv              : TEXCOORD0,
  73.         out float3 oCopiedPosition  : TEXCOORD1, //object space
  74.         out float3 oNormal          : TEXCOORD2, //object space
  75.         out float3 oReflection      : TEXCOORD3,
  76.  
  77.         uniform float4x4    worldViewProjMatrix,
  78.         uniform float4x4    worldMatrix,
  79.         uniform float3      cameraPosition,     //world space
  80.         uniform float       uvMultiplier
  81.     )
  82. {
  83.     //put position in from object space to screen space
  84.     oPosition = mul(worldViewProjMatrix, position);
  85.  
  86.     oUv = uv * uvMultiplier;
  87.     oCopiedPosition = position;
  88.     oNormal = normal;
  89.    
  90.     //reflection is calculated in world space to keep it from rotating with the object
  91.     float4 worldPosition = mul(worldMatrix, position);
  92.     float3 worldNormal = mul(float3x3(worldMatrix), normal);
  93.     oReflection = reflect(worldPosition - cameraPosition, worldNormal);
  94.  
  95.     //calculate lighting with a hardcoded directed light and some ambient
  96.     float lightStrength = dot(normal, float3(0.5, 0.33166, 0.8));
  97.     oLighting = saturate(lightStrength) * float4(1, 1, 0.9, 1) + float4(0.3, 0.3, 0.4, 1);
  98. }
  99.  
  100.  
  101. void IM_Alternative_VP
  102.     (
  103.         float4 position : POSITION,
  104.         float3 normal   : NORMAL,
  105.         float2 uv       : TEXCOORD0,
  106.  
  107.         out float4 oPosition        : POSITION,
  108.         out float4 oLighting        : COLOR0,
  109.         out float2 oUv              : TEXCOORD0,
  110.         out float3 oCopiedPosition  : TEXCOORD1, //object space
  111.         out float3 oNormal          : TEXCOORD2, //object space
  112.         out float3 oReflection      : TEXCOORD3,
  113.  
  114.         uniform float4x4    worldViewProjMatrix,
  115.         uniform float4x4    worldMatrix,
  116.         uniform float3      cameraPosition      //world space
  117.     )
  118. {
  119.     //put position in from object space to screen space
  120.     oPosition = mul(worldViewProjMatrix, position);
  121.  
  122.     oUv = uv;
  123.     oUv.x = oUv.x * 2;
  124.     oCopiedPosition = position;
  125.     oNormal = normal;
  126.    
  127.     //reflection is calculated in world space to keep it from rotating with the object
  128.     float4 worldPosition = mul(worldMatrix, position);
  129.     float3 worldNormal = mul(float3x3(worldMatrix), normal);
  130.     oReflection = reflect(worldPosition - cameraPosition, worldNormal);
  131.  
  132.     //calculate lighting with a hardcoded directed light and some ambient
  133.     float lightStrength = dot(normal, float3(0.5, 0.33166, 0.8));
  134.     oLighting = saturate(lightStrength) * float4(1, 1, 0.9, 1) + float4(0.3, 0.3, 0.4, 1);
  135. }
  136.  
  137.  
  138. void Simple_VP
  139.     (
  140.         float4 position : POSITION,
  141.  
  142.         out float4 oPosition        : POSITION,
  143.  
  144.         uniform float4x4    worldViewProjMatrix
  145.     )
  146. {
  147.     //put position in from object space to screen space
  148.     oPosition = mul(worldViewProjMatrix, position);
  149. }
  150.  
  151.  
  152. void IM_Ceiling_FP
  153.     (
  154.         float3 position : TEXCOORD1,            //object space
  155.  
  156.         out float4 oColour : COLOR,
  157.  
  158.         uniform sampler2D   ceilingTexture,
  159.         uniform float3      wallFrequencies,
  160.         uniform float3      cameraPosition      //object space
  161.     )
  162. {
  163.     //get the vector from camera to surface
  164.     float3 direction = position - cameraPosition;
  165.  
  166.     //ceiling height
  167.     float height = (floor(position.y * wallFrequencies.y) + step(0, direction.y)) / wallFrequencies.y;
  168.  
  169.     //how much of the ray is needed to get from the cameraPosition to the ceiling
  170.     float rayFraction = (height - cameraPosition.y) / direction.y;
  171.  
  172.     //(x,z)-coordinate of intersection with ceiling
  173.     float2 intersection = 4 * (cameraPosition + rayFraction * direction).xz;
  174.  
  175.     //use the intersection as the texture coordinates for the ceiling
  176.     oColour = tex2D(ceilingTexture, intersection);
  177. }
  178.  
  179.  
  180. void IM_CeilingFloor_FP
  181.     (
  182.         float3 position : TEXCOORD1,            //object space
  183.  
  184.         out float4 oColour : COLOR,
  185.  
  186.         uniform sampler2D   ceilingTexture,
  187.         uniform sampler2D   floorTexture,
  188.         uniform float3      wallFrequencies,
  189.         uniform float3      cameraPosition      //object space
  190.     )
  191. {
  192.     //get the vector from camera to surface
  193.     float3 direction = position - cameraPosition;
  194.  
  195.     //ceiling height
  196.     float height = (floor(position.y * wallFrequencies.y) + step(0, direction.y)) / wallFrequencies.y;
  197.  
  198.     //how much of the ray is needed to get from the cameraPosition to the ceiling
  199.     float rayFraction = (height - cameraPosition.y) / direction.y;
  200.  
  201.     //texture-coordinates of intersection with ceiling
  202.     float2 intersection = 4 * (cameraPosition + rayFraction * direction).xz;
  203.  
  204.     //use the intersection as the texture coordinates for the ceiling and floor
  205.     float4 ceilingColour = tex2D(ceilingTexture, intersection);
  206.     float4 floorColour = tex2D(floorTexture, intersection);
  207.    
  208.     //choose between ceiling and floor
  209.     float4 interiorColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  210.  
  211.     oColour = interiorColour;
  212. }
  213.  
  214.  
  215. void IM_DiffuseCube_FP
  216.     (
  217.         float4 lighting     : COLOR0,
  218.         float2 uv           : TEXCOORD0,
  219.         float3 position     : TEXCOORD1,    //object space
  220.         float3 reflection   : TEXCOORD3,    //object space
  221.  
  222.         out float4 oColour  : COLOR,
  223.  
  224.         uniform sampler2D   diffuseTexture,
  225.         uniform samplerCUBE cubeTexture
  226.     )
  227. {
  228.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, float4(0, 0, 0, 0));
  229. }
  230.  
  231.  
  232. void IM_DiffuseCube_Ceiling_FP
  233.     (
  234.         float4 lighting     : COLOR0,
  235.         float2 uv           : TEXCOORD0,
  236.         float3 position     : TEXCOORD1,            //object space
  237.         float3 reflection   : TEXCOORD3,        //object space
  238.  
  239.         out float4 oColour : COLOR,
  240.  
  241.         uniform sampler2D   diffuseTexture,
  242.         uniform sampler2D   ceilingTexture,
  243.         uniform samplerCUBE cubeTexture,
  244.         uniform float3      wallFrequencies,
  245.         uniform float3      cameraPosition      //object space
  246.     )
  247. {
  248.     //get the vector from camera to surface
  249.     float3 direction = normalize(position - cameraPosition);
  250.  
  251.     //ceiling height
  252.     float height = (floor(position.y * wallFrequencies.y) + step(0, direction.y)) / wallFrequencies.y;
  253.  
  254.     //how much of the ray is needed to get from the cameraPosition to the ceiling
  255.     float rayFraction = (height - cameraPosition.y) / direction.y;
  256.  
  257.     //texture-coordinates of intersection with ceiling
  258.     float2 intersection = 4 * (cameraPosition + rayFraction * direction).xz;
  259.  
  260.     //get the diffuse colour
  261.     float4 diffuseColour = tex2D(diffuseTexture, uv);
  262.  
  263.     //use the intersection as the texture coordinates for the ceiling
  264.     float4 ceilingColour = tex2D(ceilingTexture, intersection);
  265.  
  266.     //get the reflections in the window
  267.     float4 cubeColour = texCUBE(cubeTexture, reflection);
  268.  
  269.     //horizontality is used to hide the ceiling map when looking almost horizontally
  270.     //(the ceiling would seem to go to infinity otherwise)
  271.     float horizontality = 1 - abs(direction.y);
  272.     horizontality = pow(horizontality, 16);
  273.    
  274.     //mix what is seen in the windows
  275.     float4 windowColour = lerp(ceilingColour, cubeColour * 0.5f, horizontality) + cubeColour * 0.2f;
  276.  
  277.     //combine the result (alpha is where windows are)
  278.     oColour = lerp(diffuseColour * lighting, windowColour, diffuseColour.a);
  279. }
  280.  
  281.  
  282. void IM_DiffuseCube_CeilingFloor_FP
  283.     (
  284.         float4 lighting     : COLOR0,
  285.         float2 uv           : TEXCOORD0,
  286.         float3 position     : TEXCOORD1,        //object space
  287.         float3 reflection   : TEXCOORD3,        //object space
  288.  
  289.         out float4 oColour : COLOR,
  290.  
  291.         uniform sampler2D   diffuseTexture,
  292.         uniform sampler2D   ceilingTexture,
  293.         uniform sampler2D   floorTexture,
  294.         uniform samplerCUBE cubeTexture,
  295.         uniform float3      wallFrequencies,
  296.         uniform float3      cameraPosition      //object space
  297.     )
  298. {
  299.     //get the vector from camera to surface
  300.     float3 direction = normalize(position - cameraPosition);
  301.  
  302.     //ceiling height
  303.     float height = (floor(position.y * wallFrequencies.y) + step(0, direction.y)) / wallFrequencies.y;
  304.  
  305.     //how much of the ray is needed to get from the cameraPosition to the ceiling
  306.     float rayFraction = (height - cameraPosition.y) / direction.y;
  307.  
  308.     //texture-coordinates of intersection with ceiling
  309.     float2 intersection = 4 * (cameraPosition + rayFraction * direction).xz;
  310.  
  311.     //get the diffuse colour
  312.     float4 diffuseColour = tex2D(diffuseTexture, uv);
  313.  
  314.     //use the intersection as the texture coordinates for the ceiling and floor
  315.     float4 ceilingColour = tex2D(ceilingTexture, intersection);
  316.     float4 floorColour = tex2D(floorTexture, intersection);
  317.  
  318.     //choose between ceiling and floor
  319.     float4 interiorColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  320.  
  321.     //get the reflections in the window
  322.     float4 cubeColour = texCUBE(cubeTexture, reflection);
  323.  
  324.     //horizontality is used to hide the ceiling map when looking almost horizontally
  325.     //(the ceiling would seem to go to infinity otherwise)
  326.     float horizontality = 1 - abs(direction.y);
  327.     horizontality = pow(horizontality, 16);
  328.  
  329.     //mix what is seen in the windows
  330.     float4 windowColour = lerp(interiorColour, cubeColour, horizontality) + cubeColour * 0.2f;
  331.  
  332.     //combine the result (alpha is where windows are)
  333.     oColour = lerp(diffuseColour * lighting, windowColour, diffuseColour.a);
  334. }
  335.  
  336.  
  337. float4 calculateUntexturedRoomsColour
  338.     (
  339.         float3 position,            //object space
  340.         float3 wallFrequencies,
  341.         float3 cameraPosition       //object space
  342.     )
  343. {
  344.     //get the vector from camera to surface
  345.     float3 direction = position - cameraPosition;
  346.  
  347.     //calculate wall locations
  348.     float3 walls = (floor(position * wallFrequencies) + step(float3(0, 0, 0), direction)) / wallFrequencies;
  349.  
  350.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  351.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  352.  
  353.     //choose normals for each of the three possible walls
  354.     //(this depends on whether we are looking in positive or negative directions)
  355.     float3 signs = step(direction, float3(0, 0, 0)) * 2 - 1;
  356.     float3 normalX = float3(1, 0, 0) * signs.x;
  357.     float3 normalY = float3(0, 1, 0) * signs.y;
  358.     float3 normalZ = float3(0, 0, 1) * signs.z;
  359.  
  360.     //choose the closest of these walls for the normal for the lighting
  361.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  362.     float xVSy = step(rayFractions.x, rayFractions.y);
  363.     float rayFraction_xVSy = lerp(rayFractions.y, rayFractions.x, xVSy);
  364.     float3 interiorNormal = lerp(normalY, normalX, xVSy);
  365.     interiorNormal = lerp(normalZ, interiorNormal, step(rayFraction_xVSy, rayFractions.z));
  366.  
  367.     float lightStrength = dot(interiorNormal, float3(0.5, 0.33166, 0.8));
  368.     float4 interiorLight = saturate(lightStrength) * float4(1, 1, 0.9, 1) + float4(0.3, 0.3, 0.4, 1);
  369.    
  370.     return interiorLight;
  371. }
  372.  
  373.  
  374. void IM_UntexturedRooms_FP
  375.     (
  376.         float3 position : TEXCOORD1,            //object space
  377.  
  378.         out float4 oColour : COLOR,
  379.  
  380.         uniform float3      wallFrequencies,
  381.         uniform float3      cameraPosition      //object space
  382.     )
  383. {
  384.     oColour = calculateUntexturedRoomsColour(position, wallFrequencies, cameraPosition);
  385. }
  386.  
  387.  
  388. void IM_DiffuseCube_UntexturedRooms_FP
  389.     (
  390.         float4 lighting     : COLOR0,
  391.         float2 uv           : TEXCOORD0,
  392.         float3 position     : TEXCOORD1,        //object space
  393.         float3 reflection   : TEXCOORD3,        //object space
  394.  
  395.         out float4 oColour : COLOR,
  396.  
  397.         uniform sampler2D   diffuseTexture,
  398.         uniform samplerCUBE cubeTexture,
  399.         uniform float3      wallFrequencies,
  400.         uniform float3      cameraPosition      //object space
  401.     )
  402. {
  403.     float4 interiorColour = calculateUntexturedRoomsColour(position, wallFrequencies, cameraPosition);
  404.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  405. }
  406.  
  407.  
  408. float4 calculateUniformTexturedRoomsColour
  409.     (
  410.         float3      position,           //object space
  411.         sampler2D   uniformTexture,
  412.         float3      wallFrequencies,
  413.         float3      cameraPosition      //object space
  414.     )
  415. {
  416.     //get the vector from camera to surface
  417.     float3 direction = position - cameraPosition;
  418.  
  419.     //calculate wall locations
  420.     float3 walls = (floor(position * wallFrequencies) + step(float3(0, 0, 0), direction)) / wallFrequencies;
  421.  
  422.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  423.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  424.    
  425.     //texture-coordinates of intersections with walls
  426.     float2 intersectionX = (cameraPosition + rayFractions.x * direction).zy;
  427.     float2 intersectionY = (cameraPosition + rayFractions.y * direction).xz;
  428.     float2 intersectionZ = (cameraPosition + rayFractions.z * direction).xy;
  429.  
  430.     //choose normals for each of the three possible walls
  431.     //(this depends on whether we are looking in positive or negative directions)
  432.     float3 signs = step(direction, float3(0, 0, 0)) * 2 - 1;
  433.     float3 normalX = float3(1, 0, 0) * signs.x;
  434.     float3 normalY = float3(0, 1, 0) * signs.y;
  435.     float3 normalZ = float3(0, 0, 1) * signs.z;
  436.  
  437.     //choose the closest of these walls for the normal for the lighting
  438.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  439.     float xVSy = step(rayFractions.x, rayFractions.y);
  440.     float rayFraction_xVSy = lerp(rayFractions.y, rayFractions.x, xVSy);
  441.     float2 intersection = lerp(intersectionY, intersectionX, xVSy);
  442.     float3 interiorNormal = lerp(normalY, normalX, xVSy);
  443.     float xyVSz = step(rayFraction_xVSy, rayFractions.z);
  444.     interiorNormal = lerp(normalZ, interiorNormal, xyVSz);
  445.     intersection = lerp(intersectionZ, intersection, xyVSz);
  446.  
  447.     float lightStrength = dot(interiorNormal, float3(0.5, 0.33166, 0.8));
  448.     float4 interiorLight = saturate(lightStrength) * float4(1, 1, 0.9, 1) + float4(0.3, 0.3, 0.4, 1);
  449.  
  450.     float4 wallColour = tex2D(uniformTexture, 10 * intersection);
  451.  
  452.     return interiorLight * wallColour;
  453. }
  454.  
  455.  
  456. void IM_UniformTexturedRooms_FP
  457.     (
  458.         float3 position : TEXCOORD1,            //object space
  459.  
  460.         out float4 oColour : COLOR,
  461.  
  462.         uniform sampler2D   uniformTexture,
  463.         uniform float3      wallFrequencies,
  464.         uniform float3      cameraPosition      //object space
  465.     )
  466. {
  467.     oColour = calculateUniformTexturedRoomsColour(position, uniformTexture, wallFrequencies, cameraPosition);
  468. }
  469.  
  470.  
  471. void IM_DiffuseCube_UniformTexturedRooms_FP
  472.     (
  473.         float4 lighting     : COLOR0,
  474.         float2 uv           : TEXCOORD0,
  475.         float3 position     : TEXCOORD1,        //object space
  476.         float3 reflection   : TEXCOORD3,        //object space
  477.  
  478.         out float4 oColour : COLOR,
  479.  
  480.         uniform sampler2D   diffuseTexture,
  481.         uniform sampler2D   uniformTexture,
  482.         uniform samplerCUBE cubeTexture,
  483.         uniform float3      wallFrequencies,
  484.         uniform float3      cameraPosition      //object space
  485.     )
  486. {
  487.     float4 interiorColour = calculateUniformTexturedRoomsColour(position, uniformTexture, wallFrequencies, cameraPosition);
  488.  
  489.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  490. }
  491.  
  492.  
  493. float4 calculateCeilingTexturedRoomsColour
  494.     (
  495.         float3      position,           //object space
  496.         sampler2D   ceilingTexture,
  497.         float3      wallFrequencies,
  498.         float3      cameraPosition      //object space
  499.     )
  500. {
  501.     //get the vector from camera to surface
  502.     float3 direction = position - cameraPosition;
  503.  
  504.     //calculate wall locations
  505.     float3 walls = (floor(position * wallFrequencies) + step(float3(0, 0, 0), direction)) / wallFrequencies;
  506.  
  507.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  508.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  509.  
  510.     //texture-coordinates of intersection with ceiling
  511.     float2 intersection = 4 * (cameraPosition + rayFractions.y * direction).xz;
  512.  
  513.     //use the intersection as the texture coordinates for the ceiling
  514.     float4 ceilingColour = tex2D(ceilingTexture, intersection);
  515.     float ceilingWeight = step(0, direction.y);
  516.  
  517.     //choose normals for each of the three possible walls
  518.     //(this depends on whether we are looking in positive or negative directions)
  519.     float3 signs = step(direction, float3(0, 0, 0)) * 2 - 1;
  520.     float3 normalX = float3(1, 0, 0) * signs.x;
  521.     float3 normalY = float3(0, 1, 0) * signs.y;
  522.     float3 normalZ = float3(0, 0, 1) * signs.z;
  523.  
  524.     //choose the closest of these walls for the normal for the lighting
  525.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  526.     float xVSy = step(rayFractions.x, rayFractions.y);
  527.     float rayFraction_xVSy = lerp(rayFractions.y, rayFractions.x, xVSy);
  528.     float3 interiorNormal = lerp(normalY, normalX, xVSy);
  529.     ceilingWeight = lerp(ceilingWeight, 0, xVSy);
  530.     float xyVSz = step(rayFraction_xVSy, rayFractions.z);
  531.     interiorNormal = lerp(normalZ, interiorNormal, xyVSz);
  532.     ceilingWeight *= xyVSz;
  533.  
  534.     float lightStrength = dot(interiorNormal, float3(0.5, 0.33166, 0.8));
  535.     float4 interiorLight = saturate(lightStrength) * float4(1, 1, 0.9, 1) + float4(0.3, 0.3, 0.4, 1);
  536.  
  537.     return lerp(interiorLight, ceilingColour, ceilingWeight);
  538. }
  539.  
  540.  
  541. void IM_CeilingTexturedRooms_FP
  542.     (
  543.         float3 position : TEXCOORD1,            //object space
  544.  
  545.         out float4 oColour : COLOR,
  546.  
  547.         uniform sampler2D   ceilingTexture,
  548.         uniform float3      wallFrequencies,
  549.         uniform float3      cameraPosition      //object space
  550.     )
  551. {
  552.     oColour = calculateCeilingTexturedRoomsColour(position, ceilingTexture, wallFrequencies, cameraPosition);
  553. }
  554.  
  555.  
  556. void IM_DiffuseCube_CeilingTexturedRooms_FP
  557.     (
  558.         float4 lighting     : COLOR0,
  559.         float2 uv           : TEXCOORD0,
  560.         float3 position     : TEXCOORD1,        //object space
  561.         float3 reflection   : TEXCOORD3,        //object space
  562.  
  563.         out float4 oColour : COLOR,
  564.  
  565.         uniform sampler2D   diffuseTexture,
  566.         uniform sampler2D   ceilingTexture,
  567.         uniform samplerCUBE cubeTexture,
  568.         uniform float3      wallFrequencies,
  569.         uniform float3      cameraPosition      //object space
  570.     )
  571. {
  572.     float4 interiorColour = calculateCeilingTexturedRoomsColour(position, ceilingTexture, wallFrequencies, cameraPosition);
  573.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  574. }
  575.  
  576.  
  577. float4 calculateFullyTexturedRoomsColour
  578.     (
  579.         float3      position,           //object space
  580.         sampler2D   ceilingTexture,
  581.         sampler2D   floorTexture,
  582.         sampler2D   wallXYTexture,
  583.         sampler2D   wallZYTexture,
  584.         float3      wallFrequencies,
  585.         float3      cameraPosition      //object space
  586.     )
  587. {
  588.     //get the vector from camera to surface
  589.     float3 direction = position - cameraPosition;
  590.  
  591.     //calculate wall locations
  592.     float3 walls = (floor(position * wallFrequencies) + step(float3(0, 0, 0), direction)) / wallFrequencies;
  593.  
  594.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  595.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  596.  
  597.     //texture-coordinates of intersections
  598.     float2 intersectionXY = 4 * (cameraPosition + rayFractions.z * direction).xy;
  599.     float2 intersectionXZ = 4 * (cameraPosition + rayFractions.y * direction).xz;
  600.     float2 intersectionZY = 4 * (cameraPosition + rayFractions.x * direction).zy;
  601.  
  602.     //use the intersection as the texture coordinates for the ceiling
  603.     float4 ceilingColour = tex2D(ceilingTexture, intersectionXZ);
  604.     float4 floorColour = tex2D(floorTexture, intersectionXZ);
  605.     float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  606.     float4 wallXYColour = tex2D(wallXYTexture, intersectionXY);
  607.     float4 wallZYColour = tex2D(wallZYTexture, intersectionZY);
  608.  
  609.     //choose the closest of these walls for the normal for the lighting
  610.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  611.     float xVSz = step(rayFractions.x, rayFractions.z);
  612.     float4 interiorColour = lerp(wallXYColour, wallZYColour, xVSz);
  613.     float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
  614.     float xzVSy = step(rayFraction_xVSz, rayFractions.y);
  615.     interiorColour = lerp(verticalColour, interiorColour, xzVSy);
  616.    
  617.     return interiorColour;
  618. }
  619.  
  620.  
  621. void IM_FullyTexturedRooms_FP
  622.     (
  623.         float3 position : TEXCOORD1,            //object space
  624.  
  625.         out float4 oColour : COLOR,
  626.  
  627.         uniform sampler2D   ceilingTexture,
  628.         uniform sampler2D   floorTexture,
  629.         uniform sampler2D   wallXYTexture,
  630.         uniform sampler2D   wallZYTexture,
  631.         uniform float3      wallFrequencies,
  632.         uniform float3      cameraPosition      //object space
  633.     )
  634. {
  635.     oColour = calculateFullyTexturedRoomsColour(position, ceilingTexture,
  636.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, cameraPosition);
  637. }
  638.  
  639.  
  640. void IM_DiffuseCube_FullyTexturedRooms_FP
  641.     (
  642.         float4 lighting     : COLOR0,
  643.         float2 uv           : TEXCOORD0,
  644.         float3 position     : TEXCOORD1,        //object space
  645.         float3 reflection   : TEXCOORD3,        //object space
  646.  
  647.         out float4 oColour : COLOR,
  648.  
  649.         uniform sampler2D   ceilingTexture,
  650.         uniform sampler2D   floorTexture,
  651.         uniform sampler2D   wallXYTexture,
  652.         uniform sampler2D   wallZYTexture,
  653.         uniform sampler2D   diffuseTexture,
  654.         uniform samplerCUBE cubeTexture,
  655.         uniform float3      wallFrequencies,
  656.         uniform float3      cameraPosition      //object space
  657.     )
  658. {
  659.     float4 interiorColour = calculateFullyTexturedRoomsColour(position, ceilingTexture,
  660.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, cameraPosition);
  661.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  662. }
  663.  
  664.  
  665. void IM_DiffuseStrongCube_FullyTexturedRooms_FP
  666.     (
  667.         float4 lighting     : COLOR0,
  668.         float2 uv           : TEXCOORD0,
  669.         float3 position     : TEXCOORD1,        //object space
  670.         float3 reflection   : TEXCOORD3,        //object space
  671.  
  672.         out float4 oColour : COLOR,
  673.  
  674.         uniform sampler2D   ceilingTexture,
  675.         uniform sampler2D   floorTexture,
  676.         uniform sampler2D   wallXYTexture,
  677.         uniform sampler2D   wallZYTexture,
  678.         uniform sampler2D   diffuseTexture,
  679.         uniform samplerCUBE cubeTexture,
  680.         uniform float3      wallFrequencies,
  681.         uniform float3      cameraPosition      //object space
  682.     )
  683. {
  684.     float4 interiorColour = 1.2 * calculateFullyTexturedRoomsColour(position, ceilingTexture,
  685.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, cameraPosition);
  686.  
  687.     oColour = mixInteriorWithDiffuseStrongCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  688. }
  689.  
  690.  
  691. float4 calculateFullyTexturedRoomsWithLightVariationColour
  692.     (
  693.         float3      position,           //object space
  694.         sampler2D   noiseTexture,
  695.         sampler2D   ceilingTexture,
  696.         sampler2D   floorTexture,
  697.         sampler2D   wallXYTexture,
  698.         sampler2D   wallZYTexture,
  699.         float3      wallFrequencies,
  700.         float       lightThreshold,
  701.         float3      cameraPosition      //object space
  702.     )
  703. {
  704.     //get the vector from camera to surface
  705.     float3 direction = position - cameraPosition;
  706.  
  707.     //calculate wall locations part 1
  708.     float3 walls = floor(position * wallFrequencies);
  709.  
  710.     //calculate variation in the lighting per room
  711.     float lightVariation = step(calculateNoise(walls, noiseTexture), lightThreshold);
  712.     lightVariation = lightVariation * 0.7 + 0.3;
  713.  
  714.     //calculate wall locations part 2
  715.     walls += step(float3(0, 0, 0), direction);
  716.     walls /= wallFrequencies;
  717.  
  718.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  719.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  720.  
  721.     //texture-coordinates of intersections
  722.     float2 intersectionXY = 4 * (cameraPosition + rayFractions.z * direction).xy;
  723.     float2 intersectionXZ = 4 * (cameraPosition + rayFractions.y * direction).xz;
  724.     float2 intersectionZY = 4 * (cameraPosition + rayFractions.x * direction).zy;
  725.  
  726.     //use the intersection as the texture coordinates for the ceiling
  727.     float4 ceilingColour = tex2D(ceilingTexture, intersectionXZ);
  728.     float4 floorColour = tex2D(floorTexture, intersectionXZ);
  729.     float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  730.     float4 wallXYColour = tex2D(wallXYTexture, intersectionXY);
  731.     float4 wallZYColour = tex2D(wallZYTexture, intersectionZY);
  732.  
  733.     //choose the closest of these walls for the normal for the lighting
  734.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  735.     float xVSz = step(rayFractions.x, rayFractions.z);
  736.     float4 interiorColour = lerp(wallXYColour, wallZYColour, xVSz);
  737.     float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
  738.     float xzVSy = step(rayFraction_xVSz, rayFractions.y);
  739.     interiorColour = lerp(verticalColour, interiorColour, xzVSy);
  740.  
  741.     return interiorColour * lightVariation;
  742. }
  743.  
  744.  
  745. void IM_FullyTexturedRooms_LightVariation_FP
  746.     (
  747.         float3 position : TEXCOORD1,            //object space
  748.  
  749.         out float4 oColour : COLOR,
  750.  
  751.         uniform sampler2D   noiseTexture,
  752.         uniform sampler2D   ceilingTexture,
  753.         uniform sampler2D   floorTexture,
  754.         uniform sampler2D   wallXYTexture,
  755.         uniform sampler2D   wallZYTexture,
  756.         uniform float3      wallFrequencies,
  757.         uniform float       lightThreshold,
  758.         uniform float3      cameraPosition      //object space
  759.     )
  760. {
  761.     oColour = calculateFullyTexturedRoomsWithLightVariationColour(position, noiseTexture, ceilingTexture,
  762.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, lightThreshold, cameraPosition);
  763. }
  764.  
  765.  
  766. void IM_DiffuseCube_FullyTexturedRooms_LightVariation_FP
  767.     (
  768.         float4 lighting     : COLOR0,
  769.         float2 uv           : TEXCOORD0,
  770.         float3 position     : TEXCOORD1,        //object space
  771.         float3 reflection   : TEXCOORD3,        //object space
  772.  
  773.         out float4 oColour : COLOR,
  774.  
  775.         uniform sampler2D   noiseTexture,
  776.         uniform sampler2D   ceilingTexture,
  777.         uniform sampler2D   floorTexture,
  778.         uniform sampler2D   wallXYTexture,
  779.         uniform sampler2D   wallZYTexture,
  780.         uniform sampler2D   diffuseTexture,
  781.         uniform samplerCUBE cubeTexture,
  782.         uniform float3      wallFrequencies,
  783.         uniform float       lightThreshold,
  784.         uniform float3      cameraPosition      //object space
  785.     )
  786. {
  787.     float4 interiorColour = calculateFullyTexturedRoomsWithLightVariationColour(position, noiseTexture, ceilingTexture,
  788.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, lightThreshold, cameraPosition);
  789.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  790. }
  791.  
  792.  
  793. float4 calculateFullyTexturedRoomWithAlphaPlaneColour
  794.     (
  795.         float3      position,           //object space
  796.         float3      normal,             //object space
  797.         sampler2D   ceilingTexture,
  798.         sampler2D   floorTexture,
  799.         sampler2D   wallXYTexture,
  800.         sampler2D   wallZYTexture,
  801.         sampler2D   alphaPlaneTexture,
  802.         float3      wallFrequencies,
  803.         float       alphaPlaneDistance,
  804.         float3      cameraPosition      //object space
  805.     )
  806. {
  807.     //get the vector from camera to surface
  808.     float3 direction = position - cameraPosition;
  809.  
  810.     //calculate wall locations
  811.     float3 walls = (floor(position * wallFrequencies) + step(float3(0, 0, 0), direction)) / wallFrequencies;
  812.  
  813.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  814.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  815.  
  816.     //texture-coordinates of intersections
  817.     float2 intersectionXY = 4 * (cameraPosition + rayFractions.z * direction).xy;
  818.     float2 intersectionXZ = 4 * (cameraPosition + rayFractions.y * direction).xz;
  819.     float2 intersectionZY = 4 * (cameraPosition + rayFractions.x * direction).zy;
  820.  
  821.     //use the intersection as the texture coordinates for the ceiling
  822.     float4 ceilingColour = tex2D(ceilingTexture, intersectionXZ);
  823.     float4 floorColour = tex2D(floorTexture, intersectionXZ);
  824.     float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  825.     float4 wallXYColour = tex2D(wallXYTexture, intersectionXY);
  826.     float4 wallZYColour = tex2D(wallZYTexture, intersectionZY);
  827.  
  828.     //choose the closest of these walls for the normal for the lighting
  829.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  830.     float xVSz = step(rayFractions.x, rayFractions.z);
  831.     float4 wallsColour = lerp(wallXYColour, wallZYColour, xVSz);
  832.     float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
  833.     float xzVSy = step(rayFraction_xVSz, rayFractions.y);
  834.     wallsColour = lerp(verticalColour, wallsColour, xzVSy);
  835.     float rayFractionWinner = lerp(rayFractions.y, rayFraction_xVSz, xzVSy);
  836.    
  837.  
  838.     //calculate intersection with the alpha plane
  839.    
  840.     //The alpha plane uses the standerd plane equation math with variables a,b,c,d to define the plane.
  841.     //a,b,c of the plane equation of the alpha plane is just the normal of the wall.
  842.     //d can be calculated from a,b,c and a point on the plane.
  843.     //This would normally be done like this:
  844.     //    float3 pointOnAlphaPlane = position - normal * alphaPlaneDistance;
  845.     //    float d = dot(pointOnAlphaPlane, normal);
  846.     //But is optimized to this:
  847.     float d = dot(position, normal) - alphaPlaneDistance;
  848.  
  849.     //calculate the collision between the ray and the alpha plane
  850.     float alphaRayFraction = (d - dot(cameraPosition, normal)) / (dot(direction, normal));
  851.     float3 alphaPlaneIntersectionPoint = alphaRayFraction * direction + cameraPosition;
  852.  
  853.     float uvXWeight = abs(dot(float2(normal.x, normal.z), float2(1, 0)));
  854.     float2 alphaPlaneUV = float2(
  855.         alphaPlaneIntersectionPoint.x + alphaPlaneIntersectionPoint.z,
  856.         -alphaPlaneIntersectionPoint.y);
  857.     float4 alphaPlaneColour = tex2D(alphaPlaneTexture, alphaPlaneUV * 4);
  858.  
  859.     float alphaPlaneWeight = step(alphaRayFraction, rayFractionWinner) * (1 - alphaPlaneColour.a);
  860.  
  861.  
  862.     float4 interiorColour = lerp(wallsColour, alphaPlaneColour, alphaPlaneWeight);
  863.     return interiorColour;
  864. }
  865.  
  866.  
  867. void IM_FullyTexturedRooms_AlphaPlane_FP
  868.     (
  869.         float3 position     : TEXCOORD1,        //object space
  870.         float3 normal       : TEXCOORD2,        //object space
  871.  
  872.         out float4 oColour : COLOR,
  873.  
  874.         uniform sampler2D   ceilingTexture,
  875.         uniform sampler2D   floorTexture,
  876.         uniform sampler2D   wallXYTexture,
  877.         uniform sampler2D   wallZYTexture,
  878.         uniform sampler2D   alphaPlaneTexture,
  879.         uniform float3      wallFrequencies,
  880.         uniform float       alphaPlaneDistance,
  881.         uniform float3      cameraPosition      //object space
  882.     )
  883. {
  884.     float4 interiorColour = calculateFullyTexturedRoomWithAlphaPlaneColour(position, normal,
  885.         ceilingTexture, floorTexture, wallXYTexture, wallZYTexture, alphaPlaneTexture,
  886.         wallFrequencies, alphaPlaneDistance, cameraPosition);
  887.  
  888.     oColour = interiorColour;
  889. }
  890.  
  891.  
  892. void IM_DiffuseCube_FullyTexturedRooms_AlphaPlane_FP
  893.     (
  894.         float4 lighting     : COLOR0,
  895.         float2 uv           : TEXCOORD0,
  896.         float3 position     : TEXCOORD1,        //object space
  897.         float3 normal       : TEXCOORD2,        //object space
  898.         float3 reflection   : TEXCOORD3,        //object space
  899.  
  900.         out float4 oColour : COLOR,
  901.  
  902.         uniform sampler2D   ceilingTexture,
  903.         uniform sampler2D   floorTexture,
  904.         uniform sampler2D   wallXYTexture,
  905.         uniform sampler2D   wallZYTexture,
  906.         uniform sampler2D   alphaPlaneTexture,
  907.         uniform sampler2D   diffuseTexture,
  908.         uniform samplerCUBE cubeTexture,
  909.         uniform float3      wallFrequencies,
  910.         uniform float       alphaPlaneDistance,
  911.         uniform float3      cameraPosition      //object space
  912.     )
  913. {
  914.     float4 interiorColour = calculateFullyTexturedRoomWithAlphaPlaneColour(position, normal,
  915.         ceilingTexture, floorTexture, wallXYTexture, wallZYTexture, alphaPlaneTexture,
  916.         wallFrequencies, alphaPlaneDistance, cameraPosition);
  917.        
  918.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  919. }
  920.  
  921.  
  922. float4 calculateFullyTexturedRoomWithAlphaPlaneColourAndLightVariation
  923.     (
  924.         float3      position,           //object space
  925.         float3      normal,             //object space
  926.         sampler2D   noiseTexture,
  927.         sampler2D   ceilingTexture,
  928.         sampler2D   floorTexture,
  929.         sampler2D   wallXYTexture,
  930.         sampler2D   wallZYTexture,
  931.         sampler2D   alphaPlaneTexture,
  932.         float3      wallFrequencies,
  933.         float       alphaPlaneDistance,
  934.         float       lightThreshold,
  935.         float3      cameraPosition      //object space
  936.     )
  937. {
  938.     //get the vector from camera to surface
  939.     float3 direction = position - cameraPosition;
  940.  
  941.     //calculate wall locations part 1
  942.     float3 walls = floor(position * wallFrequencies);
  943.  
  944.     //calculate variation in the lighting per room
  945.     float lightVariation = step(calculateNoise(walls, noiseTexture), lightThreshold);
  946.     lightVariation = lightVariation * 0.7 + 0.3;
  947.  
  948.     //calculate wall locations part 2
  949.     walls += step(float3(0, 0, 0), direction);
  950.     walls /= wallFrequencies;
  951.  
  952.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  953.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  954.  
  955.     //texture-coordinates of intersections
  956.     float2 intersectionXY = 4 * (cameraPosition + rayFractions.z * direction).xy;
  957.     float2 intersectionXZ = 4 * (cameraPosition + rayFractions.y * direction).xz;
  958.     float2 intersectionZY = 4 * (cameraPosition + rayFractions.x * direction).zy;
  959.  
  960.     //use the intersection as the texture coordinates for the ceiling
  961.     float4 ceilingColour = tex2D(ceilingTexture, intersectionXZ);
  962.     float4 floorColour = tex2D(floorTexture, intersectionXZ);
  963.     float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  964.     float4 wallXYColour = tex2D(wallXYTexture, intersectionXY);
  965.     float4 wallZYColour = tex2D(wallZYTexture, intersectionZY);
  966.  
  967.     //choose the closest of these walls for the normal for the lighting
  968.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  969.     float xVSz = step(rayFractions.x, rayFractions.z);
  970.     float4 wallsColour = lerp(wallXYColour, wallZYColour, xVSz);
  971.     float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
  972.     float xzVSy = step(rayFraction_xVSz, rayFractions.y);
  973.     wallsColour = lerp(verticalColour, wallsColour, xzVSy);
  974.     float rayFractionWinner = lerp(rayFractions.y, rayFraction_xVSz, xzVSy);
  975.    
  976.  
  977.     //calculate intersection with the alpha plane
  978.    
  979.     //The alpha plane uses the standerd plane equation math with variables a,b,c,d to define the plane.
  980.     //a,b,c of the plane equation of the alpha plane is just the normal of the wall.
  981.     //d can be calculated from a,b,c and a point on the plane.
  982.     //This would normally be done like this:
  983.     //    float3 pointOnAlphaPlane = position - normal * alphaPlaneDistance;
  984.     //    float d = dot(pointOnAlphaPlane, normal);
  985.     //But is optimized to this:
  986.     float d = dot(position, normal) - alphaPlaneDistance;
  987.  
  988.     //calculate the collision between the ray and the alpha plane
  989.     float alphaRayFraction = (d - dot(cameraPosition, normal)) / (dot(direction, normal));
  990.     float3 alphaPlaneIntersectionPoint = alphaRayFraction * direction + cameraPosition;
  991.  
  992.     float uvXWeight = abs(dot(float2(normal.x, normal.z), float2(1, 0)));
  993.     float2 alphaPlaneUV = float2(
  994.         alphaPlaneIntersectionPoint.x + alphaPlaneIntersectionPoint.z,
  995.         -alphaPlaneIntersectionPoint.y);
  996.     float4 alphaPlaneColour = tex2D(alphaPlaneTexture, alphaPlaneUV * 4);
  997.  
  998.     float alphaPlaneWeight = step(alphaRayFraction, rayFractionWinner) * (1 - alphaPlaneColour.a);
  999.  
  1000.  
  1001.     float4 interiorColour = lerp(wallsColour, alphaPlaneColour, alphaPlaneWeight);
  1002.     return interiorColour * lightVariation;
  1003. }
  1004.  
  1005.  
  1006. void IM_FullyTexturedRooms_AlphaPlane_LightVariation_FP
  1007.     (
  1008.         float3 position     : TEXCOORD1,        //object space
  1009.         float3 normal       : TEXCOORD2,        //object space
  1010.  
  1011.         out float4 oColour : COLOR,
  1012.  
  1013.         uniform sampler2D   noiseTexture,
  1014.         uniform sampler2D   ceilingTexture,
  1015.         uniform sampler2D   floorTexture,
  1016.         uniform sampler2D   wallXYTexture,
  1017.         uniform sampler2D   wallZYTexture,
  1018.         uniform sampler2D   alphaPlaneTexture,
  1019.         uniform float3      wallFrequencies,
  1020.         uniform float       alphaPlaneDistance,
  1021.         uniform float       lightThreshold,
  1022.         uniform float3      cameraPosition      //object space
  1023.     )
  1024. {
  1025.     float4 interiorColour = calculateFullyTexturedRoomWithAlphaPlaneColourAndLightVariation(
  1026.         position, normal, noiseTexture, ceilingTexture, floorTexture, wallXYTexture, wallZYTexture,
  1027.         alphaPlaneTexture, wallFrequencies, alphaPlaneDistance, lightThreshold, cameraPosition);
  1028.  
  1029.     oColour = interiorColour;
  1030. }
  1031.  
  1032.  
  1033. void IM_DiffuseCube_FullyTexturedRooms_AlphaPlane_LightVariation_FP
  1034.     (
  1035.         float4 lighting     : COLOR0,
  1036.         float2 uv           : TEXCOORD0,
  1037.         float3 position     : TEXCOORD1,        //object space
  1038.         float3 normal       : TEXCOORD2,        //object space
  1039.         float3 reflection   : TEXCOORD3,        //object space
  1040.  
  1041.         out float4 oColour : COLOR,
  1042.  
  1043.         uniform sampler2D   noiseTexture,
  1044.         uniform sampler2D   ceilingTexture,
  1045.         uniform sampler2D   floorTexture,
  1046.         uniform sampler2D   wallXYTexture,
  1047.         uniform sampler2D   wallZYTexture,
  1048.         uniform sampler2D   alphaPlaneTexture,
  1049.         uniform sampler2D   diffuseTexture,
  1050.         uniform samplerCUBE cubeTexture,
  1051.         uniform float3      wallFrequencies,
  1052.         uniform float       alphaPlaneDistance,
  1053.         uniform float       lightThreshold,
  1054.         uniform float3      cameraPosition      //object space
  1055.     )
  1056. {
  1057.     float4 interiorColour = calculateFullyTexturedRoomWithAlphaPlaneColourAndLightVariation(
  1058.         position, normal, noiseTexture, ceilingTexture, floorTexture, wallXYTexture, wallZYTexture,
  1059.         alphaPlaneTexture, wallFrequencies, alphaPlaneDistance, lightThreshold, cameraPosition);
  1060.  
  1061.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  1062. }
  1063.  
  1064.  
  1065. void IM_DiffuseStrongCube_FullyTexturedRooms_AlphaPlane_LightVariation_FP
  1066.     (
  1067.         float4 lighting     : COLOR0,
  1068.         float2 uv           : TEXCOORD0,
  1069.         float3 position     : TEXCOORD1,        //object space
  1070.         float3 normal       : TEXCOORD2,        //object space
  1071.         float3 reflection   : TEXCOORD3,        //object space
  1072.  
  1073.         out float4 oColour : COLOR,
  1074.  
  1075.         uniform sampler2D   noiseTexture,
  1076.         uniform sampler2D   ceilingTexture,
  1077.         uniform sampler2D   floorTexture,
  1078.         uniform sampler2D   wallXYTexture,
  1079.         uniform sampler2D   wallZYTexture,
  1080.         uniform sampler2D   alphaPlaneTexture,
  1081.         uniform sampler2D   diffuseTexture,
  1082.         uniform samplerCUBE cubeTexture,
  1083.         uniform float3      wallFrequencies,
  1084.         uniform float       alphaPlaneDistance,
  1085.         uniform float       lightThreshold,
  1086.         uniform float3      cameraPosition      //object space
  1087.     )
  1088. {
  1089.     float4 interiorColour = calculateFullyTexturedRoomWithAlphaPlaneColourAndLightVariation(
  1090.         position, normal, noiseTexture, ceilingTexture, floorTexture, wallXYTexture, wallZYTexture,
  1091.         alphaPlaneTexture, wallFrequencies, alphaPlaneDistance, lightThreshold, cameraPosition);
  1092.  
  1093.     oColour = mixInteriorWithDiffuseStrongCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  1094. }
  1095.  
  1096.  
  1097. float4 calculateFullyTexturedRoomsDisplacedWallsColour
  1098.     (
  1099.         float3      position,           //object space
  1100.         sampler2D   ceilingTexture,
  1101.         sampler2D   floorTexture,
  1102.         sampler2D   wallXYTexture,
  1103.         sampler2D   wallZYTexture,
  1104.         float3      wallFrequencies,
  1105.         float3      displacementStrengths,
  1106.         float3      cameraPosition      //object space
  1107.     )
  1108. {
  1109.     //get the vector from camera to surface
  1110.     float3 direction = position - cameraPosition;
  1111.  
  1112.     //calculate wall locations
  1113.     float3 directionPositiveness = step(float3(0, 0, 0), direction);
  1114.     float3 wallIndices1 = floor(position * wallFrequencies) + directionPositiveness;
  1115.     float3 wallIndices0 = wallIndices1 - float3(1);
  1116.     wallIndices1 += frac(wallIndices1 / 2) * displacementStrengths;
  1117.     wallIndices0 += frac(wallIndices0 / 2) * displacementStrengths;
  1118.     float3 walls1 = wallIndices1 / wallFrequencies;
  1119.     float3 walls0 = wallIndices0 / wallFrequencies;
  1120.  
  1121.     //decide which walls should be used
  1122.     float3 wallsPositiveDirection = lerp(walls1, walls0, step(position, walls0));
  1123.     float3 wallsNegativeDirection = lerp(walls1, walls0, step(position, walls1));
  1124.     float3 walls = lerp(wallsNegativeDirection, wallsPositiveDirection, directionPositiveness);
  1125.  
  1126.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  1127.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  1128.  
  1129.     //texture-coordinates of intersections
  1130.     float2 intersectionXY = 4 * (cameraPosition + rayFractions.z * direction).xy;
  1131.     float2 intersectionXZ = 4 * (cameraPosition + rayFractions.y * direction).xz;
  1132.     float2 intersectionZY = 4 * (cameraPosition + rayFractions.x * direction).zy;
  1133.  
  1134.     //use the intersection as the texture coordinates for the ceiling
  1135.     float4 ceilingColour = tex2D(ceilingTexture, intersectionXZ);
  1136.     float4 floorColour = tex2D(floorTexture, intersectionXZ);
  1137.     float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  1138.     float4 wallXYColour = tex2D(wallXYTexture, intersectionXY);
  1139.     float4 wallZYColour = tex2D(wallZYTexture, intersectionZY);
  1140.  
  1141.     //choose the closest of these walls for the normal for the lighting
  1142.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  1143.     float xVSz = step(rayFractions.x, rayFractions.z);
  1144.     float4 interiorColour = lerp(wallXYColour, wallZYColour, xVSz);
  1145.     float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
  1146.     float xzVSy = step(rayFraction_xVSz, rayFractions.y);
  1147.     interiorColour = lerp(verticalColour, interiorColour, xzVSy);
  1148.    
  1149.     return interiorColour;
  1150. }
  1151.  
  1152.  
  1153. void IM_FullyTexturedRooms_DisplacedWalls_FP
  1154.     (
  1155.         float3 position : TEXCOORD1,                //object space
  1156.  
  1157.         out float4 oColour : COLOR,
  1158.  
  1159.         uniform sampler2D   ceilingTexture,
  1160.         uniform sampler2D   floorTexture,
  1161.         uniform sampler2D   wallXYTexture,
  1162.         uniform sampler2D   wallZYTexture,
  1163.         uniform float3      wallFrequencies,
  1164.         uniform float3      displacementStrengths,
  1165.         uniform float3      cameraPosition          //object space
  1166.     )
  1167. {
  1168.     oColour = calculateFullyTexturedRoomsDisplacedWallsColour(position, ceilingTexture,
  1169.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, displacementStrengths, cameraPosition);
  1170. }
  1171.  
  1172.  
  1173. void IM_DiffuseCube_FullyTexturedRooms_DisplacedWalls_FP
  1174.     (
  1175.         float4 lighting     : COLOR0,
  1176.         float2 uv           : TEXCOORD0,
  1177.         float3 position     : TEXCOORD1,            //object space
  1178.         float3 reflection   : TEXCOORD3,            //object space
  1179.  
  1180.         out float4 oColour : COLOR,
  1181.  
  1182.         uniform sampler2D   ceilingTexture,
  1183.         uniform sampler2D   floorTexture,
  1184.         uniform sampler2D   wallXYTexture,
  1185.         uniform sampler2D   wallZYTexture,
  1186.         uniform sampler2D   diffuseTexture,
  1187.         uniform samplerCUBE cubeTexture,
  1188.         uniform float3      wallFrequencies,
  1189.         uniform float3      displacementStrengths,
  1190.         uniform float3      cameraPosition          //object space
  1191.     )
  1192. {
  1193.     float4 interiorColour = calculateFullyTexturedRoomsDisplacedWallsColour(position, ceilingTexture,
  1194.         floorTexture, wallXYTexture, wallZYTexture, wallFrequencies, displacementStrengths, cameraPosition);
  1195.        
  1196.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  1197. }
  1198.  
  1199.  
  1200. float4 calculateVariationTexturedRoomsColour
  1201.     (
  1202.         float3      position,           //object space
  1203.         sampler2D   ceilingTexture,
  1204.         sampler2D   floorTexture,
  1205.         sampler1D   noiseTexture,
  1206.         sampler2D   wallTexture,
  1207.         float3      wallFrequencies,
  1208.         float3      cameraPosition      //object space
  1209.     )
  1210. {
  1211.     //get the vector from camera to surface
  1212.     float3 direction = position - cameraPosition;
  1213.  
  1214.     //calculate wall locations
  1215.     float3 corner = floor(position * wallFrequencies);
  1216.     float3 walls = corner + step(float3(0, 0, 0), direction);
  1217.     walls /= wallFrequencies;
  1218.     corner /= wallFrequencies;
  1219.  
  1220.     //how much of the ray is needed to get from the cameraPosition to each of the walls
  1221.     float3 rayFractions = (float3(walls.x, walls.y, walls.z) - cameraPosition) / direction;
  1222.  
  1223.     //texture-coordinates of intersections
  1224.     float2 intersectionXY = (cameraPosition + rayFractions.z * direction).xy;
  1225.     float2 intersectionXZ = 4 * (cameraPosition + rayFractions.y * direction).xz;
  1226.     float2 intersectionZY = (cameraPosition + rayFractions.x * direction).zy;
  1227.  
  1228.     //use the intersection as the texture coordinates for the ceiling and floor
  1229.     float4 ceilingColour = tex2D(ceilingTexture, intersectionXZ);
  1230.     float4 floorColour = tex2D(floorTexture, intersectionXZ);
  1231.  
  1232.     //decide whether to use the ceiling or the floor
  1233.     float4 verticalColour = lerp(floorColour, ceilingColour, step(0, direction.y));
  1234.  
  1235.     //Choose a random texture for this wall in this room.
  1236.     //Textures are stored in a texture atlas of 2x2=4 textures.
  1237.     float noiseXY = calculateNoiseR(corner, noiseTexture);
  1238.     noiseXY = floor(noiseXY * 4) / 4;
  1239.     float2 atlasIndexXY;
  1240.     atlasIndexXY[0] = floor(noiseXY * 2) / 2;
  1241.     atlasIndexXY[1] = (noiseXY - atlasIndexXY[0]) * 2;
  1242.     //put the intersection into room space, so that it comes within [0, 1]
  1243.     intersectionXY = (intersectionXY - corner.xy) * wallFrequencies.xy;
  1244.     //use the texture coordinate to read from the correct texture in the atlas
  1245.     float4 wallXYColour = 0.8 * tex2D(wallTexture, atlasIndexXY + intersectionXY / 2);
  1246.  
  1247.     //Choose a random texture for this wall in this room.
  1248.     //Textures are stored in a texture atlas of 2x2=4 textures.
  1249.     float noiseZY = calculateNoiseG(corner, noiseTexture);
  1250.     noiseZY = floor(noiseZY * 4) / 4;
  1251.     float2 atlasIndexZY;
  1252.     atlasIndexZY[0] = floor(noiseZY * 2) / 2;
  1253.     atlasIndexZY[1] = (noiseZY - atlasIndexZY[0]) * 2;
  1254.     //put the intersection into room space, so that it comes within [0, 1]
  1255.     intersectionZY = (intersectionZY - corner.zy) * wallFrequencies.zy;
  1256.     //use the texture coordinate to read from the correct texture in the atlas
  1257.     float4 wallZYColour = tex2D(wallTexture, atlasIndexZY + intersectionZY / 2);
  1258.  
  1259.     //choose the closest of these walls for the normal for the lighting
  1260.     //(some intelligent step-usage is needed here to compensate for the lack of an if-statement in ps_2_0)
  1261.     float xVSz = step(rayFractions.x, rayFractions.z);
  1262.     float4 interiorColour = lerp(wallXYColour, wallZYColour, xVSz);
  1263.     float rayFraction_xVSz = lerp(rayFractions.z, rayFractions.x, xVSz);
  1264.     float xzVSy = step(rayFraction_xVSz, rayFractions.y);
  1265.     interiorColour = lerp(verticalColour, interiorColour, xzVSy);
  1266.  
  1267.     return interiorColour;
  1268. }
  1269.  
  1270.  
  1271. void IM_VariationTexturedRooms_FP
  1272.     (
  1273.         float3 position : TEXCOORD1,            //object space
  1274.  
  1275.         out float4 oColour : COLOR,
  1276.  
  1277.         uniform sampler2D   ceilingTexture,
  1278.         uniform sampler2D   floorTexture,
  1279.         uniform sampler1D   noiseTexture,
  1280.         uniform sampler2D   wallTexture,
  1281.         uniform float3      wallFrequencies,
  1282.         uniform float3      cameraPosition      //object space
  1283.     )
  1284. {
  1285.     oColour = calculateVariationTexturedRoomsColour(position, ceilingTexture,
  1286.         floorTexture, noiseTexture, wallTexture, wallFrequencies, cameraPosition);
  1287. }
  1288.  
  1289.  
  1290. void IM_DiffuseCube_VariationTexturedRooms_FP
  1291.     (
  1292.         float4 lighting     : COLOR0,
  1293.         float2 uv           : TEXCOORD0,
  1294.         float3 position     : TEXCOORD1,            //object space
  1295.         float3 reflection   : TEXCOORD3,            //object space
  1296.  
  1297.         out float4 oColour : COLOR,
  1298.  
  1299.         uniform sampler2D   ceilingTexture,
  1300.         uniform sampler2D   floorTexture,
  1301.         uniform sampler1D   noiseTexture,
  1302.         uniform sampler2D   wallTexture,
  1303.         uniform sampler2D   diffuseTexture,
  1304.         uniform samplerCUBE cubeTexture,
  1305.         uniform float3      wallFrequencies,
  1306.         uniform float3      cameraPosition      //object space
  1307.     )
  1308. {
  1309.     float4 interiorColour = calculateVariationTexturedRoomsColour(position, ceilingTexture,
  1310.         floorTexture, noiseTexture, wallTexture, wallFrequencies, cameraPosition);
  1311.  
  1312.     oColour = mixInteriorWithDiffuseCube(lighting, uv, reflection, diffuseTexture, cubeTexture, interiorColour);
  1313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement