'
' This example illustrates how to create a nem PolygonMesh from an ApproximatedMesh
' and how to carry over the attributes.
'
set oCube = ActiveSceneRoot.AddGeometry("Cube","MeshSurface")
set oMaterial = oCube.AddMaterial("Blinn")
'Add Polygon Material and texture
set oPolygonCluster = oCube.ActivePrimitive.Geometry.AddCluster(siPolygonCluster, ,Array(4))
set oMaterial = oPolygonCluster.AddMaterial("Phong")
set oImage = AddImageSource( "$SI_HOME\Data\XSI_SAMPLES\Pictures\jio.jpg" )
set oImageClip = AddImageClip( oImage )
set oPhong = oMaterial.Shaders("Phong")
set oColor8Mix1 = oPhong.ambient.connectfromprogid("Softimage.sib_color_8mix.1")
set oColor8Mix2 = oPhong.diffuse.connectfromprogid("Softimage.sib_color_8mix.1")
set oTex2D = oColor8Mix1.color1.connectfromprogid("Softimage.txt2d-image-explicit.1")
call oColor8Mix2.color1.connect(oTex2D)
set oTex2D.tex.source = oImageClip
'Add Polygon Material
set oPolygonCluster = oCube.ActivePrimitive.Geometry.AddCluster(siPolygonCluster,,Array(0,1))
set oMaterial = oPolygonCluster.AddMaterial("Lambert")
'Add UV and VertexColor Properties
set oSamplePointCluster = oCube.ActivePrimitive.Geometry.AddCluster(siSampledPointCluster)
set oUVProp = oSamplePointCluster.AddProperty("Texture Projection")
set oRGBAProp = oSamplePointCluster.AddProperty("Vertex Color")
set oGApprox = oCube.Properties.Find("geomapprox")
oGApprox.Parameters("gapproxmosl").value = 1
MakeLocal oGApprox
ApproxVersion( oCube)
'This function will duplicate the approximated version of the
if selection.count > 0 then
ApproxVersion( selection(0))
else
Application.LogMessage "You must select a PolygonMesh"
end if
'This function will duplicate the approximated version of the
Sub ApproxVersion(in_obj)
Dim l_NewMesh
Dim l_Parent
Dim l_AllInputUVTxtAndCAV
Dim l_AllOutputUVTxtAndCAV
Dim l_GeometryApproximation
Dim l_NewLocalGeometryApproximation
Dim l_aVertices
Dim l_aPolygonData
Dim l_aPolygonNodeNormalArray
Dim l_aUVArray
Dim l_aColorArray
Dim l_aMaterialArray
Dim l_LocalProperties
Dim l_attribs
set l_AllInputUVTxtAndCAV = CreateObject( "XSI.Collection")
set l_AllOutputUVTxtAndCAV = CreateObject( "XSI.Collection")
'Check that in_obj is an object
if not(IsObject(in_obj)) then
Application.LogMessage "The inputed parameter must be a 3D Object."
Exit Sub
end if
if not(in_obj.BelongsTo("3D Object")) then
Application.LogMessage "The inputed object must be a 3D Object."
Exit Sub
end if
'If the object is not a PolygonMesh than exit the function
if in_obj.activeprimitive.geometry.type <> "PolygonMesh" then
Application.LogMessage in_obj & " has to be a polymesh object"
Exit Sub
end if
set l_GeometryApproximation = in_Obj.Properties.Find("geomapprox")
l_attribs = in_obj.ActivePrimitive.Geometry.GetApproximatedMeshAndAttributes( _
l_GeometryApproximation.Parameters("gapproxmosr").value,_
l_GeometryApproximation.Parameters("gapproxmosl").value,_
l_GeometryApproximation.Parameters("gapproxmoad").value,_
l_GeometryApproximation.Parameters("gapproxmoan").value )
l_aVertices = l_attribs(0)
l_aPolygonData = l_attribs(1)
l_aPolygonNodeNormalArray = l_attribs(2)
l_aPolygonNodePolygonFaceIndexArray = l_attribs(3)
l_aPolygonFaceParentPolygonFaceIndexArray = l_attribs(4)
l_aUVArray = l_attribs(5)
l_aColorArray = l_attribs(6)
l_aMaterialArray = l_attribs(7)
set l_Parent = in_obj.Parent
set l_NewMesh = l_Parent.AddPolygonMesh(l_aVertices,l_aPolygonData)
'Copy local properties
set l_LocalProperties = in_obj.LocalProperties
for each iLocProp in l_LocalProperties
CopyPaste iLocProp,,l_NewMesh,2
next
'If the geometry approximation is a local property we set the subdivision level to
'0 as it is the most likely thing users want to do.
set l_NewLocalGeometryApproximation = l_NewMesh.LocalProperties.Find("geomapprox")
if IsObject(l_NewLocalGeometryApproximation) and TypeName(l_NewLocalGeometryApproximation) <> "Nothing" then
l_NewLocalGeometryApproximation.Parameters("gapproxmosl").value = 0
end if
'Add a sample cluster if there's some VertexColors or UVProp
if IsArray(l_aUVArray) then
i = 0
do while i < ubound(l_aUVArray)
set l_Cluster = l_NewMesh.ActivePrimitive.Geometry.AddCluster(siSampledPointCluster)
set l_UVProp = l_Cluster.AddProperty("Texture Projection", ,l_aUVArray(i).Name & "_" & l_Cluster.Name)
ReDim l_UVPropArray(2,(l_Cluster.Elements.Count - 1))
for j = 0 to l_Cluster.Elements.Count-1
l_UVPropArray(0,j) = l_aUVArray(i+1)(j*3)
l_UVPropArray(1,j) = l_aUVArray(i+1)(j*3 + 1)
l_UVPropArray(2,j) = l_aUVArray(i+1)(j*3 + 2)
next
l_UVProp.Elements.Array = l_UVPropArray
l_AllInputUVTxtAndCAV.Add l_aUVArray(i)
l_AllOutputUVTxtAndCAV.Add l_UVProp
i = i+2
Loop
end if
if IsArray(l_aColorArray) then
i = 0
do while i < ubound(l_aColorArray)
set l_Cluster = l_NewMesh.ActivePrimitive.Geometry.AddCluster(siSampledPointCluster)
set l_ColorProp = l_Cluster.AddProperty("Vertex Color", ,l_aColorArray(i).Name & "_" & l_Cluster.Name)
ReDim l_ColorPropArray(3,l_Cluster.Elements.Count - 1)
for j = 0 to l_Cluster.Elements.Count-1
l_ColorPropArray(0,j) = l_aColorArray(i+1)(j*4)
l_ColorPropArray(1,j) = l_aColorArray(i+1)(j*4 + 1)
l_ColorPropArray(2,j) = l_aColorArray(i+1)(j*4 + 2)
l_ColorPropArray(3,j) = l_aColorArray(i+1)(j*4 + 3)
next
l_ColorProp.Elements.Array = l_ColorPropArray
l_AllInputUVTxtAndCAV.Add l_aColorArray(i)
l_AllOutputUVTxtAndCAV.Add l_ColorProp
i = i+2
Loop
end if
if IsArray(l_aMaterialArray) then
Dim l_bEmptyMaterialIndexArray
Dim l_CurrentMaterial
Dim l_NewMaterial
Dim l_NewCluster
Dim l_MaxIndex
Dim l_MaterialIndexArray
Dim l_Material
i = 0
do while i < ubound(l_aMaterialArray)
set l_NewCluster = l_NewMesh.ActivePrimitive.Geometry.AddCluster(siPolygonCluster, , l_aMaterialArray(i+1))
MakeLocal l_NewCluster.Material
CopyPaste l_aMaterialArray(i),,l_NewCluster,2
SIRebindByNameUserData l_NewCluster.Material, l_AllInputUVTxtAndCAV, l_AllOutputUVTxtAndCAV
i = i+2
Loop
end if
'Rebind the material that may be a local property at object level
SIRebindByNameUserData l_NewMesh.Material, l_AllInputUVTxtAndCAV, l_AllOutputUVTxtAndCAV
SelectObj l_NewMesh
End Sub |