1331{
1332 NSMutableDictionary *result = [NSMutableDictionary dictionary];
1335
1336
1339 if (col !=
nil) [result setObject:[col
normalizedArray] forKey:kOOMaterialDiffuseColorName];
1340
1343 if (col !=
nil) [result setObject:[col
normalizedArray] forKey:kOOMaterialAmbientColorName];
1344
1347 if (col !=
nil) [result setObject:[col
normalizedArray] forKey:kOOMaterialSpecularColorName];
1348
1350 if (col !=
nil) [result setObject:[col
normalizedArray] forKey:kOOMaterialSpecularModulateColorName];
1351
1354 if (col !=
nil) [result setObject:[col
normalizedArray] forKey:kOOMaterialEmissionColorName];
1355
1356
1357 texSpec = [spec objectForKey:kOOMaterialDiffuseMapName];
1358 if ([texSpec isKindOfClass:[NSString class]])
1359 {
1360 if ([texSpec length] > 0)
1361 {
1362 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1363 }
1364 }
1365 else if ([texSpec isKindOfClass:[NSDictionary class]])
1366 {
1367
1368
1369
1370 NSString *name = [texSpec objectForKey:kOOTextureSpecifierNameKey];
1371 if (name ==
nil) texSpec = [texSpec dictionaryByAddingObject:materialKey forKey:kOOTextureSpecifierNameKey];
1372 else if ([name length] == 0)
1373 {
1374 texSpec = [texSpec dictionaryByRemovingObjectForKey:kOOTextureSpecifierNameKey];
1375 }
1376 }
1377 else
1378 {
1379
1380 texSpec = [NSDictionary dictionaryWithObject:materialKey forKey:kOOTextureSpecifierNameKey];
1381 }
1382 [result setObject:texSpec forKey:kOOMaterialDiffuseMapName];
1383
1384
1385 {
1386 BOOL haveNewSpecular = NO;
1387 texSpec = [spec objectForKey:kOOMaterialSpecularColorMapName];
1388 if ([texSpec isKindOfClass:[NSString class]])
1389 {
1390 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1391 }
1392 else if (![texSpec isKindOfClass:[NSDictionary class]])
1393 {
1395 }
1397 {
1398 haveNewSpecular = YES;
1399 [result setObject:texSpec forKey:kOOMaterialSpecularColorMapName];
1400 }
1401
1402 texSpec = [spec objectForKey:kOOMaterialSpecularExponentMapName];
1403 if ([texSpec isKindOfClass:[NSString class]])
1404 {
1405 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1406 }
1407 else if (![texSpec isKindOfClass:[NSDictionary class]])
1408 {
1410 }
1412 {
1413 haveNewSpecular = YES;
1414 [result setObject:texSpec forKey:kOOMaterialSpecularExponentMapName];
1415 }
1416
1417 if (!haveNewSpecular)
1418 {
1419
1420 texSpec = [spec objectForKey:kOOMaterialCombinedSpecularMapName];
1421 if ([texSpec isKindOfClass:[NSString class]])
1422 {
1423 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1424 }
1425 else if (![texSpec isKindOfClass:[NSDictionary class]])
1426 {
1428 }
1430 {
1431 [result setObject:texSpec forKey:kOOMaterialSpecularColorMapName];
1432 texSpec = [texSpec dictionaryByAddingObject:@"a" forKey:kOOTextureSpecifierSwizzleKey];
1433 [result setObject:texSpec forKey:kOOMaterialSpecularExponentMapName];
1434 }
1435 }
1436 }
1437
1438
1439 {
1440 BOOL haveParallax = NO;
1441 BOOL haveNewNormal = NO;
1442 texSpec = [spec objectForKey:kOOMaterialNormalMapName];
1443 if ([texSpec isKindOfClass:[NSString class]])
1444 {
1445 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1446 }
1447 else if (![texSpec isKindOfClass:[NSDictionary class]])
1448 {
1450 }
1452 {
1453 haveNewNormal = YES;
1454 [result setObject:texSpec forKey:kOOMaterialNormalMapName];
1455 }
1456
1457 texSpec = [spec objectForKey:kOOMaterialParallaxMapName];
1458 if ([texSpec isKindOfClass:[NSString class]])
1459 {
1460 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1461 }
1462 else if (![texSpec isKindOfClass:[NSDictionary class]])
1463 {
1465 }
1467 {
1468 haveNewNormal = YES;
1469 haveParallax = YES;
1470 [result setObject:texSpec forKey:kOOMaterialParallaxMapName];
1471 }
1472
1473 if (!haveNewNormal)
1474 {
1475
1476 texSpec = [spec objectForKey:kOOMaterialNormalAndParallaxMapName];
1477 if ([texSpec isKindOfClass:[NSString class]])
1478 {
1479 texSpec = [NSDictionary dictionaryWithObject:texSpec forKey:kOOTextureSpecifierNameKey];
1480 }
1481 else if (![texSpec isKindOfClass:[NSDictionary class]])
1482 {
1484 }
1486 {
1487 haveParallax = YES;
1488 [result setObject:texSpec forKey:kOOMaterialNormalMapName];
1489 texSpec = [texSpec dictionaryByAddingObject:@"a" forKey:kOOTextureSpecifierSwizzleKey];
1490 [result setObject:texSpec forKey:kOOMaterialParallaxMapName];
1491 }
1492 }
1493
1494
1495 if (haveParallax)
1496 {
1497 float parallaxScale = [spec oo_floatForKey:kOOMaterialParallaxScaleName defaultValue:kOOMaterialDefaultParallaxScale];
1498 [result oo_setFloat:parallaxScale forKey:kOOMaterialParallaxScaleName];
1499
1500 float parallaxBias = [spec oo_floatForKey:kOOMaterialParallaxBiasName];
1501 [result oo_setFloat:parallaxBias forKey:kOOMaterialParallaxBiasName];
1502 }
1503 }
1504
1505
1506 {
1507 NSMutableArray *lightMaps = [NSMutableArray array];
1508 id lightMapSpecs = [spec objectForKey:kOOMaterialLightMapsName];
1509 if (lightMapSpecs !=
nil && ![lightMapSpecs isKindOfClass:[NSArray
class]])
1510 {
1511 lightMapSpecs = [NSArray arrayWithObject:lightMapSpecs];
1512 }
1513
1515 foreach (lmSpec, lightMapSpecs)
1516 {
1517 if ([lmSpec isKindOfClass:[NSString class]])
1518 {
1519 lmSpec = [NSMutableDictionary dictionaryWithObject:lmSpec forKey:kOOTextureSpecifierNameKey];
1520 }
1521 else if ([lmSpec isKindOfClass:[NSDictionary class]])
1522 {
1523 lmSpec = [[lmSpec mutableCopy] autorelease];
1524 }
1525 else
1526 {
1527 continue;
1528 }
1529
1530 id modulateColor = [lmSpec objectForKey:kOOTextureSpecifierModulateColorKey];
1531 if (modulateColor !=
nil && ![modulateColor isKindOfClass:[NSArray
class]])
1532 {
1533
1535 [lmSpec setObject:[col
normalizedArray] forKey:kOOTextureSpecifierModulateColorKey];
1536 }
1537
1538 id binding = [lmSpec objectForKey:kOOTextureSpecifierBindingKey];
1540 {
1541 if ([binding isKindOfClass:[NSString class]])
1542 {
1543 NSDictionary *expandedBinding = [NSDictionary dictionaryWithObjectsAndKeys:@"binding", @"type", binding, @"binding", nil];
1544 [lmSpec setObject:expandedBinding forKey:kOOTextureSpecifierBindingKey];
1545 }
1546 else if (![binding isKindOfClass:[NSDictionary class]] || [[binding oo_stringForKey:@"binding"] length] == 0)
1547 {
1548 [lmSpec removeObjectForKey:kOOTextureSpecifierBindingKey];
1549 }
1550 }
1551
1552 [lightMaps addObject:[[lmSpec copy] autorelease]];
1553 }
1554
1555 if ([lightMaps
count] == 0)
1556 {
1557
1558 id emissionSpec = [spec objectForKey:kOOMaterialEmissionMapName];
1559 id illuminationSpec = [spec objectForKey:kOOMaterialIlluminationMapName];
1560
1561 if (emissionSpec ==
nil && illuminationSpec ==
nil)
1562 {
1563 emissionSpec = [spec objectForKey:kOOMaterialEmissionAndIlluminationMapName];
1564 if ([emissionSpec isKindOfClass:[NSString class]])
1565 {
1566
1567 emissionSpec = [NSDictionary dictionaryWithObject:emissionSpec forKey:kOOTextureSpecifierNameKey];
1568 }
1569 else if (![emissionSpec isKindOfClass:[NSDictionary class]])
1570 {
1572 }
1573
1574 if (emissionSpec !=
nil)
1575 {
1576 illuminationSpec = [emissionSpec dictionaryByAddingObject:@"a" forKey:kOOTextureSpecifierSwizzleKey];
1577 }
1578 }
1579
1580 if (emissionSpec !=
nil)
1581 {
1582 if ([emissionSpec isKindOfClass:[NSString class]])
1583 {
1584 emissionSpec = [NSDictionary dictionaryWithObject:emissionSpec forKey:kOOTextureSpecifierNameKey];
1585 }
1586 if ([emissionSpec isKindOfClass:[NSDictionary class]])
1587 {
1589 if (col !=
nil) emissionSpec = [emissionSpec dictionaryByAddingObject:[col
normalizedArray] forKey:kOOTextureSpecifierModulateColorKey];
1590
1591 [lightMaps addObject:emissionSpec];
1592 }
1593 }
1594
1595 if (illuminationSpec !=
nil)
1596 {
1597 if ([illuminationSpec isKindOfClass:[NSString class]])
1598 {
1599 illuminationSpec = [NSDictionary dictionaryWithObject:illuminationSpec forKey:kOOTextureSpecifierNameKey];
1600 }
1601 if ([illuminationSpec isKindOfClass:[NSDictionary class]])
1602 {
1604 if (col !=
nil) illuminationSpec = [illuminationSpec dictionaryByAddingObject:[col
normalizedArray] forKey:kOOTextureSpecifierModulateColorKey];
1605
1606 illuminationSpec = [illuminationSpec dictionaryByAddingObject:[NSNumber numberWithBool:YES] forKey:kOOTextureSpecifierIlluminationModeKey];
1607
1608 [lightMaps addObject:illuminationSpec];
1609 }
1610 }
1611 }
1612
1613 [result setObject:lightMaps forKey:kOOMaterialLightMapsName];
1614 }
1615
1616 OOLog(
@"material.canonicalForm",
@"Canonicalized material %@:\nORIGINAL:\n%@\n\n@CANONICAL:\n%@", materialKey, spec, result);
1617
1618 return result;
1619}
#define OOLog(class, format,...)
OOColor * colorWithDescription:(id description)
NSArray * normalizedArray()