(* ::Package:: *) (* ::Title:: *) (*Anaglyph*) (* ::Subtitle:: *) (*T J Atherton*) (* ::Text:: *) (*History:*) (* \[Bullet] Anaglyph now integrated into one function with options to choose Method, ViewAngleDifference and ImageSize.*) (* \[Bullet] First version written as a package.*) (* *) (*Bugs and issues:*) (* \[Bullet] The function fails on some computers because the two images are not the same.*) (* \[Bullet] It would be great to generate Anaglyph images compatible with Anachrome, etc.*) BeginPackage["Anaglyph`"]; Anaglyph::usage = "Anaglyph generates an 3D image from a Graphics3D object that is suitable for viewing with 3D glasses."; Begin["`Private`"]; Options[Anaglyph]={ViewAngleDifference-> 2 Degree,Method-> "Optimized",ImageSize-> Automatic}; Anaglyph[graphics_Graphics3D,OptionsPattern[]]:= Block[{\[Alpha],isize,vp,vv,axis,gl,gr,il,ir,ils,finalimage,method}, \[Alpha] = OptionValue[ViewAngleDifference]; method =OptionValue[Method] ; isize = OptionValue[ImageSize]; vp = ViewPoint /. Options[graphics]; (* If the ViewPoint isn't set, use the default value from Graphics3D *) If[Head[vp]== Symbol,vp=ViewPoint /.Options[Graphics3D,ViewPoint]]; vv = ViewVertical /. Options[graphics]; If[Head[vv]== Symbol,vv=ViewVertical /.Options[Graphics3D,ViewVertical]]; axis = Cross[vp,Cross[vv,vp]]; gl=Show[graphics,ViewPoint-> RotationMatrix[-\[Alpha],axis].vp,ViewVertical-> axis,SphericalRegion-> True,ImageSize-> isize]; il=Image[gl]; gr=Show[graphics,ViewPoint-> RotationMatrix[\[Alpha],axis].vp,ViewVertical-> axis,SphericalRegion-> True,ImageSize-> ImageDimensions[il]]; ir = Image[gr]; Which[method =="FullColor",finalimage =Image[ColorCombine[Join[{First[ColorSeparate[il]]},Rest[ColorSeparate[ir]]]],ImageSize-> ImageDimensions[il]];, method =="HalfColor", finalimage =Image[ColorCombine[Join[{ColorConvert[il,"Grayscale"]},Rest[ColorSeparate[ir]]]],ImageSize-> ImageDimensions[il]];, (method =="Optimized") || True, ils=ColorSeparate[il]; finalimage =Image[ColorCombine[Join[{ImageAdjust[ImageAdd[ImageMultiply[ils[[2]],0.7],ImageMultiply[ils[[3]],0.3]],{0,0,1.5}]},Rest[ColorSeparate[ir]]]],ImageSize-> ImageDimensions[il]]; ]; Return[finalimage]; ]; End[]; EndPackage[];