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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| @ccclass('PBRShaderSetup') export class PBRShaderSetup extends Component { @property(MeshRenderer) meshRenderer: MeshRenderer = null!; @property(Texture2D) albedoMap: Texture2D = null!; @property(Texture2D) normalMap: Texture2D = null!; @property(Texture2D) metallicMap: Texture2D = null!; @property(Texture2D) roughnessMap: Texture2D = null!; @property(Texture2D) occlusionMap: Texture2D = null!; start() { this.setupPBRMaterial(); } private setupPBRMaterial() { const material = Material.createWithBuiltin('builtin-standard', 0); material.setProperty('albedoMap', this.albedoMap); material.setProperty('albedo', Color.WHITE); material.setProperty('normalMap', this.normalMap); material.setProperty('normalStrength', 1.0); material.setProperty('metallicRoughnessMap', this.metallicMap); material.setProperty('metallic', 0.5); material.setProperty('roughness', 0.5); material.setProperty('occlusionMap', this.occlusionMap); material.setProperty('occlusion', 1.0); material.setProperty('emissive', Color.BLACK); material.setProperty('emissiveScale', new Vec3(1, 1, 1)); this.meshRenderer.setMaterial(material, 0); } setupMetalMaterial() { const material = this.meshRenderer.getMaterial(0); if (material) { material.setProperty('metallic', 1.0); material.setProperty('roughness', 0.1); material.setProperty('albedo', new Color(200, 200, 200, 255)); } } setupRubberMaterial() { const material = this.meshRenderer.getMaterial(0); if (material) { material.setProperty('metallic', 0.0); material.setProperty('roughness', 0.9); material.setProperty('albedo', new Color(50, 50, 50, 255)); } } setupPlasticMaterial() { const material = this.meshRenderer.getMaterial(0); if (material) { material.setProperty('metallic', 0.0); material.setProperty('roughness', 0.3); material.setProperty('albedo', new Color(100, 150, 200, 255)); } } }
|