Package slumMaya :: Module shaderLight
[hide private]
[frames] | no frames]

Source Code for Module slumMaya.shaderLight

  1  # 
  2  # shaderLight -         the class that defines a slum light shader node in Maya. 
  3  #                                       based on shaderBase and mayas locator node. 
  4  # 
  5  #    Copyright (C) 2008 - Roberto Hradec 
  6  # 
  7  # --------------------------------------------------------------------------- 
  8  #        This file is part of SLUM. 
  9  # 
 10  #    SLUM is free software: you can redistribute it and/or modify 
 11  #    it under the terms of the GNU General Public License as published by 
 12  #    the Free Software Foundation, either version 3 of the License, or 
 13  #    (at your option) any later version. 
 14  # 
 15  #    SLUM is distributed in the hope that it will be useful, 
 16  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 18  #    GNU General Public License for more details. 
 19  # 
 20  #    You should have received a copy of the GNU General Public License 
 21  #    along with SLUM.  If not, see <http://www.gnu.org/licenses/>. 
 22  # --------------------------------------------------------------------------- 
 23   
 24  import maya.OpenMaya as OpenMaya 
 25  import maya.OpenMayaMPx as OpenMayaMPx 
 26  import maya.OpenMayaRender as OpenMayaRender 
 27  import maya.cmds as m 
 28  from maya.mel import eval as meval 
 29  import shaderBase 
 30   
 31  gl = OpenMayaRender.MHardwareRenderer.theRenderer().glFunctionTable() 
32 33 -class shaderNetworkRasterize:
34 - def __init__(self, nodePlug, resolution=(320,200)):
35 self.resolution = resolution 36 self.w = resolution[0] 37 self.h = resolution[1] 38 self.node = nodePlug 39 self.refresh = True 40 self.texture = None
41
42 - def free(self):
43 if self.texture: 44 gl.glDeleteTextures( 1, self.texture ); 45 self.refresh=True;
46
47 - def needRefresh(self):
48 return self.refresh
49
50 - def sample(self):
51 uCoords = OpenMaya.MFloatArray() 52 vCoords = OpenMaya.MFloatArray() 53 filterSizes = OpenMaya.MFloatArray() 54 points = OpenMaya.MFloatPointArray() 55 refPoints = OpenMaya.MFloatPointArray() 56 normals = OpenMaya.MFloatVectorArray() 57 tanUs = OpenMaya.MFloatVectorArray() 58 tanVs = OpenMaya.MFloatVectorArray() 59 colors = OpenMaya.MFloatVectorArray() 60 transps = OpenMaya.MFloatVectorArray() 61 62 # current camera 63 cameraPath = OpenMaya.M3dView.active3dView().getCamera( ) 64 cameraMat = cameraPath.inclusiveMatrix().matrix 65 66 numSamples=0; 67 for x in range(self.w): 68 for y in range(self.h): 69 uCoords.append( x/float(self.w) ) 70 vCoords.append( y/float(self.h) ) 71 numSamples += 1 72 73 # sample network and bake 74 OpenMayaRender.MRenderUtil.sampleShadingNetwork( 75 node, 76 numSamples, 77 false, #shadow 78 false, #reuse maps 79 cameraMat, 80 points, 81 uCoords, 82 vCoords, 83 normals, 84 refPoints, 85 tanUs, 86 tanVs, 87 filterSizes, 88 colors, 89 transps 90 ); 91 92 data = [] 93 if colors.length()>0: 94 numSamples=0 95 for x in range(self.w): 96 for y in range(self.h): 97 data[numSamples*3+0] = colors[numSamples].x * 255 98 data[numSamples*3+1] = colors[numSamples].y * 255 99 data[numSamples*3+2] = colors[numSamples].z * 255 100 numSamples += 1 101 return data
102
103 - def bind(self):
104 if self.refresh: 105 self.texture = gl.glGenTextures( 1 ) 106 gl.glBindTexture( gl.GL_TEXTURE_2D, self.texture ) 107 108 gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR ) 109 gl.glTexParameterf( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR ) 110 111 gl.glTexImage2D( 112 gl.GL_TEXTURE_2D, 113 0, 114 gl.GL_RGB, 115 self.w, self.h, 116 0, 117 gl.GL_RGB, 118 gl.GL_UNSIGNED_BYTE, 119 self.sample() 120 ) 121 self.refresh = False 122 123 if self.texture: 124 gl.glBindTexture( gl.GL_TEXTURE_2D, self.texture )
125
126 127 128 -class shaderLight( shaderBase.shaderBase, OpenMayaMPx.MPxLocatorNode ):
129 - def __init__(self):
130 ''' we call __init__ of shaderBase and MPxLocatorNode classes in here ''' 131 shaderBase.shaderBase.__init__(self) 132 OpenMayaMPx.MPxLocatorNode.__init__(self)
133 @staticmethod
134 - def nodeCreator():
135 ''' we override this method to return the proper object of this class ''' 136 return OpenMayaMPx.asMPxPtr( shaderLight() )
137