Difference between revisions of "Talk:ShaderMirror"

From SimsWiki
Jump to: navigation, search
m (+ MG)
 
(10 intermediate revisions by one user not shown)
Line 1: Line 1:
This thread is for the mirror shader comparisons between different versions based on the base game and EPs.
+
This thread is for the Mirror shader comparisons between different versions based on the base game and EPs.
Please keep this thread and only edit for posting the not posted EP version(s).  Preview-check before save.
+
Please keep this thread and only edit for the latest version based on EP.  Preview-check before save.
  
 
How to use?
 
How to use?
The version was first saved from TS2 the base game one up to the Pets one, so the versions are in the release order but skipping EP1-UNI.  I've not extracted the UNI one.
+
The version was first saved from TS2 the base game one up to the Seasons one, and the versions are listed as summaries.
Just choose the desired versions at the "history" tab for comparison.
+
Just choose the desired versions at the "history" tab after loading the "article" tab page for comparison. (coz there are histories for both the article and talk.)
  
Individual version can be found from the "Previous diff" and "Next diff" links in the comparison pages.
+
Individual version can be found from the summaries in the history page.
  
EP0 =/= EP2 == EP3 == EP5(incompetent) =/= EP4 == EP5
+
EP0 =/= EP1 =/= EP2 == EP3 =/= EP4 == EP5 =/= EP6 == EP7 == EP8 == MG
  
Note: For some reasons, the mirror layer 6 was set to 16 in EP4-Pets but then reverted back to 6 in EP5-SS.
+
"==" means identical/equivalent; "=/=" means not identical/equivalent
 +
"EP0" means the base game TS2 version, and the rests are self-explanatory.
  
Sorry: I guess the first copy of SS is "corrupted" or "incompetent" coz it doesn't match another SS copy or the trend of changes in matshad!
+
EP0 VS EP1:
 +
1. viewerRenderType struct upadte
 +
2. + mirrorColor
  
Thanks.
+
EP1 VS EP2/3:
[[User:Niol|niol]] 03:12, 5 March 2007 (CST)
+
1. + DetermineHardwareSupport()
 +
2. + if ($useSWVertexShaderPath)
 +
3. + if (not varExists(isContentViewer))
  
 +
EP2/3 VS EP4/5:
 +
layer 6 <--> layer 16
  
Here's th UNI version
+
EP4/5 VS EP6:
<pre>
+
+ BlankMirrorMaterial for kRenderTypePoolReflection & kRenderTypeOceanReflection
EP1-UNI
+
0xCD7FE87A
+
0x1C0532FA
+
0xBAC036BC
+
0xFF123570
+
# reflection
+
  
version 4
+
Teko's transparent variant is based on the version EP0TS2 by adding alpha-blending.
  
