static void CreateMaterial(GameObject go) { // Create a simple material asset if (go.GetComponent <Renderer>() != null) { Material material = new Material(go.GetComponent <Renderer>().sharedMaterial); material.CopyPropertiesFromMaterial(go.GetComponent <Renderer>().sharedMaterial); go.GetComponent <Renderer>().sharedMaterial = material; MaterialPropertyBlock block = new MaterialPropertyBlock(); go.GetComponent <Renderer>().GetPropertyBlock(block); #if UNITY_EDITOR if (!Directory.Exists("Assets/Materials")) { AssetDatabase.CreateFolder("Assets", "Materials"); AssetDatabase.Refresh(); } string textureName = null; if (block.GetTexture(0).name != null) { textureName = block.GetTexture(0).name; } else { textureName = material.mainTexture.name; } AssetDatabase.CreateAsset(material, "Assets/Materials/" + textureName + ".mat"); Debug.Log("Created material " + textureName + " for " + go.name); #endif } }
private TexInfo CreateDefaultTexInfo() { TexInfo defaultTexInfo = new TexInfo(); defaultTexInfo.name = DEFAULT_KEY; var smr = gameObject.GetComponent <SkinnedMeshRenderer>(); if (smr != null) { Material[] materials = smr.sharedMaterials; if (materials != null) { MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock(); for (int i = 0; i < materials.Length; ++i) { materialPropertyBlock.Clear(); smr.GetPropertyBlock(materialPropertyBlock, i); defaultTexInfo.mainTex = materialPropertyBlock.GetTexture("_MainTex"); defaultTexInfo.flowTex = materialPropertyBlock.GetTexture("_FlowTex"); break; } } } return(defaultTexInfo); }
public Texture GetMainTexture() { if (instancedProperties.Contains(mainTexturePropertyId)) { return(materialPropertyBlock.GetTexture(mainTexturePropertyId)); } else { return(material.mainTexture); } }
/// <inheritdoc /> public override ThemePropertyValue GetProperty(ThemeStateProperty property) { if (renderer == null) { return(null); } renderer.GetPropertyBlock(propertyBlock); startValue.Reset(); int propId = property.GetShaderPropertyId(); switch (property.Type) { case ThemePropertyTypes.Color: startValue.Color = propertyBlock.GetVector(propId); break; case ThemePropertyTypes.Texture: startValue.Texture = propertyBlock.GetTexture(propId); break; case ThemePropertyTypes.ShaderFloat: case ThemePropertyTypes.ShaderRange: startValue.Float = propertyBlock.GetFloat(propId); break; default: break; } return(startValue); }
static int GetTexture(IntPtr L) { int count = LuaDLL.lua_gettop(L); Type[] types0 = { typeof(MaterialPropertyBlock), typeof(int) }; Type[] types1 = { typeof(MaterialPropertyBlock), typeof(string) }; if (count == 2 && LuaScriptMgr.CheckTypes(L, types0, 1)) { MaterialPropertyBlock obj = LuaScriptMgr.GetNetObject <MaterialPropertyBlock>(L, 1); int arg0 = (int)LuaScriptMgr.GetNumber(L, 2); Texture o = obj.GetTexture(arg0); LuaScriptMgr.Push(L, o); return(1); } else if (count == 2 && LuaScriptMgr.CheckTypes(L, types1, 1)) { MaterialPropertyBlock obj = LuaScriptMgr.GetNetObject <MaterialPropertyBlock>(L, 1); string arg0 = LuaScriptMgr.GetString(L, 2); Texture o = obj.GetTexture(arg0); LuaScriptMgr.Push(L, o); return(1); } else { LuaDLL.luaL_error(L, "invalid arguments to method: MaterialPropertyBlock.GetTexture"); } return(0); }
public Texture GetBumpTexture() { switch (textureType) { case NormalMapTextureType.Sprite: if (sprite == null) { return(null); } return(sprite.texture); case NormalMapTextureType.Texture: return(texture); case NormalMapTextureType.SecondaryTexture: MaterialPropertyBlock matBlock = new MaterialPropertyBlock(); spriteRenderer.GetPropertyBlock(matBlock); Texture secondaryTexture = matBlock.GetTexture("_SecondaryTex"); Debug.Log("done" + secondaryTexture); return(null); } return(null); }
// TODO: pre-render and cache textures in each colour, and assign them to new car instances. void Start() { float bh, bs, bv; Color.RGBToHSV(baseColor, out bh, out bs, out bv); // Pick a random alt color; include -1 as an option to keep the base color. int index = Random.Range(0, altColors.Length); if (index == -1) { return; } Color newColor = altColors[index]; float nh, ns, nv; Color.RGBToHSV(newColor, out nh, out ns, out nv); // Get the properties of this sprite renderer's instance of the material. SpriteRenderer spriter = GetComponent <SpriteRenderer>(); MaterialPropertyBlock block = new MaterialPropertyBlock(); spriter.GetPropertyBlock(block); // Make a copy of the texture, so we don't alter the original asset permanently. Texture2D mainTex = (Texture2D)block.GetTexture("_MainTex"); Texture2D newTex = Instantiate <Texture2D>(mainTex); // Apparently we can't just set _MainTex or mainTexture on the material directly. block.SetTexture("_MainTex", newTex); spriter.SetPropertyBlock(block); Color[] pixels = newTex.GetPixels(); for (int i = 0; i < pixels.Length; i++) { if (pixels[i].a > 0) { float h, s, v; Color.RGBToHSV(pixels[i], out h, out s, out v); if (s > 0) { float diff = Mathf.Abs(bh - h); if (diff > .5f) { diff = 1 - diff; } if (diff < hueThreshold) { Color newPixel = Color.HSVToRGB(nh, ns, nv * v); newPixel.a = pixels[i].a; pixels[i] = newPixel; } } } } newTex.SetPixels(pixels); newTex.Apply(false); }
// Update is called once per frame void Update() { if (ren == null) { ren = GetComponent <SpriteRenderer>(); } if (matBlock == null) { InitMatBlock(); } else { matBlock.SetVector("_Dir", new Vector4(FadeDirection.x, FadeDirection.y, 0.0f, 0.0f)); if (ren.sprite != null && matBlock.GetTexture("_MainTex") != ren.sprite.texture) { matBlock.SetTexture("_MainTex", ren.sprite.texture); } } matBlock.SetFloat("_Progress", Progress); ren.SetPropertyBlock(matBlock); }
void UpdateMaterial() { if (!initialised) { // Parameters changed before Awake() has been run, do a full update when appropriate. // (Can't rely on Awake() when the GameObject is disabled. It does a lousy job as a constructor.) regenerateMesh = true; return; } if ((renderer.sharedMaterials.Length != 1) || (renderer.sharedMaterials[0] != m_material)) { // Update only when necessary to prevent false scene changes. renderer.sharedMaterials = new Material[] { m_material }; } MaterialPropertyBlock materialProperties = new MaterialPropertyBlock(); renderer.GetPropertyBlock(materialProperties); if ((m_sprite != null) && (m_sprite.texture != null)) { materialProperties.SetTexture("_MainTex", m_sprite.texture); } else if (m_material != null) { if (materialProperties.GetTexture("_MainTex") != null) { materialProperties.Clear(); } m_material.SetTexture("_MainTex", null); } renderer.SetPropertyBlock(materialProperties); }
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property) { InteractableThemePropertyValue start = new InteractableThemePropertyValue(); start.Texture = propertyBlock.GetTexture("_MainTex"); return(start); }
void Awake() { block = new MaterialPropertyBlock(); render = GetComponent <SkinnedMeshRenderer>(); render.GetPropertyBlock(block); texture = block.GetTexture("_MainTex") as Texture2D; }
public static object GetRuntimeProperty(this Renderer render, int materialIndex, int propertyIndex) { MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock(); render.GetPropertyBlock(propertyBlock); Material material = render.materials[materialIndex]; string name = material.GetPropertyName(propertyIndex); int type = material.GetPropertyType(propertyIndex); switch (type) { case 0: return(propertyBlock.GetColor(name)); case 1: return(propertyBlock.GetVector(name)); case 2: case 3: return(propertyBlock.GetFloat(name)); case 4: return(propertyBlock.GetTexture(name)); } return(null); }
void DoGetMaterialTexture() { string _propertyName = propertyName.Value; if (_propertyName == "") { _propertyName = "_MainTex"; } var go = Fsm.GetOwnerDefaultTarget(gameObject); if (!UpdateCache(go)) { return; } if (renderer.material == null) { LogError("Missing Material!"); return; } renderer.GetPropertyBlock(_propBlock); texture.Value = _propBlock.GetTexture(_propertyName); }
public void SwapSwitchTexture(MeshRenderer mr) { if (mr == null) { return; } MaterialPropertyBlock materialProperties = new MaterialPropertyBlock(); mr.GetPropertyBlock(materialProperties); string current = materialProperties.GetTexture("_MainTex").name; //some switches are "invisible" in the sense that they are placed in a non-switch texture wall if (current.Substring(0, 2) != "SW") { return; } if (current[2] == '2') { current = "SW1" + current.Substring(3); } else { current = "SW2" + current.Substring(3); } materialProperties.SetTexture("_MainTex", GetWallTexture(current)); mr.SetPropertyBlock(materialProperties); }
protected virtual void UpdateUIMeshFromComponent(Component p_meshComponent) { UIMeshGraphic v_meshGraphic = null; if (p_meshComponent != null) { var v_meshFilter = p_meshComponent as MeshFilter; var v_skinnedRenderer = p_meshComponent as SkinnedMeshRenderer; if (v_meshFilter != null) { v_meshGraphic = v_meshFilter.GetComponent <UIMeshFilter>(); if (v_meshGraphic == null) { v_meshGraphic = v_meshFilter.gameObject.AddComponent <UIMeshFilter>(); v_meshGraphic.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor; } var v_renderer = v_meshFilter.GetComponent <Renderer>(); v_meshGraphic.material = TryGetMaterialToReplace(v_renderer != null ? v_renderer.sharedMaterial : null); //Try Pick Color Component From Renderer MaterialPropertyBlock if (v_renderer != null) { MaterialPropertyBlock v_propertyBlock = new MaterialPropertyBlock(); v_renderer.GetPropertyBlock(v_propertyBlock); if (!v_propertyBlock.isEmpty) { Vector4 v_colorVector = v_propertyBlock.GetVector("_Color"); if (v_colorVector != Vector4.zero) { v_meshGraphic.color = new Color(v_colorVector.x, v_colorVector.y, v_colorVector.z, v_colorVector.w); } v_meshGraphic.Texture = v_propertyBlock.GetTexture("_MainTex") as Texture2D; } } } if (v_skinnedRenderer != null) { v_meshGraphic = v_skinnedRenderer.GetComponent <UISkinnedMeshFilter>(); if (v_meshGraphic == null) { v_meshGraphic = v_skinnedRenderer.gameObject.AddComponent <UISkinnedMeshFilter>(); } v_meshGraphic.material = TryGetMaterialToReplace(v_skinnedRenderer.sharedMaterial); //Try Pick Color Component From SkinnedRenderer MaterialPropertyBlock MaterialPropertyBlock v_propertyBlock = new MaterialPropertyBlock(); v_skinnedRenderer.GetPropertyBlock(v_propertyBlock); if (!v_propertyBlock.isEmpty) { Vector4 v_colorVector = v_propertyBlock.GetVector("_Color"); if (v_colorVector != Vector4.zero) { v_meshGraphic.color = new Color(v_colorVector.x, v_colorVector.y, v_colorVector.z, v_colorVector.w); } v_meshGraphic.Texture = v_propertyBlock.GetTexture("_MainTex") as Texture2D; } } } }
private Texture2D GetSpriteTexture() { // Get main texture trough MaterialPropertyBlock from sprite renderer MaterialPropertyBlock getBlock = new MaterialPropertyBlock(); rend.GetPropertyBlock(getBlock); return((Texture2D)getBlock.GetTexture(MAIN_TEXTURE)); }
void Update() { #if UNITY_EDITOR if (m_materialPropertyBlock == null || m_materialPropertyBlock.GetTexture("u_Heightmap") == null || m_materialPropertyBlock.GetTexture("u_Controlmap") == null || m_materialPropertyBlock.GetTexture("u_Colormap") == null) { ForceRebindMaterials(); } #endif }
private void SetMaterialProperties() { #endif Renderer r = GetComponent <Renderer>(); MaterialPropertyBlock block = new MaterialPropertyBlock(); r.GetPropertyBlock(block); block.SetTexture("_MainTex", block.GetTexture("_MainTex")); r.SetPropertyBlock(block); }
public static void OnFixAnimationTime() { var pb = new MaterialPropertyBlock(); var objs = Object.FindObjectsOfType <Animator>(); foreach (var animator in objs) { var acs = AnimationUtility.GetAnimationClips(animator.gameObject); foreach (var animationClip in acs) { animationClip.SampleAnimation(animator.gameObject, 0.0f); } var render = animator.gameObject.GetComponent <Renderer>(); if (render == null) { continue; } render.GetPropertyBlock(pb); var shader = render.material.shader; for (int i = 0; i < shader.GetPropertyCount(); i++) { var type = shader.GetPropertyType(i); switch (type) { case ShaderPropertyType.Color: render.material.SetColor(i, pb.GetColor(i)); break; case ShaderPropertyType.Vector: render.material.SetVector(i, pb.GetVector(i)); break; case ShaderPropertyType.Float: render.material.SetFloat(i, pb.GetFloat(i)); break; case ShaderPropertyType.Range: break; case ShaderPropertyType.Texture: render.material.SetTexture(i, pb.GetTexture(i)); break; default: throw new ArgumentOutOfRangeException(); } } } }
/// <inheritdoc /> public override ThemePropertyValue GetProperty(ThemeStateProperty property) { var result = new ThemePropertyValue(); int propId = property.GetShaderPropertyId(); if (renderer != null) { renderer.GetPropertyBlock(propertyBlock); switch (property.Type) { case ThemePropertyTypes.Color: result.Color = propertyBlock.GetVector(propId); break; case ThemePropertyTypes.Texture: result.Texture = propertyBlock.GetTexture(propId); break; case ThemePropertyTypes.ShaderFloat: case ThemePropertyTypes.ShaderRange: result.Float = propertyBlock.GetFloat(propId); break; default: break; } } else if (graphic != null) { switch (property.Type) { case ThemePropertyTypes.Color: result.Color = graphic.material.GetVector(propId); break; case ThemePropertyTypes.Texture: result.Texture = graphic.material.GetTexture(propId); break; case ThemePropertyTypes.ShaderFloat: case ThemePropertyTypes.ShaderRange: result.Float = graphic.material.GetFloat(propId); break; default: break; } } return(result); }
private void SetMaterialProperties() { #endif Renderer r = GetComponent <Renderer>(); MaterialPropertyBlock block = new MaterialPropertyBlock(); r.GetPropertyBlock(block); block.SetTexture("_MainTex", block.GetTexture("_MainTex")); block.SetTexture("_RampTex", gradient); block.SetColor("_TopColor", colorTop); block.SetColor("_BottomColor", colorBottom); r.SetPropertyBlock(block); }
private void SetMaterialProperties() { #endif SpriteRenderer r = GetComponent <SpriteRenderer>(); MaterialPropertyBlock block = new MaterialPropertyBlock(); r.GetPropertyBlock(block); block.SetTexture("_MainTex", block.GetTexture("_MainTex")); block.SetColor("_Color", r.color); block.SetTexture("_MaskTex", mask); block.SetFloat("_OffsetX", offsetX); block.SetFloat("_OffsetY", offsetY); r.SetPropertyBlock(block); }
public void SetSwitchTexture(MeshRenderer mr, bool state) { if (mr == null) { return; } MaterialPropertyBlock materialProperties = new MaterialPropertyBlock(); mr.GetPropertyBlock(materialProperties); string current = materialProperties.GetTexture("_MainTex").name; current = (state ? "SW2" : "SW1") + current.Substring(3); materialProperties.SetTexture("_MainTex", GetWallTexture(current)); mr.SetPropertyBlock(materialProperties); }
void Awake() { Init(); var oldTexture = block.GetTexture(idMainTex); overrideTexture = texture; var newScale = new Vector3( (float)texture.width / oldTexture.width, (float)texture.height / oldTexture.height, 1f); //Debug.Log("texture.width : "+ texture.width); //Debug.Log("texture : "+ texture.name); sr.size = newScale; //transform.localScale = Vector3.Scale(transform.localScale, newScale); }
public void UpdateMaterialProperties(Material material, MaterialPropertyBlock mpb) { if (!material.HasProperty(id)) { return; } switch (type) { case ShaderPropertyType.Color: var color = mpb.GetColor(id); if (color != default(Color)) { material.SetColor(id, color); } break; case ShaderPropertyType.Vector: var vector = mpb.GetVector(id); if (vector != default(Vector4)) { material.SetVector(id, vector); } break; case ShaderPropertyType.Float: case ShaderPropertyType.Range: var value = mpb.GetFloat(id); if (value != default(float)) { material.SetFloat(id, value); } break; case ShaderPropertyType.Texture: var tex = mpb.GetTexture(id); if (tex != default(Texture)) { material.SetTexture(id, tex); } break; } }
private void Update() { if (!Input.GetMouseButton(0)) { return; } RaycastHit hit; var angle = Random.Range(0.0f, 1.0f) * Mathf.PI * 2.0f; var radius = Mathf.Sqrt(Random.Range(0.0f, 1.0f)) * ConeRadius; var newCenterX = Input.mousePosition.x + radius * Mathf.Cos(angle); var newCenterY = Input.mousePosition.y + radius * Mathf.Sin(angle); if (!Physics.Raycast(Cam.ScreenPointToRay(new Vector2(newCenterX, newCenterY)), out hit)) { return; } var rend = hit.transform.GetComponent <Renderer>(); var meshCollider = hit.collider as MeshCollider; if (rend == null || rend.sharedMaterial == null || meshCollider == null) { return; } rend.GetPropertyBlock(prop); var tex = prop.GetTexture("_MainTex") as Texture2D; var pixelUv = hit.textureCoord; if (tex == null) { return; } pixelUv.x *= tex.width; pixelUv.y *= tex.height; StartCoroutine(DrawCircle(tex, rend, new Vector2(pixelUv.x, pixelUv.y), hit.distance)); }
/// <summary> /// Copy the value from MaterialPropertyBlock to CanvasRenderer (#41) /// </summary> void UpdateAnimatableMaterialProperties() { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (0 == m_AnimatableProperties.Length) { return; } _renderer.GetPropertyBlock(s_Mpb); for (int i = 0; i < canvasRenderer.materialCount; i++) { var mat = canvasRenderer.GetMaterial(i); foreach (var ap in m_AnimatableProperties) { switch (ap.type) { case ShaderPropertyType.Color: mat.SetColor(ap.id, s_Mpb.GetColor(ap.id)); break; case ShaderPropertyType.Vector: mat.SetVector(ap.id, s_Mpb.GetVector(ap.id)); break; case ShaderPropertyType.Float: case ShaderPropertyType.Range: mat.SetFloat(ap.id, s_Mpb.GetFloat(ap.id)); break; case ShaderPropertyType.Texture: mat.SetTexture(ap.id, s_Mpb.GetTexture(ap.id)); break; } } } }
public static int GetTexture(IntPtr l) { int result; try { int total = LuaDLL.lua_gettop(l); if (LuaObject.matchType(l, total, 2, typeof(string))) { MaterialPropertyBlock materialPropertyBlock = (MaterialPropertyBlock)LuaObject.checkSelf(l); string name; LuaObject.checkType(l, 2, out name); Texture texture = materialPropertyBlock.GetTexture(name); LuaObject.pushValue(l, true); LuaObject.pushValue(l, texture); result = 2; } else if (LuaObject.matchType(l, total, 2, typeof(int))) { MaterialPropertyBlock materialPropertyBlock2 = (MaterialPropertyBlock)LuaObject.checkSelf(l); int nameID; LuaObject.checkType(l, 2, out nameID); Texture texture2 = materialPropertyBlock2.GetTexture(nameID); LuaObject.pushValue(l, true); LuaObject.pushValue(l, texture2); result = 2; } else { LuaObject.pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function GetTexture to call"); result = 2; } } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
// Update is called once per frame void Update() { if (ren == null) { ren = GetComponent <SpriteRenderer>(); } if (matBlock == null) { InitMatBlock(); } else if (ren.sprite != null) { if (matBlock.GetTexture("_MainTex") != ren.sprite.texture) { matBlock.SetTexture("_MainTex", ren.sprite.texture); } } matBlock.SetFloat("_Progress", Progress); ren.SetPropertyBlock(matBlock); }
public Texture GetTexture(int name) { CheckLoad(); return(Block.GetTexture(name)); }