Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.86 KB | None | 0 0
  1. /** Runes of Wizardry Mod for Minecraft
  2.  *  Licensed under the GNU GPL version 3
  3.  *  
  4.  *  this file was created by Xilef11 on 2015-09-06
  5.  */
  6. package com.zpig333.runesofwizardry.client.model;
  7.  
  8. import java.util.HashMap;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import javafx.scene.shape.VertexFormat;
  14. import net.minecraft.client.Minecraft;
  15. import net.minecraft.client.renderer.block.model.BakedQuad;
  16. import net.minecraft.client.renderer.block.model.FaceBakery;
  17. import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
  18. import net.minecraft.client.renderer.texture.TextureAtlasSprite;
  19. import net.minecraft.client.resources.model.IBakedModel;
  20. import net.minecraft.client.resources.model.ModelResourceLocation;
  21. import net.minecraft.init.Blocks;
  22. import net.minecraft.item.ItemStack;
  23. import net.minecraft.util.EnumFacing;
  24.  
  25. import org.apache.commons.lang3.ArrayUtils;
  26.  
  27. import com.google.common.primitives.UnsignedInteger;
  28. import com.zpig333.runesofwizardry.api.IDustStorageBlock;
  29. import com.zpig333.runesofwizardry.client.TextureStitchEventHandler;
  30. import com.zpig333.runesofwizardry.core.References;
  31. import com.zpig333.runesofwizardry.core.WizardryLogger;
  32.  
  33. /**
  34.  * @author Xilef11
  35.  *
  36.  */
  37. public class ModelDustStorage implements IBakedModel {
  38.     private static Map<String,ModelResourceLocation> resourceMap = new HashMap<String, ModelResourceLocation>();
  39.     private IDustStorageBlock block;
  40.     private int meta;
  41.     private int bgColor,fgColor;
  42.     // create a tag (ModelResourceLocation) for our model.
  43.       public final ModelResourceLocation modelResourceLocation;
  44.  
  45.       public ModelDustStorage(IDustStorageBlock block, int meta) {
  46.         this.block=block;
  47.         this.meta=meta;
  48.         this.modelResourceLocation = getModelResourceLocation(block,meta);
  49.         this.bgColor = block.getIDust().getPrimaryColor(new ItemStack(block.getIDust(),1,meta));
  50.         this.fgColor = block.getIDust().getSecondaryColor(new ItemStack(block.getIDust(),1,meta));
  51.         WizardryLogger.logInfo("Created model for block: "+block.getName()+" "+meta+" bg: "+Integer.toHexString(bgColor)+" fg: "+Integer.toHexString(fgColor));
  52.         bgColor+=0xFF000000;//multiplies opacity by 100% when block is not in the solid layer
  53.         fgColor+=0xFF000000;
  54.         //debug - looks like the color must be big endian (rightmost msb)
  55.         //bgColor = 0xff0000;
  56.         //fgColor = 0x00ff00;
  57.       }
  58.     public static String getModelResourceLocationPath(IDustStorageBlock block, int meta){
  59.         //maybe block.getIdust().getmodid instead of refs.texturepath?
  60.         return References.texture_path+block.getName()+"_"+meta;
  61.     }
  62.     public static ModelResourceLocation getModelResourceLocation(String path){
  63.         ModelResourceLocation current = resourceMap.get(path);
  64.         if(current==null){
  65.             current=new ModelResourceLocation(path);
  66.             resourceMap.put(path, current);
  67.         }
  68.         return current;
  69.     }
  70.     public static ModelResourceLocation getModelResourceLocation(IDustStorageBlock block, int meta){
  71.         return getModelResourceLocation(getModelResourceLocationPath(block, meta));
  72.     }
  73.    
  74.     /* (non-Javadoc)
  75.      * @see net.minecraft.client.resources.model.IBakedModel#getFaceQuads(net.minecraft.util.EnumFacing)
  76.      */
  77.     @Override
  78.     public List getFaceQuads(EnumFacing face) {
  79.         List r = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelForState(Blocks.dirt.getDefaultState()).getFaceQuads(face);
  80.        
  81.         //XXX testing
  82. //      for(int i=0;i<4;i++){
  83. //          int [] quad0=((BakedQuad)r.get(0)).getVertexData();
  84. //          float x = Float.intBitsToFloat(quad0[i*7]);
  85. //          float y = Float.intBitsToFloat(quad0[1+i*7]);
  86. //          float z = Float.intBitsToFloat(quad0[2+i*7]);
  87. //          int color = quad0[3+i*7];
  88. //          int lastvalue = quad0[6+i*7];
  89. //          System.out.println("Dirt has vertex "+i+" on face "+face+" being "+x+" "+y+" "+z+" color: "+color+" last: "+lastvalue);
  90. //      }
  91.         List<BakedQuad> result = new LinkedList<BakedQuad>();
  92.         int[] bg =null;
  93.         int[] fg = null;
  94.         TextureAtlasSprite bgTex = TextureStitchEventHandler.getDustStorageBG();
  95.         //following line works, so problem is with texture... (also color is wrong and dosen't render in inventory)
  96.         //bgTex = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelForState(Blocks.dirt.getDefaultState()).getTexture();
  97.         TextureAtlasSprite fgTex = TextureStitchEventHandler.getDustStorageFG();
  98.         //tall grass shows up as black too, so problem is not with registering texture
  99.         //also, tall grass is shown on block in inventory, but the bg is still gray
  100.         fgTex=Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelForState(Blocks.tallgrass.getDefaultState()).getTexture();
  101.         //looks like a bakedquad is a full square, and we have to pass it all its vertices in the int array...
  102.         //also, tintindex should be -1
  103.         if(face==EnumFacing.EAST){
  104.             //BG color
  105.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 1, 1, bgColor, bgTex, 0, 0));
  106.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 0, 1, bgColor, bgTex, 0, 16));
  107.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 0, 0, bgColor, bgTex, 16, 16));
  108.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 1, 0, bgColor, bgTex, 16, 0));
  109.             //fg
  110.             fg = ArrayUtils.addAll(fg, vertexToInts(1.001F, 1, 1, fgColor, fgTex, 0, 0));
  111.             fg = ArrayUtils.addAll(fg, vertexToInts(1.001F, 0, 1, fgColor, fgTex, 0, 16));
  112.             fg = ArrayUtils.addAll(fg, vertexToInts(1.001F, 0, 0, fgColor, fgTex, 16, 16));
  113.             fg = ArrayUtils.addAll(fg, vertexToInts(1.001F, 1, 0, fgColor, fgTex, 16, 0));
  114.         }else if(face==EnumFacing.WEST){
  115.             //BG color
  116.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 1, 0, bgColor, bgTex, 0, 0));
  117.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 0, 0, bgColor, bgTex, 0, 16));
  118.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 0, 1, bgColor, bgTex, 16, 16));
  119.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 1, 1, bgColor, bgTex, 16, 0));
  120.             //fg
  121.             fg = ArrayUtils.addAll(fg, vertexToInts(-0.001F, 1, 0, fgColor, fgTex, 0, 0));
  122.             fg = ArrayUtils.addAll(fg, vertexToInts(-0.001F, 0, 0, fgColor, fgTex, 0, 16));
  123.             fg = ArrayUtils.addAll(fg, vertexToInts(-0.001F, 0, 1, fgColor, fgTex, 16, 16));
  124.             fg = ArrayUtils.addAll(fg, vertexToInts(-0.001F, 1, 1, fgColor, fgTex, 16, 0));
  125.         }else if(face==EnumFacing.NORTH){
  126.             //BG color
  127.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 1, 0, bgColor, bgTex, 0, 0));
  128.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 0, 0, bgColor, bgTex, 0, 16));
  129.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 0, 0, bgColor, bgTex, 16, 16));
  130.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 1, 0, bgColor, bgTex, 16, 0));
  131.             //fg
  132.             fg = ArrayUtils.addAll(fg, vertexToInts(1, 1, -0.001F, fgColor, fgTex, 0, 0));
  133.             fg = ArrayUtils.addAll(fg, vertexToInts(1, 0, -0.001F, fgColor, fgTex, 0, 16));
  134.             fg = ArrayUtils.addAll(fg, vertexToInts(0, 0, -0.001F, fgColor, fgTex, 16, 16));
  135.             fg = ArrayUtils.addAll(fg, vertexToInts(0, 1, -0.001F, fgColor, fgTex, 16, 0));
  136.         }else if(face==EnumFacing.SOUTH){
  137.             //BG color
  138.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 1, 1, bgColor, bgTex, 0, 0));
  139.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 0, 1, bgColor, bgTex, 0, 16));
  140.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 0, 1, bgColor, bgTex, 16, 16));
  141.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 1, 1, bgColor, bgTex, 16, 0));
  142.             //fg
  143.             fg = ArrayUtils.addAll(fg, vertexToInts(0, 1, 1.001F, fgColor, fgTex, 0, 0));
  144.             fg = ArrayUtils.addAll(fg, vertexToInts(0, 0, 1.001F, fgColor, fgTex, 0, 16));
  145.             fg = ArrayUtils.addAll(fg, vertexToInts(1, 0, 1.001F, fgColor, fgTex, 16, 16));
  146.             fg = ArrayUtils.addAll(fg, vertexToInts(1, 1, 1.001F, fgColor, fgTex, 16, 0));
  147.         }else if(face==EnumFacing.DOWN){
  148.             //BG color
  149.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 0, 1, bgColor, bgTex, 0, 0));
  150.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 0, 0, bgColor, bgTex, 0, 16));
  151.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 0, 0, bgColor, bgTex, 16, 16));
  152.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 0, 1, bgColor, bgTex, 16, 0));
  153.             //fg
  154.             fg = ArrayUtils.addAll(fg, vertexToInts(0, -0.001F,1, fgColor, fgTex, 0, 0));
  155.             fg = ArrayUtils.addAll(fg, vertexToInts(0, -0.001F,0, fgColor, fgTex, 0, 16));
  156.             fg = ArrayUtils.addAll(fg, vertexToInts(1,-0.001F,0, fgColor, fgTex, 16, 16));
  157.             fg = ArrayUtils.addAll(fg, vertexToInts(1,-0.001F,1, fgColor, fgTex, 16, 0));
  158.         }else if(face==EnumFacing.UP){
  159.             //BG color
  160.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 1, 0, bgColor, bgTex, 0, 0));
  161.             bg = ArrayUtils.addAll(bg, vertexToInts(0, 1, 1, bgColor, bgTex, 0, 16));
  162.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 1, 1, bgColor, bgTex, 16, 16));
  163.             bg = ArrayUtils.addAll(bg, vertexToInts(1, 1, 0, bgColor, bgTex, 16, 0));
  164.             //fg
  165.             fg = ArrayUtils.addAll(fg, vertexToInts(0, 1.001F,0, fgColor, fgTex, 0, 0));
  166.             fg = ArrayUtils.addAll(fg, vertexToInts(0, 1.001F,1, fgColor, fgTex, 0, 16));
  167.             fg = ArrayUtils.addAll(fg, vertexToInts(1,1.001F,1, fgColor, fgTex, 16, 16));
  168.             fg = ArrayUtils.addAll(fg, vertexToInts(1,1.001F,0, fgColor, fgTex, 16, 0));
  169.         }else{
  170.             throw new IllegalArgumentException("Wrong EnumFacing: "+face);//is that even possible...
  171.         }
  172.         result.add(new BakedQuad(bg, -1, face));
  173.         //FIXME no alpha blending...
  174.         BakedQuad fore = new BakedQuad(fg, -1, face);
  175.         result.add(fore);
  176.         return result;
  177.         // TODO Auto-generated method stub
  178.     }
  179.  
  180.     /* (non-Javadoc)
  181.      * @see net.minecraft.client.resources.model.IBakedModel#getGeneralQuads()
  182.      */
  183.     @Override
  184.     public List getGeneralQuads() {
  185.         List<BakedQuad> res = new LinkedList<BakedQuad>();
  186.         for(EnumFacing face : EnumFacing.VALUES){
  187.             res.addAll(getFaceQuads(face));
  188.         }
  189.         //return res;
  190.         // TODO Auto-generated method stub
  191.         List r = Minecraft.getMinecraft().getBlockRendererDispatcher().getBlockModelShapes().getModelForState(Blocks.dirt.getDefaultState()).getGeneralQuads();
  192.         return r;
  193.     }
  194.  
  195.     /* (non-Javadoc)
  196.      * @see net.minecraft.client.resources.model.IBakedModel#isAmbientOcclusion()
  197.      */
  198.     @Override
  199.     public boolean isAmbientOcclusion() {
  200.         return true;
  201.         //return false;
  202.     }
  203.  
  204.     /* (non-Javadoc)
  205.      * @see net.minecraft.client.resources.model.IBakedModel#isGui3d()
  206.      */
  207.     @Override
  208.     public boolean isGui3d() {
  209.         return true;
  210.     }
  211.  
  212.     /* (non-Javadoc)
  213.      * @see net.minecraft.client.resources.model.IBakedModel#isBuiltInRenderer()
  214.      */
  215.     @Override
  216.     public boolean isBuiltInRenderer() {
  217.         return false;
  218.     }
  219.  
  220.     /* (non-Javadoc)
  221.      * @see net.minecraft.client.resources.model.IBakedModel#getTexture()
  222.      */
  223.     @Override
  224.     public TextureAtlasSprite getTexture() {
  225.         //TODO getTexture might need to get tweaked
  226.         TextureAtlasSprite sprite =TextureStitchEventHandler.getDustStorageBG();
  227.         //sprite.
  228.         return sprite;
  229.     }
  230.  
  231.     /* (non-Javadoc)
  232.      * @see net.minecraft.client.resources.model.IBakedModel#getItemCameraTransforms()
  233.      */
  234.     @Override
  235.     public ItemCameraTransforms getItemCameraTransforms() {
  236.         return ItemCameraTransforms.DEFAULT;
  237.     }
  238.    
  239.       /**
  240.        * Converts the vertex information to the int array format expected by BakedQuads.
  241.        * @param x x coordinate
  242.        * @param y y coordinate
  243.        * @param z z coordinate
  244.        * @param color RGBA colour format - white for no effect, non-white to tint the face with the specified colour
  245.        * @param texture the texture to use for the face
  246.        * @param u u-coordinate of the texture (0 - 16) corresponding to [x,y,z]
  247.        * @param v v-coordinate of the texture (0 - 16) corresponding to [x,y,z]
  248.        * @return
  249.        */
  250.       private static int[] vertexToInts(float x, float y, float z, int color, TextureAtlasSprite texture, float u, float v)
  251.       {
  252.         return new int[] {
  253.                 Float.floatToRawIntBits(x),
  254.                 Float.floatToRawIntBits(y),
  255.                 Float.floatToRawIntBits(z),
  256.                 /* on a 0x00RRGGBB
  257.                  * on veut 0x00bbggrr
  258.                  * reverse donne 0xbbggrr00
  259.                  */
  260.                 Integer.rotateRight(Integer.reverseBytes(color),8),
  261.                 Float.floatToRawIntBits(texture.getInterpolatedU(u)),
  262.                 Float.floatToRawIntBits(texture.getInterpolatedV(v)),
  263.                 0
  264.         };
  265.       }
  266.  
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement