コンテンツにスキップ

Face単位でシェーダーをアサイン

ipynbFile geomsubset_01__Face単位でシェーダーをアサイン.ipynb

こういう9のFaceがあるMeshでテスト。

In [56]:

1
from pxr import Usd,UsdGeom,UsdShade,Sdf,Gf

In [38]:

1
2
3
4
5
stage = Usd.Stage.Open(r"D:\work\py37\sampleUSD\usdPlane.usd")
prim = stage.GetPrimAtPath('/grid2/mesh_0')
path = prim.GetPath()
# PrimからGeomMeshを取得
mesh = UsdGeom.Mesh(prim)

In [39]:

1
stage.Reload()

In [40]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Meshの情報を取得
print(mesh.GetFaceCount())
# PointBasedで取得 頂点の座標
print(mesh.GetPointsAttr().Get())
# Faceを構成する頂点ID
print(mesh.GetFaceVertexIndicesAttr().Get())
# Face単位の頂点数
print(mesh.GetFaceVertexCountsAttr().Get())
# Faceの数
print(mesh.GetFaceCount())

Success

1
2
3
4
5
9
[(-1, 0, -1), (-0.3333333, 0, -1), (0.33333337, 0, -1), (1, 0, -1), (-1, 0, -0.3333333), (-0.3333333, 0, -0.3333333), (0.33333337, 0, -0.3333333), (1, 0, -0.3333333), (-1, 0, 0.33333337), (-0.3333333, 0, 0.33333337), (0.33333337, 0, 0.33333337), (1, 0, 0.33333337), (-1, 0, 1), (-0.3333333, 0, 1), (0.33333337, 0, 1), (1, 0, 1)]
[0, 1, 5, 4, 1, 2, 6, 5, 2, 3, 7, 6, 4, 5, 9, 8, 5, 6, 10, 9, 6, 7, 11, 10, 8, 9, 13, 12, 9, 10, 14, 13, 10, 11, 15, 14]
[4, 4, 4, 4, 4, 4, 4, 4, 4]
9

In [41]:

1
2
# Subsetを定義する
subset = UsdGeom.Subset.Define(stage,path.AppendChild('subset'))

In [42]:

1
print(mesh)

Success

1
UsdGeom.Mesh(Usd.Prim(</grid2/mesh_0>))

In [43]:

1
2
# 指定IndexをSubsetに追加
subset.CreateIndicesAttr([0,1,2])

Success

1
Usd.Prim(</grid2/mesh_0/subset>).GetAttribute('indices')

In [60]:

1
2
3
4
5
6
7
8
9
# Shaderを作る
mat = UsdShade.Material.Define(stage,"/Looks/sampleMat")
shader = UsdShade.Shader.Define(stage,"/Looks/sampleMat/sampleShader")
shader.CreateIdAttr('UsdPreviewSurface')
# わかりやすい色をセット
shader.CreateInput('diffuseColor', Sdf.ValueTypeNames.Color3f).Set(Gf.Vec3f(1,0,0))
mat.CreateSurfaceOutput().ConnectToSource(shader, "surface")
# subsetにアサイン
UsdShade.MaterialBindingAPI(subset).Bind(mat)

Success

1
True

In [61]:

1
print(stage.GetRootLayer().ExportToString())

Success

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#usda 1.0
(
    defaultPrim = "grid2"
    endTimeCode = 1
    framesPerSecond = 24
    metersPerUnit = 1
    startTimeCode = 1
    timeCodesPerSecond = 24
    upAxis = "Y"
)

def Xform "grid2" (
    kind = "component"
)
{
    matrix4d xformOp:transform:xform = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) )
    uniform token[] xformOpOrder = ["xformOp:transform:xform"]

    def Mesh "mesh_0"
    {
        float3[] extent = [(-1, 0, -1), (1, 0, 1)]
        int[] faceVertexCounts = [4, 4, 4, 4, 4, 4, 4, 4, 4]
        int[] faceVertexIndices = [0, 1, 5, 4, 1, 2, 6, 5, 2, 3, 7, 6, 4, 5, 9, 8, 5, 6, 10, 9, 6, 7, 11, 10, 8, 9, 13, 12, 9, 10, 14, 13, 10, 11, 15, 14]
        uniform token orientation = "leftHanded"
        point3f[] points = [(-1, 0, -1), (-0.3333333, 0, -1), (0.33333337, 0, -1), (1, 0, -1), (-1, 0, -0.3333333), (-0.3333333, 0, -0.3333333), (0.33333337, 0, -0.3333333), (1, 0, -0.3333333), (-1, 0, 0.33333337), (-0.3333333, 0, 0.33333337), (0.33333337, 0, 0.33333337), (1, 0, 0.33333337), (-1, 0, 1), (-0.3333333, 0, 1), (0.33333337, 0, 1), (1, 0, 1)] (
            interpolation = "vertex"
        )
        uniform token subdivisionScheme = "none"

        def GeomSubset "subset"
        {
            int[] indices = [0, 1, 2]
            rel material:binding = </Looks/sampleMat>
        }
    }
}

def "Looks"
{
    def Material "sampleMat"
    {
        token outputs:surface.connect = </Looks/sampleMat/sampleShader.outputs:surface>

        def Shader "sampleShader"
        {
            uniform token info:id = "UsdPreviewSurface"
            color3f inputs:diffuseColor = (1, 0, 0)
            token outputs:surface
        }
    }
}

In [62]:

1
stage.GetRootLayer().Export(r"D:\work\py37\sampleUSD\subset_addMat.usd")

Success

1
True

こうなって

こうなる。