void MatMod::ModifyObject(TimeValue t, ModContext &mc, ObjectState *os, INode *node) { Interval valid = FOREVER; int id; pblock->GetValue(PB_MATID,t,id,valid); id--; if (id<0) id = 0; if (id>0xffff) id = 0xffff; // For version 4 and later, we process patch meshes as they are and pass them on. Earlier // versions converted to TriMeshes (done below). For adding other new types of objects, add // them here! #ifndef NO_PATCHES if(version >= MATMOD_VER4 && os->obj->IsSubClassOf(patchObjectClassID)) { PatchObject *patchOb = (PatchObject *)os->obj; PatchMesh &pmesh = patchOb->GetPatchMesh(t); BOOL useSel = pmesh.selLevel >= PO_PATCH; for (int i=0; i<pmesh.getNumPatches(); i++) { if (!useSel || pmesh.patchSel[i]) { pmesh.setPatchMtlIndex(i,(MtlID)id); } } pmesh.InvalidateGeomCache(); // Do this because there isn't a topo cache in PatchMesh patchOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else #endif // NO_PATCHES // Process PolyObjects if(os->obj->IsSubClassOf(polyObjectClassID)) { PolyObject *polyOb = (PolyObject *)os->obj; MNMesh &mesh = polyOb->GetMesh(); BOOL useSel = mesh.selLevel == MNM_SL_FACE; for (int i=0; i<mesh.numf; i++) { if (!useSel || mesh.f[i].GetFlag(MN_SEL)) { mesh.f[i].material = (MtlID)id; } } polyOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else // If it's a TriObject, process it if(os->obj->IsSubClassOf(triObjectClassID)) { TriObject *triOb = (TriObject *)os->obj; DoMaterialSet(triOb, id); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else // Fallback position: If it can convert to a TriObject, do it! if(os->obj->CanConvertToType(triObjectClassID)) { TriObject *triOb = (TriObject *)os->obj->ConvertToType(t, triObjectClassID); // Now stuff this into the pipeline! os->obj = triOb; DoMaterialSet(triOb, id); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else return; // Do nothing if it can't convert to triObject }
void NormalMod::ModifyObject(TimeValue t, ModContext &mc, ObjectState *os, INode *node) { Interval valid = FOREVER; int flip, unify; pblock->GetValue(PB_FLIP,t,flip,valid); pblock->GetValue(PB_UNIFY,t,unify,valid); // For version 4 and later, we process patch meshes as they are and pass them on. Earlier // versions converted to TriMeshes (done below). For adding other new types of objects, add // them here! #ifndef NO_PATCHES if(version >= MATMOD_VER4 && os->obj->IsSubClassOf(patchObjectClassID)) { PatchObject *patchOb = (PatchObject *)os->obj; PatchMesh &pmesh = patchOb->GetPatchMesh(t); BOOL useSel = pmesh.selLevel >= PO_PATCH; if (unify) pmesh.UnifyNormals(useSel); if (flip) pmesh.FlipPatchNormal(useSel ? -1 : -2); patchOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else // If it's a TriObject, process it #endif // NO_PATCHES if(os->obj->IsSubClassOf(triObjectClassID)) { TriObject *triOb = (TriObject *)os->obj; DoNormalSet(triOb, unify, flip); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); } // Process PolyObjects // note: Since PolyObjects must always have the normals alligned they do not // need to support unify and they do not allow normal flips on selected faces else if(os->obj->IsSubClassOf(polyObjectClassID)) { PolyObject *pPolyOb = (PolyObject *)os->obj; MNMesh& mesh = pPolyOb->GetMesh(); if (flip) { // flip selected faces only if entire elements are selected if (mesh.selLevel == MNM_SL_FACE) { // sca 12/8/2000: Use MNMesh flipping code instead of the code that was here. mesh.FlipElementNormals (MN_SEL); } else { // Flip the entire object if selected elements were not flipped for (int i=0; i<mesh.FNum(); i++) { mesh.f[i].SetFlag (MN_WHATEVER, !mesh.f[i].GetFlag(MN_DEAD)); } mesh.FlipElementNormals (MN_WHATEVER); } // Luna task 747: // We cannot support specified normals here at this time. // NOTE this assumes that both the topo and geo channels are to be freed // this means that the modifier needs to also set the channels changed to // geo and topo otherwise we will be deleting a channel we dont own. mesh.ClearSpecifiedNormals (); } pPolyOb->UpdateValidity(GEOM_CHAN_NUM,valid); pPolyOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else // Fallback position: If it can convert to a TriObject, do it! if(os->obj->CanConvertToType(triObjectClassID)) { TriObject *triOb = (TriObject *)os->obj->ConvertToType(t, triObjectClassID); // Now stuff this into the pipeline! os->obj = triOb; DoNormalSet(triOb, unify, flip); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); } else return; // Do nothing if it can't convert to triObject }
void SmoothMod::ModifyObject(TimeValue t, ModContext &mc, ObjectState *os, INode *node) { Interval valid = FOREVER; int autoSmooth, bits = 0, prevent = 0; float thresh = 0.0f; pblock->GetValue(sm_autosmooth, t, autoSmooth, valid); if (autoSmooth) { pblock->GetValue(sm_threshold, t, thresh, valid); pblock->GetValue(sm_prevent_indirect, t, prevent, valid); } else { pblock->GetValue(sm_smoothbits, t, bits, valid); } // For version 4 and later, we process patch meshes as they are and pass them on. Earlier // versions converted to TriMeshes (done below). For adding other new types of objects, add // them here! bool done = false; #ifndef NO_PATCHES if(version >= MATMOD_VER4 && os->obj->IsSubClassOf(patchObjectClassID)) { PatchObject *patchOb = (PatchObject *)os->obj; PatchMesh &pmesh = patchOb->GetPatchMesh(t); BOOL useSel = pmesh.selLevel >= PO_PATCH; if (autoSmooth) pmesh.AutoSmooth (thresh, useSel, prevent); else { for (int i=0; i<pmesh.getNumPatches(); i++) { if (!useSel || pmesh.patchSel[i]) pmesh.patches[i].smGroup = (DWORD)bits; } } pmesh.InvalidateGeomCache(); // Do this because there isn't a topo cache in PatchMesh patchOb->UpdateValidity(TOPO_CHAN_NUM,valid); done = true; } #endif // NO_PATCHES if (!done && os->obj->IsSubClassOf (polyObjectClassID)) { PolyObject *pPolyOb = (PolyObject *)os->obj; MNMesh &mesh = pPolyOb->GetMesh(); BOOL useSel = (mesh.selLevel == MNM_SL_FACE); if (autoSmooth) mesh.AutoSmooth (thresh, useSel, prevent); else { for (int faceIndex=0; faceIndex<mesh.FNum(); faceIndex++) { if (!useSel || mesh.F(faceIndex)->GetFlag(MN_SEL)) { mesh.F(faceIndex)->smGroup = (DWORD)bits; } } } // Luna task 747 // We need to rebuild the smoothing-group-based normals in the normalspec, if any: if (mesh.GetSpecifiedNormals()) { mesh.GetSpecifiedNormals()->SetParent (&mesh); mesh.GetSpecifiedNormals()->BuildNormals (); mesh.GetSpecifiedNormals()->ComputeNormals (); } pPolyOb->UpdateValidity(TOPO_CHAN_NUM,valid); done = true; } TriObject *triOb = NULL; if (!done) { if (os->obj->IsSubClassOf(triObjectClassID)) triOb = (TriObject *)os->obj; else { // Convert to triobject if we can. if(os->obj->CanConvertToType(triObjectClassID)) { TriObject *triOb = (TriObject *)os->obj->ConvertToType(t, triObjectClassID); // We'll need to stuff this back into the pipeline: os->obj = triOb; // Convert validities: Interval objValid = os->obj->ChannelValidity (t, TOPO_CHAN_NUM); triOb->SetChannelValidity (TOPO_CHAN_NUM, objValid); triOb->SetChannelValidity (GEOM_CHAN_NUM, objValid & os->obj->ChannelValidity (t, GEOM_CHAN_NUM)); triOb->SetChannelValidity (TEXMAP_CHAN_NUM, objValid & os->obj->ChannelValidity (t, TEXMAP_CHAN_NUM)); triOb->SetChannelValidity (VERT_COLOR_CHAN_NUM, objValid & os->obj->ChannelValidity (t, VERT_COLOR_CHAN_NUM)); triOb->SetChannelValidity (DISP_ATTRIB_CHAN_NUM, objValid & os->obj->ChannelValidity (t, DISP_ATTRIB_CHAN_NUM)); } } } if (triOb) { // one way or another, there's a triobject to smooth. Mesh & mesh = triOb->GetMesh(); BOOL useSel = mesh.selLevel == MESH_FACE; if (autoSmooth) mesh.AutoSmooth (thresh, useSel, prevent); else { for (int i=0; i<mesh.getNumFaces(); i++) { if (!useSel || mesh.faceSel[i]) mesh.faces[i].smGroup = (DWORD)bits; } } triOb->GetMesh().InvalidateTopologyCache(); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); } }
void OptMod::ModifyObject(TimeValue t, ModContext &mc, ObjectState *os, INode *node) { float faceThresh, edgeThresh, bias, maxEdge; int preserveMat, preserveSmooth, which, render=0, autoEdge; DWORD flags = 0; Interval valid = FOREVER; int nv,nf; int man; pblock->GetValue(PB_MANUPDATE,t,man,valid); if (man && !forceUpdate && !TestAFlag(A_RENDER)) return; forceUpdate = FALSE; if (TestAFlag(A_RENDER)) { pblock->GetValue(PB_RENDER,t,which,valid); } else { pblock->GetValue(PB_VIEWS,t,which,valid); } pblock->GetValue(PB_AUTOEDGE,t,autoEdge,valid); if (which==0) { pblock->GetValue(PB_FACETHRESH1,t,faceThresh,valid); pblock->GetValue(PB_EDGETHRESH1,t,edgeThresh,valid); pblock->GetValue(PB_BIAS1,t,bias,valid); pblock->GetValue(PB_PRESERVEMAT1,t,preserveMat,valid); pblock->GetValue(PB_PRESERVESMOOTH1,t,preserveSmooth,valid); pblock->GetValue(PB_MAXEDGE1,t,maxEdge,valid); } else { pblock->GetValue(PB_FACETHRESH2,t,faceThresh,valid); pblock->GetValue(PB_EDGETHRESH2,t,edgeThresh,valid); pblock->GetValue(PB_BIAS2,t,bias,valid); pblock->GetValue(PB_PRESERVEMAT2,t,preserveMat,valid); pblock->GetValue(PB_PRESERVESMOOTH2,t,preserveSmooth,valid); pblock->GetValue(PB_MAXEDGE2,t,maxEdge,valid); } assert(os->obj->IsSubClassOf(triObjectClassID)); TriObject *triOb = (TriObject *)os->obj; nv = triOb->GetMesh().getNumVerts(); nf = triOb->GetMesh().getNumFaces(); if (preserveMat) flags |= OPTIMIZE_SAVEMATBOUNDRIES; if (preserveSmooth) flags |= OPTIMIZE_SAVESMOOTHBOUNDRIES; if (autoEdge) flags |= OPTIMIZE_AUTOEDGE; if (faceThresh!=0.0f) { GetAsyncKeyState(VK_ESCAPE); // clear the state HCURSOR hCur; if (nf > 2000) hCur = SetCursor(LoadCursor(NULL,IDC_WAIT)); triOb->GetMesh().Optimize( faceThresh,edgeThresh, bias*0.5f, maxEdge, flags,this); if (nf > 200) SetCursor(hCur); } triOb->GetMesh().InvalidateTopologyCache (); triOb->PointsWereChanged(); triOb->UpdateValidity(GEOM_CHAN_NUM,valid); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); triOb->UpdateValidity (TEXMAP_CHAN_NUM, valid); triOb->UpdateValidity (VERT_COLOR_CHAN_NUM, valid); if (pmapParam && pmapParam->GetParamBlock()==pblock && !TestAFlag(A_RENDER)) { TSTR buf; buf.printf("%d / %d",nv,triOb->GetMesh().getNumVerts()); SetWindowText(GetDlgItem(pmapParam->GetHWnd(),IDC_OPT_VERTCOUNT),buf); buf.printf("%d / %d",nf,triOb->GetMesh().getNumFaces()); SetWindowText(GetDlgItem(pmapParam->GetHWnd(),IDC_OPT_FACECOUNT),buf); } }
void BombMod::ModifyObject( TimeValue t, ModContext &mc, ObjectState *os, INode *node) { BombObject *bobj = GetWSMObject(t); if (bobj && nodeRef && (bobj->ClassID() == Class_ID(BOMB_OBJECT_CLASS_ID,0))) { assert(os->obj->IsSubClassOf(triObjectClassID)); TriObject *triOb = (TriObject *)os->obj; Interval valid = FOREVER; if (os->GetTM()) { Matrix3 tm = *(os->GetTM()); for (int i=0; i<triOb->GetMesh().getNumVerts(); i++) { triOb->GetMesh().verts[i] = triOb->GetMesh().verts[i] * tm; } os->obj->UpdateValidity(GEOM_CHAN_NUM,os->tmValid()); os->SetTM(NULL,FOREVER); } if (waitPostLoad) { valid.SetInstant(t); triOb->UpdateValidity(GEOM_CHAN_NUM,valid); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); return; } Matrix3 tm; TimeValue det = bobj->GetDetonation(t,valid); float strength = bobj->GetStrength(t,valid) * STRENGTH_CONSTANT; float grav = bobj->GetGravity(t,valid); float chaos = bobj->GetChaos(t,valid); float chaosBase = (float(1)-chaos/2); float rotSpeed = bobj->GetSpin(t,valid) * TWOPI/float(TIME_TICKSPERSEC); int minClust = bobj->GetMinFrag(t,valid), maxClust = bobj->GetMaxFrag(t,valid); if (minClust<1) minClust = 1; if (maxClust<1) maxClust = 1; int clustVar = maxClust-minClust+1; float falloff = bobj->GetFalloff(t,valid); int useFalloff = bobj->GetFalloffOn(t,valid); int seed = bobj->GetSeed(t,valid); //tm = nodeRef->GetNodeTM(t,&valid); tm = nodeRef->GetObjectTM(t,&valid); if (t<det) { valid.Set(TIME_NegInfinity,det-1); triOb->UpdateValidity(GEOM_CHAN_NUM,valid); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); triOb->PointsWereChanged(); return; } float dt = float(t-det); valid.SetInstant(t); int n0=0,n1=1,n2=2,nv; Point3 v, p0, p1, g(0.0f,0.0f,grav*GRAVITY_CONSTANT); Tab<Point3> l_newVerts ; Face *f = triOb->GetMesh().faces; float dot; Mesh &mesh = triOb->GetMesh(); // First, segment the faces. srand((unsigned int)seed); Tab<DWORD> vclust; Tab<DWORD> vmap; Tab<Point3> nverts; vclust.SetCount(mesh.getNumVerts()); vmap.SetCount(mesh.getNumVerts()); int numClust = 0; if (minClust==1 && maxClust==1) { // Simple case... 1 face per cluster nv = triOb->GetMesh().getNumFaces() * 3; l_newVerts.SetCount(nv); vclust.SetCount(nv); for (int i=0; i<nv; i++) { vclust[i] = i/3; } for (int i=0,j=0; i<mesh.getNumFaces(); i++) { l_newVerts[j] = mesh.verts[mesh.faces[i].v[0]]; l_newVerts[j+1] = mesh.verts[mesh.faces[i].v[1]]; l_newVerts[j+2] = mesh.verts[mesh.faces[i].v[2]]; mesh.faces[i].v[0] = j; mesh.faces[i].v[1] = j+1; mesh.faces[i].v[2] = j+2; j += 3; } numClust = triOb->GetMesh().getNumFaces(); } else { // More complex case... clusters are randomely sized for (int i=0; i<mesh.getNumVerts(); i++) { vclust[i] = UNDEFINED; vmap[i] = i; } int cnum = 0; for (int i=0; i<mesh.getNumFaces(); ) { int csize = int(Rand1()*float(clustVar)+float(minClust)); if (i+csize>mesh.getNumFaces()) { csize = mesh.getNumFaces()-i; } // Make sure each face in the cluster has at least 2 verts in common with another face. BOOL verified = FALSE; while (!verified) { verified = TRUE; if (csize<2) break; for (int j=0; j<csize; j++) { BOOL match = FALSE; for (int k=0; k<csize; k++) { if (k==j) continue; int common = 0; for (int i1=0; i1<3; i1++) { for (int i2=0; i2<3; i2++) { if (mesh.faces[i+j].v[i1]== mesh.faces[i+k].v[i2]) common++; } } if (common>=2) { match = TRUE; break; } } if (!match) { csize = j; verified = FALSE; // Have to check again break; } } } if (csize==0) csize = 1; // Clear the vert map for (int j=0; j<mesh.getNumVerts(); j++) vmap[j] = UNDEFINED; // Go through the cluster and remap verts. for (int j=0;j<csize; j++) { for (int k=0; k<3; k++) { if (vclust[mesh.faces[i+j].v[k]]==UNDEFINED) { vclust[mesh.faces[i+j].v[k]] = cnum; } else if (vclust[mesh.faces[i+j].v[k]]!=(DWORD)cnum) { if (vmap[mesh.faces[i+j].v[k]]==UNDEFINED) { vclust.Append(1,(DWORD*)&cnum,50); nverts.Append(1,&mesh.verts[mesh.faces[i+j].v[k]],50); mesh.faces[i+j].v[k] = vmap[mesh.faces[i+j].v[k]] = mesh.getNumVerts()+nverts.Count()-1; } else { mesh.faces[i+j].v[k] = vmap[mesh.faces[i+j].v[k]]; } } } } cnum++; numClust++; i += csize; } nv = mesh.getNumVerts() + nverts.Count(); l_newVerts.SetCount(nv); int i; for (i=0; i<mesh.getNumVerts(); i++) l_newVerts[i] = mesh.verts[i]; for ( ; i<mesh.getNumVerts()+nverts.Count(); i++) l_newVerts[i] = nverts[i-mesh.getNumVerts()]; } // Find the center of all clusters Tab<Point3> clustCent; Tab<DWORD> clustCounts; clustCent.SetCount(numClust); clustCounts.SetCount(numClust); for (int i=0; i<numClust; i++) { clustCent[i] = Point3(0,0,0); clustCounts[i] = 0; } for (int i=0; i<nv; i++) { if (vclust[i]==UNDEFINED) continue; clustCent[vclust[i]] += l_newVerts[i]; clustCounts[vclust[i]]++; } // Build transformations for all clusters Tab<Matrix3> mats; mats.SetCount(numClust); srand((unsigned int)seed); for (int i=0; i<numClust; i++) { if (clustCounts[i]) clustCent[i] /= float(clustCounts[i]); v = clustCent[i] - tm.GetTrans(); float u = 1.0f; if (useFalloff) { u = 1.0f - Length(v)/falloff; if (u<0.0f) u = 0.0f; } dot = DotProd(v,v); if (dot==0.0f) dot = 0.000001f; v = v / dot * strength * u; v.x *= chaosBase + chaos * Rand1(); v.y *= chaosBase + chaos * Rand1(); v.z *= chaosBase + chaos * Rand1(); p1 = v*dt + 0.5f*g*dt*dt; // projectile motion // Set rotation Point3 axis; axis.x = -1.0f + 2.0f*Rand1(); axis.y = -1.0f + 2.0f*Rand1(); axis.z = -1.0f + 2.0f*Rand1(); axis = Normalize(axis); float angle = dt*rotSpeed*(chaosBase + chaos * Rand1())*u; Quat q = QFromAngAxis(angle, axis); q.MakeMatrix(mats[i]); mats[i].PreTranslate(-clustCent[i]); mats[i].Translate(clustCent[i]+p1); } // Now transform the clusters for (int i=0; i<nv; i++) { if (vclust[i]==UNDEFINED) continue; l_newVerts[i] = l_newVerts[i] * mats[vclust[i]]; } triOb->UpdateValidity(GEOM_CHAN_NUM,valid); triOb->UpdateValidity(TOPO_CHAN_NUM,valid); triOb->PointsWereChanged(); triOb->GetMesh().setNumVerts(nv,FALSE,TRUE); //assign the new vertices to the mesh for ( int i=0; i < nv; i++) triOb->GetMesh().setVert( i,l_newVerts[i]); } }