# Shaders for mirror reflections.
+
[http://www.sims2wiki.info/index.php?title=ShaderMirror&action=history click here], and choose the two versions of the same shader for comparisons or shader comprehension.
  
# Note: These materials assume that the reflection camera uses the same handed coordinate system as
+
Thanks.
# the main camera, with the x axis flipped. Therefore, when marking out the area in the stencil buffer
+
[[User:Niol|niol]] 03:12, 5 March 2007 (CST)
# to be filled in the with the reflection view, the x clip coordinate of the mirror vertices is
+
# negated.
+
#
+
# The layer numbers for the mirror pre- and post-reflection render materials (+/-99998) are referenced
+
# from C++ code for the censor-in-mirror render.
+
 
+
set reflectionRenderTarget "ScreenReflection"
+
 
+
#beginshader SimpleMirrorReflection
+
#description Assign this material to the mirror quad to make it reflective.
+
#description The mirror quad needs to be centered over the origin in its local space,
+
#description facing positive Y. You can use a transform bone to orient the mirror
+
#description however you need.
+
 
+
set mvAspect 1
+
 
+
setv4 thumbnailMirrorColor (0.5,0.6,0.75, 1)
+
setv4 impostorMirrorColor (0.4, 0.4, 0.4, 1)
+
setv4 reflectionsOffMirrorColor (0.6, 0.6, 0.6, 1)
+
define SimpleMirrorReflection()
+
  material
+
      if (viewerRenderType = $kRenderTypeShadow)
+
        create StandardShaderShadow()
+
      else
+
        if (viewerRenderType = $kRenderTypeThumbnail)
+
            create BlankMirrorMaterial(viewerRenderType $thumbnailMirrorColor)
+
        elseif (viewerRenderType = $kRenderTypeImposter)
+
            create BlankMirrorMaterial(viewerRenderType $impostorMirrorColor)
+
        else           
+
            if (not $mirrorReflectionsEnabled)
+
              set currentType (viewerRenderType)
+
              create BlankMirrorMaterial($currentType $reflectionsOffMirrorColor)
+
            else
+
              create MirrorReflectionMaterial()
+
            endif           
+
        endif       
+
      endif
+
  end
+
enddef
+
#endshader
+
 
+
define BlankMirrorMaterial(renderType mirrorColor)
+
      # mirror shader for thumbnails. Output a single flat color.
+
      shader
+
        validateRenderShaderContext -viewerRenderType  &renderType
+
       
+
        pass           
+
            shaderProgram -target vertexProgram -method assemble
+
              bindConstants 0 -bindingID geomToClip -constantCount 4
+
              shaderSource
+
                  vs_1_1
+
                  dcl_position v0
+
                  m4x4 oPos, v0, c0
+
              endShaderSource
+
            end
+
           
+
            colorScalar &mirrorColor
+
            stage
+
              textureBlend select(colorScalar) select(colorScalar)
+
            end           
+
        end
+
      end     
+
enddef
+
 
+
# shader to prep stencil buffer on the rendertarget for the mirror reflection view.
+
# When objects are rendered from the viewpoint of the reflection camera after this shader runs, they will
+
# render only to the area of the rendertarget marked out in the stencil buffer by this shader. 
+
define MirrorReflectionMaterial()
+
     
+
      # write depth into the depth buffer as seen from the main camera. Write stencil = 1
+
      # if this is the mirror we are rendering the reflection for, and 0 otherwise.
+
      shader -layer -9998  # this material must render first
+
        validateRenderShaderContext -viewerRenderType $kRenderTypeMirror  # reflection render
+
       
+
        # write stencil in area occupied by mirror
+
        pass -renderEachFrame
+
            renderTarget $reflectionRenderTarget -setViewport viewportFromParentRenderContext
+
 
+
            # test and write depth (as seen from the main camera)
+
            depthTest true -enableDepthWrite true
+
 
+
            # This material script command means the following:
+
            #
+
            # if (renderableIsTargetBounded)
+
            #    set stencil test to write 1 when depth test passes
+
            # else
+
            #    set stencil test to write 0 when depth test passes
+
            #
+
            applyStencilStateForOverlappingReflections
+
             
+
            # do not write color
+
            #colorWriteEnable -red false -green false -blue false -alpha false
+
            alphaBlend srcFactor(zero) add dstFactor(one)
+
           
+
            # since we are negating clip space x below we need to prevent this quad from being culled
+
            cullmode none
+
           
+
            # vertex program
+
            shaderProgram -target vertexProgram -method assemble           
+
              bindConstants 0 -bindingID geomToClipFromParentView -constantCount 4 # set the matrices from the parent (i.e. the main view) so that the stencil is written to the right place
+
     
+
              shaderSource
+
                  vs_1_1
+
                  dcl_position v0
+
                 
+
                  ; color output for debugging
+
                  def c5, 1,0,0,1
+
               
+
                  ; project vertex to clip coords
+
                  m4x4 r0,  v0,  c0
+
                 
+
                  ; flip the x coordinate so the stencil is marked in the right place
+
                  mov oPos.x,  -r0
+
                  mov oPos.yzw, r0                 
+
              endShaderSource
+
            end
+
        end # pass
+
      end # shader
+
 
+
      # render of the mirror from the main camera. This samples from the reflection render target texture.
+
      shader -layer 6  # this must be after all opaque objects (especially Sims), but before transparent objects and the censor compositing pass
+
        validateRenderShaderContext -viewerRenderType $kRenderTypeNormal
+
       
+
        pass -modifiedEachFrameHint  # we want the mirrors to be rendered every frame even in dirty rect mode
+
 
+
            shaderProgram -target vertexProgram -method assemble           
+
              bindConstants 0 -bindingID geomToClip -constantCount 4
+
              bindConstants 4 -bindingID clipToViewTarget -constantCount 4
+
              shaderSource
+
                  vs_1_1
+
                  dcl_position v0
+
                  def c8,    -0.5, -0.5, 1, 1
+
                  def c9,    0.5,  0.5, 0, 0
+
                 
+
                  def c10,    0.25, 0.25, 0, 0
+
                 
+
                  # project vertex to clip coords
+
                  m4x4 r0,  v0,  c0
+
                  mov oPos, r0
+
                 
+
                  # multiply 0.5 * w so homogenous divide will make it 0.5 again
+
                  mul r2, c9, r0.w
+
                 
+
                  # scale projected xy by .5 and re-center so it's in 0-w range
+
                  mad r4, r0, c8, r2
+
                 
+
                  # Try to get an exact texel to pixel mapping by shifting texture coordinates
+
                  # slightly.
+
                  # A texture coordinate of 0 must map to 1/2*(texture dimension) and a
+
                  # coordinate of 1 must map to (1 - 1/2*(texture dimension)).
+
                  # Note: This does not seem to eliminate the wobble on the mirror
+
                  # reflection entirely.
+
                  rcp r3.x, c4.x  ;# 2/width                                   
+
                  rcp r3.y, c5.y  ;# 2/height
+
                  mov r3.zw, c9.zw ;# r3 = (2/width, 2/height, 0, 0)
+
                 
+
                  mul r3, r3, c10  ;# r3 = (1/2*width, 1/2*height, 0, 0)
+
                  mad r6, r0, r3, r4
+
                  mov oT0, r6.xyww ;# move w into z so we can divide by z in the texture stage; some cards don't handle division by w correctly
+
              endShaderSource             
+
            end
+
           
+
            stage
+
              textureTransformType vector3 homogeneous # divide the xy components of the texture coordinate by z
+
              textureAddressing clamp clamp
+
              texture $reflectionRenderTarget
+
              textureBlend select(texture) select(texture)
+
            end # stage
+
                     
+
        end # pass
+
      end # shader
+
     
+
      # this shader is in case the above case fails. This does projection per vertex.
+
      shader -layer 6     
+
        validateRenderShaderContext -viewerRenderType $kRenderTypeNormal     
+
 
+
        pass -modifiedEachFrameHint  # we want the mirrors to be rendered every frame even in dirty rect mode
+
 
+
            shaderProgram -target vertexProgram -method assemble           
+
              bindConstants 0 -bindingID geomToClip -constantCount 4
+
              bindConstants 4 -bindingID clipToViewTarget -constantCount 4
+
             
+
              shaderSource
+
                  vs_1_1
+
                  dcl_position v0
+
                  def c8,    -0.5, -0.5, 1, 1
+
                  def c9,    0.5,  0.5, 0, 0
+
                 
+
                  def c10,    0.25, 0.25, 0, 0
+
                 
+
                  # project vertex to clip coords
+
                  m4x4 r0,  v0,  c0
+
                  mov oPos, r0
+
                 
+
                  # multiply 0.5 * w so homogenous divide will make it 0.5 again
+
                  mul r2, c9, r0.w
+
                 
+
                  # scale projected xy by .5 and re-center so it's in 0-w range
+
                  mad r4, r0, c8, r2
+
                 
+
                  # Try to get an exact texel to pixel mapping by shifting texture coordinates
+
                  # slightly.
+
                  # A texture coordinate of 0 must map to 1/2*(texture dimension) and a
+
                  # coordinate of 1 must map to (1 - 1/2*(texture dimension)).
+
                  # Note: This does not seem to eliminate the wobble on the mirror
+
                  # reflection entirely.
+
                  rcp r3.x, c4.x  ;# 2/width                                   
+
                  rcp r3.y, c5.y  ;# 2/height
+
                  mov r3.zw, c9.zw ;# r3 = (2/width, 2/height, 0, 0)
+
                 
+
                  mul r3, r3, c10  ;# r3 = (1/2*width, 1/2*height, 0, 0)
+
                                     
+
                  mad r1, r0, r3, r4  ; write to a temp instead of output texcoord (as above)
+
                 
+
                  rcp r1.w, r1.w    ; calc 1/w
+
                 
+
                  mul oT0.xy, r1.w, r1.xy  ; project per vertex since the intel HW (and maybe others) can't do it per pixel.
+
                 
+
              endShaderSource             
+
            end    #shader
+
           
+
            stage
+
              textureAddressing clamp clamp
+
              texture $reflectionRenderTarget
+
              textureBlend select(texture) select(texture)
+
            end # stage   
+
           
+
        end # pass
+
      end #shader 
+
enddef
+
 
+
# material to clear stencil buffer and set depth buffer to "correct" value on rendertarget.
+
# The depth value is the depth as seen from the main camera.
+
 
+
define MirrorBackClearStencilAndSetDepth()
+
  material
+
      # AV TODO: this material doesn't do anything now, so it should be removed (from go2sco as well)
+
      shader -layer 9998
+
      end # shader
+
  end # material
+
enddef
+
 
+
# used by the mirror render. Sets z to the specified value on the entire reflection render target.
+
define SetSpecifiedZOnMirrorRTT(zValue)
+
  material
+
      shader     
+
        pass       
+
            renderClipSpaceRect
+
            renderTarget $reflectionRenderTarget -setViewport fullRenderTargetViewport           
+
                       
+
            # turn off stencil test
+
            stencil false
+
           
+
            # depth test accept always, write depth
+
            depthTest true -enableDepthWrite true
+
            depthTestFunction accept
+
           
+
            # do not write color
+
            alphaBlend srcFactor(zero) add dstFactor(one)
+
           
+
            # vertex program
+
            shaderProgram -target vertexProgram -method compile -version 1_1
+
              bindConstants 0 -bindingID immediateData -data (0,0,&zValue, 0)
+
             
+
              shaderSource
+
                  float4 zValue : register(c0);
+
                 
+
                  void VertexMain(float4 position : POSITION, out float4 oPosition : POSITION)
+
                  {
+
                    oPosition = position;
+
                    oPosition.z = zValue.z;    // set specified z value
+
                  }
+
                 
+
              endShaderSource
+
            end
+
        end # pass
+
      end # shader
+
  end # material
+
enddef
+
 
+
# used by the mirror camera for dirty rect rendering
+
define SetNearZOnMirrorRTT()
+
  create SetSpecifiedZOnMirrorRTT(0)
+
enddef
+
 
+
# used by the mirror camera for non-dirty rect rendering
+
define SetFarZOnMirrorRTT()
+
  create SetSpecifiedZOnMirrorRTT(1)
+
enddef
+
 
+
# used by the censor render in the mirror. Sets stencil on the entire censor render target to 1.
+
define SetStencilOnCensorRTT()
+
  material
+
      shader     
+
        pass
+
            renderClipSpaceRect
+
                       
+
            # set stencil to 1 everywhere
+
            stencil true
+
            stencilFunction accept -refValue 1
+
            stencilOperation -onPass writeRef
+
           
+
            # depth test accept, do not write depth
+
            depthTest false -enableDepthWrite false
+
            depthTestFunction accept
+
           
+
            # do not write color
+
            alphaBlend srcFactor(zero) add dstFactor(one)
+
           
+
            shaderProgram -target vertexProgram -method assemble
+
              shaderSource
+
                  vs_1_1
+
                  dcl_position v0
+
                  mov oPos, v0
+
              endShaderSource
+
            end
+
           
+
            stage
+
              textureBlend select(colorScalar) select(colorScalar)
+
            end
+
           
+
        end # pass
+
      end # shader
+
  end # material
+
enddef
+
 
+
# common state for all objects rendered with the reflection camera
+
defaultState $kRenderTypeMirror # reflection render
+
  # stencil test passes only in the areas where the reference value has been written
+
  stencil true
+
  stencilFunction acceptIfEqual -refValue 1
+
  stencilOperation -onPass noWrite
+
end
+
 
+
# only render where the stencil is zero in turbo rect mode
+
defaultState $kRenderTypeNormal # Stencil
+
  stencil true
+
  stencilFunction acceptIfEqual -refValue 0
+
  stencilOperation -onPass noWrite
+
end
+
 
+
 
+
# Begin DO NOT MODIFY THIS BLOCK
+
# Material instances bound to reflective surfaces.
+
 
+
# Note: The following material instances are bound to exported resources and accessed
+
# by runtime code. They CANNOT be deleted without changing the export program.
+
 
+
materialDefinition simple_mirror_reflection
+
  setDefinition SimpleMirrorReflection
+
end
+
 
+
materialDefinition MirrorBackPostReflectionRenderMaterial
+
  setDefinition MirrorBackClearStencilAndSetDepth
+
end
+
 
+
# bound to the bounding mesh around the mirror plane that prevents the mirror from being
+
# undesirably culled
+
materialDefinition MirrorBoundingBoxMaterial
+
  setDefinition Null
+
end
+
 
+
# used to clear the mirror render target to z = 0 wherever the stencil has been set to 1
+
materialDefinition ZClearToNearPlaneMaterial
+
  setDefinition SetNearZOnMirrorRTT
+
end
+
 
+
# used to clear the mirror render target to z = 0 wherever the stencil has been set to 1
+
materialDefinition ZClearToFarPlaneMaterial
+
  setDefinition SetFarZOnMirrorRTT
+
end
+
 
+
# bound to a compositing pass geometry that preps the stencil buffer for rendering into
+
# the censor render target from the mirror view
+
materialDefinition CensorInMirrorStencilPrepMaterial
+
  setDefinition SetStencilOnCensorRTT
+
end
+
 
+
# End DO NOT MODIFY THIS BLOCK
+
</pre>
+

Latest revision as of 02:38, 14 June 2009

This thread is for the Mirror shader comparisons between different versions based on the base game and EPs. Please keep this thread and only edit for the latest version based on EP. Preview-check before save.

How to use? The version was first saved from TS2 the base game one up to the Seasons one, and the versions are listed as summaries. Just choose the desired versions at the "history" tab after loading the "article" tab page for comparison. (coz there are histories for both the article and talk.)

Individual version can be found from the summaries in the history page.

EP0 =/= EP1 =/= EP2 == EP3 =/= EP4 == EP5 =/= EP6 == EP7 == EP8 == MG

"==" means identical/equivalent; "=/=" means not identical/equivalent "EP0" means the base game TS2 version, and the rests are self-explanatory.

EP0 VS EP1: 1. viewerRenderType struct upadte 2. + mirrorColor

EP1 VS EP2/3: 1. + DetermineHardwareSupport() 2. + if ($useSWVertexShaderPath) 3. + if (not varExists(isContentViewer))

EP2/3 VS EP4/5: layer 6 <--> layer 16

EP4/5 VS EP6: + BlankMirrorMaterial for kRenderTypePoolReflection & kRenderTypeOceanReflection

Teko's transparent variant is based on the version EP0TS2 by adding alpha-blending.

click here, and choose the two versions of the same shader for comparisons or shader comprehension.

Thanks. niol 03:12, 5 March 2007 (CST)

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox