static const float3 BumpBasis[3] = { float3( sqrt(2.0) / sqrt(3.0), 0.0, 1.0 / sqrt(3.0)), float3( 1.0 / sqrt(6.0), 1.0 / sqrt(2.0), 1.0 / sqrt(3.0)), float3( 1.0 / sqrt(6.0), 1.0 / sqrt(2.0), 1.0 / sqrt(3.0)) }; float3 CalculateDLMContribution(float3 TangentNormal) { TangentNormal.y = TangentNormal.y; float3 lightmapContribution = 0; lightmapContribution.x = dot(BumpBasis[0], TangentNormal); lightmapContribution.y = dot(BumpBasis[1], TangentNormal); lightmapContribution.z = dot(BumpBasis[2], TangentNormal); lightmapContribution = saturate(lightmapContribution); lightmapContribution *= lightmapContribution; return lightmapContribution; } float3 CalculateDLMDiff(float3 TangentNormal, float3 DLightmap0, float3 DLightmap1, float3 DLightmap2) { float3 lightmapContribution = CalculateDLMContribution(TangentNormal); float3 light = 0; float sum = dot(lightmapContribution, float3(1.0f, 1.0f, 1.0f)); light += DLightmap0 * lightmapContribution.x; light += DLightmap1 * lightmapContribution.y; light += DLightmap2 * lightmapContribution.z; light /= sum; return light; } float3 CalculateDLMSpec(float3 TangentNormal, float3 EyeVectorTS, float3 DLightmap0, float3 DLightmap1, float3 DLightmap2) { float3 reflectedEye = reflect(normalize(EyeVectorTS), TangentNormal); float3 lightmapContribution = CalculateDLMContribution(reflectedEye); float3 light = 0; light += DLightmap0 * lightmapContribution.x; light += DLightmap1 * lightmapContribution.y; light += DLightmap2 * lightmapContribution.z; light /= 1.33f; // Arbitrary value :( float luminance = dot(light, float3(0.30f, 0.59f, 0.11f)); luminance = pow(luminance, 6.0f) * 7.0f; // Hardcoded test values return saturate(light * luminance); }
 Home
 » Viewing Profile: Topics: TheFieryScythe
TheFieryScythe
Member Since 29 May 2010Offline Last Active Yesterday, 06:19 PM
Community Stats
 Group Members
 Active Posts 5
 Profile Views 786
 Submitted Links 0
 Member Title Member
 Age Age Unknown
 Birthday Birthday Unknown

Gender
Not Telling
Topics I've Started
Specular Highlights with Directional Lightmaps
12 August 2014  06:00 AM
Directional Lightmap issues
13 July 2013  11:15 AM
Hi, a short while ago I wrote a Maxscript tool for 3ds MAX to bake out directional lightmaps but I wasn't getting the results I had expected when previewing the output. However, after getting someone with Maya and Turtle to bake out some example radiosity normal maps to compare with, it turns out my script is actually working correctly. So the issues i'm having must be shader side.
The problem I have is naturally best shown with pictures :
The test scene is a cube with a point light dead centre in it, so as you can see the lighting is being taken from the wrong directions. Looking at it I thought I might just need to flip the Y value of the normal map which partially works:
...giving me working walls, but the floor is still wrong and I don't know why .
The shader function i'm using is as follows:
static const float3 BumpBasis[3] = { float3( sqrt(2.0) / sqrt(3.0), 0.0, 1.0 / sqrt(3.0)), float3( 1.0 / sqrt(6.0), 1.0 / sqrt(2.0), 1.0 / sqrt(3.0)), float3( 1.0 / sqrt(6.0), 1.0 / sqrt(2.0), 1.0 / sqrt(3.0)) }; float3 CalculateDLM(float3 TangentNormal, float3 DLightmap0, float3 DLightmap1, float3 DLightmap2) { float3 LightmapContribution = 0; LightmapContribution.x = saturate(dot(TangentNormal, BumpBasis[0])); LightmapContribution.y = saturate(dot(TangentNormal, BumpBasis[1])); LightmapContribution.z = saturate(dot(TangentNormal, BumpBasis[2])); LightmapContribution *= LightmapContribution; float3 diffuseLight = 0; float diffuseSum = dot(LightmapContribution, float3(1.0f, 1.0f, 1.0f)); diffuseLight += DLightmap0 * LightmapContribution.x; diffuseLight += DLightmap1 * LightmapContribution.y; diffuseLight += DLightmap2 * LightmapContribution.z; diffuseLight /= diffuseSum; return diffuseLight; }
The passed in TangentNormal value is the unbiased normal map texel and the ordering of the bump basis values is from here as apparently the ordering in the valve paper is misleading.
For good measure this is how the TangentNormal values are coming out:
I'm wondering whether I need to transform the basis in some way in relation to the mesh normals but It's not clear what i'm missing from the papers/examples I've seen.
[3ds Max] Directional Lightmap baking script problems
16 May 2013  05:38 AM
Hi, i've been working on a script to bake directional lightmaps (or Radiosity Normal maps as Valve called it) using 3ds Max but i'm having issues with the lightmaps it produces.
The script modifies the normals of the objects to face the 3 basis vectors before baking the lightmaps, but I think there is something amiss when doing this as the lightmaps aren't producing the required result.
The 3 lightmaps for an unwrapped box end up coming out like this:
And the final lightmap with shader looks like this:
The code that modifies the normals is as follows:
  computeTangentSpace function taken from HalfVector on cgsociety  The face position has been removed from the resulting matrices  http://forums.cgsociety.org/showpost.php?p=3881243&postcount=11 function computeTangentSpace obj = ( local theMesh = snapshotAsMesh obj local tSpace = #()  Do we have to flip faces? local flip = false local indices = #(1, 2, 3) if dot (cross obj.transform.row1 obj.transform.row2) obj.transform.row3 <= 0 do ( indices[2] = 3 indices[3] = 2 flip = true ) for nFace = 1 to theMesh.numFaces do ( local face = getFace theMesh nFace local tface = getTVFace theMesh nFace local v1 = getVert theMesh face[indices[1]] local v2 = getVert theMesh face[indices[2]] local v3 = getVert theMesh face[indices[3]] local uv1 = getTVert theMesh tface[indices[1]] local uv2 = getTVert theMesh tface[indices[2]] local uv3 = getTVert theMesh tface[indices[3]] local dV1 = v1  v2 local dV2 = v1  v3 local dUV1 = uv1  uv2 local dUV2 = uv1  uv3 local area = dUV1.x * dUV2.y  dUV1.y * dUV2.x local sign = if area < 0 then 1 else 1 local tangent = [0,0,1] tangent.x = dV1.x * dUV2.y  dUV1.y * dV2.x tangent.y = dV1.y * dUV2.y  dUV1.y * dV2.y tangent.z = dV1.z * dUV2.y  dUV1.y * dV2.z tangent = (normalize tangent) * sign local normal = normalize (getFaceNormal theMesh nFace) if flip do normal = normal local binormal = (normalize (cross normal tangent)) * sign append tSpace (Matrix3 tangent binormal normal [0,0,0]) ) delete theMesh return tSpace ) function SetNormalsModifier nodeList &editNormalsMod lightmapType = ( normalBasis = [0, 0, 1] if (lightmapType == 1) then ( return 0 ) else if (lightmapType == 2) then ( normalBasis = [sqrt (2.0 / 3.0), 0, 1.0 / (sqrt 3.0)] ) else if (lightmapType == 3) then ( normalBasis = [(1.0 / (sqrt 6.0)), 1.0f / (sqrt 2.0), 1.0f / (sqrt 3.0)] ) else if (lightmapType == 4) then ( normalBasis = [(1.0 / (sqrt 6.0)), (1.0f / (sqrt 2.0)), 1.0f / (sqrt 3.0)] ) for j = 1 to nodeList.count do ( obj = nodeList[j]  Get the face TBN's meshFaceTBNs = computeTangentSpace obj normalFaceCount = editNormalsMod.getNumFaces node:obj normalVertexCount = editNormalsMod.getNumNormals node:obj  Create an array of arrays to contain the vertex normals for averaging later normalsListArray = #() for i = 1 to normalVertexCount do ( append normalsListArray #() ) for i = 1 to normalFaceCount do ( faceSelectionArray = #{i} normalSelection = #{}  Get the normal indices of the selected face editNormalsMod.ConvertFaceSelection &faceSelectionArray &normalSelection node:obj  Get the face TBN TBN = meshFaceTBNs[i] for k in normalSelection do (  Transform the normal basis into tangent space? normal = normalize(normalBasis * TBN)  Append the new normal to a list for this vertex append normalsListArray[k] normal ) ) for i = 1 to normalVertexCount do ( normal = [0, 0, 0]  Average the normals from the list for this vertex for k = 1 to normalsListArray[i].count do ( normal += normalsListArray[i][k] ) normal /= normalsListArray[i].count  Set the vertex normal editNormalsMod.SetNormal i (normalize normal) node:obj ) ) )
I've also attached the script asis and a DirectX shader to test with. The script is set up as a macroscript so will need to be added as a button to your UI. The shader requires lightmap coords and normal texture coords in channels 1 and 2, and has a checkbox to state which is which.
I have a feeling there is a matrix transform i'm missing to get the normal bases from Direct3D's coordinate system to 3ds' but nothing i've tried has magically fixed the problem and I don't understand everything here well enough to know the exact reason why it doesn't work.
Apologies if this is the wrong forum, was toss up between graphics theory and tools .
Problems converting vertex lighting assembly to per pixel lighting HLSL
29 May 2010  05:06 AM
add r4.xyz, c[15], r0 dp3 r4.w, r4, r4 rsq r10.w, r4.w mul r4.xyz, r4, r10.w mad r8.x, r4.w, c[15].w, v4.w dp3 r9.x, r4, r1 dp3 r10.w, r4, c[16] mul r10.w, r10.w, c[16].w add r8.z, r10.w, c[17].wr0 has the vertex world position, r1 has the vertex normal and v4 is texture coordinate 0. I don't know alot about lighting so i'm unsure as to what all of the constants contain, but my current conversion looks like this:
sampler TexSampler4 : register(s4); struct PS_INPUT{ float3 PositionWorld : COLOR0; float4 DiffuseMultiUV : TEXCOORD0; float3 Normal : TEXCOORD2; float3 BiNormal : TEXCOORD3; float3 Tangent : TEXCOORD4; }; float4 c_object_point_light0_position : register(c[15]); float4 c_object_point_light0_rotation : register(c[16]); float4 c_object_point_light0_colour : register(c[17]); void Lighting(in PS_INPUT IN, out float4 Lighting) { Lighting = 0; float3x3 WorldToTangentSpace; WorldToTangentSpace[0] = normalize(IN.Tangent); WorldToTangentSpace[1] = normalize(IN.BiNormal); WorldToTangentSpace[2] = normalize(IN.Normal); float4 PointLight0Vector = float4(mul(WorldToTangentSpace, c_object_point_light0_position.xyz  IN.PositionWorld), c_object_point_light0_position.w); float4 PointLight0Rotation = float4(mul(WorldToTangentSpace, c_object_point_light0_rotation.xyz), c_object_point_light0_rotation.w); float3 NormalBump = (2.0 * (tex2D( TexSampler4, IN.DiffuseMultiUV.xy ).xyz))  1.0; float3 VectorNorm; float2 PointAttenuation; //VS Conversion// //dp3 r4.w, r4, r4 //rsq r10.w, r4.w //mul r4.xyz, r4, r10.w VectorNorm = normalize(PointLight0Vector.xyz); //Distance attenuation? //mad r8.x, r4.w, c[15+0].w, v4.w PointAttenuation.x = (dot(PointLight0Vector.xyz, PointLight0Vector.xyz) * PointLight0Vector.w) + IN.DetailUV.w; //Diffuse //dp3 r9.x, r4, r1 PointAttenuation.x *= dot(NormalBump, VectorNorm); //Spotlight falloff //dp3 r10.w, r4, c[15+1] //mul r10.w, r10.w, c[15+1].w //add r8.z, r10.w, c[15+2].w PointAttenuation.x *= (dot(VectorNorm, PointLight0Rotation.xyz) * PointLight0Rotation.w) + c_object_point_light0_colour.w; //VS Conversion// PointAttenuation = clamp(PointAttenuation, 0, 1); Lighting.xyz += (PointAttenuation.x * c_object_point_light0_colour.xyz); }I've cut out some code from this for multiple lights/reflections and such but I think the posted code is valid. The problems i've been having are better seen in pictures :). Spotlight pointing from the players left shoulder to the vehicle: Point Light from the end of the weapon: So i'm hoping someone can see where i've messed up and set me on the right track :).